Renko lines MT4 code needed in afl

#1
Hi experts

Here is a MQ4 code called Renko_v11. given to me by a mt4 trader.

It draws two lines like breakout lines.

Request help to get it into afl code

thanks & regards
ford
----------------
HTML:
//+------------------------------------------------------------------+
//|                                                     Renko_v1.mq4 |


#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Aqua
#property indicator_color2 Magenta
//---- input parameters
extern int PeriodATR=10;
extern double Katr=1.00;
//---- indicator buffers
double UpBuffer[];
double DnBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
  int init()
  {
   string short_name;
//---- indicator line
   SetIndexStyle(0,DRAW_LINE);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(0,UpBuffer);
   SetIndexBuffer(1,DnBuffer);
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
//---- name for DataWindow and indicator subwindow label
   short_name="Renko("+PeriodATR+","+Katr+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,"Renko");
//----
   SetIndexDrawBegin(0,PeriodATR);
//----
   return(0);
  }

//+------------------------------------------------------------------+
//| Renko_v1                                                        |
//+------------------------------------------------------------------+
int start()
  {
   int i,shift;
   double Up,Dn,Brick,AvgRange,dK,ATR,BrickUp,BrickDn;
	
   for(shift=Bars-1-PeriodATR;shift>=0;shift--)
   {	
	AvgRange=0;
	for (i=PeriodATR-1;i>=0;i--)
	    { 
       dK = 1;
       AvgRange+=dK*MathAbs(High[i+shift]-Low[i+shift]);
       }
	ATR = AvgRange/PeriodATR;
	
	if (shift==Bars-1-PeriodATR)
	{
	Up=High[shift];
	Dn=Low[shift];
	Brick=Katr*(High[shift]-Low[shift]);
	} 
	
	if (shift<Bars-1-PeriodATR)  
	{
		if (Close[shift] > Up + Brick)
		{
		if (Brick==0) 
		BrickUp=0;
		else
		BrickUp=MathRound((Close[shift]-Up)/Brick)*Brick;	
		
		Up = Up + BrickUp;
		Brick = Katr*ATR;
		Dn = Up - Brick;
		BrickDn = 0;
		}
		
		if (Close[shift] < Dn - Brick) 
		{
		if (Brick==0) 
		BrickDn=0;
		else
		BrickDn=MathRound((Dn-Close[shift])/Brick)*Brick;
		
		Dn = Dn - BrickDn;
		Brick = Katr*ATR;
		Up = Dn + Brick;
		BrickUp = 0;
		}
	}
	UpBuffer[shift]=Up;
	DnBuffer[shift]=Dn;
	}	
	return(0);	
 }
 
#2
Hope it might help u.

Kindly show the picture of MT4 BASED RENKO AFL.



This is Renko afl code -


SetBarsRequired(100000,100000);
//------------------------------------------------------------------+
// Block 1 |
//------------------------------------------------------------------+
VR=ParamToggle("View Renko","Bricks|Lines/Bars",0);
FV=Param("Initial size volatility",0.5,0.001,50,0.001);
MV=Param("Method calculation volatility",0,0,2,1);
PeriodVol=Param("Period calculation volatility",14,2,100,1);
Multiplier=Param("Multiplier volatility",1,0.1,10,0.1);
MR=ParamToggle("Method Renko","floating|fixed",1);
SG=ParamToggle("Show gap","No|Yes",1);
CG=ParamColor("Colour show gap",11);
MaxBS=Param("Maximum size brick",0,0,10,0.1);
MinBS=Param("Minimum size brick",0,0,10,0.1);
RenkoUp=ParamColor("Colour Renko upwards",colorBlack);
RenkoDown=ParamColor("Colour Renko downwards",colorBlack);
SB=ParamStyle("View bars",defaultval=styleCandle,mask=maskPrice);
color3=ParamColor("Colour bars",colorBlack);
History=Param("History size",5000,2,BarCount-1,1);
//------------------------------------------------------------------+
// Block 2 |
//------------------------------------------------------------------+
i=Max(BarCount-1-History,PeriodVol+1);
r=j=0;
direction=0;
iGapUp=iGapDown=0;
rOpen[0]=rHigh[0]=rLow[0]=rClose[0]=jUp=jDown=Open;
//-------------------------------------------------------------------
switch(MV)
{
case 0: Volatility=FV; break;
case 1: Volatility=ATR(PeriodVol)*Multiplier; break;
case 2: Volatility=StDev(Open,PeriodVol)*Multiplier; break;
}
BrickSize=Volatility[i-1];
//-------------------------------------------------------------------+
// Block 3 |
//-------------------------------------------------------------------+
while(i<=BarCount-1)
{
if(SG==1)
{
if(High[i-1]<Low)
{
iGapUp=1;
}
else
{
if(Low[i-1]>High)
{
iGapDown=1;
}
}
}
//-------------------------------------------------------------------
if(MR==0)
{
BrickSize=Volatility[i-1];
if(MaxBS>0) {BrickSize=Min(MaxBS,BrickSize);}
if(MinBS>0) {BrickSize=Max(MinBS,BrickSize);}
}
//------------------------------------------------------------------+
// Block 4 |
//------------------------------------------------------------------+
if(direction==0)
{
if(Open-rClose[r]>BrickSize)
{
rClose[r]=rOpen[r]+BrickSize;
rHigh[r]=rClose[r];
direction=1;
//-------------------------------------------------------------------
BrickSize=Volatility;
if(MaxBS>0) {BrickSize=Min(MaxBS,BrickSize);}
if(MinBS>0) {BrickSize=Max(MinBS,BrickSize);}
//-------------------------------------------------------------------
if(iGapUp==1|iGapDown==1)
{
color[r]=CG;
}
else
{
color[r]=RenkoUp;
}
continue;
}
//-------------------------------------------------------------------
else
{
if(rClose[r]-Open>BrickSize)
{
rClose[r]=rOpen[r]-BrickSize;
rLow[r]=rClose[r];
direction=2;
//-------------------------------------------------------------------
BrickSize=Volatility;
if(MaxBS>0) {BrickSize=Min(MaxBS,BrickSize);}
if(MinBS>0) {BrickSize=Max(MinBS,BrickSize);}
//-------------------------------------------------------------------
if(iGapUp==1|iGapDown==1)
{
color[r]=CG;
}
else
{
color[r]=RenkoDown;
}
continue;
}
}
}
//------------------------------------------------------------------+
// Block 5 |
//------------------------------------------------------------------+
else
{
if(direction==1)
{
if(rOpen[r]-Open>BrickSize)
{
r++;
rOpen[r]=rOpen[r-1];
rHigh[r]=rOpen[r];
rClose[r]=rOpen[r]-BrickSize;
rLow[r]=rClose[r];
direction=2;
//-------------------------------------------------------------------
BrickSize=Volatility;
if(MaxBS>0) {BrickSize=Min(MaxBS,BrickSize);}
if(MinBS>0) {BrickSize=Max(MinBS,BrickSize);}
//-------------------------------------------------------------------
if(iGapUp==1|iGapDown==1)
{
color[r]=CG;
}
else
{
color[r]=RenkoDown;
}
continue;
}
//-------------------------------------------------------------------
else
{
while(Open-rClose[r]>BrickSize)
{
r++;
rOpen[r]=rClose[r-1];
rLow[r]=rOpen[r];
rClose[r]=rOpen[r]+BrickSize;
rHigh[r]=rClose[r];
//-------------------------------------------------------------------
BrickSize=Volatility;
if(MaxBS>0) {BrickSize=Min(MaxBS,BrickSize);}
if(MinBS>0) {BrickSize=Max(MinBS,BrickSize);}
//-------------------------------------------------------------------
if(iGapUp==1|iGapDown==1)
{
color[r]=CG;
}
else
{
color[r]=RenkoUp;
}
}
}
}
//------------------------------------------------------------------+
// Block 6 |
//------------------------------------------------------------------+
else
{
if(direction==2)
{
if(Open-rOpen[r]>BrickSize)
{
r++;
rOpen[r]=rOpen[r-1];
rLow[r]=rOpen[r];
rClose[r]=rOpen[r]+BrickSize;
rHigh[r]=rClose[r];
direction=1;
//-------------------------------------------------------------------
BrickSize=Volatility;
if(MaxBS>0) {BrickSize=Min(MaxBS,BrickSize);}
if(MinBS>0) {BrickSize=Max(MinBS,BrickSize);}
//-------------------------------------------------------------------
if(iGapUp==1|iGapDown==1)
{
color[r]=CG;
}
else
{
color[r]=RenkoUp;
}
continue;
}
//-------------------------------------------------------------------
else
{
while(rClose[r]-Open>BrickSize)
{
r++;
rOpen[r]=rClose[r-1];
rHigh[r]=rOpen[r];
rClose[r]=rOpen[r]-BrickSize;
rLow[r]=rClose[r];
//-------------------------------------------------------------------
BrickSize=Volatility;
if(MaxBS>0) {BrickSize=Min(MaxBS,BrickSize);}
if(MinBS>0) {BrickSize=Max(MinBS,BrickSize);}
//-------------------------------------------------------------------
if(iGapUp==1|iGapDown==1)
{
color[r]=CG;
}
else
{
color[r]=RenkoDown;
}
}
}
}
}
}
//------------------------------------------------------------------+
// Block 7 |
//------------------------------------------------------------------+
if(VR==1)
{
jOpen[j]=Open;
jHigh[j]=High;
jLow[j]=Low;
jClose[j]=Close;
//-------------------------------------------------------------------
if(direction==1)
{
jUp[j]=rClose[r];
jDown[j]=rOpen[r];
color2[j]=color[r];
}
else
{
if(direction==2)
{
jUp[j]=rOpen[r];
jDown[j]=rClose[r];
color2[j]=color[r];
}
}
j++;
}
i++;
}
//------------------------------------------------------------------+
// Block 8 |
//------------------------------------------------------------------+
if(VR==1)
{
delta=BarCount-j;
jOpen=Ref(jOpen,-delta);
jHigh=Ref(jHigh,-delta);
jLow=Ref(jLow,-delta);
jClose=Ref(jClose,-delta);
jUp=Ref(jUp,-delta);
jDown=Ref(jDown,-delta);
color2=Ref(color2,-delta);
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 ) ) ));
PlotOHLC(jOpen,jHigh,jLow,jClose,"SRI",color3,SB);
Plot(jUp,"Up",color2,styleThick);
Plot(jDown,"Down",color2,styleThick);
}
//-------------------------------------------------------------------
else
{
delta=BarCount-1-r;
rOpen=Ref(rOpen,-delta);
rHigh=Ref(rHigh,-delta);
rLow=Ref(rLow,-delta);
rClose=Ref(rClose,-delta);
color=Ref(color,-delta);
PlotOHLC(rOpen,rHigh,rLow,rClose,"SRI",color,styleCandle);
}
 
#4
rOpen[0]=rHigh[0]=rLow[0]=rClose[0]=jUp=jDown=Open;

this line,
giving error
pls see


With renko on amibroker, you have to choose smallest timeframe and then increase the brick size till it plots the chart.

Small brick size with higher tf will throw error


Happy :)
 
#6
hi
just choose 1 minute timeframe for the chart
it works.
increase to 5min or 10 min it may start giving error.
===================================================

Regarding the error-details are as follows.
=============================================================================
Error 10. Subscript out of range.
You must not access array elements outside 0..
(BarCount-1) range Occurs when you attempt to access array elements with subscripts below 0 (zero) or above BarCount-1.
(This is Amibrokers age old problem-remedy suggested below in help-not easy to apply)
// incorrect
for( bar = 0; bar < BarCount; bar++ )
{
a[ bar ] = C[ bar - 1]; // when i == 0 we are accessing C[-1] which is wrong
}


// correct
for( bar = 0; bar < BarCount; bar++ )
{
if( bar > 0 )
a[ bar ] = C[ bar - 1 ]; // only access C[ i - 1 ] when i is greater than zero
else
a[ bar ] = C[ 0 ];
}


One of most common mistakes is using hard coded upper limit of for loop and assuming that all symbols have enough quotes.

For example:

MyPeriod = 10;

for( i = 0; i < MyPeriod; i++ ) // WRONG ! this assumes that you always have at least 10 quotes !
{
// ... do something
}

This will always fail on symbols that have less quotes than 10 and it may also fail if you zoom your chart in so less than 10 quotes are actually displayed on the chart.

To ensure error-free operation you must always check for upper index being LESS than BarCount, like shown in the code below:

MyPeriod = 10;

for( i = 0; i < MyPeriod AND i < BarCount; i++ ) // CORRECT - added check for upper bound
{
// ... do something
}

Alternativelly you can enter the loop only when there are enough bars, like shown in this code:

MyPeriod = 10;

if( MyPeriod <= BarCount ) // CORRECT - check if there are enough bars to run the loop
{
for( i = 0; i < MyPeriod; i++ )
{
// ... do something
}
}
 
Last edited:

colion

Active Member
#8
But then can it only be used for 1 minute timeframe trading ?
It works for all timeframes. Just "play" around with the parameters. After you get a chart to appear modify your settings so that you duplicate a "commercial" plot from a software program that has Renko built-in (such as Metaststock) or one of the free websites.
 

Similar threads

Intraday Higher Leverage

Save up to 90% in brokerage and get higher leverage for intraday trades.

Name:Phone:
Email:City:
State:
Are you a day trader?