It's necessary to use iBarShift() for the higher time frame.
ArraySetAsSeries(ExtMaBuffer,false);
ArraySetAsSeries(ExtMaDayBuffer,false);
//--- preliminary calculation
if( prev_calculated > 1 )
{
pos = prev_calculated - 1;
}
else
{
// --- set first candle
ExtMaBuffer[0] = EMPTY_VALUE;
ExtMaDayBuffer[0] = EMPTY_VALUE;
pos=1;
}
int shift;
// Main loop of calculations
for(i=pos; i<rates_total; i++)
{
ExtMaBuffer[i] = iMA(NULL,0, 8, 0, MODE_SMA, PRICE_CLOSE, (rates_total-i-1));
shift = iBarShift(NULL,PERIOD_D1,time[i]);
ExtMaDayBuffer[i] = iMA(NULL,PERIOD_D1, 20, 0, MODE_SMA, PRICE_CLOSE, shift);
}
- You must use iBarShift. Don't mix apples and oranges.
- If you count down and use series buffers you wouldn't have to use rates_total-1 - i just i. But either way, you still have to Do your lookbacks
correctly.
First of all, great thanks for your answers. My "Daily MMA20" on a "M1 Period Chart" works !
- You must use iBarShift. Don't mix apples and oranges.
- If you count down and use series buffers you wouldn't have to use rates_total-1 - i just i. But either way, you still have to Do your lookbacks
correctly.
@whroeder1, I really appreciate your point 2. When I studied iCustom 3 months ago, each custom indicator I saw had different OnCalculate body. Really, there is plenty of examples, from ~2007 to today, while/for(;;i++)/for(;;i--), even mql4 examples are differents each other. So, I decided to take recent one, to understand it, test it, and use it. It's kind of example you see in my first post.
The post (lookbacks correctly) refer also some methods, can you advise me one ? One that will be considered as good practice. Can you modify my first code to show it ?
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
ExtMaDayBuffer => My try of DAILY MMA
//| Moving Average ChartPeriod, DayPeriod.mq4 |
//+------------------------------------------------------------------+
#property copyright ""
#property link ""
#property version "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 2
double ExtMaBuffer[]; // Chart Moving Average
double ExtMaDayBuffer[]; // Day Moving Average
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//|------------------------------------------------------------------|
void OnInit(void)
{
IndicatorShortName("Moving Average ChartPeriod, DayPeriod");
IndicatorDigits(4);
// Lines
SetIndexStyle(0,DRAW_LINE,0,4,clrYellow);
SetIndexBuffer(0,ExtMaBuffer);
SetIndexStyle(1,DRAW_LINE,0,4,clrBlue);
SetIndexBuffer(1,ExtMaDayBuffer);
// Labels
SetIndexLabel(0,"Moving Average Chart");
SetIndexLabel(1,"Moving Average Day");
// DrawBegin
SetIndexDrawBegin(0,10);
SetIndexDrawBegin(1,10);
// Buffers
SetIndexBuffer(0,ExtMaBuffer);
SetIndexBuffer(1,ExtMaDayBuffer);
}
//+------------------------------------------------------------------+
//| Main |
//+------------------------------------------------------------------+
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[])
{
int pos, i;
if( rates_total <= 10 )
{
return(0);
}
// ArraySetAsSeries
ArraySetAsSeries(ExtMaBuffer,false);
ArraySetAsSeries(ExtMaDayBuffer,false);
//--- preliminary calculation
if( prev_calculated > 1 )
{
pos = prev_calculated - 1;
}
else
{
// --- set first candle
ExtMaBuffer[0] = EMPTY_VALUE;
ExtMaDayBuffer[0] = EMPTY_VALUE;
pos=1;
}
// Main loop of calculations
for(i=pos; i<rates_total; i++)
{
ExtMaBuffer[i] = iMA(NULL,0, 8, 0, MODE_SMA, PRICE_CLOSE, (rates_total-i-1));
ExtMaDayBuffer[i] = iMA(NULL,PERIOD_D1, 20, 0, MODE_SMA, PRICE_CLOSE, (rates_total-i-1));
}
return(rates_total);
}
//+------------------------------------------------------------------+
I tried some search on websites/forum, but I did not found this kind of situation.
Thanks for help !