Wednesday, December 30, 2009

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 here 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's 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, come back later for more MT4 programming.

4 comments:

  1. You really have to re-think the way you analyze traders, your favorite trader (favor of the month) is “Cheetah” and what kind of trader open over 15 trades against the strong UP movement we started to have on Wednesday Dec 30 and still on Thursday Dec31 with the GBP/USD still going up Does not close All those losing position and instead is now “Hope trading” this is way you have Blown up your account,

    You need to look at the history or the trading log and look into the DETAILS, This will tell you all you need to know about the traders mind set. You need to reconstruct THAT DAY and look at the chart, time of day, news event and see how they handle a lose, did they stop and reserve? did they just keep revenge trade on a strong trend like “Cheetah” has done over the last 2 days, or did they have an open trade before a major news release that just made profits because they had an open floating trade? Which really shows an act of not caring sense those gains could have made the floating losses even worse.

    All you’re the analyses you have done and you still blew up your account, which tells you ITS NOT WORKING. Because you like ever one else just focus on the GAINS IN A EVERY SHORT TIME, you need at least 8 months of history this will tell you all you need to know,

    If you need to watch your account then you HAVE THE WRONG PROVIDER PEROID!

    I would post this on your friends other website, but I am sure he will read this and same applies to him.

    Those ‘Live” account sign are bullshit what happens to providers with mirco accounts? They lose pennies you lose dollars, OR what happens to provider with Accounts at Currenex or Dukascopy, really you need to re-think what you are doing.

    Jimmy

    ReplyDelete
  2. Jimmy-
    I'm sorry for your losses. You are correct, I have blown up my account. I have concluded that no amount of analysis will predict what a provider will do in the future. They can trade conservatively for months on end, then go off the deep end, and that's exactly what has happened with Cheetah.
    Perhaps you have a better method, but even if you do, i'm unlikely to risk any more money with Zulutrade.
    Instead, i'm moving in the direction of automated trading with MT4. These types of systems can be tested forward and backward over years of trading and have a far better chance of behaving in the future like they have in the past.
    Perhaps i'm wrong about that as well, but time will tell. Once again, sorry for your losses, i'm right there with you,
    Chris

    ReplyDelete
  3. Chris

    Just so you understand I did not ever use Cheetah,I have had losses,we all have but not those crazy ones.

    I will never and I mean never trade and providers who has less than 8 months of history, and I will reconstruct thier history and get into "his mind set" for each trade . I will look at charts, news events,trends, market moods, .That tells me more than anything else. As an example if I were to look back on Cheetah in the future and look at this day 4 months from now I would NEVER USE HIM, because they have show to be reckless once and they will do it again, same with Bigwin /TheKiller/Aston , each have the same habit. Short term gains to only have your gians blow and your account. I would rather have a smooth and stead gain. Look at what as happen to you , I bet you wish you AT LEAST STILL HAD your trading capital, but in seeking those fast returns everything is lost.

    Let me ask you, if Bank traders fail during the last 2 years and only a hand full have made money and those traders are guarded by the banks , why would anyone think some micky mouse trader on Zulu could make you over 60% return a year? People need to wake up and look at the LONG TERM>

    ReplyDelete
  4. Jimmy-

    Thanks again for the comment, it was a real wake-up call and was enough to push me out of Zulu-blogging game for good.

    I think I understand what you are saying with your method, but it sounds like almost as much work as trading itself. I'm not averse to hard work, but I can't spend the time in front of the terminal that proper Forex trading requires. That's why i'm moving in the direction of Expert Advisors.

    The aspect of Zulutrade that is flawed is that once the trader gets a large enough following, they start to be influenced by the # of followers themselves. Imagine how you would feel if you had a button that every time you pressed it, you made $275? I've seen Zulu providers trade conservatively for many months (Oxford Gobal FX Fund) then blow up big time. Its a case of the tail wagging the dog. And that factor alone makes Zulu unworkable because past trading is not a good predictor of future behavior.

    Fortunately, I still have plenty of trading capital, but whatever I do with Forex going forward will be on a much smaller scale say a ($1000 account) versus the 10K account I threw at Zulutrade. Some EA's i'm looking at have have about 20% drawdown and 80% a year gains over the past 10 years. Can this be replicated in 2010? We'll see.

    Thanks again for the comment and good trading,

    Chris

    ReplyDelete

Note: Only a member of this blog may post a comment.