Please use this to post code . . . it makes it easier to read.
Sorry, my bad - code reposted below:
#property indicator_separate_window #property indicator_buffers 2 #property indicator_color1 Yellow #property indicator_color2 Orange //---- buffers double hbuffer1[]; double lbuffer1[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { //---- indicators SetIndexStyle(0,DRAW_LINE); SetIndexBuffer(0,hbuffer1); SetIndexStyle(0,DRAW_LINE); SetIndexBuffer(1,lbuffer1); string short_name = "Delta is running!"; IndicatorShortName(short_name); //---- return(1); } //+------------------------------------------------------------------+ //| Custor indicator deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int start() { int OSMAlookback=5; double OSMA[5]; double OSMAh,OSMAl; for(int i = 0; i < OSMAlookback; i++) { OSMA[i]=iOsMA(NULL,NULL,12,26,9,PRICE_CLOSE,i)*10000; int dh=ArrayMaximum(OSMA) ; int dl=ArrayMinimum(OSMA) ; OSMAh=(iOsMA(NULL,NULL,12,26,9,PRICE_CLOSE,0)*10000)-OSMA[dh]; OSMAl=(iOsMA(NULL,NULL,12,26,9,PRICE_CLOSE,0)*10000)-OSMA[dl]; hbuffer1[i]= OSMAh ; lbuffer1[i]= OSMAl ; } //---- //---- return(0); } //+------------------------------------------------------------------+
Hi
Thanks for your reply - so I need another loop... I tried the following (below) but is still repainting, although I guess I'm still referencing the same value so no change. Any chance you suggest how I can take the value from the buffer and stop it changing/recalculating on every new tick?
Also I'm not sure if the loop's even necessary, but I tried calculating six seperate OSMA values (i,i+1,i+2...) ie.without a loop, then filling an array with those values and using ArrayMaximum on the array, but no joy there either, although this could quite possibly have been my childlike prodgramming :) - Even so, might this be an easier path to pursue?
Thanks.
//+------------------------------------------------------------------+ //| MACD delta5 NEW.mq4 | //| Genius | //| | //+------------------------------------------------------------------+ #property copyright "Genius" #property link "" #property indicator_separate_window #property indicator_buffers 2 #property indicator_color1 Yellow #property indicator_color2 Orange //---- buffers double hbuffer1[]; double lbuffer1[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { //---- indicators SetIndexStyle(0,DRAW_LINE); SetIndexBuffer(0,hbuffer1); SetIndexStyle(0,DRAW_LINE); SetIndexBuffer(1,lbuffer1); string short_name = "Delta is running!"; IndicatorShortName(short_name); //---- return(1); } //+------------------------------------------------------------------+ //| Custor indicator deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int start() { int OSMAlookback=5; double OSMA[5]; double OSMAh,OSMAl; for(int i = 0; i < OSMAlookback; i++) { OSMA[i]=iOsMA(NULL,NULL,12,26,9,PRICE_CLOSE,i)*10000; int dh=ArrayMaximum(OSMA) ; int dl=ArrayMinimum(OSMA) ; OSMAh=(iOsMA(NULL,NULL,12,26,9,PRICE_CLOSE,0)*10000)-OSMA[dh]; OSMAl=(iOsMA(NULL,NULL,12,26,9,PRICE_CLOSE,0)*10000)-OSMA[dl]; } int counted_bars=IndicatorCounted(); if (counted_bars<0) return(-1); if (counted_bars>0) counted_bars--; int j=Bars-counted_bars; while(j>=0) { hbuffer1[j]= OSMA[dh] ; lbuffer1[j]= OSMA[dl] ; j--; } //---- //---- return(0); } //+------------------------------------------------------------------+
This was the code from my attempt at a non-loop workaround, but there's an error because it won't compile due to the array 'OSMA(i)' - Could anyone suggest how I could put all the values (OSMA0, OSMA1, OSMA2...etc) into an array, as I've tried to below, so that I can then use ArrayMaximimum/Minimum to find the values I want?
Assigning the individual values (OSMA0, OSMA1, OSMA2...etc) to the buffers gives the values I would expect (with no repainting) it's just that dam array...
//+------------------------------------------------------------------+ //| MACD delta5 NEW.mq4 | //| Genius | //| | //+------------------------------------------------------------------+ #property copyright "Genius" #property link "" #property indicator_separate_window #property indicator_buffers 2 #property indicator_color1 Yellow #property indicator_color2 Orange //---- buffers double hbuffer1[]; double lbuffer1[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { //---- indicators SetIndexStyle(0,DRAW_LINE); SetIndexBuffer(0,hbuffer1); SetIndexStyle(0,DRAW_LINE); SetIndexBuffer(1,lbuffer1); string short_name = "Delta is running!"; IndicatorShortName(short_name); //---- return(1); } //+------------------------------------------------------------------+ //| Custor indicator deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int start() { int counted_bars=IndicatorCounted(); //---- check for possible errors if (counted_bars<0) return(-1); //---- last counted bar will be recounted if (counted_bars>0) counted_bars--; int i=Bars-counted_bars; double OSMA0,OSMA1,OSMA2,OSMA3,OSMA4,OSMA5; double OSMA[6]; //---- main calculation loop while(i>=0) { OSMA0=iOsMA(NULL,NULL,12,26,9,PRICE_CLOSE,i)*10000; OSMA1=iOsMA(NULL,NULL,12,26,9,PRICE_CLOSE,i+1)*10000; OSMA2=iOsMA(NULL,NULL,12,26,9,PRICE_CLOSE,i+2)*10000; OSMA3=iOsMA(NULL,NULL,12,26,9,PRICE_CLOSE,i+3)*10000; OSMA4=iOsMA(NULL,NULL,12,26,9,PRICE_CLOSE,i+4)*10000; OSMA5=iOsMA(NULL,NULL,12,26,9,PRICE_CLOSE,i+5)*10000; OSMA[i]={OSMA0,OSMA1,OSMA2,OSMA3,OSMA4,OSMA5}: hbuffer1[i]= OSMA3 ; //check lbuffer1[i]= OSMA[4] ; //check (same?) i--; } //---- return(0); } //+------------------------------------------------------------------+
Ok, finally managed to get it right using code from another post on this forum - in case it's any use to anyone the code's below:
int start() { //---- last counted bar will be recounted int counted_bars = IndicatorCounted(); if(counted_bars>0) counted_bars--; int limit=Bars-counted_bars; //---- Current OSMA value for(int i = limit-1; i>=0; i--)// Count down for one pass OSMAbuffer[i]=iOsMA(NULL,NULL,12,26,9,PRICE_CLOSE,i)*10000; //---- Highest OSMA value in 20 periods for(i = limit-1; i>=0; i--) // Count down for one pass Hbuffer[i] = OSMAbuffer[ArrayMaximum(OSMAbuffer, lookbackBars, i)]; //---- Lowest OSMA value in 20 periods for(i= limit-1; i>=0; i--) Lbuffer[i] = OSMAbuffer[ArrayMinimum(OSMAbuffer, lookbackBars, i)]; //---- return(0); } //+------------------------------------------------------------------+
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi
I'm trying to create an indicator that shows the difference between the current (or last bar) OSMA value, and the maximum OSMA value over the last 5 bars.
So far I've got the following:
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Yellow
#property indicator_color2 Orange
//---- buffers
double hbuffer1[];
double lbuffer1[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,hbuffer1);
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(1,lbuffer1);
string short_name = "Delta is running!";
IndicatorShortName(short_name);
//----
return(1);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int OSMAlookback=5;
double OSMA[5];
double OSMAh,OSMAl;
for(int i = 0; i < OSMAlookback; i++)
{
OSMA[i]=iOsMA(NULL,NULL,12,26,9,PRICE_CLOSE,i)*10000;
int dh=ArrayMaximum(OSMA) ;
int dl=ArrayMinimum(OSMA) ;
OSMAh=(iOsMA(NULL,NULL,12,26,9,PRICE_CLOSE,0)*10000)-OSMA[dh];
OSMAl=(iOsMA(NULL,NULL,12,26,9,PRICE_CLOSE,0)*10000)-OSMA[dl];
hbuffer1[i]= OSMAh ;
lbuffer1[i]= OSMAl ;
}
//----
//----
return(0);
}
//+------------------------------------------------------------------+
Problem is the indi seems to 'repaint' - previous 5 values change (as seen through data box on MT4) after being shown on the chart. I'm guessing this has something to do with the for loop, but how to implement it differently I just don't know...
I'm pretty green when it comes to writing indicators and so please forgive me if I've missed something obvious, but I can't see why the above isn't working properly - It's been driving me crazy!!
Any help anyone could give on this would be muchly appreciated :)