YouTube2Text

3. How Strategies Work - Algo trading from scratch with Python — Transcript

by Algo-trading with Saleh · 2,188 words · 305 segments · language en · Watch on YouTube

Full transcript

  1. 0:02having an idea for trading strategy is
  2. 0:05great but how do you turn it into code
  3. 0:07how will you define your entry and exit
  4. 0:10rules where will you define your
  5. 0:12position sizing these are the typical
  6. 0:14questions we need to answer When
  7. 0:16developing a strategy Jesse answers
  8. 0:18these questions by giving you a clear
  9. 0:19framework for defining your strategies
  10. 0:22in this video I want to show you an
  11. 0:24overview of that framework in the next
  12. 0:26videos of this series I will show you
  13. 0:27how to code in real world examples
  14. 0:29alright let's get started
  15. 0:35each strategy instance goes through a
  16. 0:37series of execution cycle steps
  17. 0:39regularly each cycle begins when a
  18. 0:42candle is closed when the execution
  19. 0:43begins it runs a series of special
  20. 0:45methods that we call them hooks let's go
  21. 0:48through examples one by one so it will
  22. 0:50be easier to digest
  23. 0:52this is a flowchart of the execution
  24. 0:54cycle hopes for example it observes if
  25. 0:56there is any open position if there is
  26. 0:58one it needs to check whether or not it
  27. 1:00should update it if there isn't any it
  28. 1:02needs to check if an entry signal is
  29. 1:04green for opening a new position for the
  30. 1:06first scenario let's imagine that we
  31. 1:08just started trading we don't have any
  32. 1:10open positions and we haven't submitted
  33. 1:12any orders either after a candle closes
  34. 1:15the first function that gets executed is
  35. 1:17called before
  36. 1:19I'm gonna show you how to write a
  37. 1:21simplified versions of these functions
  38. 1:23that I'm about to show you so I'm gonna
  39. 1:26remove all the code that's shipped with
  40. 1:27the example strategy the first method is
  41. 1:30called before and notice that its return
  42. 1:33type is none which means it doesn't have
  43. 1:34to return anything
  44. 1:36this method isn't used that often
  45. 1:38however whenever that you have to do
  46. 1:40something before executing the strategy
  47. 1:43itself you could write it in here an
  48. 1:45example of this could be just logging
  49. 1:47something we also have the opposite of
  50. 1:49the before method which is called after
  51. 1:51and obviously it is called after
  52. 1:53everything else is executed in your
  53. 1:55strategy
  54. 1:56all right after the before function we
  55. 1:59have to ask ourselves do we have any
  56. 2:00open positions and if the answer is no
  57. 2:02do we have any active entry orders in
  58. 2:05this case the answer is no so we move on
  59. 2:08to the next function which is called
  60. 2:09Shoot long and after that we have the
  61. 2:11golong function if you're trading on a
  62. 2:13Futures Market you also have access to
  63. 2:15shoot short and go short methods which
  64. 2:18are used for opening short positions but
  65. 2:20if you're trading on the spot Market you
  66. 2:22can just ignore them let's define the
  67. 2:24shoot long method the return type is a
  68. 2:27Boolean which means it has to return
  69. 2:28either true or false
  70. 2:31and then we have the golong method which
  71. 2:33is where we Define our entry order the
  72. 2:35return type is none which means it
  73. 2:37doesn't have to return anything all we
  74. 2:39need to do here is to define the buy
  75. 2:41order
  76. 2:42let's assume the current price of
  77. 2:43Bitcoin is at twenty thousand and we
  78. 2:45want to submit limit by order for the
  79. 2:48sake of keeping this example simple I
  80. 2:50will just hard code the amount of the
  81. 2:52order so I'll just buy one Bitcoin 1000
  82. 2:55below the current price which is
  83. 2:56nineteen thousand and because the price
  84. 2:59of the order is below the current price
  85. 3:01Jesse will submit a limit order
  86. 3:03now next we have filters which are
  87. 3:05conditions that we Define to filter out
  88. 3:07bad traits I'm going to show you real
  89. 3:10world examples of filters in a future
  90. 3:12video but for now let's imagine that our
  91. 3:14strategy doesn't have any filters or
  92. 3:16that it does and they are all passing at
  93. 3:19this step Jesse will submit our entry
  94. 3:20order so once again the before method
  95. 3:23gets called and just like before we
  96. 3:25don't have any open positions so the
  97. 3:26answer to this question is no
  98. 3:28but the answer to the next question
  99. 3:30which is do we have any active orders is
  100. 3:32now yes and now the question is should
  101. 3:35we cancel the previous limit order and
  102. 3:37maybe submit a new one or should we just
  103. 3:40do nothing and wait for the previous
  104. 3:42order to get executed eventually to tell
  105. 3:45Jesse how to do this we're going to have
  106. 3:46to define a method called should cancel
  107. 3:49entry which Returns the Boolean value if
  108. 3:52it returns true then Jesse is going to
  109. 3:54cancel the previous order if it returns
  110. 3:56false it's not going to do anything in
  111. 3:58this case I actually wanted to cancel
  112. 4:00the previous order and submit a new one
  113. 4:02based on the current market price which
  114. 4:04takes us to the next cycle of Step which
  115. 4:06is the after method now that our entry
  116. 4:08order is a limit order let's imagine
  117. 4:10that we waited but the order didn't get
  118. 4:12executed now the question is what should
  119. 4:14happen when the next candle is closed
  120. 4:16let's imagine that the current price of
  121. 4:18Bitcoin is at 21 000 and once again I
  122. 4:21want to buy 1000 below the current price
  123. 4:23which in this case will be twenty
  124. 4:25thousand
  125. 4:27so I have to make sure that my should
  126. 4:29cancel entry method is returning true
  127. 4:32so one more time just to confirm this is
  128. 4:34going to cancel my previous limit order
  129. 4:36at the price of 19 000 and execute the
  130. 4:39shoot long and go long method once again
  131. 4:42that means it's going to submit a new
  132. 4:44limit order at the price of 20 000.
  133. 4:47right for the next scenario let's
  134. 4:49imagine that I didn't use the limit
  135. 4:51order instead I was trying to enter a
  136. 4:55long position using a market order to
  137. 4:57submit a market order using Jesse I need
  138. 4:59to make sure that the price of my order
  139. 5:01is the same as the current price which
  140. 5:03in this case is 21
  141. 5:05000. in this case the return value of
  142. 5:07shoot cancel entry doesn't really matter
  143. 5:09but remember that you still have to
  144. 5:11define it so next time that the candle
  145. 5:13closes the question is how should we
  146. 5:15update the open position we can answer
  147. 5:17that question by defining a method
  148. 5:19called update position which is then
  149. 5:21supposed to return anything but it
  150. 5:23allows us to update our currently open
  151. 5:25position Now update in this case means
  152. 5:28adding to the size of the position
  153. 5:29reducing it or maybe even closing it
  154. 5:32let's begin by adding to the size of the
  155. 5:34current position so right now I have a
  156. 5:37position with the size of one Bitcoin
  157. 5:39let's say I want to buy another Bitcoin
  158. 5:42at the current price
  159. 5:44and let's imagine that the current price
  160. 5:45of Bitcoin is at 21 000. so adding to
  161. 5:49the size of the current position is very
  162. 5:51similar to how we open the long position
  163. 5:53in the first place now let's see an
  164. 5:55example of how it is to reduce the size
  165. 5:58of the current position I can do that by
  166. 6:00using either a stop loss or a take
  167. 6:02profit order
  168. 6:03and because the price of the order is
  169. 6:05the current price of BTC it will use a
  170. 6:08market order
  171. 6:09to close the current position all I have
  172. 6:11to do is to increase the size of my
  173. 6:13order to equal the size of my position
  174. 6:15so instead of 0.5 I will enter one this
  175. 6:19will cause the current position to close
  176. 6:20so the answer to this question will
  177. 6:23change from no into yes after this Jesse
  178. 6:26will also cancel any remaining active
  179. 6:28orders now in this case we don't have
  180. 6:30any but you might have one for example
  181. 6:33maybe other than the take profit order
  182. 6:35we also had your stop loss order so now
  183. 6:37that the position is closed we want to
  184. 6:39make sure that the Surplus order is
  185. 6:41canceled before looking for future
  186. 6:43trades after that the S8 is clean to
  187. 6:45look for future trades so Jesse will
  188. 6:47execute the shoot long and go along with
  189. 6:49it one more time to see if it needs to
  190. 6:52open a position right after closing the
  191. 6:54previous one foreign
  192. 7:00we just covered the execution socket
  193. 7:02hooks or methods but how do you hook
  194. 7:04into a specific events what I mean by
  195. 7:06events are things that happen during the
  196. 7:09lifesame candle rather than at the
  197. 7:11closing moment of it all right let me
  198. 7:13show you what I mean by an example I
  199. 7:15just showed you how to open a position
  200. 7:17using a market order which costs to open
  201. 7:19the position right after submitting the
  202. 7:21order I also showed you how to use the
  203. 7:24opted position method which is executed
  204. 7:26at the moment of closing the next candle
  205. 7:28so for example if I were to trade the
  206. 7:30one minute time frame that would have
  207. 7:32been 60 seconds later but what if I were
  208. 7:35to use a limit order in that case the
  209. 7:37position would not have opened until the
  210. 7:40order was executed and the thing with
  211. 7:42limit orders are that we never know when
  212. 7:45they are getting executed so if the
  213. 7:47current price of BTC is 20 000 my limit
  214. 7:50orders price could be 19 000. now let's
  215. 7:54imagine that the price of BTC started to
  216. 7:56go down after this and 30 seconds later
  217. 8:00the limit order was filled this will
  218. 8:02lead to opening a long position and
  219. 8:05right after opening the position what I
  220. 8:07want to do is to submit my stop loss and
  221. 8:09take profit orders now where should I
  222. 8:12write that I could write it inside the
  223. 8:14opted position method but the problem
  224. 8:16with that is it only gets called when
  225. 8:19the next candle closes which is not good
  226. 8:21enough because in this case that means I
  227. 8:23have to wait another 30 seconds before
  228. 8:25submitting my exit orders even worse if
  229. 8:28I were to trade bigger time frames such
  230. 8:30as one hour or four hours I might have
  231. 8:33had to wait hours before submitting my
  232. 8:36exit orders right so check this out to
  233. 8:39say I want to do something when the
  234. 8:41position opens I can use Jesse's
  235. 8:43built-in method called on open position
  236. 8:46to submit this stop loss order I will
  237. 8:48write self loss equals the quantity is 1
  238. 8:52and the price could be one thousand
  239. 8:54below the entry price which is 18 000
  240. 8:57and then I'll do the same for take
  241. 8:59profit which will have the quantity of 1
  242. 9:01and the price of it could be one
  243. 9:03thousand above the entry price of my
  244. 9:05position which is 20 000. since the
  245. 9:08price of my entry order was Nineteen
  246. 9:11thousand the current price at this
  247. 9:13moment will also be 19 000. and just to
  248. 9:17be clear because this one is below the
  249. 9:19current price it will be a sub order and
  250. 9:22this one because it is above the current
  251. 9:24price it'll be a limit order now let's
  252. 9:27imagine another candle closes and I want
  253. 9:30to update my take profit and stop loss
  254. 9:32orders how would I go about that
  255. 9:35inside my update position method and by
  256. 9:38the way let's imagine at this point the
  257. 9:40current price is 19 500. all I need to
  258. 9:43do is to define the orders one more time
  259. 9:46just like I did before the take profit
  260. 9:48could be 19 600
  261. 9:51which again will be a limit order
  262. 9:54and the sub loss could be
  263. 9:57at the price of 19 400 which will be a
  264. 10:02surplus because again it is below the
  265. 10:05current price so all I needed to do was
  266. 10:08to submit the orders just like I did
  267. 10:10before but there's some with different
  268. 10:12prices and behind the scenes Jesse will
  269. 10:15pick up on this cancel the previous
  270. 10:17orders and submit the new ones for me
  271. 10:21all right I just showed you how to hook
  272. 10:23into the unopened position event which
  273. 10:25is the most commonly used event that you
  274. 10:28need to know about but if you check out
  275. 10:29the documentation you'll see that
  276. 10:31whatever that happens to your position
  277. 10:33there's a built-in event for it which
  278. 10:35you can hook into for instance just like
  279. 10:37we have one for when the position opens
  280. 10:40we also have one for when it closes we
  281. 10:43also have one for when the position is
  282. 10:45increased in size which is called on
  283. 10:48increased position
  284. 10:51and we also have one for when the
  285. 10:54position is reduced and it is called
  286. 10:56unreduced position
  287. 10:59all right let me just copy
  288. 11:02this one and paste it
  289. 11:04with the other events right that's
  290. 11:07better so all you need to remember is
  291. 11:09this whenever something happens to your
  292. 11:11position a relative event is fired which
  293. 11:14you can hook into to perform some kind
  294. 11:16of operation now that could be logging
  295. 11:19something submitting an order or
  296. 11:22updating a variable or anything else
  297. 11:25that you have in mind in the next video
  298. 11:27I'm going to use the methods that I
  299. 11:29introduced in this overview to write an
  300. 11:31actual strategy using Dynamic values and
  301. 11:34not just hardcoded ones so if you
  302. 11:36haven't mastered these methods yet that
  303. 11:38is perfect to find just make sure to
  304. 11:40watch the next episode of this series
  305. 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.