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.

EXPERT ADVISOR PROGRAMMING<br />

We use MODE_HISTORY as the third argument in the OrderSelect() function to indicate that we are<br />

looping through the closed order history pool. By default, OrderSelect() uses the open order pool,<br />

so we must specify MODE_HISTORY when checking the closed order pool.<br />

We check to make sure that the currently selected order matches our chart symbol and our magic<br />

number. Then, we examine the order profit using the OrderProfit() function. If the return value<br />

indicates a profit (i.e. is greater than zero), then we increment the WinCount variable. If it's a loss,<br />

we increment LossCount.<br />

Since we are looking for consecutive wins or losses, we need to terminate the loop once an<br />

alternating condition is found. To do this, we check the WinCount or LossCount variable when<br />

checking the order profit. For example, if we have 2 consecutive losses – meaning that LossCount =<br />

2 – and our next order is a win, then both of our if statements will be false, and control will pass to<br />

the break operator, which ends the loop.<br />

The advantage of this method is that it's robust, and will not fail if the expert advisor is accidentally<br />

shut down. The EA will pick up right where it left off. Of course, this means that when you first start<br />

the EA, it will use any previous win/loss streak when determining the lot size. But as you can see, the<br />

advantages outweigh the disadvantages.<br />

Either the WinCount or the LossCount variable will contain the number of consecutive wins or losses.<br />

If we want to do a Martingale strategy, we use LossCount to determine the factor <strong>by</strong> which to<br />

increase the lot size. If we're doing an anti-Martingale, we use WinCount instead.<br />

We'll use an external integer variable called MartingaleType to determine this. If MartingaleType<br />

is set to 0, we'll use the Martingale strategy. If it's set to 1, we'll use the anti-Martingale strategy. We<br />

will also declare external variables for our multiplier (LotMultiplier), the maximum number of<br />

times to increase the lot size (MaxMartingale), and our starting lot size (BaseLotSize).<br />

// External variables<br />

extern int MartingaleType = 0;<br />

extern int LotMultiplier = 2;<br />

extern int MaxMartingale = 4;<br />

extern double BaseLotSize = 0.1;<br />

// 0: Martingale, 1: Anti-Martingale<br />

// Lot size calculation<br />

if(MartingaleType == 0) int ConsecutiveCount = LossCount;<br />

else if(MartingaleType = 1) ConsecutiveCount = WinCount;<br />

if(ConsecutiveCount > MaxMartingale) ConsecutiveCount = MaxMartingale;<br />

double LotSize = BaseLotSize * MathPow(LotMultiplier,ConsecutiveCount);<br />

140

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

Saved successfully!

Ooh no, something went wrong!