Creating New indicator from MA data

To add comments, please log in or register
Crays Loh
24
Crays Loh  

Dear all,

I hope you guys can point me in the right direction, i've search for quite sometime and trying to understand how do i use the data i got from Median price of a Moving Average Indicator. Yet to no avail.

I'm trying to get a Median Price Moving Average Indicator (successfully) and using the current bar price minus the previous bar price to get a difference in pricing chart (indicator).

But all i can do is only getting to Median Price Moving Average Indicator, i'm not sure how i can use that data in a timeseries format to gain what i had in mind, which is using current bar price - previous bar price.


Here is my code

//+------------------------------------------------------------------+
//|                                              TestCopyBuffer1.mq5 |
//|                        Copyright 2009, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "2009, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
 
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
//---- plot MA
#property indicator_label1  "MA"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- input parameters
input bool               AsSeries=true;
input int                period=1;
input ENUM_MA_METHOD     smootMode=MODE_SMA;
input ENUM_APPLIED_PRICE price=PRICE_MEDIAN;
input int                shift=0;
//--- indicator buffers
double                   MABuffer[];
int                      ma_handle;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   if(AsSeries) ArraySetAsSeries(MABuffer,true);
   Print("Indicator buffer is timeseries = ",ArrayGetAsSeries(MABuffer));
   SetIndexBuffer(0,MABuffer,INDICATOR_DATA);
   SetIndexBuffer(0,MABuffer,INDICATOR_DATA);
   Print("Indicator buffer after SetIndexBuffer() is timeseries = ",
         ArrayGetAsSeries(MABuffer));
   
//--- change the order of accessing elements of the indicator buffer
   ArraySetAsSeries(MABuffer,AsSeries);
   
   IndicatorSetString(INDICATOR_SHORTNAME,"MA("+period+")"+AsSeries);
//---
   ma_handle=iMA(Symbol(),0,period,shift,smootMode,price);
   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[])
  {
//--- Copy the values of the moving average in the buffer MABuffer
   int copied=CopyBuffer(ma_handle,0,0,rates_total,MABuffer);
 
   Print("MABuffer[0] = ",MABuffer[0]);// Depending on the value AsSeries
                                      // Will receive a very old value
                                      // Or for the current unfinished bar
 
//--- return value of prev_calculated for next call

   return(rates_total);
  }
//+------------------------------------------------------------------+

Your time is much appreciated!

To add comments, please log in or register