25.10.2016 Views

Expert Advisor Programming by Andrew R. Young

Expert Advisor Programming by Andrew R. Young

Expert Advisor Programming by Andrew R. Young

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Tips and Tricks<br />

or 4 maximum consecutive losses. You can determine this <strong>by</strong> examining the maximum consecutive<br />

loss count under the Report tab in the Strategy Tester window.<br />

Another method is to increase your lot size <strong>by</strong> a smaller multiplier. The classic Martingale strategy<br />

doubles the lot size after each consecutive loss. You may wish to use a multiplier smaller than 2.<br />

There is also the anti-Martingale strategy, where you increase the lot size after each consecutive win.<br />

Let's examine a routine where we calculate the number of consecutive wins or losses, and increase<br />

the lot size accordingly. A Martingale strategy works best when you're placing one order at a time, so<br />

we will assume that every position consists of a single trade.<br />

The user will be able to choose between a Martingale (losses) or anti-Martingale (wins) strategy. A<br />

setting to limit the maximum number of consecutive lot increases will be included, and the lot<br />

multiplier will be adjustable.<br />

First, let's calculate the number of consecutive wins or losses. We will need to loop through the order<br />

history pool backward, starting from the most recently closed order. We will increment a counter for<br />

each win or loss. As long as a pattern of consecutive wins or losses is maintained, we will continue to<br />

loop. As soon as the pattern is broken (a win is located after one or more losses, or vice versa), the<br />

loop will exit.<br />

int WinCount;<br />

int LossCount;<br />

for(int Count = OrdersHistoryTotal()-1; ; Count--)<br />

{<br />

OrderSelect(Count,SELECT_BY_POS,MODE_HISTORY);<br />

if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)<br />

{<br />

if(OrderProfit() > 0 && LossCount == 0) WinCount++;<br />

else if(OrderProfit() < 0 && WinCount == 0) LossCount++;<br />

else break;<br />

}<br />

}<br />

We start <strong>by</strong> declaring the variables for our win and loss counters. In the for operator, notice that we<br />

use OrdersHistoryTotal() to establish our initial starting position. OrdersHistoryTotal() returns<br />

the number of orders in the history pool. We subtract 1 to determine the index position for the most<br />

recent order, which is stored in the Count variable.<br />

Notice that we have omitted the second expression in the for loop – the one that determines the<br />

condition to stop looping. The semicolon must remain for any omitted expressions. We will decrement<br />

the Count variable on each iteration of the loop.<br />

139

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!