+ Reply to Thread
Page 11 of 11 FirstFirst ... 9 10 11
Results 101 to 106 of 106

Thread: Usefull MQ4 Code Snippets/Functions

  1. #6
    Junior Member peteonthenet is on a distinguished road peteonthenet's Avatar
    Join Date
    Aug 2011
    Location
    India
    Posts
    51
    Thanks
    1
    Thanked 20 Times in 15 Posts
    SubscribeSubscribe
    subscribed 0

    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);
    }

  2. The Following User Says Thank You to peteonthenet For This Useful Post:

    Alizahid (2020-10-20)

  3. #5
    Junior Member peteonthenet is on a distinguished road peteonthenet's Avatar
    Join Date
    Aug 2011
    Location
    India
    Posts
    51
    Thanks
    1
    Thanked 20 Times in 15 Posts
    SubscribeSubscribe
    subscribed 0
    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.

  4. The Following User Says Thank You to peteonthenet For This Useful Post:

    Alizahid (2020-10-20)

  5. #4
    Junior Member peteonthenet is on a distinguished road peteonthenet's Avatar
    Join Date
    Aug 2011
    Location
    India
    Posts
    51
    Thanks
    1
    Thanked 20 Times in 15 Posts
    SubscribeSubscribe
    subscribed 0

    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. The Following 1 Users Say Thank You to peteonthenet For This Useful Post:

    Alizahid (2020-10-20)

  7. #3
    Junior Member peteonthenet is on a distinguished road peteonthenet's Avatar
    Join Date
    Aug 2011
    Location
    India
    Posts
    51
    Thanks
    1
    Thanked 20 Times in 15 Posts
    SubscribeSubscribe
    subscribed 0

    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; 2011-10-06 at 05:47 PM.

  8. The Following 1 Users Say Thank You to peteonthenet For This Useful Post:

    Alizahid (2020-10-20)

  9. #2
    Junior Member peteonthenet is on a distinguished road peteonthenet's Avatar
    Join Date
    Aug 2011
    Location
    India
    Posts
    51
    Thanks
    1
    Thanked 20 Times in 15 Posts
    SubscribeSubscribe
    subscribed 0
    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; 2011-10-06 at 05:42 PM.

  10. The Following User Says Thank You to peteonthenet For This Useful Post:

    Alizahid (2020-10-20)

  11. #1
    Junior Member peteonthenet is on a distinguished road peteonthenet's Avatar
    Join Date
    Aug 2011
    Location
    India
    Posts
    51
    Thanks
    1
    Thanked 20 Times in 15 Posts
    SubscribeSubscribe
    subscribed 0

    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

  12. The Following 1 Users Say Thank You to peteonthenet For This Useful Post:

    Alizahid (2020-10-20)

+ Reply to Thread
Page 11 of 11 FirstFirst ... 9 10 11

Subscribe to this Thread (1)

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

Currently Active UsersCurrently Active Users

There are currently users online. members and guests

Forex Forum India | Forex Community Place Statistics Forex Forum India Statistics

Most users ever online was .

Welcome to our newest member,

Threads:

Posts:

Member: