Download MetaTrader 5

Prevent EA from getting restarted every time when switched to another time period - page 2

To add comments, please log in or register
gvdraganov
33
gvdraganov  

Finally done with my first code ever written and after a week of work, it finally does everything I wanted it to.. many many thanks for your help! Should we not talk again in the next days, merry Christmas!


If you are interested, here's the final code.


#include <WinUser32.mqh>
#import "user32.dll"
#import
#define N_minutes 60000
extern int templateIndex = 0;

string Charts[] = {"EURUSD", "GBPUSD", "USDJPY"};
int Periods[] = {PERIOD_M5, PERIOD_M15, PERIOD_M30,
                 PERIOD_H1, PERIOD_H4, PERIOD_D1,  PERIOD_W1,
                 PERIOD_MN1};
double MACD;
double RSI;
int i=0,limit=100;

int start()
     {
         for(int N=0;N<3;N++)
         {
            for(int M=0;M<8;M++)
               {
                 string Period_ID = 0;
                 switch(Periods[M])
                  {
                      case PERIOD_MN1: Period_ID = "MN1"; break;
                      case PERIOD_W1:  Period_ID = "W1"; break;
                      case PERIOD_D1:  Period_ID = "D1"; break;
                      case PERIOD_H4:  Period_ID = "H4"; break;
                      case PERIOD_H1:  Period_ID = "H1"; break;
                      case PERIOD_M30: Period_ID = "M30"; break;
                      case PERIOD_M15: Period_ID = "M15"; break;
                      case PERIOD_M5:  Period_ID = "M5"; break;
                  }
                  MACD = iMACD(Charts[N],Periods[M],12,26,9,PRICE_CLOSE,MODE_MAIN,0);
                  RSI = iRSI(Charts[N],Periods[M],14,PRICE_CLOSE,0);
                  if(RSI>70)
                     {
                        if(WindowHandle(Charts[N],Periods[M])>0)
                           {
                              long currChart,prevChart=ChartFirst();
                              while(i<limit)
                                 {
                                    if(ChartSymbol(prevChart)==Charts[N] && ChartPeriod(prevChart)==Periods[M])
                                       {
                                          PostMessageA(WindowHandle(Charts[N],Periods[M]),WM_COMMAND,34800+templateIndex,0);
                                          ChartSetInteger(prevChart,CHART_BRING_TO_TOP,2,true);
                                          Alert("Chart already opened: ",Charts[N],", ",Period_ID);
                                       }
                                    currChart=ChartNext(prevChart);
                                    if(currChart<0)
                                    {
                                    break;
                                    }
                                    prevChart=currChart;
                                    i++;
                                 }
                           }
                        else
                           {  
                              long IDChart = ChartOpen(Charts[N],Periods[M]);
                              PostMessageA(WindowHandle(Charts[N],Periods[M]),WM_COMMAND,34800+templateIndex,0);
                              Alert("New chart: ",Charts[N],", ",Period_ID);
                           }
                     }
               }
         }
         Alert("Finished");
         Sleep(5*N_minutes);
         return;
     }
Carl Schreiber
7109
Carl Schreiber  

Thanks for posting the result!

But are you sure PostMessageA() is working? We had to replace all the Windows'-..A()-functions (ASCII) by ..W()-functions (Wide, ANSI)?

gvdraganov
33
gvdraganov  
Well, it seems to work for me.. so far I couldn't recreate a situation in which it didn't work and I have tested it extensively.
whroeder1
15589
whroeder1  
You converted Periods to a constant initialized array. You could do the same with PeriodsToText
int Periods[] = {PERIOD_M5, PERIOD_M15, PERIOD_M30,
                 PERIOD_H1, PERIOD_H4PERIOD_D1
                 PERIOD_W1, PERIOD_MN1};           




:                                                  
string Period_ID =
0;
switch(Periods[M])
{
   case PERIOD_MN1: Period_ID = "MN1"; break;
   case PERIOD_W1:  Period_ID = "W1"; break;
   case PERIOD_D1:  Period_ID = "D1"; break;
   case PERIOD_H4:  Period_ID = "H4"; break;
   case PERIOD_H1:  Period_ID = "H1"; break;
   case PERIOD_M30: Period_ID = "M30"; break;
   case PERIOD_M15: Period_ID = "M15"; break;
   case PERIOD_M5:  Period_ID = "M5"; break;
}
int Periods[] = {PERIOD_M5, PERIOD_M15, PERIOD_M30,
                 PERIOD_H1, PERIOD_H4PERIOD_D1
                 PERIOD_W1, PERIOD_MN1};           

string PeriodsToText[] = {"M5", "M15", "M30",
                          "H1", "H4""D1",
                          "W1", "MN1"};
:                                                  
string Period_ID =
PeriodsToText[M];











12
To add comments, please log in or register