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.
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:
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:
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:
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 = 20001211;TradeArr = -1;PriceArr = 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:
Here’s the output that was created when this strategy as was added to a 19 year long ES daily bar chart:
Here is the interpreter strategy code. I simply copied and pasted the output from the file as the header to this strategy.
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