Storing Trades for Later Use in a 2D Array
Since this is part 1 we are just going to go over a very simple system: SAR (stop and reverse) at highest/lowest high/low for past 20 days.
A 2D Array in EasyLanguage is Immutable
Meaning that once you create an array all of the data types must be the same. In a Python list you can have integers, strings, objects whatever. In C and its derivatives you also have a a data structure (a thing that stores related data) know as a Structure or Struct. We can mimic a structure in EL by using a 2 dimensional array. An array is just a list of values that can be referenced by an index.
array = 3.14
array = 42
array = 2.71828
A 2 day array is similar but it looks like a table
array[1,1], array[1,2], array[1,3]
array[2,1], array[2,2], array[2,3]
The first number in the pair is the row and the second is the column. So a 2D array can be very large table with many rows and columns. The column can also be referred to as a field in the table. To help use a table you can actually give your fields names. Here is a table structure that I created to store trade information.
- trdEntryPrice (0) – column zero – yes we can have a 0 col. and row
- trdExitPrice (2)
So when I refer to tradeStruct[0, trdEntryPrice] I am referring to the first column in the first row.
This how you define a 2D array and its associate fields.
In EasyLanguage You are Poised at the Close of a Yesterday’s Bar
This paradigm allows you to sneak a peek at tomorrow’s open tick but that is it. You can’t really cheat, but it also limits your creativity and makes things more difficult to program when all you want is an accurate backtest. I will go into detail, if I haven’t already in an earlier post, the difference of sitting on Yesterday’s close verus sitting on Today’s close with retroactive trading powers. Since we are only storing trade information when can use hindsight to gather the information we need.
Buy tomorrow at highest(h,20) stop;
SellShort tomorrow at lowest(l,20) stop;
These are the order directives that we will be using to execute our strategy. We can also run a Shadow System, with the benefit of hindsight, to see where we entered long/short and at what prices. I call it a Shadow because its all the trades reflected back one bar. All we need to do is offset the highest and lowest calculations by 1 and compare the values to today’s highs and lows to determine trade entry. We must also test the open if a gap occurred and we would have been filled at the open. Now this code gets a bit hairy, but stick with it.
Notice I have stb and stb1. The only difference between the two calculations is one is displaced a day. I use the stb and sts in the EL trade directives. I use stb1 and sts1 in the Shadow System code. I guarantee this snippet of code is in every backtesting platform out there.
All the variables that start with the letter m, such as mEntryPrice, mExitPrice deal with the Shadow System. Theyare not derived from TradeStation’s back testing engine only our logic. Lets look at the first part of just one side of the Shadow System:
mPos and mEntryPrice and mExitPrice belong to the Shadow System
if mPos <> 1 then the Shadow Systems [SS] is not long. So we test today’s high against stb1 and if its greater then we know a long position was put on. But what if mPos = -1 [short], then we need to calculate the exit and the trade profit and the cumulative trade profit. If mPos = -1 then we know a short position is on and we can access its particulars from the tradeStruct 2D array. mEntryPrice = tradeStruct[numTrades,trdEntryPrice]. We can gather the other necessary information from the tradeStruct [remember this is just a table with fields spelled out for us.] Once we get the information we need we then need to stuff our calculations back into the Structure or table so we can regurgitate later. We stuff date in to the following fields trdExitPrice, trdExitDate, trdProfit and trdCumuProfit in the table.
Formatted Print: mEntryPrice:4:5
Notice in the code how I follow the print out of variables with :8:0 or :4:5? I am telling TradeStation to use either 0 or 5 decimal places. The date doesn’t need decimals but prices do. So I format that so that they will line up really pretty like.
Now that I take care of liquidating an existing position all I need to do is increment the number of trades and stuff the new trade information into the Structure.
The same goes for the short entry and long exit side of things. Just review the code. I print out the trades as we go along through the history of crude. All the while stuffing the table.
If LastBarOnChart -> Regurgitate
On the last bar of the chart we know exactly how many trades have been executed because we were keeping track of them in the Shadow System. So it is very easy to loop from 0 to numTrades.
Add 19000000 to Dates for easy Translation
Since all trade information is stored in the Structure or Table then pulling the information out using our Field Descriptors is very easy. Notice I used EL built-in valueXX to store table information. I did this to make the print statements a lot shorter. I could have just used tradeStruct[arrIndx, trdEntry] or whatever was needed to provide the right information, but the lines would be hard to read. To translate EL date to a normal looking data just add 19,000,000 [without commas].
If you format your PrintLog to a monospaced font your out put should look like this.
Why Would We Want to Save Trade Information?
The answer to this question will be answered in Part 2. Email me with any other questions…..