के नये स्टार्टाप बोनस के साथ कोई
भी जोखिम और जमा के बिना
व्यापार शुरू करें
बोनस 1000$
GET BONUS
55%
from InstaForex
on every deposit
+ Reply to Thread
Page 1 of 5 1 2 3 ... LastLast
Results 1 to 10 of 42

Thread: Usefull MQ4 Code Snippets/Functions

  1. #1
    Junior Member peteonthenet is an unknown quantity at this point peteonthenet's Avatar
    Join Date
    Aug 2011
    Location
    India
    Posts
    51
    Accumulated bonus
    10.8 USD
    Thanks
    1
    Thanked 1 Time in 1 Post

    Thumbs up Usefull MQ4 Code Snippets/Functions

    In this thread, we will collect and discuss useful code snippets/functions that we can incorporate in Expert Advisors to make coding process faster and easier

  2. <a href="http://www.mt5.com/">&#1060;&#1086;&#1088;&#1077;&#1082;&#1089; &#1087;&#1086;&#1088;&#1090;&#1072;&#1083;</a>
  3. #2
    Junior Member peteonthenet is an unknown quantity at this point peteonthenet's Avatar
    Join Date
    Aug 2011
    Location
    India
    Posts
    51
    Accumulated bonus
    10.8 USD
    Thanks
    1
    Thanked 1 Time in 1 Post
    Here is a code which you can use in an Expert Advisors to automatically adjust variables depending on if the Broker is 4 digits or 5 digits pricing.

    following code should be inside the int Init() function

    Code:
    if(Digits==5 || Digits==3){ 
                StopLoss= Stoploss * 10;   // this adjusts Stoploss variable for 5digit broker
    }
    Last edited by peteonthenet; 10-06-2011 at 05:42 PM.

  4. #3
    Junior Member peteonthenet is an unknown quantity at this point peteonthenet's Avatar
    Join Date
    Aug 2011
    Location
    India
    Posts
    51
    Accumulated bonus
    10.8 USD
    Thanks
    1
    Thanked 1 Time in 1 Post

    Count All Open Trades

    Following is a custom function which when called will return total of active trades for a particular magic number:


    Code:
    int CountAllTrades(int magic) {
       int c=0;
       for (int j=OrdersTotal()-1;j>=0;j--)
       {
          OrderSelect(j,SELECT_BY_POS,MODE_TRADES);
          if (OrderSymbol()==Symbol() && OrderMagicNumber()==magic) c++;
       }
       
       return(c);
    }
    Last edited by peteonthenet; 10-06-2011 at 05:47 PM.

  5. #4
    Junior Member peteonthenet is an unknown quantity at this point peteonthenet's Avatar
    Join Date
    Aug 2011
    Location
    India
    Posts
    51
    Accumulated bonus
    10.8 USD
    Thanks
    1
    Thanked 1 Time in 1 Post

    Close All Function

    This function will close All open trades for a particual MagicNumber of EA.

    Code:
    void CloseAll()
    {
      int total = OrdersTotal();
      for(int i=total-1;i>=0;i--)
      {
        OrderSelect(i, SELECT_BY_POS);
        int type = OrderType();
        
        if (OrderSymbol()==Symbol() && (OrderMagicNumber() == MagicNumber))  
        {
          //-- Close open BUYs
          if (type == OP_BUY) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),Slippage,CornflowerBlue);
          //-- Close open SELLS
          if (type == OP_SELL) OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),Slippage,CornflowerBlue);
        }
      }
      return;
    }

  6. #5
    Junior Member peteonthenet is an unknown quantity at this point peteonthenet's Avatar
    Join Date
    Aug 2011
    Location
    India
    Posts
    51
    Accumulated bonus
    10.8 USD
    Thanks
    1
    Thanked 1 Time in 1 Post
    If you have request for any specific code function, please feel free to post your request here and will that code posted to you here in this thread.

    Of if you have own own custom codes and you think will benefit other EA programmers, please feel to post it here for the benefit of all.

  7. #6
    Junior Member peteonthenet is an unknown quantity at this point peteonthenet's Avatar
    Join Date
    Aug 2011
    Location
    India
    Posts
    51
    Accumulated bonus
    10.8 USD
    Thanks
    1
    Thanked 1 Time in 1 Post

    Numeric Chart Period to Text String Format

    Here is the code function that will return the chart period in the nicely formation text line.

    Example: (if EA is running on 1hour chart)

    chartperiod = periodToString(Period()) // will return 1 Hour


    Code:
    string periodToString(int tf) 
    {
       string tfString;
       
       switch (tf)  
       {   
          case 1:      tfString = "1 Min";    break;
          case 5:      tfString = "5 Min";    break;
          case 15:     tfString = "15 Min";   break;  
          case 30:     tfString = "30 Min";   break;  
          case 60:     tfString = "1 Hour";   break;  
          case 240:    tfString = "4 Hour";   break;  
          case 1440:   tfString = "Daily";    break;  
          case 10080:  tfString = "Weekly";   break;  
          case 40320:  tfString = "Monthly";  break;  
          default:     tfString = "Unknown";   
       }
    
      
       return (tfString);
    }

  8. #7
    Junior Member peteonthenet is an unknown quantity at this point peteonthenet's Avatar
    Join Date
    Aug 2011
    Location
    India
    Posts
    51
    Accumulated bonus
    10.8 USD
    Thanks
    1
    Thanked 1 Time in 1 Post

    Code to take Screenshot of Chart and save it to local drive

    Here is the Code that you can put in any EA to take a screenshot of the Chart and save it to disk, you can call this on any event like if there is any new trade taken, you can save the screenshot.


    the system function to take screenshot is:
    Code:
    WindowScreenShot(filename,size_x,size_y)

    Here is the example usage:

    Code:
    string filename= "Chart"+Symbol();
    WindowScreenShot(filename,570,428)

    The above will save the chart with name "ChartEURUSD". You can make the filename very dynamic so every time screenshot is taken, it would save it with different name.

  9. #8
    Junior Member peteonthenet is an unknown quantity at this point peteonthenet's Avatar
    Join Date
    Aug 2011
    Location
    India
    Posts
    51
    Accumulated bonus
    10.8 USD
    Thanks
    1
    Thanked 1 Time in 1 Post

    CloseAllTrade Script

    Here is a code which can be saved as a script file in the Scripts Folder.

    To execute the script, drage and drop the script on the current chart, this will close all open trades and also pending orders of all symbols.


    Code:
    int start()
    {
       double total;
       int cnt;
       while(OrdersTotal()>0)
       {
          // close opened orders first
          total = OrdersTotal();
          for (cnt = total ; cnt >=0 ; cnt--)
          {
             if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)) 
             {
                switch(OrderType())
                {
                   case OP_BUY       :
                      RefreshRates();
                      OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),3,Violet);break;
                       
                   case OP_SELL      :
                      RefreshRates();
                      OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),3,Violet); break;
                }             
             }
          }
          // and close pending
          total = OrdersTotal();      
          for (cnt = total ; cnt >=0 ; cnt--)
          {
             if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)) 
             {
                switch(OrderType())
                {
                   case OP_BUYLIMIT  :OrderDelete(OrderTicket()); break;
                   case OP_SELLLIMIT :OrderDelete(OrderTicket()); break;
                   case OP_BUYSTOP   :OrderDelete(OrderTicket()); break;
                   case OP_SELLSTOP  :OrderDelete(OrderTicket()); break;
                }
             }
          }
       }
       return(0);
    }

  10. #9
    Junior Member peteonthenet is an unknown quantity at this point peteonthenet's Avatar
    Join Date
    Aug 2011
    Location
    India
    Posts
    51
    Accumulated bonus
    10.8 USD
    Thanks
    1
    Thanked 1 Time in 1 Post
    Here is code function which can be used say when ea needs to trade only during certain hours of the day and skip of certain day of the week :

    Code:
      bool ValidTime() 
      {
       if (DayOfWeek()==1 && Hour()<=6) return(false);  
        return(true);
       }

  11. <a href="http://www.mt5.com/">&#1060;&#1086;&#1088;&#1077;&#1082;&#1089; &#1087;&#1086;&#1088;&#1090;&#1072;&#1083;</a>
  12. #10
    Junior Member peteonthenet is an unknown quantity at this point peteonthenet's Avatar
    Join Date
    Aug 2011
    Location
    India
    Posts
    51
    Accumulated bonus
    10.8 USD
    Thanks
    1
    Thanked 1 Time in 1 Post

    Function to calculate total lots opened

    Here is the code fuction which will return the total number of lots which are currently running in open trades :

    Parameters: key is magic number, and type is the type of trades .


    Code:
    double gettotallot(int _key, int type)
    {
    double _lot=0;
     for(int k=OrdersTotal(); k>=0; k--)
                    {  
                      if (OrderSelect(k,SELECT_BY_POS,MODE_TRADES)&& OrderSymbol()==Symbol()&& OrderMagicNumber()==_key && OrderType()==type)
                      _lot+=OrderLots();
                      } 
                  return(_lot); 
     }

+ Reply to Thread

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts