Simple Coding Help - No Promise.

Sir do you have any different solution for fixing the repaint ? please suggest...
Nehal if you use future referencing in any form you cannot do away with repainting . . .

To avoid that you will have to change coding logic in your Hurst implementation . . .

One solution Pratap has provided, which instead of looking forward by 1/2 cycle, introduces a lag of 1/2 the cycle

another one can be just remove the Ref part of the code completely without replacing - with + . . . (or just substitute a 0 (zero) there so + or - does not matter)

@ Pratap, you can also try this change, and see how it works with the bands you are testing . . .

:) Happy
 

mastermind007

Well-Known Member
Sir do you have any different solution for fixing the repaint ? please suggest...
Nehal, Pratap


I went thru my backup to find out what I had on Hurst Bands. Lot of files I had were lost due to hardware failure on my earlier computer, so I no longer have any of his PDFs. If you have the PDFs and want to share it, upload it somewhere and post links here.

But I've read his book once before and as far as I can remember his theory, it does seem that Hurst did use future quotes and automatically corrected itself as it became aware of mistakes. If I remember right the term used was "feedback".

It is not wrong if you are able to understand that AFL is simply attempting to guess the future and AFL will auto correct as market depicts reality. If you take it as a one more "Known risk", you can trade with it.

It becomes wrong only if you take a stance that that signals once shown must not disappear.

These are all the important formulas I had collected on Hurst. Total is close to 20 but these are the ones I wanted to work with. The one that matches yours is in Formula two. Some are in "Metastock" Some are in "EasyLanguage". I used to have it in MQL too but cannot find that one now

In nutshell, I am not confident that "repainting" can be called an error in this particular case and therefore fixing the repainting may actually defeat the purpose.

Please do realize that I am not discouraging you.... There is a mystery and lots of confusion around this and if you want to trade on it, you'll have to understand what you are dealing with. First, know the risks and then decide whether to take 'em or reject 'em


Code:
----------------------------------------------------- 
-FORMULA ONE ----------------------------------------
----------------------------------------------------- 
l1:=3;
m1:=1;
l2:=80;
m2:=3;

data:=mp();

mid1:=mov(data,l1,s);
mid2:=mov(data,l2,s);

atr1:=atr(l1);
atr2:=atr(l2);

ul1:=mid1+m1*atr1;
ll1:=mid1-m1*atr1;

ul2:=mid2+m2*atr2;
ll2:=mid2-m2*atr2;

{plot}
ul1;ll1;
ul2;ll2;

----------------------------------------------------- 
-FORMULA TWO ----------------------------------------
----------------------------------------------------- 

{ Hurst Channels } 
CY1:= Input("Short Cycle length",1,1000,10 )/2; 
CY2:= Input("Medium Cycle length",1,1000,80 )/2; 
M1:= Input("Short Cycle Multiplier",.01,10 ,1 ); 
M2:= Input("Medium Cycle Multiplier",.01,10,3 ); 
T1:= Ref(Mov( CLOSE ,CY1 ,TRI ),-CY1/2)+ M1*ATR(CY1 ); 
B1:= Ref(Mov( CLOSE ,CY1 ,TRI ),-CY1/2)- M1*ATR(CY1 ); 
T2:= Ref(Mov( CLOSE ,CY2 ,TRI ),-CY2/2)+ M2*ATR(CY2 ); 
B2:= Ref(Mov( CLOSE ,CY2 ,TRI ),-CY2/2)- M2*ATR(CY2 ); 
T1;B1;T2;B2; 
----------------------------------------------------- 
-FORMULA THREE --------------------------------------
----------------------------------------------------- 
Hurst Bands 
----------------------------------------------------- 
pds:= Input("LR Periods",10,100,41); 
a:= Input("Stdev Periods",10,100,30); 
s1:=Input("Smoothing Periods",2,50,10); 
r:=LinearReg(C,pds); 
x:=Mov(r,s1,S); 

x1 :=x+1*Stdev(C,a); 
x2 :=x+2*Stdev(C,a); 
x3 :=x+3*Stdev(C,a); 

y1:= x-1*Stdev(C,a); 
y2:= x-2*Stdev(C,a); 
y3:= x-3*Stdev(C,a); 

x; 
x1; 
x2; 
x3; 
y1; 
y2; 
y3; 
----------------------------------------------------- 
-FORMULA FOUR ---------------------------------------
----------------------------------------------------- 

[LegacyColorValue = TRUE];

{Hurst_Channel Indicator}       

Input:  Price1(MedianPrice of Data1);
Input:  Length1(10),
        ChanWid1(1);  {Multiplier for ATR is positive          }
                      {Percent*100 of Current price if Negative}
Input:  Price2(MedianPrice of Data1);
Input:  Length2(60),
        ChanWid2(3);
Input:  CompMode(0);  {Compute mode for extension}
                      {0 = CMA shortening        }
           {>0= 2nd degree polynomial }
Input:  StopDate(0);

Vars:   ChanHi1(0),  ChanLo1(0), WidChan1(Iff(ChanWid1>0,ChanWid1,-ChanWid1*.001)),
        Ave1(0),     ATR1(0),
        SetBack1(IntPortion((Length1-1)/2)+1);
Vars:   ChanHi2(0),  ChanLo2(0), WidChan2(Iff(ChanWid2>0,ChanWid2,-ChanWid2*.001)),
        Ave2(0),     ATR2(0),
        SetBack2(IntPortion((Length2-1)/2)+1);
Vars:   DateStop(iff(stopdate<=0,1211231,StopDate)),Ipass(0);


If Length1>0 and Ipass=0 then begin
 
  Ave1   = Average(Price1,Length1);
  If ChanWid1>0
  then ATR1   = AvgTrueRange(Length1)
  else  ATR1   = C;
  ChanHi1= Ave1+ATR1*WidChan1;
  ChanLo1= Ave1-ATR1*WidChan1;
  
  
  Plot1[SetBack1](ChanHi1,"HH1");
  Plot2[SetBack1](ChanLo1,"HL1");
  
  If LastBarOnChart or Date>=DateStop then Begin
  Value5 = (Ave1-Ave1[SetBack1])/SetBack1;
    For Value1 = SetBack1-1 downto 0 begin
      Value3=0;
      Value4=Value1*2;
      For Value2=0 to Value4 begin
        Value3=Value3+Price1[Value2];
      End;
      Ave1=Value3/(Value4+1);
   If CompMode>0 then Ave1=Ave1*.33+(Ave1[1]+Value5*(SetBack1-Value1))*.67;
      ChanHi1= Ave1+ATR1*WidChan1;
      ChanLo1= Ave1-ATR1*WidChan1;
      Plot1[Value1](ChanHi1,"HH1");
      Plot2[Value1](ChanLo1,"HL1");
    End;
  If Length2=0 then IPass=1;
  End;
End;



{Now for second channel}
If Length2>0 and Ipass=0 then begin
  Ave2   = Average(Price2,Length2);
  If ChanWid2>0
  then ATR2   = AvgTrueRange(Length2)
  else  ATR2   = C;
  ChanHi2= Ave2+ATR2*WidChan2;
  ChanLo2= Ave2-ATR2*WidChan2;
  
  Plot3[SetBack2](ChanHi2,"HH2");
  Plot4[SetBack2](ChanLo2,"HL2");
  
  If LastBarOnChart or Date>=DateStop then Begin
  Value5 = (Ave2-Ave2[SetBack2])/SetBack2;
    For Value1 = SetBack2-1 downto 0 begin
      Value3=0;
      Value4=Value1*2;
      For Value2=0 to Value4 begin
        Value3=Value3+Price2[Value2];
      End;
      Ave2=Value3/(Value4+1);
   If CompMode>0 then Ave2=Ave2*.33+(Ave2[1]+Value5*(SetBack2-Value1))*.67;
      ChanHi2= Ave2+ATR2*WidChan2;
      ChanLo2= Ave2-ATR2*WidChan2;
      Plot3[Value1](ChanHi2,"HH2");
      Plot4[Value1](ChanLo2,"HL2");
    End;
  IPass=1;
  End;
End;
----------------------------------------------------- 
-FORMULA FIVE --------------------------------------- 
----------------------------------------------------- 
this is a misnomer, keep reading...
{Hurst_Channel Indicator}       

-- these is the user input area where the user gets to set various parameters --
SECTION 1 values
Input:  Price1(MedianPrice of Data1);
Input:  Length1(10),
        ChanWid1(1);  {Multiplier for ATR is positive          }
                      {Percent*100 of Current price if Negative}

SECTION 2 values
Input:  Price2(MedianPrice of Data1);
Input:  Length2(60),
        ChanWid2(3);

these apply to both sections
Input:  CompMode(0);  {Compute mode for extension}
                      {0 = CMA shortening        }
           {>0= 2nd degree polynomial }
Input:  StopDate(0);

this is the variable declaration area - not required in MS
Vars:   ChanHi1(0),  ChanLo1(0), WidChan1(Iff(ChanWid1>0,ChanWid1,-ChanWid1*.001)),
        Ave1(0),     ATR1(0),
        SetBack1(IntPortion((Length1-1)/2)+1);
Vars:   ChanHi2(0),  ChanLo2(0), WidChan2(Iff(ChanWid2>0,ChanWid2,-ChanWid2*.001)),
        Ave2(0),     ATR2(0),
        SetBack2(IntPortion((Length2-1)/2)+1);
Vars:   DateStop(iff(stopdate<=0,1211231,StopDate)),Ipass(0);

-- SECTION 1 --
If Length1>0 and Ipass=0 then begin
 
  Ave1   = Average(Price1,Length1);
  If ChanWid1>0
  then ATR1   = AvgTrueRange(Length1)
  else  ATR1   = C;
  ChanHi1= Ave1+ATR1*WidChan1;
  ChanLo1= Ave1-ATR1*WidChan1;
  
  this is a SMA with an ATR channel width, ala Keltner Bands and VERY similar to the results to the code posted years ago in the thread above
  Plot1[SetBack1](ChanHi1,"HH1");
  Plot2[SetBack1](ChanLo1,"HL1");
  
   this is for forecasting past the last selected date or the last bar
   only plots if CompMode>0
  If LastBarOnChart or Date>=DateStop then Begin
  Value5 = (Ave1-Ave1[SetBack1])/SetBack1;
    For Value1 = SetBack1-1 downto 0 begin
      Value3=0;
      Value4=Value1*2;
      For Value2=0 to Value4 begin
        Value3=Value3+Price1[Value2];
      End;
      Ave1=Value3/(Value4+1);
   If CompMode>0 then Ave1=Ave1*.33+(Ave1[1]+Value5*(SetBack1-Value1))*.67; this is not a 2nd degree polynomial, it's a form of IIR filter (EMA)
      ChanHi1= Ave1+ATR1*WidChan1;
      ChanLo1= Ave1-ATR1*WidChan1;
      Plot1[Value1](ChanHi1,"HH1");
      Plot2[Value1](ChanLo1,"HL1");
    End;
  If Length2=0 then IPass=1;
  End;
End;


-- SECTION 2 --
just a repeat of SECTION 1 to draw a second set of bands, with the second set of user parameters
{Now for second channel}
If Length2>0 and Ipass=0 then begin
  Ave2   = Average(Price2,Length2);
  If ChanWid2>0
  then ATR2   = AvgTrueRange(Length2)
  else  ATR2   = C;
  ChanHi2= Ave2+ATR2*WidChan2;
  ChanLo2= Ave2-ATR2*WidChan2;
  
  Plot3[SetBack2](ChanHi2,"HH2");
  Plot4[SetBack2](ChanLo2,"HL2");
  
  If LastBarOnChart or Date>=DateStop then Begin
  Value5 = (Ave2-Ave2[SetBack2])/SetBack2;
    For Value1 = SetBack2-1 downto 0 begin
      Value3=0;
      Value4=Value1*2;
      For Value2=0 to Value4 begin
        Value3=Value3+Price2[Value2];
      End;
      Ave2=Value3/(Value4+1);
   If CompMode>0 then Ave2=Ave2*.33+(Ave2[1]+Value5*(SetBack2-Value1))*.67;
      ChanHi2= Ave2+ATR2*WidChan2;
      ChanLo2= Ave2-ATR2*WidChan2;
      Plot3[Value1](ChanHi2,"HH2");
      Plot4[Value1](ChanLo2,"HL2");
    End;
  IPass=1;
  End;
End;
----------------------------------------------------- 
----------------------------------------------------- 
-----------------------------------------------------
 
Last edited:
hi,
I have a simple strategy and needs some one who can do the programming so i can backtest the same.
Strategy is based on dual timeframe.
In 30mins TF, if the close price is above 200MA, i will buy the stock, once the price cross 200MA in 5mins
Exit for the Buy is when the prices closes below 200MA on 5min TF

I need to backtest this stratergy kindly help me out .
Sir any help of the above query would be really appreciated.
 

augubhai

Well-Known Member
Happy,

Assume there is a signal bar, and I will to go long over it's high. Without looping, is there a way to identify the bar that triggers the long? Need this to make backtesting more efficient.

In case this is already answered in this thread, please let me know. I'll search it out.

Rewording: i want to find the bar whose high is higher than the current bar's high.
 
Last edited:
Hello happyji...



I wish to explore or scan stocks whose Single candle rise or fall is more then 1% and making new highs



If u c chart image

2nd candle is from 2070 to 2098... more then 1%..




Buy condition :-

i wish to explore such candles which are up or green more then 1% and making new highs


Sell condition :-

I wish to explore candles which are down or Red more then 1% and making new lows.

(One more condition :- Volume is greater then Average volume on that candle)

Below is the Intraday chart of heromotoco future ...



Thanx you in advance...
 
Last edited:
Can some convert this mq4 code into Amibroker AFL.


//----
#property indicator_separate_window
#property indicator_buffers 3
//----
#property indicator_color1 SlateBlue
#property indicator_color2 White
#property indicator_color3 White
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
double ExtMapBuffer5[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
IndicatorBuffers(5);
//---- 3 additional buffers are used for counting.
SetIndexBuffer(3, ExtMapBuffer4);
SetIndexBuffer(4, ExtMapBuffer5);
//---- indicators
SetIndexStyle(0,DRAW_HISTOGRAM);
SetIndexBuffer(0,ExtMapBuffer1);
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,ExtMapBuffer2);
SetIndexStyle(2,DRAW_LINE);
SetIndexBuffer(2,ExtMapBuffer3);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars=IndicatorCounted();
//----
//----
int limit;
//---- check for possible errors
if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
//----
for(int i=0; i<limit; i++)
ExtMapBuffer4=iMA(NULL,0,5,0,MODE_SMA,PRICE_MEDIAN ,i);
for(i=0; i<limit; i++)
ExtMapBuffer5=iMA(NULL,0,34,0,MODE_SMA,PRICE_MEDIAN,i);
for(i=0; i<limit; i++)
ExtMapBuffer1=ExtMapBuffer4-ExtMapBuffer5;
double pr=2.0/(38+1);
int pos=Bars-2;
if (counted_bars>2) pos=Bars-counted_bars-1;
while(pos>=0)
{
if((pos==Bars-2))
{ ExtMapBuffer2[pos+1]=ExtMapBuffer1[pos+1];
ExtMapBuffer3[pos+1]=ExtMapBuffer1[pos+1];
}
if (ExtMapBuffer1[pos]>=0)
{
ExtMapBuffer2[pos]=ExtMapBuffer1[pos]*pr+ExtMapBuffer2[pos+1]*(1-pr);
ExtMapBuffer3[pos]=ExtMapBuffer3[pos+1];
}
else
{
ExtMapBuffer2[pos]=ExtMapBuffer2[pos+1];
ExtMapBuffer3[pos]=ExtMapBuffer1[pos]*pr+ExtMapBuffer3[pos+1]*(1-pr);
}
pos--;
}
//----
return(0);
}
//+------------------------------------------------------------------+
 
Happy,

Assume there is a signal bar, and I will to go long over it's high. Without looping, is there a way to identify the bar that triggers the long? Need this to make backtesting more efficient.

In case this is already answered in this thread, please let me know. I'll search it out.

Rewording: i want to find the bar whose high is higher than the current bar's high.
Hello

This should be simple, unless i have misunderstood what you want

Signal_Bar = My_Condition;
BO = ValueWhen(Signal_Bar,H);

Buy = Cross(H, BO); // or H > BO;


:) Happy
 
Code:
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() ); 

TimeFrameSet(in1Minute*30);	
	HTFMA = MA(Close,200);	
	HTFUP = Close > HTFMA;
	HTFDN = Close < HTFMA;
TimeFrameRestore();
HTF_MA = TimeFrameExpand(HTFMA,in1Minute*30);
HTF_UP = TimeFrameExpand(HTFUP,in1Minute*30);
HTF_DN = TimeFrameExpand(HTFDN,in1Minute*30);
MA200  = MA(Close,200);
Buy   = Close > MA200 AND HTF_UP; 	
Sell  = Close < MA200;
Buy   = ExRem(Buy,Sell);						
Sell  = ExRem(Sell,Buy);
Short = Close < MA200 AND HTF_DN; 	
Cover = Close > MA200;
Short = ExRem(Short,Cover);						
Cover  = ExRem(Cover,Short);
SetPositionSize(1,4);	
Plot(HTF_MA,"30MIN_MA200",IIf(HTF_UP,colorBlue,colorRed),styleThick|styleNoRescale);
Plot(MA200,"MA200",IIf(C>MA200, colorBlue,colorRed),styleLine);
PlotShapes(Buy+2*Short,colorWhite,0,IIf(Buy,L,H));
PlotShapes(Cover*3+4*Sell,colorWhite,0,IIf(Cover,L,H));
_SECTION_END();

hi,
I have a simple strategy and needs some one who can do the programming so i can backtest the same.
Strategy is based on dual timeframe.
In 30mins TF, if the close price is above 200MA, i will buy the stock, once the price cross 200MA in 5mins
Exit for the Buy is when the prices closes below 200MA on 5min TF

I need to backtest this stratergy kindly help me out .
 
Hello happyji...



I wish to explore or scan stocks whose Single candle rise or fall is more then 1% and making new highs



If u c chart image

2nd candle is from 2070 to 2098... more then 1%..




Buy condition :-

i wish to explore such candles which are up or green more then 1% and making new highs


Sell condition :-

I wish to explore candles which are down or Red more then 1% and making new lows.

(One more condition :- Volume is greater then Average volume on that candle)

Below is the Intraday chart of heromotoco future ...



Thanx you in advance...

Code:
Avg_Vol = MA(V,200);  Nbar_Hi = HHV(H,20);  Nbar_Lo = LLV(L,20);
Buy  = H > Ref(H,-1)*1.01 AND H > Ref(Nbar_Hi,-1) AND V > Avg_Vol; 	
Sell = L < Ref(L,-1)*0.99 AND L < Ref(Nbar_Lo,-1) AND V > Avg_Vol;
PlotShapes(Buy+2*Sell,colorWhite,0,IIf(Buy,L,H)); Filter = Buy OR Sell
this will also flag gap-up/down by more than 1% with high volume


:) Happy
 

Similar threads