Here is one method to implement an e-ratio calculation.
As we say in developerspeak, the following is a quick and dirty approach to calculating the e-ratio. But as far as I am concerned, it does the job! It can probably be programmed more elegantly in TradersStudio but I still have to climb some of that software learning curve…
I will show you how I calculated the e-ratio for a 17-day Donchian Channel breakout coupled with a 108-day moving average filter (the red curve on this chart). All the code referenced is provided at the end of this post.
As we saw in the last post about the e-ratio, you need to run the same signal over multiple fixed trade durations and record trade data. For this the TradersStudio optimisation and custom reports functionalities fit the bill.
Step 1: code up the system to test in TraderStudio
As you can not have simultaneous open Buy and Sell positions, there are actually 2 systems (long-only and short-only). The below will be based on the Buy-only system.
The entry signal is a breakout of the Donchian channel if the MA filter conditions are met (price > MA and MA rising).
In TradersStudio this translates to:
MA = ScriptAve(Close, MALength,0) If MA > MA And Close > MA Then Buy(EntryName,1,donchianHigh+MinMove ,Stop,Day) End If
The exit is simple: close the trade after n days (tradeLength):
If BarsSinceEntry=tradeLength And MarketPosition=1 Then ExitLong("ExitLong",EntryName,1,0,CloseExit,Day) End If
Step 2: retrieve trade data in custom report
As discussed in the e-ratio post we need to get MAE, MFE and ATR for each trade. For this we use a TradersStudio custom report. The report code is executed at the end of the system test and retrieve trade info with the following command:
GetInfoAboutTrades(Info, i + 1)
we are interested in the MAE and MFE which can be derived off the Maximum Profit and Maximum Loss of each trade:
SetCell(row, 13, 2, 10, Info) 'Max Pos Profit SetCell(row, 14, 2, 10, Info) 'Max Pos Loss
For the ATR value, we need to use a little trick as it is not directly available. We record it as part of the entry name which can be output to the report.
In the system code we have:
EntryName = "DonchianBreakoutBuy" + "|" + indLength + "|" + tradeLength + "|" + avgtruerange(indLength)
which concatenates the entry name with the ATR value as well as the length of the indicator (Donchian Channel) and trade duration – this will come in handy when we do the e-ratio calculation.
Step 3: run the system through the optimizer
The system is called DonchianChanBrkoutFiltBuy and has 3 parameters:
Sub DonchianChanBrkoutFiltBuy(indLength, tradeLength, MALength)
In the optimization process, step through all values of trade duration required (e.g from 1 to 100 in increments of 1) – warning TradersStudio will take a few hours to do this (sic!).
This will generate a custom trade report for all trades containing all the data required.
Step 4: paste data in Excel
Once you run the long-only and short-only systems, we are done with TradersStudio. We need to move the data over to Excel for analysis and the e-ratio computation.
Copy the custom trade report from the short-only system run to one sheet and from the long-only run to another sheet (watch out for the number of rows: if you have more than 65,536 and a version of Excel earlier than 2007 you will need to do that in several chunks).
Step 5: Run e-ratio macro
Copy the macro code from the text file, create a new VBA module in Excel and paste the code in it.
Go back to both sheets and run the macro. This should manipulate the data and create an aggregation pivot table on a separate sheet.
Step 6: Compute the e-ratio
On a brand new sheet, copy both pivot tables (I recommend a paste/special – values only) side by side so that each trade length values corespond with each other. Sum each Total value (column D and J) together for the same Buy and Sell parameters (in column N in example file). Divide each Sum of MFE by Sum of MAE: this gives you the e-ratio for the given trade length.
Please find below code files to support the example below (all txt files except the Excel workbook). This should be easily portable to any system (i.e. just change entry criteria in the system).
Donchian Channel Indicator
Custom trade Report
Excel e-ratio macro (text file)
Excel example workbook (does not contain underlying raw data as it is 60MB!)
Or if you prefer a zipped version of all files – here it is.
Any questions, clarifications, etc. please let me know how I can help.