Create Excel CSV - Price deviations from EMA Band

 

Hi,

I was hoping someone could point me in the direction of where to read up on this or if it is even possible but essentially I want to create an ema of lets say 21 period. Then run a test using strategy tester and figure out the average pip deviation from the band over a 2/3 year period.

I understand the programming logic behind it. I could essentially do something like three separate arrays and have the tick close value inputted as a value and the ema value for that tick inputted in the other one. Then run a basic loop to figure out the price/pip difference between index position and  then input that in the third array.

There may be better ways to do this so please feel free to let me know about that as well. However the rough logic I understand but I do not know how to export this to a CSV value in order to run analysis on the results I generate.

Any further information required, please let me know.

Thanks

 
Your topic has been moved to the section: Expert Advisors and Automated Trading — In the future, please consider which section is most appropriate for your query.
 
notreallyolivertwist:

Hi,

I was hoping someone could point me in the direction of where to read up on this or if it is even possible but essentially I want to create an ema of lets say 21 period. Then run a test using strategy tester and figure out the average pip deviation from the band over a 2/3 year period.

I understand the programming logic behind it. I could essentially do something like three separate arrays and have the tick close value inputted as a value and the ema value for that tick inputted in the other one. Then run a basic loop to figure out the price/pip difference between index position and  then input that in the third array.

There may be better ways to do this so please feel free to let me know about that as well. However the rough logic I understand but I do not know how to export this to a CSV value in order to run analysis on the results I generate.

Any further information required, please let me know.

Thanks

You don't need the tester for that .

You can go back in bars and for each bar that has :

  • your ema data for its previous bar
  • your ema data for that bar
  • ohlc data for that bar

export the values .

If you don't know what analysis you could do , an idea is to have a column with ema deviation (open deviation to previous ema , and close deviation to current ema ,because ema you see is at the close time of the bar) . Then multiple columns spanning following bars , i.e. bar+1 bar+2 etc .

Then you could derive stats that show you -for example- that anytime  the open is above the previous ema between a range then the following 2 bars are bullish or smth.


 
notreallyolivertwist: I want to create an ema of lets say 21 period. Then run a test using strategy tester and figure out the average pip deviation from the band

Until you can state your requirements in concrete terms, it can not be coded.

An EMA is line, not a band.

 
Lorentzos Roussos #:

You don't need the tester for that .

You can go back in bars and for each bar that has :

  • your ema data for its previous bar
  • your ema data for that bar
  • ohlc data for that bar

export the values .

If you don't know what analysis you could do , an idea is to have a column with ema deviation (open deviation to previous ema , and close deviation to current ema ,because ema you see is at the close time of the bar) . Then multiple columns spanning following bars , i.e. bar+1 bar+2 etc .

Then you could derive stats that show you -for example- that anytime  the open is above the previous ema between a range then the following 2 bars are bullish or smth.


Literally what I am trying to do. Do you perhaps have any examples on the forums of that? I have tried something but it does not output the data to a csv file. Additionally if I do not use the tester. What should I use in it's place then?

Thanks
 

Don't double post! You already had another thread open.

          General rules and best pratices of the Forum. - General - MQL5 programming forum (2017)
 
William Roeder #:

Don't double post! You already had another thread open.

          General rules and best pratices of the Forum. - General - MQL5 programming forum (2017)
Received a posting error when I initially posted this. Hence I did not think it had posted. However my issue is no longer on how to export but why does this not work as I found how to export.
 
notreallyolivertwist #: Received a posting error when I initially posted this.

Which was three (3) days before your second post. I don't beleive that.

 
William Roeder #:

Which was three (3) days before your second post. I don't beleive that.

Believe what you want. It is what it is and I got busy coding my EA over the weekend using libraries documents and only ran into an issue today again. Thus attempting to repost. I have had multiple issue today alone just trying to reply to a message to the point of having to repeatedly reload the page. So I know for a fact that it is true :)

However what is more important is that I am asking for help and not here to argue.

 
notreallyolivertwist #:
Literally what I am trying to do. Do you perhaps have any examples on the forums of that? I have tried something but it does not output the data to a csv file. Additionally if I do not use the tester. What should I use in it's place then?

Thanks

You place your exporter on the chart , you make sure it loads the data you want , then cautiously per bar check if the data is there and if it is write it to a csv file (as i can read you just managed to export to a csv type)

I'll post an example here later as i'm bored.

#property copyright "Read the discussion"
#property link      "https://www.mql5.com/en/forum/449510"
//loader settings 
  input int BarsToLoadMinimum=1000;//Bars to load minimum
  input int MaxAttempts=100;//Max Attempts at Minimum bars
  input bool BounceIfNotMin=true;//Bounce if the min is not met
//ema 
  input int MaPeriod=30;//Ma period
  input ENUM_MA_METHOD MaMethod=MODE_EMA;//Ma method
  input ENUM_APPLIED_PRICE MaPrice=PRICE_CLOSE;//Ma price
bool Loaded=false;
bool Busy=false;
int Attempts=0;
int Load=0;
int OnInit()
  {
  Loaded=false;
  Busy=false;
  Attempts=0;
  Load=0;
  EventSetMillisecondTimer(44);
  return(INIT_SUCCEEDED);
  }
void OnDeinit(const int reason)
  {
  EventKillTimer();
  }
void OnTick(){}
void OnTimer(){
//if not busy
if(!Busy)
{
Busy=true;
//-----------------------
 
  //if not loaded
  if(!Loaded){
  int valids=checkBarValidity(BarsToLoadMinimum);
   Comment("Attempt ("+IntegerToString(Attempts)+")Valids("+IntegerToString(valids)+")");
  if(valids>=(BarsToLoadMinimum-1)){
    Load=BarsToLoadMinimum;
    Loaded=true;
    }else{
    Attempts++;
    if(Attempts>MaxAttempts){
      //and we accept it 
        if(!BounceIfNotMin){
          if(valids>0){
          Load=valids;
          Loaded=true;
          }else{
          Alert("0 valid bars ");
          ExpertRemove();
          }
          }else{
          Alert("Cannot load min required bars");
          ExpertRemove();
          }
      }
    }  
  }
  //if loaded
  else{
  //stop the clock
  EventKillTimer();
  //create the indicator handle 
    int EMAHandle=iMA(_Symbol,_Period,MaPeriod,0,MaMethod,MaPrice);
    if(EMAHandle!=INVALID_HANDLE){
    double mas[];
    MqlRates rates[];
    int copy_ma=CopyBuffer(EMAHandle,0,0,Load,mas);
    int copy_ra=CopyRates(_Symbol,_Period,0,Load,rates);
    if(copy_ma>0&&copy_ra>0){
    int ma_offset=0,ra_offset=0;//offsets in array access
    int total=copy_ma;
    if(copy_ra<copy_ma){total=copy_ra;ra_offset=copy_ma-copy_ra;}
    else if(copy_ma<copy_ra){total=copy_ma;ma_offset=copy_ra-copy_ma;}
    //open csv file 
      string tab="\t";
      int f=FileOpen("output.csv",FILE_WRITE|FILE_TXT);
    //loop in data 0 is the oldest remember 
      FileWriteString(f,"Time"+tab+"Open"+tab+"High"+tab+"Low"+tab+"Close"+tab+"PrevMA"+tab+"ThisMA"+tab+"\n");
      for(int i=1;i<total;i++){
      string line=DoubleToString(rates[i+ra_offset].open,_Digits)+tab;
             line+=DoubleToString(rates[i+ra_offset].high,_Digits)+tab;
             line+=DoubleToString(rates[i+ra_offset].low,_Digits)+tab;
             line+=DoubleToString(rates[i+ra_offset].close,_Digits)+tab;
             line+=DoubleToString(mas[i+ma_offset-1],_Digits)+tab;
             line+=DoubleToString(mas[i+ma_offset],_Digits)+tab+"\n";
             StringReplace(line,".",",");
      FileWriteString(f,TimeToString(rates[i+ra_offset].time,TIME_DATE|TIME_MINUTES)+tab+line);
      }
      FileClose(f);
      Alert("Done");
    //loop in data 
    }else{Alert("Error");}
    IndicatorRelease(EMAHandle);
    ExpertRemove();
    }else{
    Alert("Cannot create indicator");
    ExpertRemove();
    }
  }
  
//-------------------------------------
Busy=false;
}

}


int checkBarValidity(int min){
int valids=0;
MqlRates rates[];
int copied=CopyRates(_Symbol,_Period,0,min,rates);
//maximum allowed gap in seconds 
  int seconds_gap=60*60*24*7;//week
//if the period is bigger than the max gap set it 
  if(_Period>seconds_gap){seconds_gap=_Period;}
if(copied>0){
for(int i=1;i<copied;i++){
   int seconds_difference=((int)rates[i].time)-((int)rates[i-1].time);
   if(seconds_difference>seconds_gap){
     break;
     }else{
     valids++;
     }
   }
}
return(valids);
}
 
Lorentzos Roussos #:

You place your exporter on the chart , you make sure it loads the data you want , then cautiously per bar check if the data is there and if it is write it to a csv file (as i can read you just managed to export to a csv type)

I'll post an example here later as i'm bored.

Thanks for taking the time to respond. I really appreciate it

Reason: