# The Perfect System

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 PardoThe 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.

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 }
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.

``````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];
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];
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];
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];
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];
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];
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];
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);
begin
end;
begin
sell this bar on close;
end;
begin
sellShort this bar on close;
end;
begin
end;
cnt = cnt + 1;
If DateArr[cnt] = DateArr[cnt-1] then
Begin
print("two trades same day ",d," ",dateArr[cnt]);
begin
buy this bar on close ;
end;
begin
sell this bar on close;
end;
begin
print("looking to go short at ",PriceArr[cnt]);
sellShort this bar on close;
end;