YouTube2Text

Simple SWING trading strategy using EMAs in Python that delivers IMPRESSIVE results! — Transcript

by Algo-trading with Saleh · 3,066 words · 406 segments · language en · Watch on YouTube

Full transcript

  1. 0:00hey guys it's Sol today I want to write
  2. 0:01a swing Trend following strategy that
  3. 0:04means we're going to try to write a
  4. 0:05trend for as long as possible and only
  5. 0:08exit when we think the trend is over
  6. 0:10next I will use this strategy as the
  7. 0:12base for my next video which is going to
  8. 0:14be about using the genetic algorithm to
  9. 0:16optimize the results that we already
  10. 0:18have so let's get into it the main
  11. 0:21indicators that I want to use in this
  12. 0:23strategy are going to be three moving
  13. 0:25average lines now I'm using the EMA the
  14. 0:27purple line here has the period of 20 1
  15. 0:30the yellow one has a period of 50 and
  16. 0:33the blue one has a period of 100 now the
  17. 0:36way you can change that on trading view
  18. 0:37by the way is by going here and changing
  19. 0:40the length all right so we want to open
  20. 0:43a long position only when the 21 period
  21. 0:47is above the 50 and both of them are
  22. 0:49above the blue line here which is 100
  23. 0:52period so this would be an example of
  24. 0:54when we would go long now here's an
  25. 0:55example for a short trade and obviously
  26. 0:57all the conditions now have to be the
  27. 0:59opposite for for us to take a short
  28. 1:00trade so here which is the purple line
  29. 1:0321 moving average is below the yellow
  30. 1:06one and both of them are below the the
  31. 1:08value for the 100 moving average we
  32. 1:10would go short I also have another
  33. 1:12condition because in these cases when we
  34. 1:14are touching the perple line I don't
  35. 1:16want to go short so I will also say that
  36. 1:18I want to make sure the price is below
  37. 1:20the purple line so now let's go to Jesse
  38. 1:23and create a new strategy I'm going to
  39. 1:25name it Trend swing Trader
  40. 1:30now if I go back to my editor and look
  41. 1:33for it I will find it here right so this
  42. 1:36is the code that Jesse just generated
  43. 1:38for us and we're going to edit it so the
  44. 1:40first thing I'm going to do is to define
  45. 1:41a new property and name it Trend and in
  46. 1:45it we're going to Define three moving
  47. 1:47averages so the first one is going to be
  48. 1:49ta EMA and I will pass in the current
  49. 1:53candles and as for the period I will
  50. 1:55pass 21 the second one is going to be
  51. 1:58the same code but with the period perod
  52. 2:00of 50 and the third one is going to be
  53. 2:02the same but with a period of 100 now my
  54. 2:04condition is pretty simple if
  55. 2:07E3 is smaller than
  56. 2:10E2 and that is smaller than E1 smaller
  57. 2:14than the current price then we are in
  58. 2:16uptrend so I will return
  59. 2:19one otherwise if the condition is the
  60. 2:23opposite I will return minus one and if
  61. 2:27none of the above is true I will return
  62. 2:29zero which means we are in a ranging
  63. 2:30market so my shootong method is going to
  64. 2:33be simply return When the current Trend
  65. 2:38equals 1 and the opposite will go for
  66. 2:42the shoot short method so we want to
  67. 2:45make sure the trend is minus one in
  68. 2:47other words in a downtrend as for
  69. 2:49position sizing first I'm going to give
  70. 2:50it my entire Capital to just open the
  71. 2:53position with the market order so I'm
  72. 2:54going to say Quantity equals uols size
  73. 2:58to quantity first I'm going to pass my
  74. 3:01entire available margin and for entry I
  75. 3:05will pass the current price actually
  76. 3:08let's create a new variable here for it
  77. 3:11so entry is going to
  78. 3:13be self
  79. 3:17price and as for fee rate I will pass in
  80. 3:21self fee rate so my buy order is going
  81. 3:26to be quantity and then the entry and I
  82. 3:28will do the opposite for my go short
  83. 3:36Method All right
  84. 3:39so and we don't really care about the
  85. 3:41shoot cancel entry because we are
  86. 3:42opening the position with the market
  87. 3:44order so it doesn't really matter if
  88. 3:46this function is returning true or false
  89. 3:48all right so we have our entry rules now
  90. 3:50but what about our exit rules when would
  91. 3:52we exit this trade what would be our
  92. 3:54take profit and stoploss orders so for
  93. 3:56that let's go back to the Chart as for
  94. 3:58the Stop Plus I'm going to use the ATR
  95. 4:00indicator which is one of my favorites
  96. 4:02when it comes to setting the suus what
  97. 4:04it does is it tells us the average price
  98. 4:07that every candle is changing so for
  99. 4:09example in this point in time when we
  100. 4:11are opening the position the average
  101. 4:13true range is $
  102. 4:17886 so if I do it like this and this is
  103. 4:21going to be my subplus this would be
  104. 4:24that value
  105. 4:25880 but because this is too short I'm
  106. 4:28going to use two times of this this so I
  107. 4:31will increase
  108. 4:32this to something around here and for my
  109. 4:35take profit I will use something similar
  110. 4:38but because this is a swing trading
  111. 4:40strategy we want to be able to write the
  112. 4:42trend for as long as possible so here's
  113. 4:44what I'm going to do I will exit half my
  114. 4:46position at this point and when we hit
  115. 4:48this point I'm going to update my suplus
  116. 4:51to become a bit lower and we're going to
  117. 4:53keep doing this for as long as the trend
  118. 4:55goes down so this way we can write the
  119. 4:57trend for as long as possible and
  120. 4:58hopefully we'll be able to close it
  121. 5:00somewhere around here all right so let's
  122. 5:02go back to the code I'm going to say
  123. 5:05when the position opens and I'm going to
  124. 5:07use the unopen position method for this
  125. 5:09if it's a long position I will say my
  126. 5:12suplus order is going to be well first
  127. 5:15we need to pass the quantity and that's
  128. 5:17going to be the current positions
  129. 5:20quantity and the price is going to be
  130. 5:22the current price minus the current ATR
  131. 5:26multiplied by two and my take profit
  132. 5:29it's going to be the same but multiply
  133. 5:32it by three but as I said I want to exit
  134. 5:35only half my position at this point not
  135. 5:37the entire so I will divide this by two
  136. 5:40and if it's a short position I will do
  137. 5:43the
  138. 5:44opposite so in a short position my
  139. 5:46stopus is going to be two times of the
  140. 5:48ATR above the current price and my take
  141. 5:51profit I mean half of it is going to be
  142. 5:54the current price minus three times of
  143. 5:58the current ATR all right so we are
  144. 6:00exiting half of our position this way we
  145. 6:03also have our suplus but what about that
  146. 6:05other half of our take profit when this
  147. 6:07order is executed I want to bring down
  148. 6:09my stop l order obviously I'm talking
  149. 6:12about a short position here if it was a
  150. 6:14long position it would be the other way
  151. 6:15and I want to use some kind of trailing
  152. 6:18now how can we do that with J so here's
  153. 6:20how I'm going to Define another method
  154. 6:22called unreduced position now this event
  155. 6:25method will get triggered right after
  156. 6:27the size of my position gets reduced now
  157. 6:30notice the word reduced here it means
  158. 6:32it's not closed it's just reduced in our
  159. 6:34case it will get reduced in half so if
  160. 6:37that's the case I'm going to say if it's
  161. 6:40a long
  162. 6:42position my
  163. 6:44stoploss is going to be the quantity of
  164. 6:47my position at this point in time which
  165. 6:49is going to be self. position quantity
  166. 6:52and as for the price I want it to be at
  167. 6:54my entry price so this way even if the
  168. 6:57price goes against me I will exit the
  169. 6:59second half of my position in break even
  170. 7:01so the first half gave me profits the
  171. 7:04second half would give me nothing it
  172. 7:06will also lose me nothing so I will
  173. 7:08still sit in profit so I will say
  174. 7:12self-position do entry price now I won't
  175. 7:15do any take profit because if this
  176. 7:17method is getting executed that means my
  177. 7:20take profit order has already been
  178. 7:22executed so we don't have any take
  179. 7:23profit Order anymore so we don't need to
  180. 7:25update it either and our suplus is
  181. 7:28getting updated to be here here now so
  182. 7:30by the way when you do this Jesse will
  183. 7:33automatically cancel the previous stop
  184. 7:35order and submit the new one for us so
  185. 7:37we don't need to worry about canceling
  186. 7:39that previous one so all I need to say
  187. 7:40at this point is to say l if it's a
  188. 7:43short position do the same thing all
  189. 7:45right now if you remember I said I want
  190. 7:47to exit the second half of my position
  191. 7:49with some kind of trailing sub order and
  192. 7:51right now we only set the suplus once
  193. 7:54and update it twice here so how would it
  194. 7:57be trailing because let's say the trend
  195. 7:59keeps going down and in that case we
  196. 8:02need to keep updating that order right
  197. 8:04so the way I do that is I Define another
  198. 8:07method called update position and in it
  199. 8:10I'm going to say if it's a long position
  200. 8:12the subplus is going to
  201. 8:15be the current positions quantity and
  202. 8:18the price is going to be the current
  203. 8:21price minus the current
  204. 8:24ATR multiplied by two and for a short
  205. 8:27position it would be the current price
  206. 8:29added by two times of the current ATR
  207. 8:32but here's the thing if I do it just
  208. 8:34like this it will always update that
  209. 8:36suus even if the price is going against
  210. 8:39us so I'm going to update my code that
  211. 8:42this is going to be the minimum of
  212. 8:44either this or the current positions
  213. 8:47entry price which was our previous stop
  214. 8:50price that we set here so I'm also going
  215. 8:52to do the same here all right so we're
  216. 8:56almost finished but there's one more
  217. 8:58thing we need to do so so if I run this
  218. 9:00strategy right now this update position
  219. 9:03method is going to get executed on every
  220. 9:05candle and that means it will even get
  221. 9:07executed before this method is executed
  222. 9:10and that's not cool because first we
  223. 9:12need one of these orders to get executed
  224. 9:15and then when the position is reduced
  225. 9:18then we want to update our suplus on
  226. 9:21every new candle after that so here I'm
  227. 9:24going to add one condition I will say if
  228. 9:27the reduced count equals =
  229. 9:301 then I want to do this all right so we
  230. 9:33are ready to execute our first back test
  231. 9:35now let's go back to
  232. 9:37Jesse this exchange is fine this symbol
  233. 9:41is also fine and as for time frame I
  234. 9:43will choose the 4
  235. 9:45hours and I will pick my
  236. 9:49strategy and I'm back testing it since
  237. 9:51the beginning of 2022 up until a few
  238. 9:54days ago the fast mode is on and so is
  239. 9:57the Benchmark option all right let's
  240. 10:00execute it all right so it looks
  241. 10:03actually pretty okay and we are beating
  242. 10:05the
  243. 10:07market the max throw down isus
  244. 10:1023% and the
  245. 10:12profit is 77% the win rate is
  246. 10:1647% and we have an average win to loss
  247. 10:20ratio of 1.4 okay so this looks okay but
  248. 10:23I believe we can improve it so the first
  249. 10:25thing I'm going to do is to do what I
  250. 10:27always do for my position sizing cuz
  251. 10:29right now we are using our entire
  252. 10:31capital for every single trade and
  253. 10:32that's never good so I'm going to go
  254. 10:34back to my source code and in my goong
  255. 10:37method instead of doing it like
  256. 10:40this first I will calculate my sub price
  257. 10:43right here and it's going to be the
  258. 10:45entry minus 2 * of the
  259. 10:48ATR and instead of using this function I
  260. 10:52will use risk to quantity as for the
  261. 10:55first one I will pass my available
  262. 10:58margin the entry will be the current
  263. 11:01price then I will pass the subop price
  264. 11:05as a third argument and the fee rate is
  265. 11:07going to be my current fee all right so
  266. 11:11I will also do the same for my short
  267. 11:15positions and let's go back to Jesse now
  268. 11:18remember these numbers it's 77% profit
  269. 11:21with a Max draw down of minus
  270. 11:2423% let's reexecute
  271. 11:27it all right it's saying we're missing
  272. 11:30one required argument let's go back here
  273. 11:33so I forgot to set the risk per capital
  274. 11:35number which is actually the most
  275. 11:36important number in this function so I'm
  276. 11:39going to risk 3% of my Capital per each
  277. 11:41trade I'll do the same here let's go
  278. 11:44back run it again all right so not only
  279. 11:48I increase our profit slightly but also
  280. 11:51our Max throughout the number is reduced
  281. 11:53so that's pretty cool it's very small
  282. 11:55change in our position sizing made our
  283. 11:57results better even though we using all
  284. 11:59the same indicator entry and exit rules
  285. 12:02so how cool is that all right there's
  286. 12:03another Improvement that I want to do to
  287. 12:05it so if you take a look at the price
  288. 12:07here this is ranging market and our
  289. 12:09Equity curve is going down here this is
  290. 12:11a typical issue with Trend following
  291. 12:13strategies because when you're trading
  292. 12:15the trend you want to avoid the ranging
  293. 12:17conditions and one of the best ways that
  294. 12:19I do that is by using the adx
  295. 12:22indicator so let's remove the ATR the
  296. 12:25way the adx works is that we have a
  297. 12:28value between 0 and 100 and it will not
  298. 12:31tell us the direction of the trend it
  299. 12:33will only tell us the strength of the
  300. 12:35current Trend in other words if I set a
  301. 12:37threshold let's say at number 25
  302. 12:40whenever the value of the ATR is below
  303. 12:42this below this threshold line you can
  304. 12:45see that we are not in a trend and we
  305. 12:48want to avoid trading but whenever the
  306. 12:50value goes above it then we are in a
  307. 12:52trend that's now here we were in an
  308. 12:57uptrend we were also in an uptrend
  309. 12:59here and here we are in a downtrend all
  310. 13:03right let's go to the beginning of my
  311. 13:04strategy let's define a new property
  312. 13:06called the adx and in it I'm going to
  313. 13:10say return whether or not the current
  314. 13:14adx is bigger than my threshold which
  315. 13:19will be 25 and then I will go to my
  316. 13:23Shong method and add another condition I
  317. 13:26will say and self. adx I will also do
  318. 13:28the same for my shoot short
  319. 13:30function all right so let's go back to
  320. 13:34Jesse now remember this profit number
  321. 13:3879% and this Max rodon minus 18 let's
  322. 13:42re-execute it and now not only my profit
  323. 13:47increased drastically my Max zon is also
  324. 13:50reduced to a significantly better number
  325. 13:53and if you take a look at the equity
  326. 13:54curve when the price was in a Range our
  327. 13:57Equity curve did not go down drastically
  328. 13:59anymore the win rate is at 52% and our
  329. 14:03average win to loss ratio is
  330. 14:051.57 so this is pretty cool but I want
  331. 14:09to show you something if I were to back
  332. 14:10test this since the beginning of
  333. 14:15202024 you will get these results which
  334. 14:18look really good but one might say that
  335. 14:21hey we're not beating the market I want
  336. 14:23to make more than what a Buy and Hold
  337. 14:25would give me and I don't really care
  338. 14:27about the volatility well if that's you
  339. 14:30here's why this strategy is still better
  340. 14:32because my Max Ron is minus 10% while
  341. 14:36the market was doing significantly worse
  342. 14:38and my volatility tolerance is much more
  343. 14:41than 10% I can take maybe up minus 30%
  344. 14:45so if I go back to our strategy and
  345. 14:48increase our risk per capital from three
  346. 14:51to let's say five and rerun the back
  347. 14:53test now we are making more and the
  348. 14:56result of it is that the Maxon is just a
  349. 14:59bit worse but it's still significantly
  350. 15:01better than the market itself so if you
  351. 15:04still want to beat the market I can just
  352. 15:05multiply
  353. 15:07this by some kind of number let's say
  354. 15:10two and rerun it now we are making more
  355. 15:14than what the market was making and my
  356. 15:17Maxon is still in acceptable number so I
  357. 15:20can increase my position size even
  358. 15:22further if I have the tolerance for that
  359. 15:24amount of volatility which I personally
  360. 15:27don't but I'm just saying that if your
  361. 15:29goal is to just beat the profits that
  362. 15:31the market would have given you by
  363. 15:32simply buying and holding your positions
  364. 15:34and you don't care about the volatility
  365. 15:36then you can do this now obviously to do
  366. 15:38this we're going to have to use leverage
  367. 15:40and we are using the leverage right now
  368. 15:41in fact my leverage number for this
  369. 15:44exchange is at number two so you can
  370. 15:47even increase that to another number
  371. 15:49such as five or 10 and make huge profits
  372. 15:52but I personally don't do that because
  373. 15:54volatility is really important to me
  374. 15:56because I want to be able to sleep at
  375. 15:57night so you do whatever works for you
  376. 15:59and I do the same for me now before I
  377. 16:01forget I kept working on this strategy
  378. 16:03and I made a premium version of it and I
  379. 16:04submitted it to our strategies index
  380. 16:06page which you can find here and these
  381. 16:08are the results for 2022 so if you are a
  382. 16:11premium user just go ahead and download
  383. 16:13the code for this strategy now for my
  384. 16:15next video I'm going to take this
  385. 16:17strategy that we just wrote and then use
  386. 16:19Jess's optimization mode which uses the
  387. 16:21genetic algorithm to optimize our
  388. 16:23parameters to get better numbers out of
  389. 16:25it if that's something you're interested
  390. 16:27make sure to stick around for the next
  391. 16:28video now before I leave you we have a
  392. 16:30giveaway now a random person who likes
  393. 16:32this video subscribes to the channel and
  394. 16:34posts a comment is going to receive 1
  395. 16:35million bunk token all right let's pick
  396. 16:37the winner for my previous
  397. 16:39video and the winner
  398. 16:41is it was helpful to see someone
  399. 16:44familiar with the framework build a
  400. 16:45complete strategy from a scratch thank
  401. 16:47you so much for your comment please
  402. 16:49reach out to me so that I can send you
  403. 16:50your tokens all right thanks for
  404. 16:52watching and happy Trading
  405. 16:54[Music]
  406. 16:59oh

About this transcript

This page contains the full transcript of Simple SWING trading strategy using EMAs in Python that delivers IMPRESSIVE results! by Algo-trading with Saleh, generated from the public captions YouTube serves with the video. The transcript has 3,066 words across 406 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.