Jump to content

Metatrader coders libraries- share functions


finimej

Recommended Posts

The target group of this thread is the metatrader coders who would like to share their functions and to make the life easier for each other.

There are many basic things that we all use in one EA, like money management, trailstop, news filters, add alert to indicators.

 

So I would first give two links as basics.

1) where to start, - Metatrader Development Course

http://www.metatrader.info/node/34
[url]http://www.metatrader.info/node/27[/url] 

 

2) Metatrader programming reference manual - in pdf to download here

http://www.formulatrader.com/download/mq4/mql4_manual.pdf

 

3) you can also search your function in codebase

http://codebase.mql4.com/
[url]http://docs.mql4.com/index[/url]

 

4) Here is the EA framework with building Magic number, normal compounding money management and trailstop building,

all your need is adding icustom call to get signal from your indicators.

http://www.formulatrader.com/download/EA_framework.zip

Link to comment
Share on other sites

news filters

 

Here is the news filters. download the indicator FFCAL at

http://www.formulatrader.com/download/FFCal.mq4

line to call in the main funciton start()

  if(NewsFilter&&NewsHandling())return(0);


/*
News Handling, refer to FFCal documentation, 
*/
bool NewsHandling() {
    static int PrevMinute = -1;

    if (Minute() != PrevMinute)
    {
        PrevMinute = Minute();
   
        int minutesSincePrevEvent = iCustom(NULL, 0, "FFCal", true, IncludeMediumImpact, false, true, true, 1, 0);
        int minutesUntilNextEvent =iCustom(NULL, 0, "FFCal", true, IncludeMediumImpact, false, true, true, 1, 1);
        NewsTime = false;
        if(minutesUntilNextEvent<=MinBeforeNewsCloseAction){
             ForceCloseAll(Magic, OP_BUY,Red) ;
             ForceCloseAll(Magic, OP_SELL,Red) ;
             NewsTime=true;
             return(true);
         }
        if((minutesUntilNextEvent<=MinBeforeNewsNoTrade)||(minutesSincePrevEvent<=MinAfterNewsToTrade)){
           NewsTime=true; 
        }
        
    }
  return(NewsTime);
}

Link to comment
Share on other sites

4 digits or 5 digits

 

This code to be used in the init()

MyDigits = MarketInfo(Symbol(), MODE_TICKVALUE);
     MyPoint = Point;
     if (Digits == 5 || Digits == 3) {
        MyPoint = 10.0 * MyPoint;
        MyDigits = 10.0 * MyDigits;
     }

 

then you use MyPoints to replace the normal Point, and use the MyDigits to replace the normal Digits.

So your system is now ready for the both 4 digits and 5 digits brokers already.

Link to comment
Share on other sites

Re: 4 digits or 5 digits

 

This code to be used in the init()

MyDigits = MarketInfo(Symbol(), MODE_TICKVALUE);
     MyPoint = Point;
     if (Digits == 5 || Digits == 3) {
        MyPoint = 10.0 * MyPoint;
        MyDigits = 10.0 * MyDigits;
     }

 

then you use MyPoints to replace the normal Point, and use the MyDigits to replace the normal Digits.

So your system is now ready for the both 4 digits and 5 digits brokers already.

 

That's totally wrong. MODE_TICKVALUE will give you the profit/loss generated by the current rate moving by Point if you had an open trade of 1 lot. Even if you were using Digits instead of that, why would you multiply it by 10... It'd result in MyDigits being 50 instead of 4.

 

Use this instead:

     MyDigits = Digits;
     MyPoint = Point;
     if (Digits % 2 == 1) {
        MyPoint *= 10;
        MyDigits -= 1;
     }

 

If you do use this code, be very careful with CFDs and any exotic pairs that might have 3 digits by default and 4 digits on a 5-digit broker.

Link to comment
Share on other sites

Re: Metatrader coders libraries- share functions

 

Birt, thanks for correct.

one more function here.

because Metatrader is not so good in debug, so I very often use this function to print out the orders and signals for checking.

 

bool SaveLog(int iTicket_1) 
{
     string LogMsg="";
     Handle = FileOpen(FileName,FILE_CSV|FILE_WRITE|FILE_READ,",");
     if (Handle < 1)
     {
     Alert("Error writing to file."); 
     return(FALSE);        
     }
     FileSeek(Handle, 0, SEEK_END);
    //  = "---------------------------------------------------------------------------------------------------";
    // FileWrite(Handle, LogMsg);
     if (OrderSelect(iTicket_1,SELECT_BY_TICKET)==true) {
        int i.BarElasped = iBarShift(Symbol(),TimeFrame,OrderOpenTime(),true); 
      
        if (OrderType()== OP_BUY){
           double d.BandDownNbarsPips =  GetDisBandDowninPips(OrderOpenTime());      
           if  (NewBar() && d.BandDownNbarsPips<0){
               LogMsg = ">>Buy Order, open " + TimeToStr(OrderOpenTime())+ ", " + DoubleToStr(OrderOpenPrice(),6) + ", ticket "+ OrderTicket()+", "+ OrderSymbol()+ ", BarElasped= " + i.BarElasped + ", BandDown In Pips= "+DoubleToStr(d.BandDownNbarsPips,6);
               FileWrite(Handle, LogMsg);
            }
        }
        if (OrderType()== OP_SELL){ 
           double d.BandUpNbarsPips =  GetDisBandUpinPips(OrderOpenTime());
          if  (NewBar()&& d.BandUpNbarsPips>0){
               LogMsg = ">>Sell Order, open " + TimeToStr(OrderOpenTime())+ ", " + DoubleToStr(OrderOpenPrice(),6) + ", ticket "+ OrderTicket()+", "+ OrderSymbol()+ ", BarElasped= " + i.BarElasped + ", BandUp In Pips= "+DoubleToStr(d.BandUpNbarsPips,6);
              FileWrite(Handle, LogMsg);
           }
        }
     }
     FileClose(Handle); 
} 

 

Call part

int HandleOpenPositions()
{
  int cnt;
  double Support, Resistant;
  for(cnt=OrdersTotal()-1;cnt>=0;cnt--)
  {
     OrderSelect (cnt, SELECT_BY_POS, MODE_TRADES);
     if ( OrderSymbol() != Symbol()) continue;
     if ( OrderMagicNumber() != MagicNumber)  continue;
     SaveLog(OrderTicket());
     if(OrderType() == OP_BUY)
     {         
        if (CheckExitCondition(OP_BUY, OrderOpenTime()))
        {
            CloseOrder(OrderTicket(),OrderLots(),OP_BUY);
        }          
     }      
     if(OrderType() == OP_SELL)
     {
        if (CheckExitCondition(OP_SELL,OrderOpenTime()))
        {
            CloseOrder(OrderTicket(),OrderLots(),OP_SELL);
        }          
     }
  }
}

Link to comment
Share on other sites

ordersend for ECN broekrs like MBtrading

 

ordersend for ECN broekrs like MBtrading

ordertick = OrderSend(Symbol(), OP_BUY, Lots, Bid, Slippage, 0, 0, Comments, MagicNumber, 0, Blue);
  if (ordertick > 0)
     if (OrderSelect(ordertick, SELECT_BY_TICKET)) 
         OrderModify(OrderTicket(), OrderOpenPrice(),  StoplossPrice, TakeProfitsPrice, 0);

Link to comment
Share on other sites

Re: ordersend for ECN broekrs like MBtrading

 

That OrderModify() call should be

 

for OP_BUY operation: OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() - StoplossDistance, OrderOpenPrice() + TakeProfitDistance, 0);

for OP_SELL operation: OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice() + StoplossDistance, OrderOpenPrice() - TakeProfitDistance, 0);

 

Or

 

OrderModify(OrderTicket(), OrderOpenPrice(), StoplossPrice, TakeProfitPrice, 0);

Link to comment
Share on other sites

Re: 4 digits or 5 digits

 

This code to be used in the init()

MyDigits = MarketInfo(Symbol(), MODE_TICKVALUE);
     MyPoint = Point;
     if (Digits == 5 || Digits == 3) {
        MyPoint = 10.0 * MyPoint;
        MyDigits = 10.0 * MyDigits;
     }

Why do you use MODE_TICKVALUE? Should it instead be:

MyDigits = MarketInfo(Symbol(), MODE_DIGITS);? (or eqivalently, MyDigits = Digits;?)

 

And MyDigits should be subtracted by 1 as birt has shown. Please correct the code otherwise it may cause confusion.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...