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 />

double BuyStopLoss = OpenPrice - (StopLoss * UsePoint);<br />

If you are receiving an error message when attempting to place, close or modify an order, focus your<br />

efforts on the issue indicated <strong>by</strong> the error message. Here are a couple of the most common error<br />

messages cause <strong>by</strong> programming errors:<br />

• Error 129: Invalid Price – The opening price is invalid. For market orders, make sure the<br />

current Bid or Ask price is being passed, according to the order type. For pending orders,<br />

make sure the price is above or below the current price, as required <strong>by</strong> the order type. Also<br />

check to see that the pending order price is not too close to the current price (i.e. inside the<br />

stop level).<br />

• Error 130: Invalid Stops – Either the stop loss or take profit price is incorrect. Check to<br />

see that the stop loss and take profit prices are placed above or below the current price,<br />

depending on whether the order type is buy or sell. Also check to see that the stop loss or<br />

take profit price is not too close to the current price (i.e. inside the stop level).<br />

• Error 131: Invalid Trade Volume – The lot size is incorrect. Make sure that the lot size<br />

does not exceed the broker minimum or maximum, and that the lot size is normalized to the<br />

correct step value (0.1 or 0.01 on most brokers).<br />

Descriptions of all error messages can be found in the MQL Reference under Standard Constants –<br />

Error Codes. If you need additional assistance with an error that you are receiving, check the forums<br />

at MQL4.com.<br />

Troubleshooting Intermittent Trading Errors<br />

While most serious bugs can be found simply <strong>by</strong> back testing, others will occur only during real-time<br />

trading. Errors of logic can result in trades not being placed correctly, and these bugs can take some<br />

effort to locate. If there are trades being placed incorrectly during demo or live trading, we need as<br />

much information as necessary to troubleshoot the problem.<br />

We're going to add an optional feature to log trade and status information in real-time, so that we<br />

have a record of it when troubleshooting trades. We'll be using Print() statements as previously,<br />

but we'll be logging indicator values, prices – any information that will be helpful in debugging. We'll<br />

also add an external variable to turn logging on and off.<br />

// External variables<br />

extern bool Debug = true;<br />

143

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

Saved successfully!

Ooh no, something went wrong!