Simple SWING trading strategy using EMAs in Python that delivers IMPRESSIVE results! — Transcript
Full transcript
- 0:00hey guys it's Sol today I want to write
- 0:01a swing Trend following strategy that
- 0:04means we're going to try to write a
- 0:05trend for as long as possible and only
- 0:08exit when we think the trend is over
- 0:10next I will use this strategy as the
- 0:12base for my next video which is going to
- 0:14be about using the genetic algorithm to
- 0:16optimize the results that we already
- 0:18have so let's get into it the main
- 0:21indicators that I want to use in this
- 0:23strategy are going to be three moving
- 0:25average lines now I'm using the EMA the
- 0:27purple line here has the period of 20 1
- 0:30the yellow one has a period of 50 and
- 0:33the blue one has a period of 100 now the
- 0:36way you can change that on trading view
- 0:37by the way is by going here and changing
- 0:40the length all right so we want to open
- 0:43a long position only when the 21 period
- 0:47is above the 50 and both of them are
- 0:49above the blue line here which is 100
- 0:52period so this would be an example of
- 0:54when we would go long now here's an
- 0:55example for a short trade and obviously
- 0:57all the conditions now have to be the
- 0:59opposite for for us to take a short
- 1:00trade so here which is the purple line
- 1:0321 moving average is below the yellow
- 1:06one and both of them are below the the
- 1:08value for the 100 moving average we
- 1:10would go short I also have another
- 1:12condition because in these cases when we
- 1:14are touching the perple line I don't
- 1:16want to go short so I will also say that
- 1:18I want to make sure the price is below
- 1:20the purple line so now let's go to Jesse
- 1:23and create a new strategy I'm going to
- 1:25name it Trend swing Trader
- 1:30now if I go back to my editor and look
- 1:33for it I will find it here right so this
- 1:36is the code that Jesse just generated
- 1:38for us and we're going to edit it so the
- 1:40first thing I'm going to do is to define
- 1:41a new property and name it Trend and in
- 1:45it we're going to Define three moving
- 1:47averages so the first one is going to be
- 1:49ta EMA and I will pass in the current
- 1:53candles and as for the period I will
- 1:55pass 21 the second one is going to be
- 1:58the same code but with the period perod
- 2:00of 50 and the third one is going to be
- 2:02the same but with a period of 100 now my
- 2:04condition is pretty simple if
- 2:07E3 is smaller than
- 2:10E2 and that is smaller than E1 smaller
- 2:14than the current price then we are in
- 2:16uptrend so I will return
- 2:19one otherwise if the condition is the
- 2:23opposite I will return minus one and if
- 2:27none of the above is true I will return
- 2:29zero which means we are in a ranging
- 2:30market so my shootong method is going to
- 2:33be simply return When the current Trend
- 2:38equals 1 and the opposite will go for
- 2:42the shoot short method so we want to
- 2:45make sure the trend is minus one in
- 2:47other words in a downtrend as for
- 2:49position sizing first I'm going to give
- 2:50it my entire Capital to just open the
- 2:53position with the market order so I'm
- 2:54going to say Quantity equals uols size
- 2:58to quantity first I'm going to pass my
- 3:01entire available margin and for entry I
- 3:05will pass the current price actually
- 3:08let's create a new variable here for it
- 3:11so entry is going to
- 3:13be self
- 3:17price and as for fee rate I will pass in
- 3:21self fee rate so my buy order is going
- 3:26to be quantity and then the entry and I
- 3:28will do the opposite for my go short
- 3:36Method All right
- 3:39so and we don't really care about the
- 3:41shoot cancel entry because we are
- 3:42opening the position with the market
- 3:44order so it doesn't really matter if
- 3:46this function is returning true or false
- 3:48all right so we have our entry rules now
- 3:50but what about our exit rules when would
- 3:52we exit this trade what would be our
- 3:54take profit and stoploss orders so for
- 3:56that let's go back to the Chart as for
- 3:58the Stop Plus I'm going to use the ATR
- 4:00indicator which is one of my favorites
- 4:02when it comes to setting the suus what
- 4:04it does is it tells us the average price
- 4:07that every candle is changing so for
- 4:09example in this point in time when we
- 4:11are opening the position the average
- 4:13true range is $
- 4:17886 so if I do it like this and this is
- 4:21going to be my subplus this would be
- 4:24that value
- 4:25880 but because this is too short I'm
- 4:28going to use two times of this this so I
- 4:31will increase
- 4:32this to something around here and for my
- 4:35take profit I will use something similar
- 4:38but because this is a swing trading
- 4:40strategy we want to be able to write the
- 4:42trend for as long as possible so here's
- 4:44what I'm going to do I will exit half my
- 4:46position at this point and when we hit
- 4:48this point I'm going to update my suplus
- 4:51to become a bit lower and we're going to
- 4:53keep doing this for as long as the trend
- 4:55goes down so this way we can write the
- 4:57trend for as long as possible and
- 4:58hopefully we'll be able to close it
- 5:00somewhere around here all right so let's
- 5:02go back to the code I'm going to say
- 5:05when the position opens and I'm going to
- 5:07use the unopen position method for this
- 5:09if it's a long position I will say my
- 5:12suplus order is going to be well first
- 5:15we need to pass the quantity and that's
- 5:17going to be the current positions
- 5:20quantity and the price is going to be
- 5:22the current price minus the current ATR
- 5:26multiplied by two and my take profit
- 5:29it's going to be the same but multiply
- 5:32it by three but as I said I want to exit
- 5:35only half my position at this point not
- 5:37the entire so I will divide this by two
- 5:40and if it's a short position I will do
- 5:43the
- 5:44opposite so in a short position my
- 5:46stopus is going to be two times of the
- 5:48ATR above the current price and my take
- 5:51profit I mean half of it is going to be
- 5:54the current price minus three times of
- 5:58the current ATR all right so we are
- 6:00exiting half of our position this way we
- 6:03also have our suplus but what about that
- 6:05other half of our take profit when this
- 6:07order is executed I want to bring down
- 6:09my stop l order obviously I'm talking
- 6:12about a short position here if it was a
- 6:14long position it would be the other way
- 6:15and I want to use some kind of trailing
- 6:18now how can we do that with J so here's
- 6:20how I'm going to Define another method
- 6:22called unreduced position now this event
- 6:25method will get triggered right after
- 6:27the size of my position gets reduced now
- 6:30notice the word reduced here it means
- 6:32it's not closed it's just reduced in our
- 6:34case it will get reduced in half so if
- 6:37that's the case I'm going to say if it's
- 6:40a long
- 6:42position my
- 6:44stoploss is going to be the quantity of
- 6:47my position at this point in time which
- 6:49is going to be self. position quantity
- 6:52and as for the price I want it to be at
- 6:54my entry price so this way even if the
- 6:57price goes against me I will exit the
- 6:59second half of my position in break even
- 7:01so the first half gave me profits the
- 7:04second half would give me nothing it
- 7:06will also lose me nothing so I will
- 7:08still sit in profit so I will say
- 7:12self-position do entry price now I won't
- 7:15do any take profit because if this
- 7:17method is getting executed that means my
- 7:20take profit order has already been
- 7:22executed so we don't have any take
- 7:23profit Order anymore so we don't need to
- 7:25update it either and our suplus is
- 7:28getting updated to be here here now so
- 7:30by the way when you do this Jesse will
- 7:33automatically cancel the previous stop
- 7:35order and submit the new one for us so
- 7:37we don't need to worry about canceling
- 7:39that previous one so all I need to say
- 7:40at this point is to say l if it's a
- 7:43short position do the same thing all
- 7:45right now if you remember I said I want
- 7:47to exit the second half of my position
- 7:49with some kind of trailing sub order and
- 7:51right now we only set the suplus once
- 7:54and update it twice here so how would it
- 7:57be trailing because let's say the trend
- 7:59keeps going down and in that case we
- 8:02need to keep updating that order right
- 8:04so the way I do that is I Define another
- 8:07method called update position and in it
- 8:10I'm going to say if it's a long position
- 8:12the subplus is going to
- 8:15be the current positions quantity and
- 8:18the price is going to be the current
- 8:21price minus the current
- 8:24ATR multiplied by two and for a short
- 8:27position it would be the current price
- 8:29added by two times of the current ATR
- 8:32but here's the thing if I do it just
- 8:34like this it will always update that
- 8:36suus even if the price is going against
- 8:39us so I'm going to update my code that
- 8:42this is going to be the minimum of
- 8:44either this or the current positions
- 8:47entry price which was our previous stop
- 8:50price that we set here so I'm also going
- 8:52to do the same here all right so we're
- 8:56almost finished but there's one more
- 8:58thing we need to do so so if I run this
- 9:00strategy right now this update position
- 9:03method is going to get executed on every
- 9:05candle and that means it will even get
- 9:07executed before this method is executed
- 9:10and that's not cool because first we
- 9:12need one of these orders to get executed
- 9:15and then when the position is reduced
- 9:18then we want to update our suplus on
- 9:21every new candle after that so here I'm
- 9:24going to add one condition I will say if
- 9:27the reduced count equals =
- 9:301 then I want to do this all right so we
- 9:33are ready to execute our first back test
- 9:35now let's go back to
- 9:37Jesse this exchange is fine this symbol
- 9:41is also fine and as for time frame I
- 9:43will choose the 4
- 9:45hours and I will pick my
- 9:49strategy and I'm back testing it since
- 9:51the beginning of 2022 up until a few
- 9:54days ago the fast mode is on and so is
- 9:57the Benchmark option all right let's
- 10:00execute it all right so it looks
- 10:03actually pretty okay and we are beating
- 10:05the
- 10:07market the max throw down isus
- 10:1023% and the
- 10:12profit is 77% the win rate is
- 10:1647% and we have an average win to loss
- 10:20ratio of 1.4 okay so this looks okay but
- 10:23I believe we can improve it so the first
- 10:25thing I'm going to do is to do what I
- 10:27always do for my position sizing cuz
- 10:29right now we are using our entire
- 10:31capital for every single trade and
- 10:32that's never good so I'm going to go
- 10:34back to my source code and in my goong
- 10:37method instead of doing it like
- 10:40this first I will calculate my sub price
- 10:43right here and it's going to be the
- 10:45entry minus 2 * of the
- 10:48ATR and instead of using this function I
- 10:52will use risk to quantity as for the
- 10:55first one I will pass my available
- 10:58margin the entry will be the current
- 11:01price then I will pass the subop price
- 11:05as a third argument and the fee rate is
- 11:07going to be my current fee all right so
- 11:11I will also do the same for my short
- 11:15positions and let's go back to Jesse now
- 11:18remember these numbers it's 77% profit
- 11:21with a Max draw down of minus
- 11:2423% let's reexecute
- 11:27it all right it's saying we're missing
- 11:30one required argument let's go back here
- 11:33so I forgot to set the risk per capital
- 11:35number which is actually the most
- 11:36important number in this function so I'm
- 11:39going to risk 3% of my Capital per each
- 11:41trade I'll do the same here let's go
- 11:44back run it again all right so not only
- 11:48I increase our profit slightly but also
- 11:51our Max throughout the number is reduced
- 11:53so that's pretty cool it's very small
- 11:55change in our position sizing made our
- 11:57results better even though we using all
- 11:59the same indicator entry and exit rules
- 12:02so how cool is that all right there's
- 12:03another Improvement that I want to do to
- 12:05it so if you take a look at the price
- 12:07here this is ranging market and our
- 12:09Equity curve is going down here this is
- 12:11a typical issue with Trend following
- 12:13strategies because when you're trading
- 12:15the trend you want to avoid the ranging
- 12:17conditions and one of the best ways that
- 12:19I do that is by using the adx
- 12:22indicator so let's remove the ATR the
- 12:25way the adx works is that we have a
- 12:28value between 0 and 100 and it will not
- 12:31tell us the direction of the trend it
- 12:33will only tell us the strength of the
- 12:35current Trend in other words if I set a
- 12:37threshold let's say at number 25
- 12:40whenever the value of the ATR is below
- 12:42this below this threshold line you can
- 12:45see that we are not in a trend and we
- 12:48want to avoid trading but whenever the
- 12:50value goes above it then we are in a
- 12:52trend that's now here we were in an
- 12:57uptrend we were also in an uptrend
- 12:59here and here we are in a downtrend all
- 13:03right let's go to the beginning of my
- 13:04strategy let's define a new property
- 13:06called the adx and in it I'm going to
- 13:10say return whether or not the current
- 13:14adx is bigger than my threshold which
- 13:19will be 25 and then I will go to my
- 13:23Shong method and add another condition I
- 13:26will say and self. adx I will also do
- 13:28the same for my shoot short
- 13:30function all right so let's go back to
- 13:34Jesse now remember this profit number
- 13:3879% and this Max rodon minus 18 let's
- 13:42re-execute it and now not only my profit
- 13:47increased drastically my Max zon is also
- 13:50reduced to a significantly better number
- 13:53and if you take a look at the equity
- 13:54curve when the price was in a Range our
- 13:57Equity curve did not go down drastically
- 13:59anymore the win rate is at 52% and our
- 14:03average win to loss ratio is
- 14:051.57 so this is pretty cool but I want
- 14:09to show you something if I were to back
- 14:10test this since the beginning of
- 14:15202024 you will get these results which
- 14:18look really good but one might say that
- 14:21hey we're not beating the market I want
- 14:23to make more than what a Buy and Hold
- 14:25would give me and I don't really care
- 14:27about the volatility well if that's you
- 14:30here's why this strategy is still better
- 14:32because my Max Ron is minus 10% while
- 14:36the market was doing significantly worse
- 14:38and my volatility tolerance is much more
- 14:41than 10% I can take maybe up minus 30%
- 14:45so if I go back to our strategy and
- 14:48increase our risk per capital from three
- 14:51to let's say five and rerun the back
- 14:53test now we are making more and the
- 14:56result of it is that the Maxon is just a
- 14:59bit worse but it's still significantly
- 15:01better than the market itself so if you
- 15:04still want to beat the market I can just
- 15:05multiply
- 15:07this by some kind of number let's say
- 15:10two and rerun it now we are making more
- 15:14than what the market was making and my
- 15:17Maxon is still in acceptable number so I
- 15:20can increase my position size even
- 15:22further if I have the tolerance for that
- 15:24amount of volatility which I personally
- 15:27don't but I'm just saying that if your
- 15:29goal is to just beat the profits that
- 15:31the market would have given you by
- 15:32simply buying and holding your positions
- 15:34and you don't care about the volatility
- 15:36then you can do this now obviously to do
- 15:38this we're going to have to use leverage
- 15:40and we are using the leverage right now
- 15:41in fact my leverage number for this
- 15:44exchange is at number two so you can
- 15:47even increase that to another number
- 15:49such as five or 10 and make huge profits
- 15:52but I personally don't do that because
- 15:54volatility is really important to me
- 15:56because I want to be able to sleep at
- 15:57night so you do whatever works for you
- 15:59and I do the same for me now before I
- 16:01forget I kept working on this strategy
- 16:03and I made a premium version of it and I
- 16:04submitted it to our strategies index
- 16:06page which you can find here and these
- 16:08are the results for 2022 so if you are a
- 16:11premium user just go ahead and download
- 16:13the code for this strategy now for my
- 16:15next video I'm going to take this
- 16:17strategy that we just wrote and then use
- 16:19Jess's optimization mode which uses the
- 16:21genetic algorithm to optimize our
- 16:23parameters to get better numbers out of
- 16:25it if that's something you're interested
- 16:27make sure to stick around for the next
- 16:28video now before I leave you we have a
- 16:30giveaway now a random person who likes
- 16:32this video subscribes to the channel and
- 16:34posts a comment is going to receive 1
- 16:35million bunk token all right let's pick
- 16:37the winner for my previous
- 16:39video and the winner
- 16:41is it was helpful to see someone
- 16:44familiar with the framework build a
- 16:45complete strategy from a scratch thank
- 16:47you so much for your comment please
- 16:49reach out to me so that I can send you
- 16:50your tokens all right thanks for
- 16:52watching and happy Trading
- 16:54[Music]
- 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.