4. Entering and Exiting Trades - Algo trading from scratch with Python — Transcript
Full transcript
- 0:01[Music]
- 0:02Jesse gives you a clear framework for
- 0:04defining your strategies in some cases
- 0:06though there are multiple ways for
- 0:08performing the same task such as exiting
- 0:10trades while this brings flexibility it
- 0:13might be confusing to beginners so in
- 0:15this video I want to show you various
- 0:16ways you have for entering and exiting
- 0:19trades in Jesse let's begin by creating
- 0:21a new strategy
- 0:24and name it
- 0:25golden
- 0:26cross
- 0:28right so if I open my vs code and look
- 0:31up Golden I should find it and in case
- 0:34you're curious to know where it's
- 0:35located it's inside my strategies folder
- 0:38foreign
- 0:43there is only one way of entering trades
- 0:46in Jessie but it is a 2s sub process
- 0:48assuming you're looking for long
- 0:50positions you will start by defining
- 0:52your interior inside the shoot long
- 0:54method which returns a Boolean value
- 0:57when it returns true then it is followed
- 0:59by the golang method which is where you
- 1:02define your actual entry order an easy
- 1:04way of remembering this is to ask
- 1:06ourselves should we long this asset if
- 1:10yes then let's go long all right in case
- 1:12of short positions we have shoot short
- 1:15and go short methods but they only work
- 1:18for Futures Trading so just to keep it
- 1:20simple I'm gonna trade only long
- 1:22positions in this video so I'll just
- 1:24remove these because we don't need them
- 1:26all right let's write a real world
- 1:28example this is the chart for BTC usdt
- 1:31on trading View and I've added two
- 1:33moving averages the blue line is the EMA
- 1:35with a period of 20 and the orange one
- 1:38is the EMA with a period of 50. and the
- 1:41time frame of the chart is 15 minutes
- 1:44let's say I want to go long whenever the
- 1:46EMA 20 crosses the email 50. I'm gonna
- 1:49use Jesse's indicator module which has
- 1:52already been imported for me I'll Define
- 1:54two new properties the first one I'm
- 1:57going to call Ema 20.
- 2:00inside it I'm going to return ta which
- 2:04is the indicator module of Jesse and to
- 2:07access the EMA I will say dot EMA the
- 2:10first expected parameter of it is
- 2:12candles which I can pass by saying self
- 2:14candles
- 2:16and the second one is the period which I
- 2:18will pass Tony
- 2:20all right next I will just copy this
- 2:22code and paste it and change 20 to 50.
- 2:27next we need to define the shoot log
- 2:30method I'll check if the ema20 crosses
- 2:33the ema50
- 2:34if it does then I'll return true next
- 2:37I'm going to update the colon method
- 2:40which is where we Define the entry order
- 2:42when it comes to submitting orders we
- 2:44need two things the quantity and the
- 2:47price I want to keep this example super
- 2:49simple so as for the entry price I will
- 2:51just use the current price
- 2:53as for the quantity I'll just use half
- 2:56of the existing balance in my account
- 2:58the capital in my account is in your
- 3:00city so I'll need to convert it to BTC
- 3:03to do that I'll use the size to quantity
- 3:06utility function of Jesse and just like
- 3:08the indicator module the utils module
- 3:11has already been imported for me
- 3:14the first parameter of it is the
- 3:16position size like I said I want to use
- 3:19half of my Capital so I'll just pass
- 3:22self dot balance
- 3:25and because I want half of it I will
- 3:27multiply this by 0.5 and the second
- 3:30parameter is the entry price which I
- 3:32just defined earlier
- 3:34and finally I can submit my entry order
- 3:37because this is for a long position I
- 3:39will say self dot pi equals
- 3:43the first parameter is the quantity and
- 3:46the second one is the entry price and
- 3:48because the entry price of the order is
- 3:50the current price this is going to be a
- 3:52market order because I'm using a market
- 3:54order when my trading candle closes and
- 3:57their strategy gets executed I will
- 3:59immediately be sitting on a long
- 4:01position okay that's good but how do we
- 4:04exit this position the first way could
- 4:06be to Simply close this position
- 4:07whenever the opposite of our interior
- 4:10rule is true just to make it simpler I'm
- 4:12gonna refactor my entry rule
- 4:15I'll Define a new property and call it
- 4:18trend
- 4:20for an uptrend
- 4:22I will say if
- 4:24self EMA 20 is above self EMA 50 return
- 4:31one
- 4:32and else which means downtrends return
- 4:35-1 then I can refactor my shoot long
- 4:38method into return to when self.trend is
- 4:42one so now one way to exit this trade is
- 4:45this whenever a new candle closes we
- 4:48check to see if the trend is downward if
- 4:51so then we'll close the position to
- 4:53write the code for this I'm gonna Define
- 4:55the update position method and inside it
- 4:58I'm gonna say
- 5:00if self-trend is -1
- 5:04then I want to close the position
- 5:06immediately or in other words using a
- 5:09market order to do that I can use either
- 5:12the self-take profit or self Surplus
- 5:15variables it doesn't really matter but
- 5:17I'll just use this Surplus
- 5:19the first parameter is the quantity
- 5:21which I will use the quantity of my
- 5:24current position by saying self position
- 5:26dot quantity
- 5:28and the second one is the price because
- 5:30I wanted to be a market order I will
- 5:32pass self dot price
- 5:34because it is a common use case you want
- 5:37to close your current position using a
- 5:39market order Jesse offers a shortcut
- 5:41method for it and I can call it by
- 5:44saying self.liquidit since this is a lot
- 5:46cleaner I'm gonna remove this line
- 5:49okay this sounds good but what if I knew
- 5:53at which price I wanted to close my
- 5:55position at the time that the position
- 5:57is opened maybe I want to exit at two
- 6:00times the range of the current candle
- 6:02all right let me show you what I mean by
- 6:04the range of the candle on an actual
- 6:06chart if this is a current candle at the
- 6:08time of entering this trade the range of
- 6:10it is going to be the difference between
- 6:12the low and the high of it and two times
- 6:15of the range could be somewhere around
- 6:17here
- 6:18all right so I'm going to define a new
- 6:20property and call it current range
- 6:24it's going to return
- 6:26the high price of the grand candle
- 6:28subtracted by the low price of the grand
- 6:31candle
- 6:32so now I can set my exit orders at the
- 6:35time of opening the position to do that
- 6:37we're gonna have to use the on open
- 6:39position method first let's do the
- 6:41Surplus the quantity should be the
- 6:44quantity of my current position
- 6:46and the price will be the current price
- 6:50subtracted by
- 6:52self current range which we just defined
- 6:56earlier
- 6:57and because I want two times of that I'm
- 6:59going to multiply this by 2.
- 7:02the take profit is going to be very
- 7:04similar
- 7:05the quantity will again be the quantity
- 7:08of my current position and the price of
- 7:10it will be the current price but this
- 7:12time added by the current range
- 7:14multiplied by 2.
- 7:16all right so now when the position opens
- 7:19Jesse will submit both the stop loss and
- 7:22take profit orders right away
- 7:29right we just learned how to both enter
- 7:31and exit a trade now let's take it up a
- 7:34notch instead of exiting my position all
- 7:37at once maybe I want to exit half my
- 7:39position at the initial take profit
- 7:41price and exit the other half using some
- 7:43kind of a trailing stop this might sound
- 7:45a little Advanced but I promise it's
- 7:48incredibly easy to write
- 7:49to do that the first thing I need to do
- 7:51is to modify my initial take profit
- 7:54order to only exit half the position
- 7:56size to do that all I need to do is to
- 7:59divide the quantity by two
- 8:01next to exit the second half of my
- 8:03position inside my opted position method
- 8:05I will use this built-in property called
- 8:07reduce count which tells me how many
- 8:10times the position size has been reduced
- 8:12since the trade was opened now in this
- 8:14case we only reduce the position once
- 8:16and then we want to close it so I'll
- 8:18just say if self reduce count is 1.
- 8:23self Surplus equals the quantity will be
- 8:27the quantity of my current position at
- 8:30this time and the price will be the
- 8:33current price subtracted by self current
- 8:36range
- 8:38right but why is this a trailing stop
- 8:40that's because at this point we know
- 8:42that the opted position method will get
- 8:44executed on every new candle close
- 8:46assuming of course that our stop loss
- 8:49hasn't been executed yet and the
- 8:51position is still open so every time
- 8:53that the update position gets executed
- 8:54it will cancel the previous stop loss
- 8:57and submit a new one based on the
- 8:58current price conditions I will also
- 9:00modify this line into L if just to make
- 9:04sure that my position won't get close
- 9:06before the sub price is reached
- 9:13right now just to make sure that my code
- 9:15Works let's try to run a back test
- 9:18I will change a strategy to Golden Cross
- 9:21the time frame 15 minutes is fine
- 9:25the symbol is correct and the exchange
- 9:27is also correct I want to see some
- 9:30charts so I will enable this option
- 9:32I will also enable quantstat reports and
- 9:36then I will change the duration of my
- 9:37backtest to be the first quarter of
- 9:402021.
- 9:43right let's start the back test
- 9:47and we get an error
- 9:49right it says the format of the self
- 9:51take profit is invalid let's take a look
- 9:54at the strategy and see what I did wrong
- 9:59okay I see my mistake here the price of
- 10:01my take profit order was supposed to be
- 10:03the current price added by the current
- 10:05range property but instead I wrote
- 10:08current candle okay so with any luck if
- 10:11I run the back test again it should work
- 10:13this time all right it works just fine
- 10:16here's the chart for the equity curve
- 10:20and here are the metrics which are
- 10:23negative by the way which I guess we
- 10:25kind of had it coming because first of
- 10:27all this is an example strategy and
- 10:29second of all it's just a work in
- 10:30progress we can also take a look at the
- 10:32Legacy chart which is useful because it
- 10:35shows us to buy and sell orders executed
- 10:37by Jesse
- 10:38we also enable the constat rewards so
- 10:41let's take a look at that and here we
- 10:43have access to a lot of more metrics and
- 10:46charts that are useful for evaluating
- 10:48the strategies performance in the next
- 10:51video we're gonna learn about the
- 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.