Schedule Function - Algorithmic Trading with Python and Quantopian p. 3

sentdex · Beginner ·🛠️ AI Tools & Apps ·9y ago

Key Takeaways

The video demonstrates the use of the schedule function in Quantopian for algorithmic trading with Python, covering its parameters, applications, and best practices for backtesting and strategy development. It utilizes Quantopian and Python for scheduling functions and backtesting.

Full Transcript

what's up everybody Welcome To Part 15 of our python for finance tutorial series as well as part three of the quantopian section of that series uh in the last tutorial we learned how to kind of get a hold of our leverage and make sure that wasn't getting out of hand and now in this tutorial what we're going to cover is the schedule function because in the last one we saw despite getting control of our leverage we were still making like way too many trades in a day we really just want to make like one trade a day um and and and with this especially when we're addressing basically a simple moving average crossover of one day um this is one example of something you might want to do just like once a day but with the schedule function what that allows you to do is schedule functions to occur daily weekly monthly yearly you know like whatever you need um they can let you I'm not positive on the yearly so I'll I take that back I'm not 100% on that um but anyway it lets you schedule functions so what we're going to do is work on that so now what we're going to do is um we're literally just going to write schedule uncore function and begin schedule function again like some of the other magical things like get open order um it just simply exists okay it's like again from quantopian import asterisk that's what we're going to pretend we've done so we can see the parameters here a function you need a date rule a Time Rule and half days equals true or half days if you want half the half days is like sometimes the market will just close early so for example you might you might think that you're going to cuz what you can do with a Time rule is you can you can offset things so you can offset Market close by two hours and you might think that's always going to be you know still after noon right um but on a half day that's not true so maybe you're thinking okay I want to trade at 1 p.m. Eastern every time so I'm going to do such and such or whatever um but on half days you won't be trading at that same exact time so you might not want to trade during half days that's just me making up a scenario I don't know um anyway I can't imagine too many other scenarios where half days would matter that much to you but anyways it's there if you need it obviously somebody needed it so anyway schedule function you just pick whatever function you want to schedule so in this case say for example we want to have a function that runs daily and or just a function that runs and does something at all like um we're going to call this one Define um MO uh we'll call this ma crossover handling okay and let's say this function does something right um this function will not run like handle data is just known by quantopian to be a function that is going to run every day or or every period rather and in this case actually it's every minute and may crossover handling however that's a custom one that's our own so it's not going to run that automatically we have to call it into to to run so that's the way we do that is with schedule function because it's not like a regular Python program like if you just did like this and you and you called it down here um I don't even know what's going to happen but it's not going to run this function every day it's probably it would even in Python terms it would only run it once so anyway the way we're going to do is with schedule function so the function we're going to schedule is ma cross over handling and then we've got um date rules so date uncore rules this is when do you want this to run so you've got these choices like I said I guess I was right you can't run it yearly so daily weekly either Week start or week end that's awesome month start or month end we're going to do every day um and we we're not going to pass any parameters but you can do offsets here I'm pretty sure um now we're also going to add time rules so timecore rules and let's see if this is going to let yeah okay so just just so I can have proper space because I'm like all zoomed in here um you you don't need to enter down here but anyways um time rules Dot and we want to do you have two options Market open or Market close we're going to say Market open and then and then you have an offset here so this is like an offset so the default is 1 minute after the market open so I'm going to say hours equals 1 so this would be 1 hour after the Market opens um if you do Market close hours one would be 1 hour before before the close Okay so one of them is after the time one of them is like before the time like it's not going to hours one you don't have to do like Market close hours minus one it it's goes the other way anyway at least I'm pretty sure on that one um now okay so it's going to call Ma crossover handling to happen every day at Market open 1 hour after so 1 hour after market open cool so now we're ready to build this function now this function basically is going to do every everything that handle data does and um handle data actually becomes irrelevant at this point because we're going to just take everything from it so I'm just going to highlight everything cut and paste and we're just going to delete handle data we don't even need it anymore but you can see here we're referencing context and we're also referencing data so we need to make sure that we're passing context data um into here otherwise we've got everything we need now one major bonus that we're going to see here is that this is going to run way faster than um than before I'm going to build it just to check for syntax errors sure enough um what do you say line seven did I not close this off or something no I did here right for some reason this has gotten all all what let's see is it not okay so we have we did not close this off man that Zoom when I zoomed out it actually gave me I'm pretty sure like line five was separated anyway let's check again and we'll hit less here okay so you can already see it's like zooming by I'm going to go ahead and cancel and we'll run the full back test just so we can see our daily trades um but you can see it's going much faster because now it's just it's calculating those smas it's looking at prices really one time a day rather than every minute which is making a huge difference in our performance um because not every strategy needs to check every minute okay so okay so yeah we've got we we've done much better coming back down to the transactions these should be 2 million yeah about 2 million 2 million because every time again already explained it so cool and this one's a little larger just simply because we've made we've made money okay so uh cool so that is enough for just a simple strategy the back tester and you again you can look at all kinds of stuff like your transaction details your positions any log output I don't oh we do I guess are we still logging um I thought I got rid of logging the oh no we're still logging the history um anyway actually we can just delete that so we're doing all that um so yeah so so that's the schedule function that's all the strategy kind of stuff a real basic strategy a nice back test but really the back test I mean the back test is seeming to suggest that this might actually be a good strategy and we could even try it on like a different uh company so so like let's do um rather than Apple let's do eBay cool and I think that other back test is actually running let's see if I can cancel that let's just delete I wonder if it's in progress and you hit delete if it stops the progress I don't know it's a mystery of Life y'all so so let's run this one let's see how we do this was with eBay oh we're not doing so hot yeah oh all right mine's a 47% okay wonder how eBay did in that time did eBay lose 47% you could you could part of your custom data could be the percent change for the actual company you can also even change your benchmark I uh let's see if I can find Benchmark in here real quick Benchmark setting a custom Benchmark set bench this is so great I think L I wonder if I can just do did but symbol let's go to the algorithm here um initialize boom um this shouldn't be apple but oh well symbol that's unfortunate I wonder if we can pass a sid rather than the because I think symbol something returns a sid let's let's try that let's see if we can get let's see if we can do that looks good so The Benchmark is eBay and this is our algorithm so as you can see eBay is doing eBay is like just fine I mean it's not doing the greatest but but yeah we're doing horrible okay so obviously is not good for all of the companies um okay so anyways that's enough for now in the coming tutorials we're actually going to leave the algorithms behind for now and we're going to play in The Notebook environment which is arguably really the place you you should start in when you're starting a strategy I just still kind of think algorithms is good to get your feet just a little wet on the whole kind of platform um and then yeah it's time to probably dive into notebooks cuz once you get to this point okay now you're going to really start thinking about developing a strategy um and the way that you're going to do that is most likely through notebooks so anyways that's what you have to look forward to if you have any questions comments concerns whatever anything blackbox magical whatever uh let me know below I'll do my best to help you out um otherwise I will see you in another tutorial

Original Description

Welcome to part 3 of using Quantopian and Zipline. In this tutorial, we're going to cover the schedule_function. https://pythonprogramming.net https://twitter.com/sentdex https://www.facebook.com/pythonprogramming.net/ https://plus.google.com/+sentdex
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from sentdex · sentdex · 0 of 60

← Previous Next →
1 Matplotlib Python Tutorial Part 1: Basics and your first Graph!
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
2 Python Encryption Tutorial with PyCrypto
Python Encryption Tutorial with PyCrypto
sentdex
3 Python's Logging Function
Python's Logging Function
sentdex
4 wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
5 wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
6 wxPython Programming Tutorial 3: Menu Bar and Menu Button
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
7 wxPython Programming Tutorial 4: Panels
wxPython Programming Tutorial 4: Panels
sentdex
8 wxPython Programming Tutorial 5: User Input Saved To Variables
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
9 wxPython Programming Tutorial 6: Multiple Choice Input
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
10 wxPython Programming Tutorial 7: Adding Static Text and Colors
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
11 wxPython Programming Tutorial 8: Custom Button Images
wxPython Programming Tutorial 8: Custom Button Images
sentdex
12 wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
13 Basic PHP Tutorial 13: Multi-dimensional Array
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
14 Basic PHP Tutorial 15: Functions and Global Variables
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
15 Basic PHP Tutorial 12: Associative Array
Basic PHP Tutorial 12: Associative Array
sentdex
16 Basic PHP Tutorial 14: Foreach loop
Basic PHP Tutorial 14: Foreach loop
sentdex
17 Basic PHP Tutorial 16: Include and Require
Basic PHP Tutorial 16: Include and Require
sentdex
18 Basic PHP Tutorial 7: Assignment, comparison and Logical operators
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
19 Basic PHP Tutorial 4: Variables and Comments
Basic PHP Tutorial 4: Variables and Comments
sentdex
20 Basic PHP Tutorial 11: Arrays part 1, basic array
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
21 Basic PHP Tutorial 6: If else and else if conditionals cont'd
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
22 Basic PHP Tutorial 1: Intro to PHP
Basic PHP Tutorial 1: Intro to PHP
sentdex
23 Basic PHP Tutorial 3: HTML with PHP
Basic PHP Tutorial 3: HTML with PHP
sentdex
24 Basic PHP Tutorial 9: While Loop
Basic PHP Tutorial 9: While Loop
sentdex
25 Basic PHP Tutorial 10: Switch Statement
Basic PHP Tutorial 10: Switch Statement
sentdex
26 Basic PHP Tutorial 2: Print and Echo
Basic PHP Tutorial 2: Print and Echo
sentdex
27 Basic PHP Tutorial 5: If else and else if conditional statements
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
28 Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
29 Basic PHP Tutorial 17: User Input Form Example / String Manipulation
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
30 Basic PHP Tutorial 18: HTML Entities and forms cont'd
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
31 Basic PHP Tutorial 19: Finding words in strings
Basic PHP Tutorial 19: Finding words in strings
sentdex
32 Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
33 Basic PHP Programming Tutorial 22: Hashing part 2: salting
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
34 Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
35 Basic PHP Programming Tutorial 21: MD5 Hashing For Security
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
36 Basic PHP Programming Tutorial 24: String similarity
Basic PHP Programming Tutorial 24: String similarity
sentdex
37 Basic PHP Programming Tutorial 25: Time and Time stamps
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
38 Basic PHP Programming Tutorial 26: Die and Exit
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
39 Basic PHP Programming Tutorial 27: MySQL Databases Part 1
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
40 Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
41 Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
42 Basic PHP Programming Tutorial 30: MySQL database in Use
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
43 Django Tutorial Web Development with Python Part 1: Installing Django
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
44 Python Tutorial: File Deletion and Folder Deletion / directory deletion
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
45 Python Tutorial: How to Rename Files and Move Files with Python
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
46 3D Graphs in Matplotlib for Python: Basic 3D Line
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
47 3D Plotting in Matplotlib for Python: 3D Scatter Plot
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
48 3D Charts in Matplotlib for Python: Multiple datasets scatter plot
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
49 Sikuli Tutorial 1: Visually programming in python!
Sikuli Tutorial 1: Visually programming in python!
sentdex
50 Sikuli Tutorial 2: Program visually in python!
Sikuli Tutorial 2: Program visually in python!
sentdex
51 Sikuli Tutorial 3: Program visually in python!
Sikuli Tutorial 3: Program visually in python!
sentdex
52 3D Bar Charts in Python and Matplotlib
3D Bar Charts in Python and Matplotlib
sentdex
53 3D Plane wire frame Graph Chart in Python
3D Plane wire frame Graph Chart in Python
sentdex
54 Raspberry Pi Part 1 Introduction
Raspberry Pi Part 1 Introduction
sentdex
55 Raspberry Pi Part 8: First Download and Update! (Firmware)
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
56 Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
57 Raspberry Pi Part 11: Remote Desktop
Raspberry Pi Part 11: Remote Desktop
sentdex
58 Twitter Analysis: How to rank a user's influence
Twitter Analysis: How to rank a user's influence
sentdex
59 GPIO Tutorial for Pi Part 2 - Programming the GPIO
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
60 GPIO Tutorial for Raspberry Pi Part 1 - Setting up
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex

This video teaches how to use the schedule function in Quantopian for algorithmic trading with Python, including how to schedule functions, configure time rules, and run backtests. It also covers best practices for strategy development and backtesting. By watching this video, viewers can learn how to build and test their own algorithmic trading strategies using Quantopian and Python.

Key Takeaways
  1. Schedule a function using the schedule function from Quantopian
  2. Choose a date rule, such as daily, weekly, or monthly
  3. Select a time rule, such as offsetting market close by a specified number of hours
  4. Set half days to true or false, depending on whether to trade during early market closures
  5. Pass parameters to the scheduled function, if necessary
  6. Build schedule function
  7. Run back test
  8. Configure time rules
  9. Delete logging history
  10. Run backtests with different companies
💡 The schedule function in Quantopian allows for efficient backtesting and strategy development by enabling the scheduling of functions to occur at specific times or intervals, such as daily, weekly, or monthly.

Related AI Lessons

Up next
How to Open HPL Files (HP-GL Plotter)
File Extension Geeks
Watch →