0% found this document useful (0 votes)
5 views

Programming and Testing7

for AE testing MT4

Uploaded by

Degus Tambahan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Programming and Testing7

for AE testing MT4

Uploaded by

Degus Tambahan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Programming and Testing

The NNFX Way

Welcome to Programming and Testing the NNFX way.


These instructions will guide you through setting up and using the Expert Advisor to find your top
indicators. Once found the EA can be used to find algorithms based on your best indicators.

INSTALLATION
To install the different files you need to be in the proper folder area.
Click on the MT4 File menu and select Open Data Folder.
You should see something like this.

Click on MQL4.
You should see something like this.

These folders are where you will copy the needed files after they are extracted.
Download the following files from the Group Units section.
If a file has zip2 as the extension rename it to have zip as the extension.
Watch for updates to individual indicators and include files and only use the most recent versions.

Indicators
NNFX_Indicators.zip2 contains over 60 custom indicators used by the Expert Advisor.
Extract the files and copy them to the Indicator folder.
It is recommended to use these versions even if you already have them. Minor changes were
made so they are compatible with the expert.

Expert Advisor
The expert advisor is AlgoEA_V55.
Copy the file to the Experts folder.

Include Files
Include files are a way to make changes to programs easier.
Different Experts can use the same include files for common functions like Algo_V1.mqh.

NNFX Include.zip2

The following contain the code support file

Algo_V1.mqh General support code and common functions


AlgoIndCandidates_V3.mqh Code for Candidate indicators
AlgoInd2LC_V4.mqh Code for 2 line cross indicators
AlgoIndZLC_V4.mqh Code for zero line cross indicators
AlgoIndBaseLine_V3.mqh Code for baseline indicators
AlgoVol_V4.mqh Code for Volatility indicators

The following contains all the input variables for the custom indicators.

AlgoInputsBaseLine_V4.mqh baseline indicator inputs


AlgoInputsPrimary2LC_V4.mqh 2 line cross indicator inputs
AlgoInputsPrimaryZLC_V4.mqh zero line cross indicator inputs
AlgoInputsPrimary_V4.mqh additional candidate indicator inputs
AlgoInputsVol_V4.mqh volume/volatility indicator inputs

Extract the files and copy to the Include folder.

If MT4 is running close it.

Restart MT4 and the copied files will compile automatically.


TESTING
These instructions will guide you on how to use the MT4 Strategy Tester with the AlgoEA. The expert is a
test bed for finding the best settings for over 60 indicators and has inputs to also determine the best
combinations for algos.

The expert uses menus to select the indicator to use.

Most of the rules for NNFX are coded in the expert including:

1. Use of Average Daily Range (ADR) in calculations.


2. Stoploss and Take Profit values are inputs as percentage with defaults set to 100 and 150.
3. Money Management uses NNFX rules with percentage input default of 1 or fixed lots.
4. The trailing stops use percentage of ADR.
5. Menu selection of Baseline for entry, confirmation or exit and usage of FU candle.
6. Menu selection of 2 line cross indicator for entry, confirmation or exit.
7. Menu selection of Zero line cross indicator for entry, confirmation or exit.
8. Menu selection of other candidates for entry, confirmation or exit.
9. Menu selection of Volatility/Volume indicator.

Once you have the best settings for all indicators, there are inputs to determine the best combinations
for each menu. The results can be copied to Excel to determine the best settings that work on all pairs.

Let’s get started!

Start MT 4 as usual.

Open the Strategy Tester by clicking on the View menu item and select Strategy Tester.
When the tester opens you can make the following selections.

1. Use the V to select the pair you want to test. I suggest using one of the major pairs.
2. Use the V to select the model to use. Every tick is best however Open Prices is much faster and
can be used to narrow the ranges to test.
3. Select the date range you want to test and make sure the Use date box is checked.

Visual mode will open a chart to show Expert progress and is not used here.

1. Use V to select the Expert Advisor to test. In this case AlgoEA_V5. The newest is now version 55.
2. Set the period to Daily or whatever timeframe you want to test.
3. Determine the usual spread for the pair and enter here, especially when the market is closed or
around end of day or use Current Spread when the market is open.
4. Click this button to open the Expert properties window.
5. Click open chart to see the trades placed by the expert when Optimization is not checked.

Make sure Optimization is checked.

When properties are set click Start. Leave enough space below the buttons to see the progress bar and
time remaining. That is done by floating the mouse over the top edge of the tester until the mouse
pointer changes then click and drag up.
Expert Properties
When the Expert Properties window opens you will see a list of inputs for the expert advisor.

The Value column holds the default value found in the code and is the value used when Run is selected.

When optimizing a value Start is used for the first value, Step is used for subsequent values and Stop is
the final value to test. A Start of 5 with Step 1 and Stop 10 will test 5, 6, 7, 8, 9, and 10 for that input.

There is code to send an Email when EmailAlert is true. Code will be added later to send a text message.

Open Second Trade follows NNFX rules and until optimized settings are determined for the indicators
this should be left as false.

ATR_PeriodValue defaults to 14 and other values can be tested.

The next inputs are for the different indicator menus.

The first is Baseline with 32 entries with 0 used to not use baseline. NOTE : Image 2 shows an error with
2 items numbered 14.
UseSize is for testing FU candles based of BaseATRPercent.
Entry_BaseLine is where you will start optimizing each baseline indicator’s inputs.
Later Confirm_BaseLine and ExitBaseline will be used to build algos. A Zero (0) entry means do not use.
I suggest making a list of the indicators with the number of inputs to optimize and checking off the ones
completed. Scroll down to see the inputs for all indicators.
This list will also be used when determining which indicators to remove from further testing.
Enter the number of the indicator to test in the value column.
Scroll down to find the input for that indicator. The inputs are arranged by menu item and alphabetical.
Set the Start, Step and Stop values for each input.
With optimization checked, click Start button. See examples below for more detail.
All other menu inputs below are left at 0 as are items other than the indicator to test.
The 16 Two Line Cross, 10 Zero Line Cross and 10 Candidate indicators are tested the same way as
BaseLine by entering the value number to test and setting the test values of Start, Step and Stop for the
selected indicator. NOTE : the Confirm_ZLC box should not be checked while testing individual
indicators.
Volatility is tested the same as above.
The money management uses all default vales as defines for NNFX so no need to change these. They can
be tested the same as indicators using the Start, Step and Stop values.
Trailing Stop is part of trade management and can help turn a loss to a small win or at least a breakeven
trade.

The types are:

1. Immediately – each time price moves in your favor the stoploss is also moved.
2. Delayed - standard trail where stoploss is moved after the trailing value is reached.
3. Breakeven - moves the stop to the LockInPips amount when the value entered is reached
and does not move after that.
4. Two level trail – the first level can be like breakeven and the second level like the standard
trail.
5. Trail by x pips the opposite side of a moving average.

All inputs after Trailing Stop are the inputs for indicators and are clearly marked.
TESTING EXAMPLES

When you start testing you should first make a list of all indicators by type.
Select the expert and open the properties window.

Scroll down to the indicator input area and add the number of inputs to your name list.

For example EMA has 1 input so your list would have EMA 1.
I would suggest only using the Close price when that is an option, at least for now.
To speed the process of learning start with those indicators having 1 input.
They usually complete in a few minutes.
Once you have completed your list you can start testing.

Click Reset before testing each indicator. All inputs are set back to default and unchecked.
It is important to do this step to prevent the wrong values being tested.

Make sure Genetic algorithm is not checked.

Click on the Inputs tab.

Scroll down to the menu area that has the indicator you want to test
Let’s start with EMA, one of the dirty dozen.
This will show you why it is not such a good indicator even though it is said to be so great when using
EMA 50 or EMA 100.
This will be removed from the list soon, as will the other indicators you find that do not work well on
your chosen pair.

EMA is number 31 so enter that number in the Value column for Entry_BaseLine. Pay attention to the
numbers as they might have changed from the picture.

To test the other Baseline indicators simply enter the number from the menu.

If you want to test the FU candle click the box in front of UseSize. That will cause the tester to use false
and true so you get 2 values returned for each setting.

DO NOT CHECK THE BOX in front of the Entry_BaseLine input. That is for later use.
Next scroll down to the input values for EMA.

Click the box in front of the

For an input be optimized make sure to click the box in front of the input name.

For EMA click the box in front of EMABasePeriod as shown above by the check mark.

In the start column, double click in the input area and enter 4. A single click does not work. Double click
and enter 1 in the Step column. Double click and enter 100 in the Stop column. You will usually only go
to 40 or 50 for this value in other indicators.

This causes the tester to use every value from the start value in the step size entered through the Stop
value, in this case equivalent to 97 tests.

When ready click OK.


You should now see this.

This shows the test will make 97 variations of the inputs as shown on the left and take about 30 minutes
to complete as shown over the Stop button.

View the progress of the tester by clicking on Optimization Results.

To see negative results Right Click in the results area before testing and uncheck Skip Useless Results in
the Popup Menu. You only need to do this once.
When the test completes the results will be in order by highest profit first.

It will also show the best values for the different inputs, in this case EMABasePeriod.

In this example EMA 5 had the highest profit while EMA4 had less drawdown. These are things to
consider when deciding the value to use for replacing the default.

You may also want to consider number of trades, profit factor and expected profit when deciding on
what value to use as the new default.

For this test of T3(2) there are 2 possibilities.

The highest profit was with a period of 12 and b value of 1.3. Drawdown was 4.9% with 70.24% winrate
on 84 trades.
A possible better setting is actually third on the list.
Less profit by about $160 but also 7 placed less trades and had lower drawdown at 3.93. Winrate was
about the same at 70.13%
.EDITING
Editing the Input files.

To open the Editor Right click on the EA name in the Navigator and the following window should open.
The version number might change so always use the most recent.
Click on the plus box for Include to expand the list and double click on the AlgoInputs file you want to
modify with the new default value, in this case AlgoInputsBaseLine_V4.

Double Click on the 16. It should show with a blue background.


Change the value to 4.

Notice the * after the file name. That means the file has not been saved yet. To save it click on the single
disk icon in the icon menu bar.

Click in the Expert Advisor area to make it active and click Compile on the icon menu bar.

There should be 4 warnings. These are not important at this time.

Go back to the main strategy tester and open the properties again. Click Reset and Scroll down to the
EMABasePeriod input area and see the new default value.
Reducing the Indicator Lists
When you have finished optimizing all of the possible entry indicators you can begin reducing the list.
Each person will have different results depending on broker and length of test period.
This does not involve any real programming, just removing lines of code and renumbering the menus
and functions that access the indicators.

In this example I will remove the duplicate indicators and their inputs.
You will remove any other indicators you do not want to use from each category and save those files in
the same way.

As another filter an indicator with good results over 3 years or more might have failed this year so
should be removed.

Change the date range on the tester to be from Jan 1, 2020 to present.
Run the tester and click the box in front of the Entry_BaseLine input.
Use Start 1, Step 1 and Stop as highest number of baseline.
Check optimizer results and once again note the bad ones for removal and add them to your list.

Open AlgoEA_V55 and the include files AlgoInputsBaseLine_V4 and AlgoIndBaseLine_V3 in the Editor.
Edit the Expert Code

Start with the Expert code.

Click in front of the line 68 for menu item shown in blue. Then press backspace.

Continue doing the same for the menu items by clicking in front of the line to highlight in blue.

2. Same as DEMA and the first one done

6. Same as DEMA

9. Same as FAMA MrPip

17. Same as HMA Trend

20. Same as T3

22. Same as TEMA


Your list should now look like this and a few of these indicators you find as poor performers will
be deleted as well.

Save As AlgoEA_V56 as shown. DO NOT SKIP THIS STEP.

That way your old version is still available.


Edit the Input Code

Highlight the lines shown and press backspace or delete one line at a time.
Continue deleting the now unneeded input lines for each dropped indicator in the list.
When complete Save As AlgoInputsBaseLine_V5 as shown to keep your old file.

Now do the same deletion process in the AlgoIndBaseLine_V3 and Save As AlgoIndBaseLine_V5 to keep
the old version.
Fixing the Numbers

When deletions are complete double click on the number to change so it is highlighted in blue as shown
in the above picture. Enter the new value. Continue so the numbers are in sequence 1 to number of
indicators left in your list.
The new file will look like this.
With everything properly modified and saved for BaseLine include files make the final modifications to
the expert advisor.

Scroll down the code to the include files for inputs. They are currently V4.

Change the 4s to 5s as show. Do not update AlgoInputsVol_V4 yet.


Scroll to bottom of code.

Modify the version number for AlgoIndBaseLine to V5 as shown.

Repeat the same steps for each category and use version 5 for all include files except Vol related.
When all are properly saved select the EA window and Click the Compile button.

Hopefully you will have no errors. If you do then try to figure out where you changed the numbers if
they are correct, especially the sequential part. Also notice if the error involves undeclared variables.
That means you missed deleting the lines in the code file for the indicator that uses those inputs.
Creating Algorithms
Now that all the indicators are optimized it is time to show the real power of the EA.

It can be used to test every possible combination of Entry and Confirmation indicators while you attend
to other things.

At the start of testing you had 31 Baseline, 16 Two Line cross, 10 Zero Line cross, and 11 Candidates.

That will results in 31*16*10*11*4 = 218,240 passes to get all possible algos. The final 4 is from using
each category as entry and confirmation.

You now have less in each category and if around half results in 16*8*5*6*4 = 15,360 passes being
needed to get all possible algos using your list and settings.

This is a multistep process.

Let’s start with Baseline as entry since that is one of the NNFX rules. The pattern is Entry_BaseLine,
Confirm_2LC, and Confirm_ZLC. Click the box for Entry_BaseLine and set the values for Start, Step and
Stop based on how many baseline indicators you have left.

Click the box for Confirm_2LC and set the Start, Step and Stop values based on your list.

Click the box for Confirm_ZLC and set the Start, Step and Stop values based on your list.
Click Optimize, enter spread to use and date range of your longest test or at least 4 years.

Then click Start.

When complete you will get results in the Optimization Results Tab in descending order by profit as
shown.

Right click and select Copy All.

I then Paste the results to Excel. I set up a spreadsheet with 4 Tabs as shown.

Scroll down to values around line 192. I use $1000 as cutoff for profit. All rows below that are deleted
leaving 192 possible algos to use for BaseLine entry
I then do the same by using Sort on the Drawdown column and deleting all those with drawdown over 5
as shown. That leaves 78 algos for Baseline Entry.

Now repeat the process using the following patterns:

Entry_2LC, Confirm_BaseLine, Confirm_ZLC


Entry_ZLC ,Confirm_BaseLine, Confirm_2LC

You can also test Entry_Candidate and Confirm_Candidate in the same way

I use
Entry_Candidate, Confirm_BaseLine, Confirm2LC
Entry_Candidate, Confirm_BaseLIne, ConfirmZLC
And consider adding
Entry_BaseLIne, Confirm_2LC, Confirm_Candidate
Entry_BaseLIne, Confirm_ZLC, Confirm_Candidate
Entry_2LC, Confirm_BaseLine, Confirm_Candidate
Entry_ZLC, Confirm_BaseLine, Confirm_Candidate

There are a total of 9 combinations. Once algos are developed the indicators will be tested for Exit in the
same way. The resulting indicator numbers used in each algo will be entered to a switch statement for
use in testing other pairs.
The Algo Switch

When you have the spreadsheet of potential algos completed those numbers will be entered into a
switch statement that is part of a function that sets the values for the algo to test. This will make testing
other pairs much faster because only successful algos are used. An example will be posted to save typing
with only the change of numbers required. Copy and Paste can be used to easily expand the supplied
function.

Open your spreadsheet with the results of your algo tests and click on the BaseLine tab.

Click on the 1 in front of the first row to highlight it.


Click Insert.

Add Labels on the first row.

To make the results easier to read select the cell for Entry_BaseLine= and highlight that portion as
shown. Make sure to include the = sign at the end. Right click on the highlighted area and select Copy.
Select Replace from the Menu and use Control V to paste the value in the Find what entry. Leave the
Replace with entry blank.

Click Replace All.


The following window should appear showing completion of the replacement..
Repeat the above steps with Confirm_2LC=

Repeat again for Confirm_ZLC=

You can also delete the DD Column as we only want DD %.


Select Custom Sort from the Sort and Filter Menu.

Replace the Profit in the Sort By with Entry_BaseLine with Smallest to Largest and Click Add Level twice.

Add the two columns as shown with Order Smallest to Largets and click OK.
You will now have something like this.

Open the MetaEditor and then open AlgoSets_V1.mqh. Resize the window to overlap the spreadsheet
as shown for easier reading and editing.

Modify the entries as shown for each case. Be very careful and double check your changes as you go.
The values in the first row in the spreadsheet are used to modify the first case. The values in the second
row are used for the second case. Make the modifications for all of you baseline entry indicators first.
Pay close attention to the variable names. If you have less baseline indicators delete the lines until you
reach the case number 79 with an Entry_2LC variable set. If you have more you can copy enough of the
baseline entry cases and change the indicator values where needed. You will renumber at the end.
Make sure to save regularly.

Repeat the same process for Entry_2LC, Entry_ZLC and Entry_Candidate.

If you want to use any of the optional algo patterns you will need to copy and paste one of the case
statements, modify the variable name, and copy enough for that pattern set. Case statement 1 is from
line 11 to 14. The break; ends the case statement.

Again be very careful to double or even triple check your numbers.

When finished change the case numbers to be sequencial starting from 1 to the number of algos you
have. Note the number of the final case and modify line 9 to use that number as part of the variable
name. In this case the 217 became 219. This later became 497.

Save the file.


Open your most recent Expert Advisor version.

Add the 2 new lines shown here as lines 57 and 58. In my case I have algo497. This is a reminder for the
Stop value in Start, Step and Stop.

Scroll down to the init function and add the line as shown in line 267 if (UseAlgos) GetAlgos();.
Scroll to the bottom the the expert file and add the final line #include <AlgoSets_V1.mqh as shown.

Save the expert using Save As and give it a new version number.

Now test the new version in the startegy tester by setting UseAlgos to true. Start = 1, Step = 1 and Stop
is your number of algos. If any of the results look wrong double check the numbers for each variable for
that algo in the spread sheet.

Before using the expert on the other pairs there is one more step, the surprise that will reduce
drawdown and increase win rate, at least for some of the algos.
Trade Management

What are the most important elements for successful trading?


In the NNFX way of trading we know:
1. Average Daily Range used in calculations
2. Having a good entry.
3. Money Management
4. Trade Psychology
5. The final piece

Items 1 and 2 have been developed using the expert up to this point.
Item 3 is built in to the expert and item 4 is not a concern when automated.
That leaves item 5.
This was asked in the NNFX group and some found the answer while others might have no clue to the
concept.
To me it is Trade Management in the form of trailing stop.
So far you either hit stoploss or takeprofit.
With the NNFX way the stoploss is not moved until the first take profit is reached. If a trade moves to
90% of take profit before reversing to hit stoploss then that is bad trade management.
If ADR is over 100 pips I will have already locked breakeven as a worse case. A different trail might lock
in 50 pips or more.
There are 5 trailing stop methods included in the EA. I suggest starting by testing method 2 from start =
15, step = 5 to stop = 30 for each algo you have. Make sure to use Evry Tick mode for better accuracy.
You might be very surprised. Here are my results for the 497 algos I am testing.
Increase1 is the increase in profit by using a trail of 15% and Increase2 is trail by 20%.
Note the decrease in draw down, DD1 vs DD, and increased win rate with WinRate1 vs WinRate
.
Testing Exit Indicators

If you want to use the exit rule for when your entry indicator reverses signal befor stoploss or takeprofit
is reached add the following code lines to your expert advisor file.

First add line 56. This is the variable to test from false to true.

Then change the GetExit function as shown.

I copied the original code and pasted above it before modifying variable names and adding the if else
structure as shown. First check for an exit indicator in use. If not assign the entry indicator as the exit
indicator if the exit rule is used.

No need for a new version so just save and compile.

You might also like