00:00so let's talk about high frequency
00:01trading and how you can build your own
00:04high frequency trading system starting
00:06now high frequency trading or hft for
00:09short is basically a trading strategy
00:12style where you're holding positions or
00:14instruments for a very short amount of
00:16time it could be an hour could be
00:18minutes could be seconds nanoseconds
00:20even ticks in today's video we're going
00:22to be talking about an hft strategy that
00:24you can use as a retail Trader to find
00:27an edge in the market I'm going to be
00:29very specific and show you in-depth code
00:32examples so you can actually use this
00:35strategy for yourself it's not going to
00:37be one of those vague videos where you
00:39learn a little bit but you don't have
00:40any actual material at the end of the
00:42video so this video is going to be very
00:43specific with the strategy code examples
00:45and how to use it and I think you're
00:47going to be able to take it and find an
00:49edge in the market before I start hi my
00:51name is Jacob I build and code automated
00:53trading systems in the Futures market
00:55and show you in full transparency on a
00:57monthly basis on how my automated
00:58trading systems do I have a couple hft
01:01trading systems in my portfolio and I
01:03wanted to share a strategy idea today
01:05that you can use yourself okay so the
01:07strategy I wanted to talk about is using
01:10cumulative Delta of an order book of an
01:13instrument so I know that was a handful
01:15or a mouthful if you will uh but let's
01:17explain so first Delta is obviously the
01:20difference between two variables right
01:22and cumulative Delta would be the
01:25overall change between two variables
01:27over a span of time okay so when I say
01:30cumulative Delta of an instrument I'm
01:32talking about the difference between the
01:34bid and the ask right of that instrument
01:37so anytime a instrument is trading right
01:39there's buyers and sellers right buyers
01:42usually get filled at the ask price and
01:44sellers get filled at the bid price but
01:47there's always a spread right and that
01:48that spread or Delta is the same thing
01:50basically the same kind of word just a
01:52different way of saying it but you can
01:54measure that Delta over a period of time
01:56and forecast what that potential
01:59instrument is going to do in the next
02:01minute the next second the next hour
02:03okay so let's explain what I mean by
02:05cumulative Delta so basically it's the
02:07difference between the market buys and
02:10the market sells at each price okay for
02:13each candle right so if you have a one
02:15minute candle you could have a different
02:17cumulative Delta a bid and ask prices
02:19versus an hour or a second okay so the
02:22Delta is calculated through subtraction
02:24of the volume of contracts or shares if
02:26it's a stock traded at the bid price
02:28from the volume of contracts traded at
02:31the ask price okay so the trades
02:34executed at the ask price are those
02:35trades that were initiated by aggressive
02:37buyers right if you do a market buy
02:39you're getting filled at that ask price
02:42the trades executed at the bid price are
02:44those trades that were initiated by
02:46aggressive sellers okay so if you really
02:48want to get out of a position in a
02:50market usually you know do a market sell
02:52you're going to get filled at the bid
02:53maybe a little bit worse maybe a little
02:54bit better depending on the slippage but
02:56that's usually the case for sellers so
02:59thus the positive Delta reflects a
03:01higher volume of aggressive buys at the
03:03ask price in the result of trading of
03:06aggressive sellers right so if you have
03:08a positive Delta in a one minute or five
03:10minute time frame there was more buyers
03:12and sellers during that period of time
03:14and vice versa then if a Delta is
03:16negative in turn reflects a higher
03:19volume of aggressive sellers at the bid
03:20price and the result of trading of
03:22aggressive sellers okay so volume of
03:25trading at the ask price subtracted with
03:27a volume of trading at the bid price is
03:29equal to the Delta right so we all know
03:31Delta is you know once again the
03:33difference between two variables and in
03:35this case one of our variables is the
03:37volume of bids and the other variable is
03:39a volume of ask prices
03:41to calculate Delta you need to know the
03:43following variables the bid price the
03:45ask price and the most recent price at
03:47which the instrument was traded the
03:49volume of the most recent trade and the
03:51time of its execution
03:53my platform ninjatrader has an indicator
03:55called cumulative Delta uh specifically
03:58they call it order flow cumulative Delta
04:00and it actually measures as an indicator
04:02the Delta for that time period okay so
04:04it uses once again the difference
04:06between the volume traded bid versus ask
04:08for a time frame so if you have one
04:11minute candlesticks and you apply this
04:12indicator this order flow cumulative
04:14Delta you will see the difference
04:16between bid and asks for that minute it
04:18kind of adds up for that bar size here's
04:21the source code for the cumulative Delta
04:23and how it's actually calculated so if
04:24you're using another platform like TD
04:26Ameritrade trading view tradingview
04:28might have an indicator but this is how
04:30it's actually calculated on the back end
04:33as an indicator and you can use this
04:34yourself if you have your own say custom
04:37infrastructure we're getting the
04:38difference between the bid and ask
04:40prices okay so these are on a
04:42tick-by-tick basis so ninjatrader
04:44actually requires you to have a
04:46secondary tick data series to calculate
04:49this indicator so even though you're on
04:51a five minute or one minute chart
04:52there's a Tech Series in the background
04:54that's actually calculating this and
04:56updating it when the bar closes so
04:58that's super important okay so I have
05:00some example code here that counts the
05:03amount of time the Delta crosses above
05:06100 okay now this 100 variable is
05:09obviously static it could be you know
05:11optimized it could be a parameter if you
05:13will and will vary depending on the
05:15instrument so in this example I use
05:16crude oil Futures CL and usually it's
05:20it's around the 100 range it can go up
05:21to 300 as well but usually 100 was the
05:24average so I counted the amount of times
05:26that it crossed above 100 right with
05:29using a variable called the the total
05:33long Delta crosses so if Delta crossed
05:35above 100 or is greater than 100 I count
05:38it as a variable and then I also save
05:41the close price of when it crossed
05:44because I want to save that close price
05:45so I can forecast use it to forecast uh
05:48what's going to happen in the next bar
05:49and then subsequently I have also
05:52counting if the Delta crosses below
05:54negative 100 right so if it's negative
05:56100 there's more sellers in that time
05:59and I have a different variable called
06:00total short Delta crosses that I
06:03increment if it if it crosses below uh
06:06negative 100 and once again I save the
06:08last close price in a separate variable
06:10called last short close okay
06:12so then what I do is I check all right
06:17Delta cross below that right basically
06:20saying if last long close does not equal
06:22to zero so it's not equal to zero if
06:24there was a cross of that Delta of one
06:25of 100 either positive 100 or negative
06:28so if it doesn't equal zero that means
06:31there was a cross recently then I come
06:34in and I say all right well what
06:36happened in the next bar did it go up
06:38did it continue right if Delta crossed
06:40it but went up above 100 was the next
06:42bar higher or was it lower so the first
06:45if statement is saying all right is the
06:48current close now greater than the last
06:50long close that we saved if it is then I
06:52increment a variable called long
06:53continuation meaning that it kept going
06:56up the next bar and it would have been
06:57better to go long on that trade
06:59and then else if close was either equal
07:02or less than the last long close then I
07:06actually increment a variable called
07:07reversal which means even though the
07:09Delta crossed above 100 it actually
07:11reversed and went down in the next time
07:13period right so we incremented that
07:17and then I reset last long close to zero
07:19because I want to wait for the next
07:20Delta cross and I don't want it to keep
07:21counting and then I do the exact same
07:24thing for the short trade where I check
07:27uh hey was there a Delta cross below
07:30negative 100 recently with the Sif
07:32statement if flash were close to not
07:34equal to zero if so if the close is less
07:37than the last short close that means it
07:38kept going down it would have been
07:39better to short so I increment a short
07:41continuation variable right uh else that
07:45means the current close is either equal
07:47to or greater than the last short close
07:48and it would have been better to go long
07:50so I increment a variable called short
07:52reversal right and I increment that by
07:54and then once again I reset last shore
07:56close to zero because I don't want it to
07:58keep counting I want to wait for the
07:59next Delta close or the next Delta cross
08:02my mistake and then at the end I print
08:04all the results of the percentage chance
08:06by taking the essentially four variables
08:10either long continuation long reversal
08:12short continuation short reversal and I
08:15developed I divide it by the total
08:17crosses that happened in that time
08:18period right and that time period could
08:20be your your in Sample data essentially
08:22it could be two years or one year and
08:24then I multiply that 100 by 100 to get a
08:27percentage chance okay so let's actually
08:29run this experiment with oil on the last
08:31two years we'll do actually you know
08:33let's let's run this example
08:35with oil uh for the last two years right
08:39so 2018 and 2019 and we're actually
08:41going to test it out of sample 2020 to
08:432022 and see if we can find some Edge in
08:47all right so our analysis is in for oil
08:49and so I ran that code that I talked
08:52about and the only interesting number at
08:54least on the one minute was uh
08:56percentage chance of a long reversal on
08:59the next bar basically meaning if the
09:02Delta crosses below 100 and then
09:06actually let me double check the code I
09:09percentage of long reversal
09:16uh long reversal is better better to
09:18enter Shore if it crosses above the
09:20Delta 100 so that at 54 is somewhat
09:26um this is over six months of data so 54
09:30of the time it's better to actually
09:32short oil if the Delta crosses above 100
09:36on a one minute bar now you can test
09:38this with a lot of different bar sizes
09:40this is only one minute so
09:42um you could definitely try this on five
09:44minute 10 15 60 daily bars
09:48um to get maybe a more you know higher
09:50percentage chance obviously you'll have
09:51less data points because there'll be
09:53less bars to actually test with but you
09:56could use this 54 and say all right
09:57let's build a trading system where we go
09:59short if the Delta cross is above 100
10:01and exit on the next bar or have maybe a
10:05bigger profit Target or stop loss to
10:07build your strategy but this type what
10:09I'm trying to get at is this
10:10mathematical approach of thinking of
10:15is very important right you should never
10:17use a random indicator because you feel
10:19like it there should be some statistical
10:21analysis so what I would do next at this
10:24part is I ran this for six months and
10:26the reason I think I initially earlier
10:28in the video I accidentally said two
10:30to run cumulative Delta at least on the
10:32ninjatrader platform you need tick data
10:34and my provider kinetic only provides
10:36the last six months of tick data so the
10:39smart thing to do would be to next build
10:42a system where you enter short on a
10:44Delta cross above of 100 and do a simple
10:47back test maybe probably a three month
10:51and see how it performs and see if it
10:53meets your goals of say for example my
10:55goal with any trading system is my net
10:57profit at least in the back test has to
10:59be at least double the maximum drawdown
11:01so usually you know a profit factor of
11:05ideally 1.5 or higher but basically you
11:08build a system where you go short if the
11:10Delta crosses above 100 so actually
11:11let's code this together so I can show
11:14so here's the code for counting the
11:18counting the Delta crosses so next what
11:23I'll write a comment here let me zoom in
11:27from our analysis of oil CL
11:34there is a 54 chance
11:44short after a Delta cross above 100.
11:52so now what we do is we would say
11:57I'll just copy some of this statement
11:59here if the Delta close is greater than
12:05we're going to enter short
12:10and we probably want to check that we're
12:11not in a position so if Delta close is
12:14greater than 100 and
12:21we are in a flat position meaning we
12:22don't have any positions open right now
12:31uh oops flat lanter short
12:40if we're in a position
12:49what am I doing wrong here
13:01and what should happen is our win rate
13:03should be 54 right when we ran analysis
13:06it says that 54 of the time
13:09when we go short after a Delta cross
13:11above 100 we so we our win rate should
13:14be 54 now we don't know if it's gonna be
13:16profitable or not because there's more
13:17factors like okay how big were the wins
13:19what was the distribution like but the
13:22win rate should be the same so if Delta
13:24closes greater than 100 and we're flat
13:26we're going to enter short
13:28and then if we're short
13:30so this will wait for the next bar will
13:35one last check let's make sure we're on
13:37the minute data series I don't want to
13:38enter on the tick data series
13:42we did our analysis on bar close
13:52so that's good let's rerun this back
13:53test and our win rate should be around
14:08now for win rate is not 54 then there's
14:11obviously a bug with my my calculations
14:18because it printed percentage chance of
14:20long reversal on next bar 54 all right
14:23so what did it come out to
14:25so I got 51.82 it was profitable
14:31so that's pretty close although
14:34yeah I must be I don't even have
14:36slippage or commissions on
14:38uh that wouldn't affect the win rate but
14:41it would affect the net returns
14:44I guess it's close enough 51.8 in my
14:46calculation it was 54 percent
14:50I'm curious if I had commissions and
14:52slippage in here well I probably want
15:04how many trades are made yeah 2644 so
15:10allowed it does turn negative
15:15so when you when you factor in slippage
15:19it's incredibly negative
15:21because there's there's so many trades
15:22there's 2644 so over time it's just not
15:25worth it it adds up right that's that's
15:37so maybe to make our
15:40calculation more accurate we should also
15:43when we're counting these we should also
15:48slippage and commission
15:54so that's why you always account for
15:56these when you're back testing so
15:57our initial calculation is a little bit
15:59flawed because after slippage and
16:01commission the because there's so many
16:03trades they do add up but if we do
16:07for an initial run is okay but obviously
16:12starting to build more of the system and
16:14actually get into the exits you will
16:19to get over that hump
16:21I would say you could try a longer time
16:23frames maybe on five or ten minute or in
16:27also apply some type of weight to the
16:31amount of price movement right so
16:34um we're just looking at the next bar
16:36close but maybe you want to look for a
16:38price movement a certain amount of ticks
16:39or points to be able to forecast to get
16:4451.8 win rate compared to our 54. It's
16:49relatively close I mean that's about two
16:53so it's not terrible uh but it is
16:56relatively close and I'd be happy enough
16:57with it so the next step what I would do
16:59personally if you're going to take this
17:01and build your own trading system is
17:03number one get an out of sample uh
17:05period so I ran this from May of this
17:07year to today November 25th
17:10um or until November 25th that was
17:12yesterday but you'd probably want more
17:15data uh maybe like 2021 or 2020
17:19um to test this out of sample because
17:21this is all in Sample so essentially
17:23I've optimized it for that sample c um
17:26sample size you probably want other
17:27sample size of maybe another data set uh
17:30to test that on and make sure that it
17:32still performs also you have to talk
17:34about exits right how are you going to
17:36exit are you going to always exit on the
17:38next bar like I would text I would test
17:39on the next bar having a um you know a
17:42normal profit Target a stop loss
17:44exiting after a certain price has moved
17:47there's lots of ways to do the exit so
17:49that's another analysis in itself this
17:51specifically was just for
17:53um you know high frequency trading
17:54system on entries but what I wanted you
17:57to learn from this video was taking a
17:59mathematical approach to approach to
18:01find Edge which I think is super
18:03important and I think a lot of algo
18:06Traders need to incorporate this more of
18:08using math to find Edge and not based on
18:13um you know this indicator looks great
18:15it should all be mathematical so
18:17um that's the high frequency trading
18:19system using cumulative Delta I
18:21personally have about two versions of
18:22this in my live portfolio one with oil
18:24and one with es which is S P 500 futures
18:29but the only difference that I have is
18:33um this 100 is a parameter so I can test
18:36different uh Delta cumulative Deltas
18:40um in my optimizations to to do that so
18:43that's only different so what I would do
18:44is um this code by the way will be
18:46available on my GitHub in the link below
18:48in the comment below in the comments
18:49below but I have this 100 negative 100
18:51and 100 as a parameter and I can kind of
18:53test uh different values and then I also
18:56test different time frames like my oil
18:58trading system is on five minutes and
18:59the es is on 15 minutes so that's from
19:01my testing so we initially did this on
19:03one minute but you could try this on
19:04many different time frames and maybe get
19:06more accurate statistical results with
19:09different time frames one minute is
19:10pretty granular but I wanted to show you
19:12how you can use math to find an edge
19:15using high frequency trading systems and
19:17specifically now you don't have to use
19:19it um you can use math on swing trading
19:22systems or longer hold time systems but
19:24um you know if your if your goal is to
19:27always be in and out of the market
19:28really quickly and you have an edge you
19:31may as well exploit it so anyways that's
19:33the video guys high frequency trading
19:34systems using order flow cumulative
19:36Delta I hope you found value let me know
19:38in the comments below if you found value
19:40in this this and if you're going to
19:42build a strategy around it I think it's
19:44really cool to to calculate stats and
19:46math and I never used to like it when I
19:48was younger but if you can find
19:49profitable trading systems to add your
19:51portfolio I think it's incredibly
19:52valuable and it's something you should
19:54do on a day-to-day basis so that's a
19:56video guys have a good week we'll see