Monday, January 4, 2010

MetaTrader - MQL4 programming - Part 3

Welcome back Forex fans.

Here's my 3rd installment on programming MetaTrader. In case you missed my prior posts, you can find them MetaTrader programming part 1 and MetaTrader programming part 2.

In this post, we'll take a look at the MarketInfo() function. This function pulls some necessary information from the broker about the symbol in question. Go ahead and create a new EA file called MarketInfo. Paste the following code above the init() function:

string sSymbol;

Now paste this code into the init() function:

int init()
{
// Grab current symbol
Print("Marketinfo Startup");

// Grab current symbol
sSymbol = Symbol();

// Print it out and finish up
Print("Symbol: ", sSymbol);

return(0);
}

Now setup your Start() function as follows:

int start()
{
double dRetval;

dRetval = MarketInfo(sSymbol,MODE_LOW);
Print("Days high: ", dRetval);

dRetval = MarketInfo(sSymbol,MODE_HIGH);
Print("Day low : ", dRetval);

/* Skip Bid, Ask, Point, Digits, they pre-defined variables */

dRetval = MarketInfo(sSymbol,MODE_SPREAD);
Print("Spread: ", dRetval);

dRetval = MarketInfo(sSymbol,MODE_STOPLEVEL);
Print("Stop Level: ", dRetval);

dRetval = MarketInfo(sSymbol,MODE_LOTSIZE);
Print("Lot Size: ", dRetval);

return(0);
}

/* Skip these
MODE_LOTSIZE 15 Lot size in the base currency.
MODE_TICKVALUE 16 Minimal tick value in the deposit currency.
MODE_TICKSIZE 17 Minimal tick size in the quote currency.
MODE_SWAPLONG 18 Swap of a long position.
MODE_SWAPSHORT 19 Swap of a short position.
MODE_STARTING 20 Trade starting date (usually used for futures).
MODE_EXPIRATION 21 Trade expiration date (usually used for futures).
MODE_TRADEALLOWED 22 Trade is allowed for the symbol.
MODE_MINLOT 23 Minimal permitted lot size.
MODE_LOTSTEP 24 Step for changing lots.
MODE_MAXLOT 25 Maximal permitted lot size.
MODE_SWAPTYPE 26 Swap calculation method. 0 - in points; 1 - in the symbol base currency; 2 - by interest; 3 - in the margin currency.
MODE_PROFITCALCMODE 27 Profit calculation mode. 0 - Forex; 1 - CFD; 2 - Futures.
MODE_MARGINCALCMODE 28 Margin calculation mode. 0 - Forex; 1 - CFD; 2 - Futures; 3 - CFD for indexes.
MODE_MARGININIT 29 Initial margin requirements for 1 lot.
MODE_MARGINMAINTENANCE 30 Margin to maintain open positions calculated for 1 lot.
MODE_MARGINHEDGED 31 Hedged margin calculated for 1 lot.
MODE_MARGINREQUIRED 32 Free margin required to open 1 lot for buying.
MODE_FREEZELEVEL 33 Order freeze level in points. If the execution price lies within the range defined by the freeze level, the order cannot be modified, canceled or closed.
*/

As we can see from the code, the MarketInfo() function allows us to pull the day's high and low when using the MODE_HIGH and MODE_LOW parameters. This could be useful when coding an Expert Advisor. The daily high/low counters reset when the daily interest rate roll occurs which is at 5PM EST.

Calling MarketInfo() with the MODE_SPREAD parameter pulls the current spread for the pair which you can get just as easily by subtracting the Bid from the Ask and dividing by Point.

The MODE_STOPLEVEL parameter returns the number of pips away from the current price at which you are allowed to place the a stop order or take profit order. In the above example, the Stop level value is 4 so if we went long the market at an Ask price of 1.4340, the closest that we could place a take-profit order would be 1.4344. If the current Bid price were 1.4338, the closest we could place a stop-loss order would be 1.4334.

When I first found out about this, I thought was kind of unfair. I suppose it prevents the market maker from constantly getting scalped and ensures that on trades that go against the customer, they make at least the spread plus the stop level. Of course, there's nothing preventing you from entering market orders to close a position within the StopLevel.

The MODE_FREEZELEVEL parameter returns the number of pips away from the current price at which limit orders are "frozen" and cannot be changed. For example, if the FREEZELEVEL were 5 and the current Bid/Ask was 1.4356 x 1.4358 and you had a limit sell order at 1.4360, you would not be allowed to modify or cancel your limit order since it was within 5 pips of the Bid price.

On my terminal FREEZELEVEL came back at zero, so i'm not sure its enforced with FXDD on their Metatrader platform.

Now save and compile the EA and drag it onto a chart of EUR/USD. Now drag it onto a few other charts such as GBP/USD and GBP/JPY to get a feel for the different Spread and Stoplevel values.

That's it, tomorrow we will take a look at Account and Terminal related functions.

2 comments:

  1. Hey Everybody,

    I've included a list of the highest ranking forex brokers:
    1. Best Forex Broker
    2. eToro - $50 minimum deposit.

    Here is a list of the best forex instruments:
    1. ForexTrendy - Recommended Probability Software.
    2. EA Builder - Custom Strategies Autotrading.
    3. Fast FX Profit - Secret Forex Strategy.

    I hope you find these lists beneficial...

    ReplyDelete