//+------------------------------------------------------------------+ //| PMO_logger.mq5 | //| Copyright 2019, Marco Calabrese | //| https://marcocalabrese.eu/algotrading | //+------------------------------------------------------------------+ #property copyright "Copyright 2019,MarcoCalabrese " #property link "https://marcocalabrese.eualgotrading" #property version "1.00" #property strict #property indicator_separate_window #property indicator_buffers 3 #property indicator_plots 1 //--- plot Histogram #property indicator_label1 "PMO" #property indicator_type1 DRAW_HISTOGRAM #property indicator_color1 clrWhite #property indicator_style1 STYLE_SOLID #property indicator_width1 1 //--- input parameters input datetime startingTime; input int MA_close=3; input int MA_open=21; input bool data_logging = true; input string filename="PMO_test.csv"; //--- indicator buffers double PMOBuffer[]; double closeBuffer[]; double openBuffer[]; //----global vars--- int counter=1; double sum_close=0; double sum_open=0; bool first_val_checked; int first_valid_index; int file_handle=data_logging?FileOpen(filename,FILE_READ|FILE_WRITE|FILE_CSV,';'):-1; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { string title = "PMO("+TimeToString(startingTime)+","+IntegerToString(MA_close)+","+IntegerToString(MA_open)+")"; string shorterTitle="PMO("+IntegerToString(MA_close)+","+IntegerToString(MA_open)+")"; //--- indicator buffers mapping SetIndexBuffer(0,PMOBuffer); SetIndexBuffer(1,closeBuffer); SetIndexBuffer(2,openBuffer); PlotIndexSetString(0,PLOT_LABEL,shorterTitle); IndicatorSetString(INDICATOR_SHORTNAME,title); IndicatorSetInteger(INDICATOR_DIGITS,8); //--- return(INIT_SUCCEEDED); } void OnDeinit(const int reason) { //--- data_logging? FileClose(file_handle):; } //+------------------------------------------------------------------+ //| 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[]) { //--- ArraySetAsSeries(PMOBuffer,true); ArraySetAsSeries(closeBuffer,true); ArraySetAsSeries(openBuffer,true); ArraySetAsSeries(open,true); ArraySetAsSeries(high,true); ArraySetAsSeries(time,true); //--- return value of prev_calculated for next call int total = rates_total - prev_calculated; double _tmp_sum_close,_tmp_sum_open; if(total > 0){ for(int i=total-1;i>=0;i--){ if(time[i] >= startingTime){ if(first_val_checked == false){ first_val_checked = true; first_valid_index = i; if(data_logging){ FileWrite(file_handle,"Time","Close","Open","Counter","CloseBuffer","OpenBuffer","PMOBuffer"); } } sum_close += close[i]; sum_open += open[i]; closeBuffer[i] = close[i] *counter / sum_close; openBuffer[i] = open[i]*counter / sum_open; PMOBuffer[i] = simpleMA(i,MA_close,closeBuffer,first_valid_index)-simpleMA(i,MA_open,openBuffer,first_valid_index); if(data_logging){ FileWrite(file_handle,time[i],close[i],open[i],counter,closeBuffer[i],openBuffer[i],PMOBuffer[i]); }; counter++; } else{ PMOBuffer[i] = 0; } } }else{ _tmp_sum_close = sum_close +close[0]; _tmp_sum_open = sum_open + open[0]; closeBuffer[0] = close[0] *counter / _tmp_sum_close; openBuffer[0] = open[0] *counter / _tmp_sum_open; PMOBuffer[0] = simpleMA(0,MA_close,closeBuffer,first_valid_index)-simpleMA(0,MA_open,openBuffer,first_valid_index); } return(rates_total); } //+------------------------------------------------------------------+ double simpleMA(const int pos, const int avg_positions, const double &data[],int arr_tail_pos){ double _local_sum = 0; for(int i=pos+avg_positions-1; i >= pos;i--){ if(i > arr_tail_pos){ _local_sum += 1; // when requested data exceed buffer limit set trailing 1s }else{ _local_sum += data[i]; } } return _local_sum/avg_positions; }