WARNING CHEATING AHEAD 😉 There is no Holy Grail!
Having the results of a Perfect Profit System for any given market can be helpful when trying to determine the quality and viability of one’s own algorithm (ala Bob Pardo – The Evaluation and Optimization of Trading Strategies). The level of correlation between a user defined algorithm and the PPS can indicate the quality of an algorithm – if its not been overly curve-fit.
Michael Bryant of AdapTrade.com has written an excellent article on how to use the correlation between a user defined algo and the PPS to optimize the algo to increase the correlation co-efficient between the two. The premise being the closer you get to the PPS with your own strategy the better off you are.
Click here for Michael Bryant’s excellent article.
Bob Pardo’s perfect system simply accumulated the absolute differences between consecutive closes to create the PPS equity stream. In Michael Bryant’s article he utilizes price swings based of either price points or fractions of ATR (average true range.) Bryant’s version takes advantage of 20/20 hindsight and creates a more realistic trade count. Bob’s version would trade every day. Michael’s version trades much, much, less.
My version, with the benefit of hindsight, picks the highest close and lowest close of every month and uses the data to sellShort and buy at these levels respectively. I wanted to actually show the trades on the charts as they maybe helpful as well. So to do this you have to run two strategies : 1) one that picks and outputs the trades and 2) one that takes the output from one and executes the trades.
There were two ways to accomplish this : 1) calculate the monthly turning points and output the data to a file and then insert the data as 3rd party index into another chart of the same market and program a strategy that interprets the index or 2) calculate the monthly turning points and output the information as pure EasyLanguage code snippet and paste that code into another strategy. I chose the second method as it also helps show the reader how to do some really cool stuff.
Before I started to program this research tool I asked myself “How can you pick the monthly peaks and valleys as you run through the data?” Well I knew I had to be at the end of the month and then look back enough bars to choose these points. Since the number of trading days in a month are variable I knew I had to use a while loop. While loops are really cool and easy to use, but you must be careful or you will land in Steve Jobs 1 infinite loop. TradeStation is very patient and will let you know before crashing and burning though. Here is the snippet of code that loops back in time to gather the monthly peaks and valleys:
If month(d) <> month(d[1]) then // are we in a new month?
Begin
Value1 = 1;
hiMonthPrice = 0;
loMonthPrice = 999999;
testMonth = month(date);
While month(d[value1]) = month(d[value1+1]) //loop until the prior month
Begin
If c[value1] > hiMonthPrice then
Begin
hiMonthPrice = c[value1]; //store the peak price
hiMonthBar = value1; // store the peak bar number
end;
If c[value1] < loMonthPrice then
Begin
loMonthPrice = c[value1]; //store the valley price
loMonthBar = value1; // store the valley bar number
end;
Value1 = value1 + 1;
end;
// print(d," monthHi ",hiMonthPrice," ",hiMonthBar);
// print(d," monthLo ",loMonthPrice," ",loMonthBar);
Looping Backward - Collecting Peaks 'n Valleys
Okay we now have the prior month’s peak and valley values. So we need to store this information into a list. EasyLanguage allow for user defined arrays, which are really just indexable (I think that is a word) lists. Here is a list of arrays that we will be using and how they are declared:
Array: equPerf[5000](0), { equity at each bar, perfect trades }
trIdeal[2000](0), { equity at end of perfect trades }
datesPerf[2000](0), { dates of perfect trades }
BorSPerf[2000](0), { buys or sells of perfect trades}
pricePerf[2000](0); { prices of perfect trades}
I over dimensioned these Arrays - 2 per month right?
To declare an array you use the Keyword array and then give the array a name and then use square brackets to tell the computer the maximum number of elements in the array then instantiate each array element to a value that is surrounded by parenthesis. Here we assigned each value in the arrays to zero.
Now that we have our lists and we are running through the chart and stopping at the end of each month and collecting peaks and valleys how do we turn this into an EasyLanguage strategy? I had to think about this which I usually don’t do I just start programming. I did just start programming but I didn’t get the results I wanted so then I had to think about it. I knew this strategy needed to be in the market 100% of the time so it would be a stop and reverse system. The first trade would be important so I needed to know which occurred first in the month – the peak or the valley. If it was a peak then the first trade would be a sell and the second would be a buy, since we are simply selling peaks and buying valleys. I then extrapolated this idea out to each month. If I exited a month in a long position than I would want the following month’s valley to be first. In a perfect would it would be peak, valley, peak, valley and so on. That doesn’t always happen. Sometimes you buy a valley late in a month and the market starts off the next month in a decline. So you have to wait for the monthly peak to sell short and this could lead to a sizable loss – remember we are looking for an Almost Perfect System. I could have programmed it so that the buying late in the month and subsequent market decline scenario didn’t occur, but it would require eliminating some trades. Since there’s not that many trades to start with I decided to go a different route. So what I did was program an exception when there was a flux in the peak-valley continuum. If I was exiting the month in a long position and a valley preceded a peak in the following month I simply reversed my position on the first day of the month. Easy fix right? At the beginning of the month I wanted all my trades set up from the preceding month – I did this to just make things simpler. Here is the snippet of code that puts the data into the arrays we will use later to create the EasyLanguage code:
If tempBors = 1 then
Begin
If hiMonthBar > loMonthBar then
Begin // long and peak occurs first
BorSPerf[trdCnt] = -1;
datesPerf[trdCnt] = d[hiMonthBar];
pricePerf[trdCnt] = c[hiMonthBar];
print(datesPerf[trdCnt]," Sell ",pricePerf[trdCnt]);
trdCnt = trdCnt + 1;
BorSPerf[trdCnt] = 1;
datesPerf[trdCnt] = d[loMonthBar];
pricePerf[trdCnt] = c[loMonthBar];
print(datesPerf[trdCnt]," Buy ",pricePerf[trdCnt]);
trdCnt = trdCnt + 1;
end
else
begin // long and valley occurs first
BorSPerf[trdCnt] = -1;
datesPerf[trdCnt] = d[value1];
pricePerf[trdCnt] = c[value1];
print(datesPerf[trdCnt]," Sell Conflict ",pricePerf[trdCnt]);
trdCnt = trdCnt + 1;
BorSPerf[trdCnt] = 1;
datesPerf[trdCnt] = d[loMonthBar];
pricePerf[trdCnt] = c[loMonthBar];
print(datesPerf[trdCnt]," Buy ",pricePerf[trdCnt]);
trdCnt = trdCnt + 1;
BorSPerf[trdCnt] = -1;
datesPerf[trdCnt] = d[hiMonthBar];
pricePerf[trdCnt] = c[hiMonthBar];
print(datesPerf[trdCnt]," Sell ",pricePerf[trdCnt]);
trdCnt = trdCnt + 1;
end;
end;
Decision Process When Coming Into Month Long
Once you go through the entire chart you should have all of you trade arrays set up. So on the last bar of the chart you need to print this data out to a file. But you want to print it out so you can simply copy and paste the output into another strategy – in other words you want to be syntactically correct.
Use the keyword LastBarOnChart to predicate the output process. You can only print out strings to a file in EasyLanguage so all numbers have to be converted to strings – use NumToStr to accomplish this feat. I think I have discussed formatted output somewhere in a post, but here is a quick reminder. Let’s say you want to convert 1234.5678 to the string 1234.56, you would use NumToStr(value1 , 2). The second argument in the function call informs the computer to truncate the decimal to two places. If you want to concatenate multiple strings together (i.e. connect strings together) you simply use the + sign. Look at this line of code:
StrOut = “DateArr[“+NumtoStr(value1,0)+”] = ” + numToStr(19000000+datesPerf[value1], 0) + “;TradeArr[“+numtoStr(value1,0)+”] = ” +numToStr(BorSPerf[value1],0) + “;PriceArr[“+NumtoStr(value1,0)+”] = ” + numToStr(pricePerf[value1],2) + “;”+ Newline;
This is what it creates:
DateArr[1] = 20001211;TradeArr[1] = -1;PriceArr[1] = 1368.50;
This is a neat line of code where I mix hard coded word strings with converted numerical values to strings to create syntactically correct EasyLanguage in one complete, albeit long, string.
Now all you need now is an interpreter strategy. The output from the first pass program creates the arrays needed for the interpreter program and then outputs all of the trades as pre-filled arrays. Here is the output program in its entirety:
input: FName ("c:\PerfectSystem\output.txt"); { file name to write results to }
Var: HighBar(0), LowBar(0),ibar(0), StrOut(""),
BorS (0),daysInEquity(0),equCnt(0),dataIndex(0),tempMP(0),trdCnt(0),trdNum(0),
oteEqu(0),clsTrdEqu(0), hiMonthPrice(0),hiMonthBar(0),
loMonthPrice(0),loMonthBar(0),testMonth(0),firstTime(true);
Array: equPerf[5000](0),trPerf[2000](0),datesPerf[2000](0),
BorSPerf[2000](0),pricePerf[2000](0);
If month(d) <> month(d[1]) then // are we in a new month
Begin
Value1 = 1;
hiMonthPrice = 0;
loMonthPrice = 999999;
testMonth = month(date);
While month(d[value1]) = month(d[value1+1]) //loop until the prior month
Begin
If c[value1] > hiMonthPrice then
Begin
hiMonthPrice = c[value1]; //store the peak price
hiMonthBar = value1; // store the peak bar number
end;
If c[value1] < loMonthPrice then
Begin
loMonthPrice = c[value1]; //store the valley price
loMonthBar = value1; // store the valley bar number
end;
Value1 = value1 + 1;
end;
// print(d," monthHi ",hiMonthPrice," ",hiMonthBar);
// print(d," monthLo ",loMonthPrice," ",loMonthBar);
vars: tempBorS(0);
If firstTime then
begin
tempBorS = -1;
If hiMonthBar > loMonthBar then tempBorS = 1;
trdCnt = 1;
firstTime = false;
end
else
begin
tempBorS = BorSPerf[trdCnt-1];
end;
If tempBors = 1 then
Begin
If hiMonthBar > loMonthBar then
Begin // long and peak occurs first
BorSPerf[trdCnt] = -1;
datesPerf[trdCnt] = d[hiMonthBar];
pricePerf[trdCnt] = c[hiMonthBar];
print(datesPerf[trdCnt]," Sell ",pricePerf[trdCnt]);
trdCnt = trdCnt + 1;
BorSPerf[trdCnt] = 1;
datesPerf[trdCnt] = d[loMonthBar];
pricePerf[trdCnt] = c[loMonthBar];
print(datesPerf[trdCnt]," Buy ",pricePerf[trdCnt]);
trdCnt = trdCnt + 1;
end
else
begin // long and valley occurs first
BorSPerf[trdCnt] = -1;
datesPerf[trdCnt] = d[value1];
pricePerf[trdCnt] = c[value1];
print(datesPerf[trdCnt]," Sell Conflict ",pricePerf[trdCnt]);
trdCnt = trdCnt + 1;
BorSPerf[trdCnt] = 1;
datesPerf[trdCnt] = d[loMonthBar];
pricePerf[trdCnt] = c[loMonthBar];
print(datesPerf[trdCnt]," Buy ",pricePerf[trdCnt]);
trdCnt = trdCnt + 1;
BorSPerf[trdCnt] = -1;
datesPerf[trdCnt] = d[hiMonthBar];
pricePerf[trdCnt] = c[hiMonthBar];
print(datesPerf[trdCnt]," Sell ",pricePerf[trdCnt]);
trdCnt = trdCnt + 1;
end;
end;
If tempBors = -1 then
Begin
If loMonthBar > hiMonthBar then
Begin
BorSPerf[trdCnt] = 1;
datesPerf[trdCnt] = d[loMonthBar];
pricePerf[trdCnt] = c[loMonthBar];
print(datesPerf[trdCnt]," Buy ",pricePerf[trdCnt]);
trdCnt = trdCnt + 1;
BorSPerf[trdCnt] = -1;
datesPerf[trdCnt] = d[hiMonthBar];
pricePerf[trdCnt] = c[hiMonthBar];
print(datesPerf[trdCnt]," Sell ",pricePerf[trdCnt]);
trdCnt = trdCnt+ 1;
end
else
begin
BorSPerf[trdCnt] = 1;
datesPerf[trdCnt] = d[value1];
pricePerf[trdCnt] = c[value1];
print(datesPerf[trdCnt]," Buy Conflict ",pricePerf[trdCnt]);
trdCnt = trdCnt + 1;
BorSPerf[trdCnt] = -1;
datesPerf[trdCnt] = d[hiMonthBar];
pricePerf[trdCnt] = c[hiMonthBar];
print(datesPerf[trdCnt]," Sell ",pricePerf[trdCnt]);
trdCnt = trdCnt + 1;
BorSPerf[trdCnt] = 1;
datesPerf[trdCnt] = d[loMonthBar];
pricePerf[trdCnt] = c[loMonthBar];
print(datesPerf[trdCnt]," Buy ",pricePerf[trdCnt]);
trdCnt = trdCnt + 1;
end;
end;
end;
tempMP = 0;
vars:cntDown(0);
If lastBarOnChart then
Begin
FileAppend(FName, NewLine + "arrays: DateArr[5000](0),TradeArr[5000](0),PriceArr[5000](0);" + NewLine);
For value1 = 1 to trdCnt
Begin
StrOut = "DateArr["+NumtoStr(value1,0)+"] = " + numToStr(19000000+datesPerf[value1], 0) + ";TradeArr["+numtoStr(value1,0)+"] = " +numToStr(BorSPerf[value1],0) + ";PriceArr["+NumtoStr(value1,0)+"] = " + numToStr(pricePerf[value1],2) + ";"+ Newline;
FileAppend(FName, StrOut);
end;
end;
Almost Perfect System EasyLanguage Creator Code
Here’s the output that was created when this strategy as was added to a 19 year long ES daily bar chart:
arrays: DateArr[5000](0),TradeArr[5000](0),PriceArr[5000](0);
DateArr[1] = 20001211;TradeArr[1] = -1;PriceArr[1] = 1368.50;
DateArr[2] = 20001220;TradeArr[2] = 1;PriceArr[2] = 1245.75;
DateArr[3] = 20010102;TradeArr[3] = -1;PriceArr[3] = 1265.50;
DateArr[4] = 20010105;TradeArr[4] = 1;PriceArr[4] = 1269.50;
DateArr[5] = 20010131;TradeArr[5] = -1;PriceArr[5] = 1346.50;
DateArr[6] = 20010201;TradeArr[6] = 1;PriceArr[6] = 1348.00;
DateArr[7] = 20010205;TradeArr[7] = -1;PriceArr[7] = 1328.25;
DateArr[8] = 20010228;TradeArr[8] = 1;PriceArr[8] = 1203.75;
DateArr[9] = 20010307;TradeArr[9] = -1;PriceArr[9] = 1231.25;
DateArr[10] = 20010322;TradeArr[10] = 1;PriceArr[10] = 1070.00;
DateArr[11] = 20010402;TradeArr[11] = -1;PriceArr[11] = 1100.75;
DateArr[12] = 20010403;TradeArr[12] = 1;PriceArr[12] = 1059.25;
DateArr[13] = 20010419;TradeArr[13] = -1;PriceArr[13] = 1209.75;
DateArr[14] = 20010501;TradeArr[14] = 1;PriceArr[14] = 1223.50;
DateArr[15] = 20010521;TradeArr[15] = -1;PriceArr[15] = 1265.50;
DateArr[16] = 20010530;TradeArr[16] = 1;PriceArr[16] = 1201.00;
Sample OutPut
Here is the interpreter strategy code. I simply copied and pasted the output from the file as the header to this strategy.
arrays: DateArr[5000](0),TradeArr[5000](0),PriceArr[5000](0);//
DateArr[1] = 20001211;TradeArr[1] = -1;PriceArr[1] = 1368.50;// All these lines
DateArr[2] = 20001220;TradeArr[2] = 1;PriceArr[2] = 1245.75;// created by Perfect
DateArr[3] = 20010102;TradeArr[3] = -1;PriceArr[3] = 1265.50;// System Out Put
DateArr[4] = 20010105;TradeArr[4] = 1;PriceArr[4] = 1269.50;//
DateArr[5] = 20010131;TradeArr[5] = -1;PriceArr[5] = 1346.50;//
DateArr[6] = 20010201;TradeArr[6] = 1;PriceArr[6] = 1348.00;//
DateArr[7] = 20010205;TradeArr[7] = -1;PriceArr[7] = 1328.25;//
DateArr[8] = 20010228;TradeArr[8] = 1;PriceArr[8] = 1203.75;//
DateArr[9] = 20010307;TradeArr[9] = -1;PriceArr[9] = 1231.25;//
DateArr[10] = 20010322;TradeArr[10] = 1;PriceArr[10] = 1070.00;//
...
...
...
...
DateArr[539] = 20180920;TradeArr[539] = -1;PriceArr[539] = 2939.50;//
vars: cnt(1);
If date +19000000 = DateArr[cnt] then
Begin
print("Found Date : ",date);
If tradeArr[cnt] = 1 then
begin
buy this bar on close;
end;
If tradeArr[cnt] = 2 then
begin
sell this bar on close;
end;
If tradeArr[cnt] = -1 then
begin
sellShort this bar on close;
end;
If tradeArr[cnt] = -2 then
begin
buyToCover this bar on close;
end;
cnt = cnt + 1;
If DateArr[cnt] = DateArr[cnt-1] then
Begin
print("two trades same day ",d," ",dateArr[cnt]);
If tradeArr[cnt] = 1 then
begin
buy this bar on close ;
end;
If tradeArr[cnt] = 2 then
begin
sell this bar on close;
end;
If tradeArr[cnt] = -1 then
begin
print("looking to go short at ",PriceArr[cnt]);
sellShort this bar on close;
end;
If tradeArr[cnt] = -2 then
begin
buyToCover this bar on close;
end;
cnt = cnt + 1;
end;
end;
Almost Perfect System Interpreter Code
I think I discussed how this code works in a prior post. If I go back and see that I didn’t I will edit and update this post. So how did it do?
Here is a zip file that contains both ELDs for the strategies : PERFECTSYSTEM
Like this:
Like Loading...
Related
Discover more from George Pruitt
Subscribe to get the latest posts sent to your email.