close and open metatrader
//+------------------------------------------------------------------+ //| Test 4.mq4 | //| Parham.PM | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Parham.trader" #property link "https://www.mql4.com" #property version "1.00" #property strict #property indicator_chart_window datetime last_recognized_candle_time; int k; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { last_recognized_candle_time=iTime(Symbol(),0,0); k=1; ObjectCreate(StringConcatenate("x",k),OBJ_VLINE,0,iTime(Symbol(),0,k),0); //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) { //--- if(last_recognized_candle_time!=iTime(Symbol(),0,0)) { k=0; ObjectsDeleteAll(); OnInit(); return(rates_total); } if(k<5) { k++; ObjectCreate(StringConcatenate("x",k),OBJ_VLINE,0,iTime(Symbol(),0,k),0); } //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- ObjectsDeleteAll(); } //+------------------------------------------------------------------+
Guarding just time of the last candle is not a safe detection, compare rather the prev_calculated parameter.
Guarding just time of the last candle is not a safe detection, compare rather the prev_calculated parameter.
I google it and found it out ,
As you said, using perv_calculated was actually the most suitable solution for this issue.
Thank you again.
what if I want to use the prev_calculated parameters only when the Metatrader terminal opens not the time when I change the timeframe. consider the following code:
//+------------------------------------------------------------------+ //| Test 4.mq4 | //| Parham.PM | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Parham.trader" #property link "https://www.mql4.com" #property version "1.00" #property strict #property indicator_chart_window int k; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { k=1; ObjectCreate(StringConcatenate("x",k),OBJ_VLINE,0,iTime(Symbol(),0,k),0); //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) { //--- if(prev_calculated==0) { k=0; ObjectsDeleteAll(); OnInit(); return(rates_total); } if(k<5) { k++; ObjectCreate(StringConcatenate("x",k),OBJ_VLINE,0,iTime(Symbol(),0,k),0); } //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- ObjectsDeleteAll(); } //+------------------------------------------------------------------+
As you can see above, the prev_calculated==0 would be true when I change the chart timeframe or close and reopen the metatrader application.
Is there any way to code it in a way to make prev_calculated==0 to be true only if I open the metatrader not when I change the chart timeframe?
Thanking in advance,
hi friends,
I faced with a problem that my written indicator works well when the metatrader is open and it works accurately. But when I close the metatrader and reopen it after a while, my indicator does not examine the newly added candles that are created when the metatrader was off, but as I reset my indicator by changing the timeframe or remove and reattach my indicator to the chart it again works properly and calculate the written codes on the newly added candles.
I'd appreciate if anyone could help me how can I solve this problem.
thanking in advance