learning mql5

hayseed

Master Trader
Jul 27, 2010
1,206
283
149
usa
checking for proper alignment of 5, 10, 20, 200 smas and returning color......

trend type filter......h

//------

Code:
//------
//------

    for(int r = 0; r < ArraySize(tfs); r++)
      {

    int ma5[15];    
    int ma10[15];    
    int ma20[15];    
    int ma200[15];    
 

 

      ma5[r]      = iMA(symbol, tfs[r],5,0,MODE_SMA,PRICE_CLOSE);
      ma10[r]     = iMA(symbol, tfs[r],10,0,MODE_SMA,PRICE_CLOSE);
      ma20[r]     = iMA(symbol, tfs[r],20,0,MODE_SMA,PRICE_CLOSE);
      ma200[r]    = iMA(symbol, tfs[r],200,0,MODE_SMA,PRICE_CLOSE);



 
        double ma5s[];

        ArraySetAsSeries(ma5s,true);  

        CopyBuffer(ma5[r],0,0,4,ma5s);

 
        double ma10s[];

        ArraySetAsSeries(ma10s,true);  

        CopyBuffer(ma10[r],0,0,4,ma10s);

 
 
        double ma20s[];

        ArraySetAsSeries(ma20s,true);  

        CopyBuffer(ma20[r],0,0,4,ma20s);

 
        double ma200s[];

        ArraySetAsSeries(ma200s,true);  

        CopyBuffer(ma200[r],0,0,4,ma200s);

     


        clrmasalign[r]  = clrYellow;
       
        if(( (ma5s[0] > ma10s[0]) && (ma5s[0] > ma20s[0])  && (ma10s[0] > ma20s[0])  && (ma20s[0] > ma200s[0]) )  &&  ( (ma5s[0] > ma5s[1]) && (ma10s[0] > ma10s[1])  && (ma20s[0] > ma20s[1])  && (ma200s[0] > ma200s[1]) )  )    
         
           {
           clrmasalign[r] = clrBlue;
           }


        if( ( (ma5s[0] < ma10s[0]) && (ma5s[0] < ma20s[0])  && (ma10s[0] < ma20s[0])  && (ma20s[0] < ma200s[0]) )   &&  ( (ma5s[0] < ma5s[1]) && (ma10s[0] < ma10s[1])  && (ma20s[0] < ma20s[1])  && (ma200s[0] < ma200s[1]) )  )    
         
           {
           clrmasalign[r] = clrRed;
           }

      }

//------  
//------

//------

Screenshot 2024-11-29 032708.png
 

hayseed

Master Trader
Jul 27, 2010
1,206
283
149
usa
working on it......h

//------

history deal constants.....

datetime conversions.......

deal property string......

history deal get integer......



//--------

MQL5:
double orders[20][20];
 
string symbols[5] = {"YM", "ES", "NQ", "RT", "GC"};
 
 
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
 
 
void OnStart()
  {
//--- request all the existing history on the account
   if(!HistorySelect(0, TimeCurrent()))
     {
      Print("HistorySelect() failed. Error ", GetLastError());
      return;
     }
 
 
  uint     totals = HistoryDealsTotal();
 
  Print("Number of historical deals on the account: ", totals);
 
  string   symbol    = "";
  double   profit    =  0;
  ulong    ticket     = 0;
  datetime time;
 
  MqlDateTime structure;
 
//--- for all deals
   for(uint i=0;i<totals;i++)
     {
       if((ticket=HistoryDealGetTicket(i)) > 0 )
 
 
      symbol = StringSubstr(HistoryDealGetString(ticket,DEAL_SYMBOL), 0, 2);  // substring  needed due to contract months changing
 
      //Print(symbol);
 
        for(int j = 0; j< ArraySize(symbols); j++)
           {
           if(symbol == symbols[j])
            {
              orders[j][0] = orders[j][0] +1;
              orders[j][1] = orders[j][1] + HistoryDealGetDouble(ticket,DEAL_PROFIT);
              orders[j][2] = orders[j][2] + HistoryDealGetDouble(ticket, DEAL_VOLUME);  
              //orders[j][3] = orders[j][1] +
              //orders[j][4] = orders[j][1] +
              orders[j][5] = orders[j][5] + HistoryDealGetDouble(ticket, DEAL_COMMISSION);  
 
//-----                dayoftheweek  
 
              time =  HistoryDealGetInteger(ticket,DEAL_TIME);
 
              TimeToStruct(time, structure);
 
              int weekday = structure.day_of_week;  // Print(weekday);  //  Print(TimeToString(time, TIME_DATE));
 
              if(weekday == 1) {orders[j][11] = orders[j][11] + 1;  Print(symbols[j]+" monday orders "+orders[j][11]);}  
              if(weekday == 2) {orders[j][12] = orders[j][12] + 1;  Print(symbols[j]+" tuesday orders "+orders[j][12]);}
              if(weekday == 3) {orders[j][13] = orders[j][13] + 1;  Print(symbols[j]+" wednesday orders "+orders[j][13]);}
              if(weekday == 4) {orders[j][14] = orders[j][14] + 1;  Print(symbols[j]+" thursday orders "+orders[j][14]);}
              if(weekday == 5) {orders[j][15] = orders[j][15] + 1;  Print(symbols[j]+" friday orders "+orders[j][15]);}
//-----
 
           }
 
           }
 
 
      }
 
  Print("YM " +orders[0][0]+"  profit "+ orders[0][1] + "  ES " + orders[1][0]+"  profit "+ orders[1][1]  + "  NQ " +orders[2][0]+"  profit "+ orders[2][1]  + "  RTY " +orders[3][0]+"  profit "+ orders[3][1]  );  
 
  }
 
Last edited by a moderator:

hayseed

Master Trader
Jul 27, 2010
1,206
283
149
usa
Vladimir Karputov , aka barabashkakvn, from perhaps ukraine, posted on the mq5 site his Two MA Color N Bars Square - indicator for MetaTrader 5.......

this was something i had tried to do a few months ago, before finally setting it aside...... all it takes is a single concept error to end all hope.......

have written and posted hundreds or these type indicators for mq4....... they are useful to me.....

i modified , only slightly, his indicator to do what i could not before........ lines 75 and 90 in his code below......

his original is below,...... along with a hma(10) and hma(20)...... and alma and sma(200)...... you will need the alma and hma also..... will expand these.....

seems best to create a template......

the idea , for me, is small sized trades with prevailing trend....... h

//-----

he has a youtube channel...... some of his videos are translated in to english......


Screenshot 2024-12-27 062041.png
//-------
 

Attachments

  • hma.mq5
    12.6 KB · Views: 1
  • hma.ex5
    14.4 KB · Views: 0
  • alma.mq5
    7.1 KB · Views: 1
  • alma.ex5
    9.4 KB · Views: 0
  • Two MA Color N Bars Square v alma sma200.mq5
    23.3 KB · Views: 1
  • Two MA Color N Bars Square v alma sma200.ex5
    19.9 KB · Views: 1
  • Two MA Color N Bars Square v hma10 hma20.mq5
    23.4 KB · Views: 1
  • Two MA Color N Bars Square v hma10 hma20.ex5
    19.6 KB · Views: 1
  • Two MA Color N Bars Square.ex5
    19.3 KB · Views: 1
Last edited:
  • 👍
Reactions: Enivid

hayseed

Master Trader
Jul 27, 2010
1,206
283
149
usa
the coppock indicator was intended monthly charts...... few can trade monthly charts...... not me for sure......

it seems to work equally well for me on lower timeframes......

if you looked at the math, it's really just comparing 2 roc's, rate of changes, and the creating a moving average of that change......

a single ma applied to the roc indicator is a very close approximation of the coppock...... such a indicator is on the mq5 site......

roc with a signal ma.......

the idea for me with both the coppock and the roc with ma, is to alert on the zero line crossing....... a 0 line cross with confirming hull and alma ma's seems a fair signal.....

had to edit the roc with ma to work with the mym, dow 30, futures....... some indicators will not work well with symbols that are whole numbers only......

adjustable digits version below......h
//------

MYMH25M15 22.png

//-----
 

Attachments

  • ROC_with_Signal_MA digits.ex5
    12.7 KB · Views: 0
  • ROC_with_Signal_MA digits.mq5
    9.1 KB · Views: 0

hayseed

Master Trader
Jul 27, 2010
1,206
283
149
usa
archive.....h

//------

initial expert code......

//------

MQL5:
void OnTick()
  {
//---
 
//----
     double a = (GlobalVariableGet("MYMH251") + GlobalVariableGet("MYMH255") + GlobalVariableGet("MYMH2515") + GlobalVariableGet("MYMH2530") + GlobalVariableGet("MYMH2560") + GlobalVariableGet("MYMH25240") + GlobalVariableGet("MYMH251440"));
     double b = (GlobalVariableGet("MESH251") + GlobalVariableGet("MESH255") + GlobalVariableGet("MESH2515") + GlobalVariableGet("MESH2530") + GlobalVariableGet("MESH2560") + GlobalVariableGet("MESH25240") + GlobalVariableGet("MESH251440"));
     double c = (GlobalVariableGet("MNQH251") + GlobalVariableGet("MNQH255") + GlobalVariableGet("MNQH2515") + GlobalVariableGet("MNQH2530") + GlobalVariableGet("MNQH2560") + GlobalVariableGet("MNQH25240") + GlobalVariableGet("MNQH251440"));
     double d = (GlobalVariableGet("M2KH251") + GlobalVariableGet("M2KH255") + GlobalVariableGet("M2KH2515") + GlobalVariableGet("M2KH2530") + GlobalVariableGet("M2KH2560") + GlobalVariableGet("M2KH25240") + GlobalVariableGet("M2KH251440"));
 
     Comment("Alerts are "+alerts+ "     mym "+DoubleToString(a,0) +  "   mes "+DoubleToString(b,0) +  "   mmq "+DoubleToString(c,0) +  "   m2k "+DoubleToString(d,0));
 
    if(alerts)
    {
    if((GlobalVariableGet("MMYH251") + GlobalVariableGet("MMYH255") + GlobalVariableGet("MMYH2515") + GlobalVariableGet("MMYH2530") + GlobalVariableGet("MMYH2560") + GlobalVariableGet("MMYH25240") + GlobalVariableGet("MMYH251440"))  >=  60.0) {Alert(" MMY buy");}
    if((GlobalVariableGet("MMYH251") + GlobalVariableGet("MMYH255") + GlobalVariableGet("MMYH2515") + GlobalVariableGet("MMYH2530") + GlobalVariableGet("MMYH2560") + GlobalVariableGet("MMYH25240") + GlobalVariableGet("MMYH251440"))  <= -60.0) {Alert(" MMY sell");}
 
    if((GlobalVariableGet("MESH251") + GlobalVariableGet("MESH255") + GlobalVariableGet("MESH2515") + GlobalVariableGet("MESH2530") + GlobalVariableGet("MESH2560") + GlobalVariableGet("MESH25240") + GlobalVariableGet("MESH251440"))  >=  60.0) {Alert(" MES buy");}
    if((GlobalVariableGet("MESH251") + GlobalVariableGet("MESH255") + GlobalVariableGet("MESH2515") + GlobalVariableGet("MESH2530") + GlobalVariableGet("MESH2560") + GlobalVariableGet("MESH25240") + GlobalVariableGet("MESH251440"))  <= -60.0) {Alert(" MES sell");}
 
    if((GlobalVariableGet("MNQH251") + GlobalVariableGet("MNQH255") + GlobalVariableGet("MNQH2515") + GlobalVariableGet("MNQH2530") + GlobalVariableGet("MNQH2560") + GlobalVariableGet("MNQH25240") + GlobalVariableGet("MNQH251440"))  >=  60.0) {Alert(" MNQ buy");}
    if((GlobalVariableGet("MNQH251") + GlobalVariableGet("MNQH255") + GlobalVariableGet("MNQH2515") + GlobalVariableGet("MNQH2530") + GlobalVariableGet("MNQH2560") + GlobalVariableGet("MNQH25240") + GlobalVariableGet("MNQH251440"))  <= -60.0) {Alert(" MNQ sell");}
 
    if((GlobalVariableGet("M2KH251") + GlobalVariableGet("M2KH255") + GlobalVariableGet("M2KH2515") + GlobalVariableGet("M2KH2530") + GlobalVariableGet("M2KH2560") + GlobalVariableGet("M2KH25240") + GlobalVariableGet("M2KH251440"))  >=  60.0) {Alert(" M2K buy");}
    if((GlobalVariableGet("M2KH251") + GlobalVariableGet("M2KH255") + GlobalVariableGet("M2KH2515") + GlobalVariableGet("M2KH2530") + GlobalVariableGet("M2KH2560") + GlobalVariableGet("M2KH25240") + GlobalVariableGet("M2KH251440"))  <= -60.0) {Alert(" M2K sell");}
    }
 
  }
//+------------------------------------------------------------------+

//-----

//-----

initial companion indicator code.......

MQL5:
// alerts v3
//           if(run == false)
//            {
            if((almas[1] > almas[2]) && (hmas[1] > hmas[2]) && (coppocks[1] > coppocks[2]))  { clr[j] = clrBlue; GlobalVariableSet(_Symbol+tfs[j], 10); }  //Print(GlobalVariableGet(_Symbol+tfs[j]) +" b");
 
            if((almas[1] < almas[2]) && (hmas[1] < hmas[2]) && (coppocks[1] < coppocks[2]))  { clr[j] = clrRed;  GlobalVariableSet(_Symbol+tfs[j],-10); }  //Print(GlobalVariableGet(_Symbol+tfs[j]) +" s");
 
 
            if( !((almas[1] > almas[2]) && (hmas[1] > hmas[2]) && (coppocks[1] > coppocks[2])) && !((almas[1] < almas[2]) && (hmas[1] < hmas[2]) && (coppocks[1] < coppocks[2])))   {clr[j] = clrYellow;  GlobalVariableSet(_Symbol+tfs[j],0);}
 
//            }
 
//-----
 
 
           if((buy[j]  == false) &&  ((almas[1] > almas[2]) && (hmas[1] > hmas[2]) && (coppocks[1] > coppocks[2])))                 //  && (schaffs[1] > 0.20) &&   ((almas[2] < almas[3]) || (hmas[2] < hmas[3]) || (coppocks[2] < 0.0) || (schaffs[2] < 0.20) || (run == false)))  // || (run == false)
 
           {
           buy[j] = true;   sell[j] = false; //  clr[j] = clrBlue;  GlobalVariableSet(_Symbol+tfs[j],-1);     
 
 
           if((j == 1) || (j == 2) || (j == 3) || (j == 4))  {Alert(_Symbol + "  "+ tfs[j]+"  minute bullish change ");}
 
           }   
 
 
 
           if((sell[j] == false) &&  ((almas[1] < almas[2]) && (hmas[1] < hmas[2]) && (coppocks[1] < coppocks[2])))                 //  && (schaffs[1] < 0.96) &&   ((almas[2] > almas[3]) || (hmas[2] > hmas[3]) || (coppocks[2] > 0.0) || (schaffs[1] > 0.96) || (run == false)))  // || (run == false)
 
           {
           buy[j] = false;  sell[j] = true;  //clr[j] = clrRed; GlobalVariableSet(_Symbol+tfs[j],10);
 
 
           if((j == 1) || (j == 2) || (j == 3) || (j == 4))  {Alert(_Symbol + "  "+ tfs[j]+"  minute bearish change ");}
 
 
           }


//-----

Screenshot 2025-02-14 082330.png
Post automatically merged:

.....h

//-----

Screenshot 2025-02-14 140301.png
 

hayseed

Master Trader
Jul 27, 2010
1,206
283
149
usa
break operator.....

attempt at counting bars since last signal on tfs listed......h

//------

MQL5:
//---      count completed bars since last signal.....
 
 
           int a = 0;
           int b = 0;
 
 
           for(int k=0;k<15;k++)
              {
              if( ((almas[1+k] > almas[2+k]) && (hmas[1+k] > hmas[2+k]) && (coppocks[1+k] > coppocks[2+k])) )  {a++;} else break;
              }
 
           for(int l=0;l<15;l++)
              {             
              if( ((almas[1+l] < almas[2+l]) && (hmas[1+l] < hmas[2+l]) && (coppocks[1+l] < coppocks[2+l])) )  {b++;} else break;
              }
 
 
//---               Print(tfs[j] +" minutes aa = " +a);           //------ verified count 
 
//---               Print(tfs[j] +" minutes  bb = " +b);          //-----  verified count
 

hayseed

Master Trader
Jul 27, 2010
1,206
283
149
usa
market view arrangement using simple global variables..... blue buy..... red sell...... yellow coin toss.....
//-----


to do list....

1. code a price change since signal or group of signals..... such as since all 15's and 30's turned blue....

.....h


//-----

MQL5:
    for(int r = 0; r < 4; r++)                                //  string sym[4]          = {"MYMH25", "MESH25", "MNQH25", "M2KH25"};
      {
 
       Display(sym[r],  20, 200+(r*100));             
       ObjectSetText(sym[r], sym[r],10,"Verdana", clrAqua);
 
 
      for(int t = 0; t < 7; t++)                              //  string tfs[14]         = {"1", "5", "15", "30", "60", "240", "1440"}; 
      {
       color  clr    = clrYellow;
       string symbol = sym[r]+tfs[t];
 
       Display(tfs[t],  200+(t*100), 100);             
       ObjectSetText(tfs[t], tfs[t],10,"Verdana", clrAqua);
 
 
       if(GlobalVariableGet(symbol) ==  10) {clr = clrBlue;}
       if(GlobalVariableGet(symbol) == -10) {clr = clrRed;}
 
       Display(symbol,  200+(t*100), 200+(r*100));       //       Display(symbol,  20+(r*100), 100+(t*100)); 
 
       ObjectSetText(symbol, "ÛÛÛÛ",10,"Terminal", clr);
 
      }
      }

//-----
//-----

Screenshot 2025-02-16 100944.png
 

hayseed

Master Trader
Jul 27, 2010
1,206
283
149
usa
added current signal length......

still need price change since current signal......h

//-----

MQL5:
     if(NewBar() || weekend)
     { 
 
 
    for(int r = 0; r < 4; r++)                        // string sym[4]          = {"MYMH25", "MESH25", "MNQH25", "M2KH25"};       
      {
 
       Display(sym[r],  20, 200+(r*100));             
       ObjectSetText(sym[r], sym[r],10,"Verdana", clrAqua);
 
 
      for(int t = 0; t < 7; t++)                      //   string tfs[14]         = {"1", "5", "15", "30", "60", "240", "1440"};                   
      {
       color  clr    = clrYellow;
       string symbol = sym[r]+tfs[t];
 
       Display(tfs[t],  200+(t*300), 100);             
       ObjectSetText(tfs[t], tfs[t],10,"Verdana", clrAqua);
 
 
       if(GlobalVariableGet(symbol) ==  10) {clr = clrLime;}
       if(GlobalVariableGet(symbol) == -10) {clr = clrViolet;}
 
       Display(symbol,  200+(t*300), 200+(r*100));     
 
       ObjectSetText(symbol, "ÛÛÛÛ",10,"Terminal", clr);
 
 
 
       Display(symbol+"count",  300+(t*300), 200+(r*100));       
 
       ObjectSetText(symbol+"count", IntegerToString(GlobalVariableGet(symbol+"count")),10,"Verdana", clr);
 
      }
      }
 
   }

//-----
Screenshot 2025-02-16 205841.png
 

hayseed

Master Trader
Jul 27, 2010
1,206
283
149
usa
psar[1], psar[2] and 20 sma........h
//------


MQL5:
//------ alerts v count.mq5  sets global variables
 
 
           if(ma20s[1] > ma20s[2])  {GlobalVariableSet(_Symbol+tfs[j]+"ma", 10);}  else  {GlobalVariableSet(_Symbol+tfs[j]+"ma", -10);}             
 
 
 
           if(iHigh(_Symbol,tf[j],0) > psars[0])
           {         
           psarclr[j]     = clrBlue;  GlobalVariableSet(_Symbol+tfs[j]+"p1",10);                       
           }
           else
           {         
           psarclr[j]     = clrRed;  GlobalVariableSet(_Symbol+tfs[j]+"p1",-10);                             
           }
 
           if(iHigh(_Symbol,tf[j],1) > psars[1])
           {         
           psarclr2[j]    = clrBlue;  GlobalVariableSet(_Symbol+tfs[j]+"p2",10);                             
           }
           else
           {         
           psarclr2[j]    = clrRed;  GlobalVariableSet(_Symbol+tfs[j]+"p2",-10);                             
           }

//-----


MQL5:
       //if(sym[r]+tfs[t]"p1")
 
       color clrs1 = clrYellow;   //----- set clrRed default
       color clrs2 = clrYellow;   //----- set clrRed default
       color clrs3 = clrYellow;   //----- set clrRed default
 
       if(GlobalVariableGet(symbol+"p1") ==  10)  {clrs1 = clrLime;} else {clrs1 = clrRed;}   //--- reduce else
 
       Display(symbol+"p1",  300+(t*300), 200+(r*100));       
       ObjectSetText(symbol+"p1", "Û",10,"Terminal", clrs1);
 
 
 
       if(GlobalVariableGet(symbol+"p2") ==  10)  {clrs2 = clrLime;} else {clrs2 = clrRed;}   //--- reduce else
 
       Display(symbol+"p2",  330+(t*300), 200+(r*100));       
       ObjectSetText(symbol+"p2", "Û",10,"Terminal", clrs2);
 
 
 
       if(GlobalVariableGet(symbol+"ma") ==  10)  {clrs3 = clrLime;} else {clrs3 = clrRed;}   //--- reduce else
 
       Display(symbol+"ma",  270+(t*300), 200+(r*100));       
       ObjectSetText(symbol+"ma", "Û",10,"Terminal", clrs3);
 

hayseed

Master Trader
Jul 27, 2010
1,206
283
149
usa
migrating from mq4 to mq5......

11. Date and Time Functions ......h

//------

MQL5:
     Comment("alerts dashboard.mq5 emailed  "+ hours()+"  "+minutes());  
 
     if(NewBar() || weekend)
     {  
 
//------  hour and minute are variables set in inputs section
 
    if((hours() == hour) && (minutes() > minute))  {Alert("crtitical time check trades");}    //------ verified
 
//------    
 
//------  https://www.mql5.com/en/articles/81
//------  11. Date and Time Functions
 
int hours()
  {
   MqlDateTime tm;
   TimeCurrent(tm);
   return(tm.hour);
  }
 
 
int minutes()
  {
   MqlDateTime tm;
   TimeCurrent(tm);
   return(tm.min);
  }
//------
 
Last edited:

hayseed

Master Trader
Jul 27, 2010
1,206
283
149
usa
trading against the trend or in highly volatile times adds an unnecessary element of risk......h

//------
//------
Screenshot 2025-02-20 074030.png
 

hayseed

Master Trader
Jul 27, 2010
1,206
283
149
usa
note to me......

1. code a atr triangle projection indicator....... this is something i do manually.....

today is a terrible example because it is working so well.....

first place both horizontal and vertical lines on the days close....... then place rays above and below, about 1% or the daily atr...... extend out 24 hours.....

you will have a chart such as below.....

the idea is the atr is the atr for a reason...... h
//-------

Screenshot 2025-02-26 105505.png
 

hayseed

Master Trader
Jul 27, 2010
1,206
283
149
usa
example of using symbol last to get the last price...... SYMBOL_LAST Price of the last deal......


color symbol relative to last price above/below day open......

a timeframe input might be useful.....h
//------

MQL5:
    for(int r = 0; r < 9; r++)                        
      {
 
//-----    if(iOpen(_Symbol,PERIOD_D1,0) < SymbolInfoDouble(_Symbol,SYMBOL_LAST))  {GlobalVariableSet(_Symbol+"last", 10);}   else {GlobalVariableSet(_Symbol+"last", -10);}
 
 
       color lastclr = clrRed;
       if(GlobalVariableGet(sym[r]+"last") ==  10) {lastclr = clrLime;}
 
       Display(sym[r],  20, 200+(r*100));            
       ObjectSetText(sym[r], sym[r],10,"Verdana", lastclr);

//-----

Screenshot 2025-02-28 115247.png
 
Last edited: