Lesson 2.
2
TradeStation Walkthrough
Tomas: Okay, so, each module, each group of techniques, each approach, which will
be described in different modules, will come or is coming with a workspace
which looks like this.
On the left you will always have a window which I will use to explain to you
the overall concept for that given module.
And you can see you'll also have indicators. These are custom-built indicators
to explain the concept further. And all the indicators are already here in the
chart. So, that's just for the sake of explanation.
Here we have the original modelling strategy, the DPS testing strategy, be-
cause that's the one we always want to compare our results against.
And this will be the most important one. This is a window with so-called opti-
mization code which I will explain to you in a minute. But, basically, this is a
code that will test all the different cases within each given module, within
each given technique, and it will help us to find the best combination.
And then we have the result code. It's called result code. And that's already
stripped code which already has got implemented the very best dynamic po-
sition technique for that given module that came out from this optimization
window.
So, each module will have these four workspaces and charts for these differ-
ent purposes.
Let's start with the indicator.
Let me open it.
Okay, so with this indicator it will be pretty clear how the market concept
works. But let me explain to you this from the very beginning.
Page 1 of 14
(c) 2018 [Link] / Distribution without author´s approval is not permitted.
TRADING INVOLVES A SUBSTANTIAL RISK OF LOSS AND IS NOT SUITABLE FOR ALL INVESTORS,
PAST PERFORMANCE IS NOT NECESSARILY INDICATIVE OF FUTURE RESULTS
So, the first version, how we can apply this concept, is that we take 100 bars
back and we establish a range among last 100 bars. And then we split this
zone, these highs and lows, this range into three different zones.
So here at this very last bar we would count 100 bars back, and we would
find the lowest low, 100 bars back, which is this one. And then we would find
the highest high for 100 bars back, which is here.
Then we take the range and we split this range into three different zones.
And each zone will have a different position size.
It makes lot of sense, because if we have a certain range, again, we will have
different probabilities of success if we are very near to the bottom, or if we
are close to the retracement line, or if we are close to the market high. We
will have different probabilities for our long and short positions.
So, that's the very first technique we will use.
And you can see here in the indicator all we do is, basically, we take close.
Then we find out the highest close within 100 days. The lowest close within
100 days. And that's how we establish the highest and lowest. Then we split
it into three zones. And we create these three zones. And we get this result.
Okay, so that's just for explanation.
The code - actually you'll see it here. That's the code with Setup Number One.
This is the first technique - which only uses close, and then highest high and
lowest low within last 100 days.
Okay, now, when we started working with this concept in our hedge fund we
found out that there are stronger and better application than that.
And, specifically, two more applications.
So, the second one is that instead of using close price, we used moving aver-
age.
So, we basically take moving average and then we find the highest highs and
lowest lows for that moving average within 100 bars, or 100 days. And we
split this input into three zones.
So, that's the second approach, when we take moving average.
Page 2 of 14
(c) 2018 [Link] / Distribution without author´s approval is not permitted.
TRADING INVOLVES A SUBSTANTIAL RISK OF LOSS AND IS NOT SUITABLE FOR ALL INVESTORS,
PAST PERFORMANCE IS NOT NECESSARILY INDICATIVE OF FUTURE RESULTS
And I recommend you not to optimise it that much, because we want to
avoid overfitting, or over-optimization. And I recommend to you choose one
period for all the tests, all the strategies, and stay with that.
I recommend 25. You can also try 20, or 30. But we like working with one
fixed period so we avoid any overfit, any over-optimization. And we simply
take moving average with period 25, and then we establish highest high and
lowest low from this moving average. Then we, again, use three different
zones. And get the different zone for different position sizing.
So, you can see it here. Okay, so this cyan/bluish curve, this is the moving av-
erage 25 - 25 bars back. And now we have highest high and lowest low of
that moving average for the last 100 bars.
And then we again split into the zones.
And we definitely get way different results than if we used just the close.
And sometimes the first one works better, sometimes the second one. But, if
you start comparing them against each other they can be significantly differ-
ent.
And then, after using this technique, we expanded it in our hedge fund even
further, and that's probably the best application.
So what we did, then we used a difference of two moving averages.
So we took one fast moving average with period 5. And then slow moving av-
erage with period 25. Now, very important, we always keep ratio 5:1.
So, you can use - slow period you can use, let's say, 50, but then your fast will
be 10. Or the slow period will be 25, sorry, 100, then your fast period would
be 20.
So, always keep ratio 5:1, okay?
Here you have an important note about that.
Now, what we do in third case, we take the difference of two averages, slow
and fast, and we kind of construct something like a MACD indicator, moving
average convergence divergency, which is similar. It's a difference of two
moving averages.
Page 3 of 14
(c) 2018 [Link] / Distribution without author´s approval is not permitted.
TRADING INVOLVES A SUBSTANTIAL RISK OF LOSS AND IS NOT SUITABLE FOR ALL INVESTORS,
PAST PERFORMANCE IS NOT NECESSARILY INDICATIVE OF FUTURE RESULTS
And then we basically use this difference to set up the zones.
So, here, in this second chart, in the second part, you can see the yellow one
is the slow one, and the white one or bluish one is the fast one. And then we
can see we have – if we have a divergence we know that markets are more or
less expanding; and if we have convergence, like here, we know that markets
are contracting.
Then we use this difference - which should be this last chart - we use this dif-
ference, again, which is this bluish chart - that's the difference between the
moving averages.
And we again set up highest high and lowest low and split into three zones.
And we, again, get completely different results with this approach, using two
moving averages.
So, we basically take one idea, splitting some range into three zones, and we
can either use the basic chart, that means working with the close price, or we
use single moving average. Or we use difference of two moving averages.
And the difference of two moving averages makes most sense because the
divergency and convergency of two moving averages is really telling us some-
thing about this trend, about the strongest divergency and weakest conver-
gency during last 100 bars. And we then use these extremes, the lowest and
highest, and see where the current market fits - how we fit in general with
the contraction and expansion, and within each zone we definitely have com-
pletely different probabilities.
So you can see this is a pretty sophisticated use of dependent input already.
That's what we found out - that if we want to use dependent input we need
to be more creative and use a little bit more advanced approaches like this
one. But, you’ll see it works really, really good.
Okay, so, here's the indicator.
We have three cases.
And now one important thing, we always, in our hedge fund, use 100 bars
back. We do not change this period.
Page 4 of 14
(c) 2018 [Link] / Distribution without author´s approval is not permitted.
TRADING INVOLVES A SUBSTANTIAL RISK OF LOSS AND IS NOT SUITABLE FOR ALL INVESTORS,
PAST PERFORMANCE IS NOT NECESSARILY INDICATIVE OF FUTURE RESULTS
Again, first we want to completely avoid overfit, over-optimization, so we
keep it fixed. And second, this is, for us, the minimum statistically valid sam-
ple size.
So with 100 bars we feel enough confidence to give it statistical validity.
And we never change that. We never use any other number.
And then what we do, we use these three techniques.
We test them first on our base chart, that means the underlying base
timeframe and chart we use for our strategy.
And then, we test it also on daily timeframe. So that will leave us with six dif-
ferent techniques; three on the base timeframe, and then three same tech-
niques on a higher timeframe, specifically daily. So, we want to test this ap-
proach among last 100 trading days on a daily chart.
Okay, so, that's the contraction/expansion based DPS technique.
And now, we have a special code here which is called optimization code.
How does this code work?
So, basically, you take this code and you copy/paste your strategy.
Okay, so we have the testing strategy you already know, and what we do, we
copy/paste the strategy here. Insert your strategy code here. Okay, we insert
the strategy here.
And the exit from the strategy we put at the very end.
Okay, so very easy.
Basically anything before the entry we copy/paste. Everything before the en-
try part we copy/paste, and we put it here. Insert your strategy code here.
Okay, and then anything after the entry part, which would be here, that
means anything related to exits, we copy/paste here at the very end, in the
strategy exit part code here.
And now, we need to make a small modification of this entry part.
Page 5 of 14
(c) 2018 [Link] / Distribution without author´s approval is not permitted.
TRADING INVOLVES A SUBSTANTIAL RISK OF LOSS AND IS NOT SUITABLE FOR ALL INVESTORS,
PAST PERFORMANCE IS NOT NECESSARILY INDICATIVE OF FUTURE RESULTS
So, normally we have here a: Filter_Long then buy next bar myBreakoutLong
stop;
Here we have different case for each zone.
So, we basically say: If Filter_Long, then buy Contracts_zone_1_long;
That means number of contracts, number of contracts, and we keep the best.
The rest: next bar myBreakoutLong.
And for short: If Filter_Short, then sell short Contract_zone_1
That means number of contract zones for short in the Zone Number 1.
next bar myBreakoutshort
And then we copy/paste it and make the same for Zone 2 and Zone 3, and we
always change Contract Zones Number 1 in first zone, Contract Zone Number
2 in second, and Contract Zone 3 in the third zone.
Again, this is an advanced course, so I assume you understand what we're
trying to do here. You already have some basic understanding of coding and
EasyLanguage, but basically what we do here, we use each zone as we estab-
lished it. And we specify the entry within each zone with different number of
contracts which will be user-defined as an input.
Okay, so here at the beginning we have different number contracts for Zone
Number 1 long; Zone Number 2 long; Zone Number 3 long.
And different number of contracts for Zone Number 1, Zone Number 2, and
Zone Number 3, for short rates.
And here in this part we just need to specify.
So, if the entry signal happens in the Zone Number 1, then we will trade this,
whatever the number behind this input is, number of contracts for long, and
this number of contracts for short.
Okay?
Page 6 of 14
(c) 2018 [Link] / Distribution without author´s approval is not permitted.
TRADING INVOLVES A SUBSTANTIAL RISK OF LOSS AND IS NOT SUITABLE FOR ALL INVESTORS,
PAST PERFORMANCE IS NOT NECESSARILY INDICATIVE OF FUTURE RESULTS
So, for example, 1 contract long and 1 short will be in Zone Number 1. 2 con-
tracts in Zone Number 2, and 3 contracts in Zone Number 3.
So, that's the only thing you need to take care of and be aware of.
I think that if you just have a look at the codes you very easily see how to do
that. It's just a matter of copy/paste. You will do this in couple of seconds, lit-
erally in 20 seconds: copy/paste, change, insert this number of contracts in-
put. And that's it.
And now, you have a special code, which I call Optimization Code, and this
code will go through all the six techniques, okay?
So, it will go through the first technique, which is the basic market contrac-
tion/expansions, when we basically only set the range among last 100 bars,
split it into three zones, and then define each zone separately - define what is
Zone 1, what is Zone 2, what is Zone 3.
The second approach uses moving average.
The third one uses difference between two moving averages.
The fourth one uses, again, just close, but on daily data. So, we will insert
Data 3, which will be daily data.
The fifth one will use one moving average on daily data, again, on Data 3.
And technique number six will use the difference between two moving aver-
ages on daily data, again.
So, with this code we will - how I'll assemble this code is like this. So, first
data will always be your underlying market for your strategy. Whatever strat-
egy you copy/paste, this will be your underlying, that's logical.
Then if your strategy uses second timeframe, then just set up your second
timeframe. If it doesn't, just use the same timeframe as you are using in Data
Number 1.
And Data 3 always will be your strategy's market daily timeframe, because
we will be evaluating all these three market contraction/expansion dynamic
position sizing techniques on daily data as well.
Page 7 of 14
(c) 2018 [Link] / Distribution without author´s approval is not permitted.
TRADING INVOLVES A SUBSTANTIAL RISK OF LOSS AND IS NOT SUITABLE FOR ALL INVESTORS,
PAST PERFORMANCE IS NOT NECESSARILY INDICATIVE OF FUTURE RESULTS
From my experience, quite often it works better if we use daily data as un-
derlying.
And it also adds kind of independence, even if it's a dependent input. But, if
we have strategy, let's say it updates only 20-minute charts, and we use for
dynamic position sizing daily data, we're having new level of independence.
It’s kind of become an independent input and that's what we want primarily
to work with - as much independence as possible to avoid any curve-fitting,
and also make it truly independent. Give it high relevance.
So, we have this code and this code basically goes through all the six tech-
niques with your existing strategy that you just need to copy/paste. And
then, one big difference for you, or something new - maybe you didn't antici-
pate it. Now, besides optimising inputs one through six, that means six differ-
ent DPS techniques within the market contraction/expansions group, we will
test - we will also optimise the number of contracts for each zone differently
for long and short. And now we will put into optimization either one from 1
to 3, or from 0 to 2.
Whatever you prefer.
We prefer one from 1 to 3 in our hedge fund, but you can use both, whatever
you prefer.
And the surprising difference, or something new probably for you, we will
use this optimization, 1 to 3, with each input. So, at the end, we can have -
it's not going to be that we will have one contract for Zone 1, two contracts
for Zone 3, and two contracts for Zone 2, and three contracts for Zone 3, but
we can have, let's say, 1, 3, 3.
That's the power of this technique and optimization, that it finds even better
solution if we allow different number of contracts within each zone - differ-
ent for short and different for long.
But always between 1 and 3, or 2 to 6 by step two, okay?
Or 0 to 2, step one; or 0 to 4, step two.
So, you know what I mean - we will have three different versions.
But it's totally okay if you have, let's say, 3, 1, 3, and here 2, 2, 1.
Page 8 of 14
(c) 2018 [Link] / Distribution without author´s approval is not permitted.
TRADING INVOLVES A SUBSTANTIAL RISK OF LOSS AND IS NOT SUITABLE FOR ALL INVESTORS,
PAST PERFORMANCE IS NOT NECESSARILY INDICATIVE OF FUTURE RESULTS
Okay. It's totally okay, and you will get significantly better results, like really
good results if you use it like this.
So, that's the code.
That's not yet the final smart code, because the final smart code in the bonus
lesson will be a big code with all the techniques, but this is something like a
very, very mini-smart code for this first approach, the market contraction/ex-
pansion.
And what you do, you basically just insert it and see what happens.
So, that's this window.
Okay, into this window I have already added this optimization code.
And now what you want to do, you basically want to optimise techniques
Number 1 from 1 to 6; and here, number of contracts for each zone, differ-
ently long and short, between 1 and 3, 1 and 3, 1 and 3, 1 and 3, 1 and 3, 1
and 3, 1 and 3 - which will leave you with about four-and-a-half thousand it-
erations.
And now, super, super important, you want to – or you need to - use 20%
out-of-sample.
Because the result which we will get, we will need to validate on out-of-sam-
ple data. So, I'll show you later.
Now, because the optimization on this laptop, which I'm using for recording
this module, it takes about one-and-a-half hour, I did the optimization in ad-
vance.
So, first I skip to the results, and then I'll get back and show you how to set it
up.
So, we run optimization, and then we have some optimization results, as you
already know.
Now, what we do with the results - first of all, always use in-sample only.
Okay, that's very important. Choose in-sample. And then you sort it by your
fitness function, which, as you already know, in our hedge fund we always
use TradeStation Index.
Page 9 of 14
(c) 2018 [Link] / Distribution without author´s approval is not permitted.
TRADING INVOLVES A SUBSTANTIAL RISK OF LOSS AND IS NOT SUITABLE FOR ALL INVESTORS,
PAST PERFORMANCE IS NOT NECESSARILY INDICATIVE OF FUTURE RESULTS
And now, you see the results.
And how do you interpret these results?
What do you do?
So, first of all, what you're really interested in is this first column. That's the
technique, the number of the technique. And very quickly scan, let's say, first
20.
Okay?
And you see that there are some highly repetitive techniques. So, within the
first 20, the highest amount of techniques represented here is technique
number 6 and 5.
Okay, 6 and 5.
So, 6 and 5 are the best two techniques, the most robust one, because they
are represented in first 20 top results.
We also have 1, but it's only twice.
But, 6 and 5 has got the most of occurrences within the first 20 best results.
So, that's what we want to use, either 6 or 5.
You see both are on daily data, on second timeframe. So second timeframe
helps quite a lot here.
And now we need to make a decision if we go with 6 or 5, because both can
be good. What I do, I look at average trade, and I can see that number 2, that
means technique 5, delivers way higher average trade in general.
If you compare number 6 and number 5, you see that technique number 5
delivers way better, every trade.
So we will take the first reasonable, technique number 5, which is line num-
ber 2, because it's got very good average trade, and that's the solution. Tech-
nique number 5.
And you see that for long we would have Zone Number 1 would trade three
contracts; Zone Number 2, two contracts, Zone Number 3, two contracts.
Page 10 of 14
(c) 2018 [Link] / Distribution without author´s approval is not permitted.
TRADING INVOLVES A SUBSTANTIAL RISK OF LOSS AND IS NOT SUITABLE FOR ALL INVESTORS,
PAST PERFORMANCE IS NOT NECESSARILY INDICATIVE OF FUTURE RESULTS
When it comes to shorts, Zone Number 1 would take one contract; Zone
Number 2, three contracts; and Zone Number 1, one contract.
So, we use the solution, okay?
And now we need to evaluate the out-of-sample.
So we have a look at optimization - sorry, not optimization - we have a look
on Performance Report. And we use in-sample only.
So here we have in-sample. Okay, everything goes all right.
And now we look separately on out-of-sample, and we want to see some rea-
sonable continuation of the overall tendency on out-of-sample to confirm
that this is the solution to go with - the technique number 5.
And you see that out-of-sample works very nicely, absolutely in accordance
with in-sample, 13,000 profit. So if we merge them, we have equity, which
looks pretty same all the way along.
So, this is definitely the technique we would choose from those six. So, that
would be technique number 5.
And that's going to be our setup and our technique, okay?
So, what we will have will be technique number 5, and we will use - the num-
ber of contracts will be 3 to 2, 1, 3, 1.
That will be our solution.
And then we just strip this into final code. Which I will show you in a minute.
Before that, let's finish this optimization.
So, when you do the optimization you set up all the optimization ranges, as I
explained, 1 to 6, 0, 1 to 3, 1 to 3, 1 to 3, 1 to 3, 1 to 3, 1 to 3.
And before you optimise, very important, in advanced setting you always use
at least 20% of out-of-sample data for later validation.
Page 11 of 14
(c) 2018 [Link] / Distribution without author´s approval is not permitted.
TRADING INVOLVES A SUBSTANTIAL RISK OF LOSS AND IS NOT SUITABLE FOR ALL INVESTORS,
PAST PERFORMANCE IS NOT NECESSARILY INDICATIVE OF FUTURE RESULTS
You can use 25% if you want to, and that's okay too. 20 or 25. It's up to you.
And you will optimise without last 20 or 25% of data. That you will use for
validation the way I showed you.
Okay?
So, that's what you do.
And then you will have the result technique, which we already know in our
case is the technique number 5.
So, we create strip code, which I call result code, where we take the tech-
nique number 5, which would be this one: simple moving average on daily
data.
And we use it here.
Okay, so we strip everything, and we use the input is: average of Data 2.
The final strategy uses only one data feed, one timeframe, so we change it,
we swap it to Data 2, which will be daily data.
We keep the zone calculation, and here we have the strategy, and here we
keep all this part. Basically we keep almost everything. We just strip all the
cases that we don't need, and we only keep the case number 5.
So, it's very simple. Again, just use these two codes. Compare them against
each other. You will see very easily what I did.
Again, this is an advanced course. It's called Dynamic Position Sizing for Ad-
vanced Traders, so we expect you to understand this basic stuff already.
So, now with the final strategy, we only have this winning contraction/expan-
sion DPS technique which is the higher picture advanced market contrac-
tion/expansion technique with one moving average.
And we have this number of contracts, 3 to 2, 1, 3, 1.
So, here, the last window here, you have the strategy and the chart.
You see result call. We have basic timeframe of our basic market, and here
we have daily timeframe.
Page 12 of 14
(c) 2018 [Link] / Distribution without author´s approval is not permitted.
TRADING INVOLVES A SUBSTANTIAL RISK OF LOSS AND IS NOT SUITABLE FOR ALL INVESTORS,
PAST PERFORMANCE IS NOT NECESSARILY INDICATIVE OF FUTURE RESULTS
So, we can now compare the results.
So, here we set up the same: 5, 3, 2, 2, 1,3,1.
We should see same result. So, strategy performance report.
So, we should have same equity curve. And the same numbers, 70,775. And
here we have 70,775. So, it matches.
And that's the final strategy we have here.
Okay, so I hope you understand everything, what we've done here, what we
did here. We will do basically the same thing with other modules. But, other
modules will use a different input technique - sometimes dependent, some-
times independent - but we'll work with different techniques.
But I hope this one, contraction/expansion is clear to you. And let's have a
look how much we actually improved the strategy.
So, before that, just reminder what we want to do in TradeStation: sort by in-
sample TradeStation Index. We look at top 20 results. And we see which one
repeats mostly, which is case number 6 and 5.
We choose the one with higher average trade, and that would be our solu-
tion.
Then we validate on out-of-sample – which we did.
And the results are like this.
So, with this dynamic position sizing we improved net profit from 25,000 to
70, which is something we would expect because we're trading up to three
contracts now, so the improvement is 178%.
We have little bit increase of the maximum drawdown, which again, it's quite
logical with the net profit but what's really important is the net profit ratio,
which jumped from 4.3 to 9.3, which is 116% improvement.
Which is huge, huge, huge improvement.
Page 13 of 14
(c) 2018 [Link] / Distribution without author´s approval is not permitted.
TRADING INVOLVES A SUBSTANTIAL RISK OF LOSS AND IS NOT SUITABLE FOR ALL INVESTORS,
PAST PERFORMANCE IS NOT NECESSARILY INDICATIVE OF FUTURE RESULTS
So, yes, we have a little bit higher draw down now, by 29%, but because of
that we also improved our net profit by almost 200%, so we're now milking
this strategy like crazy.
We're getting way more money from this single strategy, with a very small
cost of a very, very gently increased drawdown. We improved the overall ra-
tio by 116%, which is huge.
So, you see dynamic position sizing can have massive impact, massive results.
And it will be a great, great thing that will significantly improve your overall
portfolio - all your strategies.
Okay, so that was the very first dynamic positions approach. The contrac-
tion/expansion technique with dependent input, but partially also independ-
ent if we use daily data. And I'm looking forward to presenting to you the
next dynamic position sizing, which is coming in Module Number 3.
Page 14 of 14
(c) 2018 [Link] / Distribution without author´s approval is not permitted.
TRADING INVOLVES A SUBSTANTIAL RISK OF LOSS AND IS NOT SUITABLE FOR ALL INVESTORS,
PAST PERFORMANCE IS NOT NECESSARILY INDICATIVE OF FUTURE RESULTS