Custom indicator MA on different timeframe

 

Hello all

I am trying to show the daily 13MA direction on a 1 Hour timeframe chart in a separate window using the coding as below.


//--------------------------------------------------- 1 ---------------------------------------------------------------------------------------
//DECLARING INDICATOR PROPERTIES
#property indicator_separate_window //Drawn in separate window
#property indicator_buffers 3 //Number of Buffers

#property indicator_color2 LimeGreen //Colour of long indication
#property indicator_color3 OrangeRed //Colour of short indication

#property indicator_width2 4 //Line width of long indication
#property indicator_width3 4 //Line width of short indication

#property indicator_minimum -1.1 //Minimum height of indicator window
#property indicator_maximum 1.1 //Maximum height of indicator window

//--------------------------------------------------- 2 ---------------------------------------------------------------------------------------
double ind[]; //DECLARING VARIABLES USED
double ind_long[];
double ind_short[];
int previous;
//--------------------------------------------------- 3 ---------------------------------------------------------------------------------------
void init() //Special function init()
{
IndicatorBuffers(3); //Number of buffer required for indicator
SetIndexBuffer(0, ind); //Assigning array 'ind' to buffer 0
SetIndexStyle(0, DRAW_NONE); //Line style to buffer 0
SetIndexBuffer(1, ind_long); //Assigning array 'ind' to buffer 1
SetIndexStyle(1, DRAW_HISTOGRAM); //Line style to buffer 1
SetIndexBuffer(2, ind_short); //Assigning array 'ind' to buffer 2
SetIndexStyle(2, DRAW_HISTOGRAM); //Line style to buffer 2
previous = 0; //Resetting variable 'previous'
}
//--------------------------------------------------- 4 ---------------------------------------------------------------------------------------
int start() // Special function start()
{
datetime TimeArray[];
int counted_bars=IndicatorCounted();
int limit;
int i;
int y=0;

ArrayCopySeries(TimeArray,MODE_TIME,Symbol(),PERIOD_D1);

limit=Bars-counted_bars;
for(i=0,y=0;i<limit;i++)
{
if (Time[i]<TimeArray[y]) y++;
{
double MA_1_Day_Ago = iMA(NULL,PERIOD_D1,13,0,MODE_SMA,0,i+1);
double MA_2_Day_Ago = iMA(NULL,PERIOD_D1,13,0,MODE_SMA,0,i+2);

//--------------------------------------------------- 5 ---------------------------------------------------------------------------------------
// Initialize Latest Buffer Value
ind[i] = 0.0;
ind_long[i] = 0.0;
ind_short[i] = 0.0;
//--------------------------------------------------- 6 ---------------------------------------------------------------------------------------
// Long Signal
if ((MA_1_Day_Ago > MA_2_Day_Ago))
{
ind[i] = 1;
ind_long[i] = 1;
previous = 1;
}
//--------------------------------------------------- 7 ---------------------------------------------------------------------------------------
// Short Signal
if ((MA_1_Day_Ago < MA_2_Day_Ago))
{
ind[i] = -1;
ind_short[i] = -1;
previous = -1;
}
//--------------------------------------------------- 8 ---------------------------------------------------------------------------------------
// Reset previous if neccesary
if ((MA_1_Day_Ago < MA_2_Day_Ago) && previous == 1)
{
previous = 0;
}
if ((MA_1_Day_Ago > MA_2_Day_Ago) && previous == -1)
{
previous = 0;
}
}
}
//--------------------------------------------------- 9 ---------------------------------------------------------------------------------------
}
return(0);

I am see the that the coding is not finding the correct daily MA value on the correct time.

Can anyone help with this problem?

 
Hello! Try like this: I dont know if it is the same, but it should be.
double MA_1_Day_Ago = iMA(NULL,0,780,0,MODE_SMA,0,i+1);
 
01005379:
Hello! Try like this: I dont know if it is the same, but it should be.

Hello 01005379

Thank you for suggestion,

I changed the coding with your suggestion, although there were some changes, I'm still not getting the correct data in the indicator window.

 
ima(Symbol(),PERIOD_D1,MA_period, MA_mode,MA_shift,MA_price,iBarsShift(Symbol,PERIOD_D1,Time[0],false));
//z
 

Hello zzuegg

With your suggestion

I replaced the following coding,

double MA_1_Day_Ago = iMA(NULL,PERIOD_D1,13,0,MODE_SMA,0,(NULL,PERIOD_D1,Time[0],i+1));
double MA_2_Day_Ago = iMA(NULL,PERIOD_D1,13,0,MODE_SMA,0,(NULL,PERIOD_D1,Time[0],i+2));


but was unable to compile the program, it giving a error of ('=' - no lvalue present) which I have no idea what it means.


Have I applied your suggestion correctly?

 

try this...

double MA_1_Day_Ago = iMA(NULL,PERIOD_D1,13,0,MODE_SMA,0,iBarsShift(NULL,PERIOD_D1,Time[i],false)+1);
double MA_2_Day_Ago = iMA(NULL,PERIOD_D1,13,0,MODE_SMA,0,iBarsShift(NULL,PERIOD_D1,Time[i],false)+2);
 
zzuegg:

try this...


Hello zzuegg


Thank you, it now works


I made one amendment, that being, I changed (iBarsShift) to (iBarShift).


Thank you again.

Reason: