Download MetaTrader 5

Scripts and chart change

To add comments, please log in or register
sd59
509
sd59  

If I run the script below and change charts during it I get the message shown - does anybody know how to get the script to "ignore" a chart change command and just continue?

thanks

void OnStart()
  {
    while(IsStopped() == False)
   {
    .
    . some code
    
    Sleep(200);
   }
  }
whroeder1
16086
whroeder1  
Scripts are to run once and exit. EA's can run continuously. Change it.
sd59
509
sd59  
WHRoeder:
Scripts are to run once and exit. EA's can run continuously. Change it.

OK fair enough.

I would like to change chart timeframe with a delay between changes but the Sleep() function does not work with the code below. Any suggestions please?

thanks

#import "user32.dll"
   int      PostMessageA(int hWnd,int Msg,int wParam,int lParam);
   int      GetWindow(int hWnd,int uCmd);
   int      GetParent(int hWnd);
#import

#define GW_HWNDFIRST 0
#define GW_HWNDNEXT  2
#define WM_COMMAND   0x0111


int intCmd[3] = {33137,33138,33139};
int i=0;

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   int intParent = GetParent( WindowHandle( Symbol(), Period() ) );   
   int intChild = GetWindow( intParent, GW_HWNDFIRST );  
   
   for(i=0;i<3;i++)
   {
   //change timeframe
   PostMessageA( intParent, WM_COMMAND, intCmd[i], 0 );   
   Sleep(3000);
   }
  
   return;
  }
honest_knave
Moderator
2322
honest_knave  
sd59:

OK fair enough.

I would like to change chart timeframe with a delay between changes but the Sleep() function does not work with the code below. Any suggestions please?

thanks

 

Why don't you just use ChartSetSymbolPeriod()?

sd59
509
sd59  
honest_knave:

 

Why don't you just use ChartSetSymbolPeriod()?

thanks for bringing this to my attention I had no idea it existed! It does work - I am trying to change through a few timeframes in a measured way - so with regular pauses between changes. Because the function re-initialises the EA I tried this but it's still a bit clunky. It doesn't change between timeframes smoothly - any ideas please? thanks

int tf[3] = {PERIOD_M1,PERIOD_M15,PERIOD_H4};
int i=0;

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
     ChartSetSymbolPeriod(0,NULL,tf[i]);
   
   if(UninitializeReason() == REASON_CHARTCHANGE)
   {
    i++;
    if(i>=3)i=0;
    Print(i);
    Sleep(3000);
   }
   return;
  }
honest_knave
Moderator
2322
honest_knave  
sd59:

thanks for bringing this to my attention I had no idea it existed! It does work - I am trying to change through a few timeframes in a measured way - so with regular pauses between changes. Because the function re-initialises the EA I tried this but it's still a bit clunky. It doesn't change between timeframes smoothly - any ideas please? thanks

#property strict

int tf[3] = {PERIOD_M1,PERIOD_M15,PERIOD_H4};

int OnInit()
  {
   EventSetTimer(3);
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
   EventKillTimer();
  }

void OnTick()
  {
  }

void OnTimer()
  {
   static int i=0;
   ChartSetSymbolPeriod(0,NULL,tf[i]);   
   i++;
   if(i>=ArraySize(tf)) { i=0; }
  }
sd59
509
sd59  
honest_knave:

Fantastic! Thanks very much!
honest_knave
Moderator
2322
honest_knave  
sd59:
Fantastic! Thanks very much!
You're welcome.
whroeder1
16086
whroeder1  
Your code
Simplified
i++;
if(i>=ArraySize(tf)) { i=0; }
i = (i + 1) % ArraySize(tf);
3 % 3 = 0
sd59
509
sd59  
honest_knave:
You're welcome.

Hi sorry to start this up again but I am struggling to understand the documentation on EventSetTimer to take it a bit further than above. The Timer works fine but it is continuous until stopped. I would like to scroll through some charts, perform some calculations and then let it sleep for a longer period of time - say 15 minutes (or whatever)

The code below doesn't work as the EventSetTimer overrides everything but I think you can see what I am trying to do. Any help appreciated thanks.

#property strict

int tf[3] = {PERIOD_M1,PERIOD_M15,PERIOD_H4};
int nbar=0;

int OnInit()
  {
   EventSetTimer(3);
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
   EventKillTimer();
  }

void OnTick()
  {
  }

void OnTimer()
  {
   if(nbar != ibars(NULL,PERIOD_M15)
   {
   static int i=0;
   ChartSetSymbolPeriod(0,NULL,tf[i]);   

   .
   .
   SOME CALCS HERE
   .
   .
   i++;
   if(i>=ArraySize(tf)) { i=0; }
   }
   nbar = ibars(NULL, PERIOD_M15);
  }
Ex Ovo Omnia
3355
Ex Ovo Omnia  
sd59:

Hi sorry to start this up again but I am struggling to understand the documentation on EventSetTimer to take it a bit further than above. The Timer works fine but it is continuous until stopped. I would like to scroll through some charts, perform some calculations and then let it sleep for a longer period of time - say 15 minutes (or whatever)

The code below doesn't work as the EventSetTimer overrides everything but I think you can see what I am trying to do. Any help appreciated thanks.

Not sure (I never tried) if the static int i=0; keeps its value after the reinit. Can you confirm?

12
To add comments, please log in or register