Where to set time frame for indicator handle

 
int OnInit()
  {
//--- Get handle for KD indicator
   KDHandle=iStochastic(NULL,0,K_Period,D_Period,3,MODE_SMA,STO_LOWHIGH);
//--- Get the handle for Moving Average indicator
//   maHandle=iMA(_Symbol,_Period,MA_Period,0,MODE_EMA,PRICE_CLOSE);
//--- What if handle returns Invalid Handle
   if(KDHandle<0)
     {
      Alert("Error Creating Handles for indicators - error: ",GetLastError(),"!!");
      return(-1);
     }

.....
void OnTick()
  {

//---if want to set another time frame for KDHandle, can put code here inside OnTick()???
if last_loss<-1000
KDHandle=iStochastic(NULL,time_frame,K_Period,D_Period,3,MODE_SMA,STO_LOWHIGH);

...

//--- Do we have enough bars to work with
   if(Bars(_Symbol,_Period)<60) // if total bars is less than 60 bars
     {
      Alert("We have less than 60 bars, EA will now exit!!");
      return;
     }

 

But if add code inside OnTick(), every new tick happens, the handle will renew a new value not like inside OnInit(). Is it right for changing time fram for indicator handle?

MQL5.community - User Memo
MQL5.community - User Memo
  • 2010.02.25
  • MetaQuotes Software Corp.
  • www.mql5.com
You have just registered and most likely you have questions such as, "How do I insert a picture to my a message?" "How do I format my MQL5 source code?" "Where are my personal messages kept?" You may have many other questions. In this article, we have prepared some hands-on tips that will help you get accustomed in MQL5.community and take full advantage of its available features.
 
king1898:

But if add code inside OnTick(), every new tick happens, the handle will renew a new value not like inside OnInit(). Is it right for changing time fram for indicator handle?

In general, it's better to have handle(s) initialized 1 time in OnInit(). If you need data of an other timeframe, simply add a new handle. You need an handle for each set of parameters (symbol, timeframe, indicator parameters) you need data from.
 
angevoyageur:
In general, it's better to have handle(s) initialized 1 time in OnInit(). If you need data of an other timeframe, simply add a new handle. You need an handle for each set of parameters (symbol, timeframe, indicator parameters) you need data from.

you mean i can add below code inside OnTick() to replace KDHandle that set in OnInit(), right?

KDHandle=iStochastic(NULL,time_frame,K_Period,D_Period,3,MODE_SMA,STO_LOWHIGH);

 

 
king1898:

you mean i can add below code inside OnTick() to replace KDHandle that set in OnInit(), right?

KDHandle=iStochastic(NULL,time_frame,K_Period,D_Period,3,MODE_SMA,STO_LOWHIGH);

 

but if add in OnTick(), when every new tick happens the KDHandle will be set on time, it is not good for this action on performance? Is there any problem? Thanks for your reply.
 
king1898:

you mean i can add below code inside OnTick() to replace KDHandle that set in OnInit(), right?

KDHandle=iStochastic(NULL,time_frame,K_Period,D_Period,3,MODE_SMA,STO_LOWHIGH);

 

No.

Forum

Where to set time frame for indicator handle

angevoyageur, 2013.11.11 12:51

In general, it's better to have handle(s) initialized 1 time in OnInit(). If you need data of an other timeframe, simply add a new handle. You need an handle for each set of parameters (symbol, timeframe, indicator parameters) you need data from.

 

Yes, i understand your answer and write below code. Thank you very muh!

int OnInit()
  {
//--- Get handle for KD indicator
   KDHandle1=iStochastic(NULL,PERIOD_M5,K_Period,D_Period,3,MODE_SMA,STO_LOWHIGH);
   KDHandle2=iStochastic(NULL,PERIOD_H1,K_Period,D_Period,3,MODE_SMA,STO_LOWHIGH);

 But new question is coming, when the time frame is set to 1 hour in EA but the chart also is in 5 mintues and we have two KD handles so when we open order it will open two order both in 5 mintues and 1 hour, OnTick() is by every tick in 5 minutes or in 1 hour?

 
king1898:

Yes, i understand your answer and write below code. Thank you very muh!

 But new question is coming, when the time frame is set to 1 hour in EA but the chart also is in 5 mintues and we have two KD handles so when we open order it will open two order both in 5 mintues and 1 hour, OnTick() is by every tick in 5 minutes or in 1 hour?

Maybe you can try the Chinese section of the forum, as your question is not understandable.