3. How Strategies Work - Algo trading from scratch with Python — Transcript
Full transcript
- 0:02having an idea for trading strategy is
- 0:05great but how do you turn it into code
- 0:07how will you define your entry and exit
- 0:10rules where will you define your
- 0:12position sizing these are the typical
- 0:14questions we need to answer When
- 0:16developing a strategy Jesse answers
- 0:18these questions by giving you a clear
- 0:19framework for defining your strategies
- 0:22in this video I want to show you an
- 0:24overview of that framework in the next
- 0:26videos of this series I will show you
- 0:27how to code in real world examples
- 0:29alright let's get started
- 0:35each strategy instance goes through a
- 0:37series of execution cycle steps
- 0:39regularly each cycle begins when a
- 0:42candle is closed when the execution
- 0:43begins it runs a series of special
- 0:45methods that we call them hooks let's go
- 0:48through examples one by one so it will
- 0:50be easier to digest
- 0:52this is a flowchart of the execution
- 0:54cycle hopes for example it observes if
- 0:56there is any open position if there is
- 0:58one it needs to check whether or not it
- 1:00should update it if there isn't any it
- 1:02needs to check if an entry signal is
- 1:04green for opening a new position for the
- 1:06first scenario let's imagine that we
- 1:08just started trading we don't have any
- 1:10open positions and we haven't submitted
- 1:12any orders either after a candle closes
- 1:15the first function that gets executed is
- 1:17called before
- 1:19I'm gonna show you how to write a
- 1:21simplified versions of these functions
- 1:23that I'm about to show you so I'm gonna
- 1:26remove all the code that's shipped with
- 1:27the example strategy the first method is
- 1:30called before and notice that its return
- 1:33type is none which means it doesn't have
- 1:34to return anything
- 1:36this method isn't used that often
- 1:38however whenever that you have to do
- 1:40something before executing the strategy
- 1:43itself you could write it in here an
- 1:45example of this could be just logging
- 1:47something we also have the opposite of
- 1:49the before method which is called after
- 1:51and obviously it is called after
- 1:53everything else is executed in your
- 1:55strategy
- 1:56all right after the before function we
- 1:59have to ask ourselves do we have any
- 2:00open positions and if the answer is no
- 2:02do we have any active entry orders in
- 2:05this case the answer is no so we move on
- 2:08to the next function which is called
- 2:09Shoot long and after that we have the
- 2:11golong function if you're trading on a
- 2:13Futures Market you also have access to
- 2:15shoot short and go short methods which
- 2:18are used for opening short positions but
- 2:20if you're trading on the spot Market you
- 2:22can just ignore them let's define the
- 2:24shoot long method the return type is a
- 2:27Boolean which means it has to return
- 2:28either true or false
- 2:31and then we have the golong method which
- 2:33is where we Define our entry order the
- 2:35return type is none which means it
- 2:37doesn't have to return anything all we
- 2:39need to do here is to define the buy
- 2:41order
- 2:42let's assume the current price of
- 2:43Bitcoin is at twenty thousand and we
- 2:45want to submit limit by order for the
- 2:48sake of keeping this example simple I
- 2:50will just hard code the amount of the
- 2:52order so I'll just buy one Bitcoin 1000
- 2:55below the current price which is
- 2:56nineteen thousand and because the price
- 2:59of the order is below the current price
- 3:01Jesse will submit a limit order
- 3:03now next we have filters which are
- 3:05conditions that we Define to filter out
- 3:07bad traits I'm going to show you real
- 3:10world examples of filters in a future
- 3:12video but for now let's imagine that our
- 3:14strategy doesn't have any filters or
- 3:16that it does and they are all passing at
- 3:19this step Jesse will submit our entry
- 3:20order so once again the before method
- 3:23gets called and just like before we
- 3:25don't have any open positions so the
- 3:26answer to this question is no
- 3:28but the answer to the next question
- 3:30which is do we have any active orders is
- 3:32now yes and now the question is should
- 3:35we cancel the previous limit order and
- 3:37maybe submit a new one or should we just
- 3:40do nothing and wait for the previous
- 3:42order to get executed eventually to tell
- 3:45Jesse how to do this we're going to have
- 3:46to define a method called should cancel
- 3:49entry which Returns the Boolean value if
- 3:52it returns true then Jesse is going to
- 3:54cancel the previous order if it returns
- 3:56false it's not going to do anything in
- 3:58this case I actually wanted to cancel
- 4:00the previous order and submit a new one
- 4:02based on the current market price which
- 4:04takes us to the next cycle of Step which
- 4:06is the after method now that our entry
- 4:08order is a limit order let's imagine
- 4:10that we waited but the order didn't get
- 4:12executed now the question is what should
- 4:14happen when the next candle is closed
- 4:16let's imagine that the current price of
- 4:18Bitcoin is at 21 000 and once again I
- 4:21want to buy 1000 below the current price
- 4:23which in this case will be twenty
- 4:25thousand
- 4:27so I have to make sure that my should
- 4:29cancel entry method is returning true
- 4:32so one more time just to confirm this is
- 4:34going to cancel my previous limit order
- 4:36at the price of 19 000 and execute the
- 4:39shoot long and go long method once again
- 4:42that means it's going to submit a new
- 4:44limit order at the price of 20 000.
- 4:47right for the next scenario let's
- 4:49imagine that I didn't use the limit
- 4:51order instead I was trying to enter a
- 4:55long position using a market order to
- 4:57submit a market order using Jesse I need
- 4:59to make sure that the price of my order
- 5:01is the same as the current price which
- 5:03in this case is 21
- 5:05000. in this case the return value of
- 5:07shoot cancel entry doesn't really matter
- 5:09but remember that you still have to
- 5:11define it so next time that the candle
- 5:13closes the question is how should we
- 5:15update the open position we can answer
- 5:17that question by defining a method
- 5:19called update position which is then
- 5:21supposed to return anything but it
- 5:23allows us to update our currently open
- 5:25position Now update in this case means
- 5:28adding to the size of the position
- 5:29reducing it or maybe even closing it
- 5:32let's begin by adding to the size of the
- 5:34current position so right now I have a
- 5:37position with the size of one Bitcoin
- 5:39let's say I want to buy another Bitcoin
- 5:42at the current price
- 5:44and let's imagine that the current price
- 5:45of Bitcoin is at 21 000. so adding to
- 5:49the size of the current position is very
- 5:51similar to how we open the long position
- 5:53in the first place now let's see an
- 5:55example of how it is to reduce the size
- 5:58of the current position I can do that by
- 6:00using either a stop loss or a take
- 6:02profit order
- 6:03and because the price of the order is
- 6:05the current price of BTC it will use a
- 6:08market order
- 6:09to close the current position all I have
- 6:11to do is to increase the size of my
- 6:13order to equal the size of my position
- 6:15so instead of 0.5 I will enter one this
- 6:19will cause the current position to close
- 6:20so the answer to this question will
- 6:23change from no into yes after this Jesse
- 6:26will also cancel any remaining active
- 6:28orders now in this case we don't have
- 6:30any but you might have one for example
- 6:33maybe other than the take profit order
- 6:35we also had your stop loss order so now
- 6:37that the position is closed we want to
- 6:39make sure that the Surplus order is
- 6:41canceled before looking for future
- 6:43trades after that the S8 is clean to
- 6:45look for future trades so Jesse will
- 6:47execute the shoot long and go along with
- 6:49it one more time to see if it needs to
- 6:52open a position right after closing the
- 6:54previous one foreign
- 7:00we just covered the execution socket
- 7:02hooks or methods but how do you hook
- 7:04into a specific events what I mean by
- 7:06events are things that happen during the
- 7:09lifesame candle rather than at the
- 7:11closing moment of it all right let me
- 7:13show you what I mean by an example I
- 7:15just showed you how to open a position
- 7:17using a market order which costs to open
- 7:19the position right after submitting the
- 7:21order I also showed you how to use the
- 7:24opted position method which is executed
- 7:26at the moment of closing the next candle
- 7:28so for example if I were to trade the
- 7:30one minute time frame that would have
- 7:32been 60 seconds later but what if I were
- 7:35to use a limit order in that case the
- 7:37position would not have opened until the
- 7:40order was executed and the thing with
- 7:42limit orders are that we never know when
- 7:45they are getting executed so if the
- 7:47current price of BTC is 20 000 my limit
- 7:50orders price could be 19 000. now let's
- 7:54imagine that the price of BTC started to
- 7:56go down after this and 30 seconds later
- 8:00the limit order was filled this will
- 8:02lead to opening a long position and
- 8:05right after opening the position what I
- 8:07want to do is to submit my stop loss and
- 8:09take profit orders now where should I
- 8:12write that I could write it inside the
- 8:14opted position method but the problem
- 8:16with that is it only gets called when
- 8:19the next candle closes which is not good
- 8:21enough because in this case that means I
- 8:23have to wait another 30 seconds before
- 8:25submitting my exit orders even worse if
- 8:28I were to trade bigger time frames such
- 8:30as one hour or four hours I might have
- 8:33had to wait hours before submitting my
- 8:36exit orders right so check this out to
- 8:39say I want to do something when the
- 8:41position opens I can use Jesse's
- 8:43built-in method called on open position
- 8:46to submit this stop loss order I will
- 8:48write self loss equals the quantity is 1
- 8:52and the price could be one thousand
- 8:54below the entry price which is 18 000
- 8:57and then I'll do the same for take
- 8:59profit which will have the quantity of 1
- 9:01and the price of it could be one
- 9:03thousand above the entry price of my
- 9:05position which is 20 000. since the
- 9:08price of my entry order was Nineteen
- 9:11thousand the current price at this
- 9:13moment will also be 19 000. and just to
- 9:17be clear because this one is below the
- 9:19current price it will be a sub order and
- 9:22this one because it is above the current
- 9:24price it'll be a limit order now let's
- 9:27imagine another candle closes and I want
- 9:30to update my take profit and stop loss
- 9:32orders how would I go about that
- 9:35inside my update position method and by
- 9:38the way let's imagine at this point the
- 9:40current price is 19 500. all I need to
- 9:43do is to define the orders one more time
- 9:46just like I did before the take profit
- 9:48could be 19 600
- 9:51which again will be a limit order
- 9:54and the sub loss could be
- 9:57at the price of 19 400 which will be a
- 10:02surplus because again it is below the
- 10:05current price so all I needed to do was
- 10:08to submit the orders just like I did
- 10:10before but there's some with different
- 10:12prices and behind the scenes Jesse will
- 10:15pick up on this cancel the previous
- 10:17orders and submit the new ones for me
- 10:21all right I just showed you how to hook
- 10:23into the unopened position event which
- 10:25is the most commonly used event that you
- 10:28need to know about but if you check out
- 10:29the documentation you'll see that
- 10:31whatever that happens to your position
- 10:33there's a built-in event for it which
- 10:35you can hook into for instance just like
- 10:37we have one for when the position opens
- 10:40we also have one for when it closes we
- 10:43also have one for when the position is
- 10:45increased in size which is called on
- 10:48increased position
- 10:51and we also have one for when the
- 10:54position is reduced and it is called
- 10:56unreduced position
- 10:59all right let me just copy
- 11:02this one and paste it
- 11:04with the other events right that's
- 11:07better so all you need to remember is
- 11:09this whenever something happens to your
- 11:11position a relative event is fired which
- 11:14you can hook into to perform some kind
- 11:16of operation now that could be logging
- 11:19something submitting an order or
- 11:22updating a variable or anything else
- 11:25that you have in mind in the next video
- 11:27I'm going to use the methods that I
- 11:29introduced in this overview to write an
- 11:31actual strategy using Dynamic values and
- 11:34not just hardcoded ones so if you
- 11:36haven't mastered these methods yet that
- 11:38is perfect to find just make sure to
- 11:40watch the next episode of this series
- 11:41Almost right away
About this transcript
This page contains the full transcript of 3. How Strategies Work - Algo trading from scratch with Python by Algo-trading with Saleh, generated from the public captions YouTube serves with the video. The transcript has 2,188 words across 305 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.