A Trading System Development Tutorial For The Amibroker Stock Charting/Analysis Program
A Trading System Development Tutorial For The Amibroker Stock Charting/Analysis Program
A Trading System Development Tutorial For The Amibroker Stock Charting/Analysis Program
This tutorial was prepared in response to a post by Dimitris Tsokakis, on the AmiBroker
Users list, in which he introduces the basic idea for a StoCCI system (see text on page 2).
The procedures used below reflect personal design philosophy and preferences. I do not
use any commissions or interest in my calculations, if these play a role the system it is so
weak that I discard it. You may or may not agree with my approach, either way I
encourage you to express your opinion on the AmiBroker list. It is only through frank
dialogue and criticism that we can improve our skills. Kindly report any errors or
omissions to me privately: [email protected]
Important Note: This report outlines typical procedures that you may use to develop a
trading system. Do not trade this system as is; no profitability is suggested or implied.
Posted on [email protected]
By: Dimitris Tsokakis [[email protected]]
Subject: [amibroker] Tranformations
Date: Mon 7/29/02 5:41 AM
Thank you Dimitris, for this and many other interesting posts!
Preliminary observations
There are many ways to do this however it is my personal preference to develop on data
(starts 1/1/1997 for me) before 1/1/01 and perform out-of-sample testing on the
subsequent period of 1/1/01 to today. I will adopt these choices for this project.
To duplicate this exercise you will need INTC data from 1/1/97 to today. I use QuotePlus
data, if you use another data source your results may be slightly different. Do not be
concerned about that; it is the procedures that are important, not the exact figures or
profits. Before starting you must make INTC your current stock, select it in your
workspace. I use these three testing periods:
When working with new systems I always first determine the optimum direction of
signal-threshold crossings. This is a reversal system so there are only two ways the
StoCCI indicator can cross the thresholds.
If the system were utilizing cash positions there would be four possible combinations. I
perform this step on “All Quotations” because the system, at this stage, performs very
poorly; using a longer testing period will give me a better indication. I assume that the
optimum crossings remain constant for the system.
For this test I also set the Threshold levels to “neutral” that would be 50 in this case. I do
this to prevent distortion of the results by far-out threshold choices. I use this code to test
the crossovers:
StOcci=100*(CCI(P)-LLV(CCI(P),LB))/(HHV(CCI(P),LB)-LLV(CCI(P),LB));
Plot(StoCCI,"StoCCI",4,1);
StoCCI=MA(stocci,Ps);
Buy = IIf(TM==1,Cross(STOCCI,BLEVEL),Cross(BLEVEL,STOCCI));
Sell = IIf(TM==1,Cross(Slevel,STOCCI),Cross(STOCCI,SLevel));
Buy = D*Buy;
Sell = D*Sell;
Short = Sell;
Cover = Buy;
The clear winner, which I confirmed with tests over other periods (not shown) is TM=2.
Modifying the basic formula we now have:
StOcci=100*(CCI(P)-LLV(CCI(P),LB))/(HHV(CCI(P),LB)-LLV(CCI(P),LB));
Plot(StoCCI,"StoCCI",4,1);
StoCCI=MA(stocci,Ps);
Buy = Cross(BLEVEL,STOCCI);
Period Optimizing
All optimizations are performed on data from 1/1/1997 to 1/1/01. This reserves data from
1/1/01 to today for out-of-sample testing.
Looking at the charts and pick the best periods is not easy. Clearly the highest gain is
obtained with Ps of 10, 11 and 12. We also note that the CCI Period (P) is 5 for each of
these three cases. So, to start I’ll pick the middle value of 11 for smoothing and use a
period of 5 for the of CCI().
We still have three choices (8, 3 and 6) for the LookBack value. Further testing is needed
to make a decision on this. To get a good feel of how they work I overlay the equities in
one display, I use the following code to do this.
Figure 1 - Ps=1
Figure 3 - Ps=3
Figure 4 - Ps=4
Figure 6 - Ps=6
Figure 7 - Ps=7
Figure 9 - Ps=9
Figure 10 - Ps=10
Figure 12 - Ps=12
Figure 13 - Ps=13
Figure 15 - Ps=15
// System #1
P = 5; //Optimize("Pd",6,2,10,1); //Periods as provided by Dimitris
LB = 8; //Optimize("LB",14,1,20,1);
Ps = 11; //Optimize("Ps",5,1,6,1);
D = 1; //DateNum()>1000301; //Disable datenum()
Blevel = 50; //Optimize("BL",12,10,20,1); //Use neutral thresholds
Slevel = 50; //Optimize("SL",70,70,90,1);
StOcci=100*(CCI(P)-LLV(CCI(P),LB))/(HHV(CCI(P),LB)-LLV(CCI(P),LB));
StoCCI=MA(stocci,Ps);
Buy = Cross(BLEVEL,STOCCI);
Sell = Cross(STOCCI,SLevel);
Buy = D*Buy;
Sell = D*Sell;
Short = Sell;
// System #2
/* Listing #2 - Smoothed Stochastic CCI*/
P = 5; //Optimize("Pd",6,2,10,1); //Periods as provided by Dimitris
LB = 3; //Optimize("LB",14,1,20,1);
Ps = 11; //Optimize("Ps",5,1,6,1);
D = 1; //DateNum()>1000301; //Disable datenum()
Blevel = 50; //Optimize("BL",12,10,20,1); //Use neutral thresholds
Slevel = 50; //Optimize("SL",70,70,90,1);
StOcci=100*(CCI(P)-LLV(CCI(P),LB))/(HHV(CCI(P),LB)-LLV(CCI(P),LB));
StoCCI=MA(stocci,Ps);
Buy = Cross(BLEVEL,STOCCI);
Sell = Cross(STOCCI,SLevel);
Buy = D*Buy;
Sell = D*Sell;
Short = Sell;
Cover = Buy;
E2=Equity();
Plot(E2,"E2 LB=8 (BLK)",1,1);
//System #3
/* Listing #2 - Smoothed Stochastic CCI*/
P = 5; //Optimize("Pd",6,2,10,1); //Periods as provided by Dimitris
LB = 6; //Optimize("LB",14,1,20,1);
Ps = 11; //Optimize("Ps",5,1,6,1);
D = 1; //DateNum()>1000301; //Disable datenum()
Blevel = 50; //Optimize("BL",12,10,20,1); //Use neutral thresholds
Slevel = 50; //Optimize("SL",70,70,90,1);
StOcci=100*(CCI(P)-LLV(CCI(P),LB))/(HHV(CCI(P),LB)-LLV(CCI(P),LB));
StoCCI=MA(stocci,Ps);
Buy = Cross(BLEVEL,STOCCI);
Sell = Cross(STOCCI,SLevel);
Buy = D*Buy;
Sell = D*Sell;
Short = Sell;
Cover = Buy;
E3=Equity();
Plot(E3,"E3 LB=6 (BLU)",6,1);
GraphXSpace = 5;
/*
The following chart show three equities for the 8, 3 and 6 LookBack periods. These
systems were optimized before the vertical purple line; to the right of the Purple line is
the out-of-sample period.
Threshold Optimization
We now are working with a modified formula that gives about 5500% profits since
1/1/97 and 280% for the out of sample period 1/1/01 to 7/26/02. It is time to optimize the
thresholds and I will use this formula:
/* Listing #4
/* Smoothed Stochastic CCI*/
D = 1; //DateNum()>1000301; //Disable datenum()
Blevel = Optimize("BL",49,10,20,1); //Use neutral thresholds
Slevel = Optimize("SL",49,70,90,1);
StOcci=100*(CCI(5)-LLV(CCI(5),3))/(HHV(CCI(5),3)-LLV(CCI(5),3));
Buy = Cross(BLEVEL,STOCCI);
Sell = Cross(STOCCI,SLevel);
Buy = D*Buy;
Sell = D*Sell;
Short = Sell;
Cover = Buy;
E1=Equity();
Plot(E1,"Equity Listing #4",4,1);
GraphXSpace = 5;
/*
I first optimize the thresholds separately, to save time. Doing this I obtain 49 for each, the
Long and the Short Thresholds. Next I optimize them simultaneously to make sure there
is no interaction between the thresholds – while I can’t explain it I have seen this happen.
Below are the Flat and 3D charts for thresholds from 25-75.
The 3D chart reveals a central point of high profits. Normally such sharp peaks worry me
however in this case there are very few competitive peaks so it appears that this stock has
some characteristics that make it response favorable to our system.
Also note that this chart uses a linear axis for the gain while the profits were
compounded. I tried a log scale but it wasn’t as clear to see the system’s performance, so
I show you the linear chart.
All in all I am pleased with the gain concentration around our thresholds of 49/49, so I’ll
continue working with these.
Our system now gives about 2100% profit in the development period (1997-2001) and
gives about 400% profit during the out-of-sample period. Total profits since 1/1/1997 are
about 12400%.
I believe that Stops are more time sensitive than periods and should be optimized using
more recent data. This is my personal preference. Contrary to what many traders say I
believe that Stops must be optimized separately for Long and Short positions. I modified
the formula to do just that.
Again, to save time, I perform a quick optimization at 1% increments I find that 8% for
Long and 9% for Short positions increase the returns. I have found that fractional
percentages can make a lot of difference so I will repeat the optimization with 0.1%
increments over a narrower range. My formula:
/* Listing #5
/* Smoothed Stochastic CCI*/
D = 1; //DateNum()>1000301; //Disable datenum()
StOcci=100*(CCI(5)-LLV(CCI(5),3))/(HHV(CCI(5),3)-LLV(CCI(5),3));
StoCCI=MA(stocci,11);
Buy = Cross(49,STOCCI);
Sell = Cross(STOCCI,49);
Buy = D*Buy;
Sell = D*Sell;
Short = Sell;
Cover = Buy;
E = Equity();
Plot(E,"Equity wo Stops",4,1);
LPos = Flip(Buy,Sell);
SPos = Flip(Short,Cover);
LPT = 8.1; //Optimize("LProfitTarget",8,5,10,0.1);
SPT = 9.6; //Optimize("SProfitTarget",9,5,10,0.1);
PT = IIf(LPos,LPT,IIf(SPos,SPT,0));
ApplyStop(1,1,PT,1);
E = Equity();
Plot(E,"Equity with PT Stops",6,1);
GraphXSpace = 5;
/*
Profits increased from about 12K% to 20K% for the 1997-2002 range. Most of the profit
increase took place in the out-of-sample period, see chart below.
This looks OK but I would like to verify the distribution of the stops, there is always
something to learn! I exported the optimization values from AmiBroker and produced a
3D-Chart, this is what it looks like.
Net profit improved 19 times, approximately in proportion to the number of trades. Draw
Down is reduced about 50%. Below is a chart showing both the starting and final
Figure 18 - The starting position (BLK) and result of our efforts (RED)
Performance Overview
The final equity curve for the 1/1/97 to today period is now:
Conclusion
The above showed that with a systematic approach and by taking the time to make some
charts you can improve on almost any trading system. The stock and formulas are
incidental and you can apply the process on your favorite formulas and stocks.
If you give it a try and the performance of your system improves, please share your
experience with me. No need to email formulas, I am only interested to know what
difficulties you encountered, what other development techniques you employed, and how
you improved on the system.
Do not trade this system as is; no future profitability is suggested or implied. In fact, I
would not trade it since it works only well on INTC.
The next step is up to you J I wish you the best of luck in your trading efforts!
Herman.