# 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[1] = 3.14

array[2] = 42

array[3] = 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.

1. trdEntryPrice (0) – column zero – yes we can have a 0 col. and row
2. trdEntryDate(1)
3. trdExitPrice (2)
4. trdExitDate(3)
5. trdID(4)
6. trdPos(5)
7. trdProfit(6)
8. trdCumuProfit(7)

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.

``arrays: tradeStruct[10000,7](0);vars: trdEntryPrice (0),      trdEntryDate(1),       trdExitPrice (2),      trdExitDate(3),      trdID(4),      trdPos(5),      trdProfit(6),      trdCumuProfit(7);``
2D array and its 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.

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.

``stb = highest(h,20);sts = lowest(l,20);stb1 = highest(h[1],20);sts1 = lowest(l[1],20);buy("Sys-L") 1 contract next bar at stb stop;sellShort("Sys-S") 1 contract next bar at sts stop;mp = marketPosition*currentContracts;totTrds = totalTrades;if mPos <> 1 thenbegin	if h >= stb1 then	begin		if mPos < 0 then // close existing short position		begin			mEntryPrice = tradeStruct[numTrades,trdEntryPrice];			mExitPrice = maxList(o,stb1);			tradeStruct[numTrades,trdExitPrice] = mExitPrice;			tradeStruct[numTrades,trdExitDate] = date;			mProfit = (mEntryPrice - mExitPrice) * bigPointValue - mCommSlipp;			cumuProfit += mProfit;			tradeStruct[numTrades,trdCumuProfit] = cumuProfit;			tradeStruct[numTrades,trdProfit] = mProfit;			print(d+19000000:8:0," shrtExit ",mEntryPrice:4:5," ",mExitPrice:4:5," ",mProfit:6:0," ",cumuProfit:7:0);		print("-------------------------------------------------------------------------");			end;			numTrades +=1;		mEntryPrice = maxList(o,stb1);		tradeStruct[numTrades,trdID] = 1;		tradeStruct[numTrades,trdPOS] = 1;		tradeStruct[numTrades,trdEntryPrice] = mEntryPrice;		tradeStruct[numTrades,trdEntryDate] = date;		mPos = 1;		print(d+19000000:8:0," longEntry ",mEntryPrice:4:5);	end;end;if mPos <>-1 thenbegin	if l <= sts1 then	begin		if mPos > 0 then // close existing long position		begin			mEntryPrice = tradeStruct[numTrades,trdEntryPrice];			mExitPrice = minList(o,sts1);			tradeStruct[numTrades,trdExitPrice] = mExitPrice;			tradeStruct[numTrades,trdExitDate] = date;			mProfit = (mExitPrice - mEntryPrice ) * bigPointValue - mCommSlipp;			cumuProfit += mProfit;			tradeStruct[numTrades,trdCumuProfit] = cumuProfit;			tradeStruct[numTrades,trdProfit] = mProfit;			print(d+19000000:8:0," longExit ",mEntryPrice:4:5," ",mExitPrice:4:5," ",mProfit:6:0," ",cumuProfit:7:0);			print("---------------------------------------------------------------------");		end;			numTrades +=1;		mEntryPrice =minList(o,sts1);		tradeStruct[numTrades,trdID] = 2;		tradeStruct[numTrades,trdPOS] =-1;		tradeStruct[numTrades,trdEntryPrice] = mEntryPrice;		tradeStruct[numTrades,trdEntryDate] = date;		mPos = -1;		print(d+19000000:8:0," ShortEntry ",mEntryPrice:4:5);	end;end;``
Shadow System - Generic forany SAR System

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:

``if mPos <> 1 thenbegin	if h >= stb1 then	begin		if mPos < 0 then // close existing short position		begin			mEntryPrice = tradeStruct[numTrades,trdEntryPrice];			mExitPrice = maxList(o,stb1);			tradeStruct[numTrades,trdExitPrice] = mExitPrice;			tradeStruct[numTrades,trdExitDate] = date;			mProfit = (mEntryPrice - mExitPrice) * bigPointValue - mCommSlipp;			cumuProfit += mProfit;			tradeStruct[numTrades,trdCumuProfit] = cumuProfit;			tradeStruct[numTrades,trdProfit] = mProfit;			print(d+19000000:8:0," shrtExit ",mEntryPrice:4:5," ",mExitPrice:4:5," ",mProfit:6:0," ",cumuProfit:7:0);		print("-------------------------------------------------------------------------");			end;	``

### 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 arraymEntryPrice = 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.

``		numTrades +=1;		mEntryPrice = maxList(o,stb1);		tradeStruct[numTrades,trdID] = 1;		tradeStruct[numTrades,trdPOS] = 1;		tradeStruct[numTrades,trdEntryPrice] = mEntryPrice;		tradeStruct[numTrades,trdEntryDate] = date;		mPos = 1;		print(d+19000000:8:0," longEntry ",mEntryPrice:4:5);``

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.

``if lastBarOnChart thenbegin	print("Trade History");	for arrIndx = 1 to numTrades	begin		value20 = tradeStruct[arrIndx,trdEntryDate];		value21 = tradeStruct[arrIndx,trdEntryPrice];		value22 = tradeStruct[arrIndx,trdExitDate];		value23 = tradeStruct[arrIndx,trdExitPrice];		value24 = tradeStruct[arrIndx,trdID];		value25 = tradeStruct[arrIndx,trdProfit];		value26 = tradeStruct[arrIndx,trdCumuProfit];				print("---------------------------------------------------------------------");		if value24 = 1 then		begin			string1 = buyStr;			string2 = sellStr;		end;		if value24 = 2 then		begin			string1 = shortStr;			string2 = coverStr;		end;			print(value20+19000000:8:0,string1,value21:4:5," ",value22+19000000:8:0,string2,			  value23:4:5," ",value25:6:0," ",value26:7:0);	end;end;``

#### 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…..