- These calculations are done on every bar on the chart. We are now filling the
array TradeProfit with the result of each trade. An array is a series of values
stored in a specific order. In this case, the first position in the array will hold
the result of the first trade, and so on. The second array, TradeLength, is
filled with data regarding the length of the trade. Each array can store 1,000
trades, as specified under step two. - All remaining calculations will only be executed on the very last bar on the
chart. Note: If you plan to use this code and use the criteria Open Next Bar
(or Open Tomorrow) for your entries and exits, you need to use the alterna-
tive function. The first thing we need to do is to run a loop (that starts with
the word “For”) to calculate the total profit (variable SumProfit) by adding
all the values contained in the array TradeProfit. Take note of all new equity
highs (variable MaxProfit): From these, we can calculate the maximum
drawdown (variables DrawDown, MaxDrawDown, DDRatio, and
MaxDDRatio). We also count the number of winning trades (variable
WinningTrades) and sum up the length of all trades in the variable
SumTrLen, using the data from the array TradeLength. - Moving out of the first loop, we now can calculate the variables
PercentInTrade, AverageProfit, AverageTrLen, and PercentWinners, which
all should be self-explanatory. - The second loop (also starting with the word “For”) sums up the squared
profits from all trades in the SumSquareProfit variable. - Moving out of the second loop, we can go on and calculate the standard devi-
ation of all trades (variable StDevProfit), and the ratio between the average
trade and the standard deviation of all trades (variable RiskRatio). The vari-
able RiskRatio measures the risk of the system by comparing the average
trade with the dispersion of all trades (the standard deviation). The higher the
value of the average trade and the less dispersed all the individual trades, the
more sure you can be regarding the outcome of each individual trade, and,
consequently, the less risky the system. - The calculation for the variable LosingTrades should be self-explanatory.
The variable ProfitFactor stores the relation between every dollar earned and
every dollar lost, calculated as the gross profit, divided by the gross loss.
Obviously, if the gross profit from all winning trades is larger than the gross
loss from all losing trades, the variable ProfitFactor will be greater than 1,
which is a must for a profitable system. The variable RiskFactor calculates
the relationship between the final (net) profit and every dollar risked, which
is not the same as every dollar lost. To calculate the dollars risked, the sys-
tem must have a stop-loss level. To calculate the dollars risked for all trades,
first divide the gross loss by the number of losing trades, to get a value for
the average losing trade. Second, assuming the dollars lost in the average los-
ing trade also represent the value we were willing to risk and lose in each and
90 PART 2 Trading System Development