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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Tips and Tricks<br />

// Global variables<br />

string GlobalVariablePrefix;<br />

int init()<br />

{<br />

GlobalVariablePrefix = Symbol()+Period()+"_"+"ProfitBuster"+"_"+MagicNumber+"_";<br />

}<br />

We use the current symbol and period, along with an identifier for the EA and the MagicNumber<br />

external variable. Now, when we set a global variable using GlobalVariableSet(), we use the<br />

prefix that we defined above, along with the actual variable name:<br />

GlobalVariableSet(GlobalVariablePrefix+Counter,Counter);<br />

So if we're trading on EURUSD on the M15 timeframe with an EA named "ProfitBuster", using 11 as<br />

our magic number and Counter as our variable name, the name of our global variable will be<br />

EURUSD15_ProfitBuster_11_Counter. You can use any convention you wish for naming your global<br />

variables, but including the above information is strongly recommended.<br />

To retrieve the value of a global variable, use the function GlobalVariableGet() with the variable<br />

name as the argument:<br />

Counter = GlobalVariableGet(GlobalVariablePrefix+Counter);<br />

To delete a global variable, use the function GlobalVariableDel() with the variable name as the<br />

argument. To delete all global variables placed <strong>by</strong> your EA, use the function<br />

GlobalVariableDeleteAll() with your prefix as the argument.<br />

GlobalVariableDel(GlobalVariablePrefix+Counter);<br />

GlobalVariableDeleteAll(GlobalVariablePrefix);<br />

For more information on global variable functions, see the Global variables topic in the MQL<br />

Reference.<br />

Check Order Profit<br />

Sometimes it may be useful to check the current profit on an order, or to check the total profit on an<br />

order that has already closed. There are two ways to check profit. To get the profit in the deposit<br />

currency, use the OrderProfit() function. You must first select the order using OrderSelect().<br />

137

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

Saved successfully!

Ooh no, something went wrong!