YouTube2Text

4. Entering and Exiting Trades - Algo trading from scratch with Python — Transcript

by Algo-trading with Saleh · 1,799 words · 263 segments · language en · Watch on YouTube

Full transcript

  1. 0:01[Music]
  2. 0:02Jesse gives you a clear framework for
  3. 0:04defining your strategies in some cases
  4. 0:06though there are multiple ways for
  5. 0:08performing the same task such as exiting
  6. 0:10trades while this brings flexibility it
  7. 0:13might be confusing to beginners so in
  8. 0:15this video I want to show you various
  9. 0:16ways you have for entering and exiting
  10. 0:19trades in Jesse let's begin by creating
  11. 0:21a new strategy
  12. 0:24and name it
  13. 0:25golden
  14. 0:26cross
  15. 0:28right so if I open my vs code and look
  16. 0:31up Golden I should find it and in case
  17. 0:34you're curious to know where it's
  18. 0:35located it's inside my strategies folder
  19. 0:38foreign
  20. 0:43there is only one way of entering trades
  21. 0:46in Jessie but it is a 2s sub process
  22. 0:48assuming you're looking for long
  23. 0:50positions you will start by defining
  24. 0:52your interior inside the shoot long
  25. 0:54method which returns a Boolean value
  26. 0:57when it returns true then it is followed
  27. 0:59by the golang method which is where you
  28. 1:02define your actual entry order an easy
  29. 1:04way of remembering this is to ask
  30. 1:06ourselves should we long this asset if
  31. 1:10yes then let's go long all right in case
  32. 1:12of short positions we have shoot short
  33. 1:15and go short methods but they only work
  34. 1:18for Futures Trading so just to keep it
  35. 1:20simple I'm gonna trade only long
  36. 1:22positions in this video so I'll just
  37. 1:24remove these because we don't need them
  38. 1:26all right let's write a real world
  39. 1:28example this is the chart for BTC usdt
  40. 1:31on trading View and I've added two
  41. 1:33moving averages the blue line is the EMA
  42. 1:35with a period of 20 and the orange one
  43. 1:38is the EMA with a period of 50. and the
  44. 1:41time frame of the chart is 15 minutes
  45. 1:44let's say I want to go long whenever the
  46. 1:46EMA 20 crosses the email 50. I'm gonna
  47. 1:49use Jesse's indicator module which has
  48. 1:52already been imported for me I'll Define
  49. 1:54two new properties the first one I'm
  50. 1:57going to call Ema 20.
  51. 2:00inside it I'm going to return ta which
  52. 2:04is the indicator module of Jesse and to
  53. 2:07access the EMA I will say dot EMA the
  54. 2:10first expected parameter of it is
  55. 2:12candles which I can pass by saying self
  56. 2:14candles
  57. 2:16and the second one is the period which I
  58. 2:18will pass Tony
  59. 2:20all right next I will just copy this
  60. 2:22code and paste it and change 20 to 50.
  61. 2:27next we need to define the shoot log
  62. 2:30method I'll check if the ema20 crosses
  63. 2:33the ema50
  64. 2:34if it does then I'll return true next
  65. 2:37I'm going to update the colon method
  66. 2:40which is where we Define the entry order
  67. 2:42when it comes to submitting orders we
  68. 2:44need two things the quantity and the
  69. 2:47price I want to keep this example super
  70. 2:49simple so as for the entry price I will
  71. 2:51just use the current price
  72. 2:53as for the quantity I'll just use half
  73. 2:56of the existing balance in my account
  74. 2:58the capital in my account is in your
  75. 3:00city so I'll need to convert it to BTC
  76. 3:03to do that I'll use the size to quantity
  77. 3:06utility function of Jesse and just like
  78. 3:08the indicator module the utils module
  79. 3:11has already been imported for me
  80. 3:14the first parameter of it is the
  81. 3:16position size like I said I want to use
  82. 3:19half of my Capital so I'll just pass
  83. 3:22self dot balance
  84. 3:25and because I want half of it I will
  85. 3:27multiply this by 0.5 and the second
  86. 3:30parameter is the entry price which I
  87. 3:32just defined earlier
  88. 3:34and finally I can submit my entry order
  89. 3:37because this is for a long position I
  90. 3:39will say self dot pi equals
  91. 3:43the first parameter is the quantity and
  92. 3:46the second one is the entry price and
  93. 3:48because the entry price of the order is
  94. 3:50the current price this is going to be a
  95. 3:52market order because I'm using a market
  96. 3:54order when my trading candle closes and
  97. 3:57their strategy gets executed I will
  98. 3:59immediately be sitting on a long
  99. 4:01position okay that's good but how do we
  100. 4:04exit this position the first way could
  101. 4:06be to Simply close this position
  102. 4:07whenever the opposite of our interior
  103. 4:10rule is true just to make it simpler I'm
  104. 4:12gonna refactor my entry rule
  105. 4:15I'll Define a new property and call it
  106. 4:18trend
  107. 4:20for an uptrend
  108. 4:22I will say if
  109. 4:24self EMA 20 is above self EMA 50 return
  110. 4:31one
  111. 4:32and else which means downtrends return
  112. 4:35-1 then I can refactor my shoot long
  113. 4:38method into return to when self.trend is
  114. 4:42one so now one way to exit this trade is
  115. 4:45this whenever a new candle closes we
  116. 4:48check to see if the trend is downward if
  117. 4:51so then we'll close the position to
  118. 4:53write the code for this I'm gonna Define
  119. 4:55the update position method and inside it
  120. 4:58I'm gonna say
  121. 5:00if self-trend is -1
  122. 5:04then I want to close the position
  123. 5:06immediately or in other words using a
  124. 5:09market order to do that I can use either
  125. 5:12the self-take profit or self Surplus
  126. 5:15variables it doesn't really matter but
  127. 5:17I'll just use this Surplus
  128. 5:19the first parameter is the quantity
  129. 5:21which I will use the quantity of my
  130. 5:24current position by saying self position
  131. 5:26dot quantity
  132. 5:28and the second one is the price because
  133. 5:30I wanted to be a market order I will
  134. 5:32pass self dot price
  135. 5:34because it is a common use case you want
  136. 5:37to close your current position using a
  137. 5:39market order Jesse offers a shortcut
  138. 5:41method for it and I can call it by
  139. 5:44saying self.liquidit since this is a lot
  140. 5:46cleaner I'm gonna remove this line
  141. 5:49okay this sounds good but what if I knew
  142. 5:53at which price I wanted to close my
  143. 5:55position at the time that the position
  144. 5:57is opened maybe I want to exit at two
  145. 6:00times the range of the current candle
  146. 6:02all right let me show you what I mean by
  147. 6:04the range of the candle on an actual
  148. 6:06chart if this is a current candle at the
  149. 6:08time of entering this trade the range of
  150. 6:10it is going to be the difference between
  151. 6:12the low and the high of it and two times
  152. 6:15of the range could be somewhere around
  153. 6:17here
  154. 6:18all right so I'm going to define a new
  155. 6:20property and call it current range
  156. 6:24it's going to return
  157. 6:26the high price of the grand candle
  158. 6:28subtracted by the low price of the grand
  159. 6:31candle
  160. 6:32so now I can set my exit orders at the
  161. 6:35time of opening the position to do that
  162. 6:37we're gonna have to use the on open
  163. 6:39position method first let's do the
  164. 6:41Surplus the quantity should be the
  165. 6:44quantity of my current position
  166. 6:46and the price will be the current price
  167. 6:50subtracted by
  168. 6:52self current range which we just defined
  169. 6:56earlier
  170. 6:57and because I want two times of that I'm
  171. 6:59going to multiply this by 2.
  172. 7:02the take profit is going to be very
  173. 7:04similar
  174. 7:05the quantity will again be the quantity
  175. 7:08of my current position and the price of
  176. 7:10it will be the current price but this
  177. 7:12time added by the current range
  178. 7:14multiplied by 2.
  179. 7:16all right so now when the position opens
  180. 7:19Jesse will submit both the stop loss and
  181. 7:22take profit orders right away
  182. 7:29right we just learned how to both enter
  183. 7:31and exit a trade now let's take it up a
  184. 7:34notch instead of exiting my position all
  185. 7:37at once maybe I want to exit half my
  186. 7:39position at the initial take profit
  187. 7:41price and exit the other half using some
  188. 7:43kind of a trailing stop this might sound
  189. 7:45a little Advanced but I promise it's
  190. 7:48incredibly easy to write
  191. 7:49to do that the first thing I need to do
  192. 7:51is to modify my initial take profit
  193. 7:54order to only exit half the position
  194. 7:56size to do that all I need to do is to
  195. 7:59divide the quantity by two
  196. 8:01next to exit the second half of my
  197. 8:03position inside my opted position method
  198. 8:05I will use this built-in property called
  199. 8:07reduce count which tells me how many
  200. 8:10times the position size has been reduced
  201. 8:12since the trade was opened now in this
  202. 8:14case we only reduce the position once
  203. 8:16and then we want to close it so I'll
  204. 8:18just say if self reduce count is 1.
  205. 8:23self Surplus equals the quantity will be
  206. 8:27the quantity of my current position at
  207. 8:30this time and the price will be the
  208. 8:33current price subtracted by self current
  209. 8:36range
  210. 8:38right but why is this a trailing stop
  211. 8:40that's because at this point we know
  212. 8:42that the opted position method will get
  213. 8:44executed on every new candle close
  214. 8:46assuming of course that our stop loss
  215. 8:49hasn't been executed yet and the
  216. 8:51position is still open so every time
  217. 8:53that the update position gets executed
  218. 8:54it will cancel the previous stop loss
  219. 8:57and submit a new one based on the
  220. 8:58current price conditions I will also
  221. 9:00modify this line into L if just to make
  222. 9:04sure that my position won't get close
  223. 9:06before the sub price is reached
  224. 9:13right now just to make sure that my code
  225. 9:15Works let's try to run a back test
  226. 9:18I will change a strategy to Golden Cross
  227. 9:21the time frame 15 minutes is fine
  228. 9:25the symbol is correct and the exchange
  229. 9:27is also correct I want to see some
  230. 9:30charts so I will enable this option
  231. 9:32I will also enable quantstat reports and
  232. 9:36then I will change the duration of my
  233. 9:37backtest to be the first quarter of
  234. 9:402021.
  235. 9:43right let's start the back test
  236. 9:47and we get an error
  237. 9:49right it says the format of the self
  238. 9:51take profit is invalid let's take a look
  239. 9:54at the strategy and see what I did wrong
  240. 9:59okay I see my mistake here the price of
  241. 10:01my take profit order was supposed to be
  242. 10:03the current price added by the current
  243. 10:05range property but instead I wrote
  244. 10:08current candle okay so with any luck if
  245. 10:11I run the back test again it should work
  246. 10:13this time all right it works just fine
  247. 10:16here's the chart for the equity curve
  248. 10:20and here are the metrics which are
  249. 10:23negative by the way which I guess we
  250. 10:25kind of had it coming because first of
  251. 10:27all this is an example strategy and
  252. 10:29second of all it's just a work in
  253. 10:30progress we can also take a look at the
  254. 10:32Legacy chart which is useful because it
  255. 10:35shows us to buy and sell orders executed
  256. 10:37by Jesse
  257. 10:38we also enable the constat rewards so
  258. 10:41let's take a look at that and here we
  259. 10:43have access to a lot of more metrics and
  260. 10:46charts that are useful for evaluating
  261. 10:48the strategies performance in the next
  262. 10:51video we're gonna learn about the
  263. 10:52concept of filters in Jesse

About this transcript

This page contains the full transcript of 4. Entering and Exiting Trades - Algo trading from scratch with Python by Algo-trading with Saleh, generated from the public captions YouTube serves with the video. The transcript has 1,799 words across 263 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.