Simple Coding Help - No Promise.

_SECTION_BEGIN("Gordon Rose");

nBars = Param("Number of bars", 12, 5, 40);
bTrace = Param("Include trace output", 1, 0, 1);
nNoPivsInSetup = Param("No. Pivs in Setup", 4, 3, 4, 1);
bShowTCZ = Param("Show TCZ", 1, 0, 1);
nMinBarsBtwPivs = Param("Min. number of bars btw. pivots", 1, 1, 10, 1);
nMinPctBtwPivs = Param("Min. percent diff. btw. pivots", .05, .04, .2, .01);
bLastBarCanBePiv = Param("Last bar can be a pivot", 1, 0, 1);
retrcTolerance = .01;
tczTolerance = .005;
nNumBarsToScan = 120;

nExploreBarIdx = 0;
nExploreDate = 0;
nCurDateNum = 0;
DN = DateNum();
DT = DateTime();

bTCZLong = False;
bTCZShort = False;
nAnchorPivIdx = 0;

ADX8 = ADX(8);

if(Status("action")==1) {
bDraw = True;
bUseLastVis = Param("Use last visible bar", 1, 0, 1);
} else {
bDraw = False;
bUseLastVis = False;
bTrace = False;
nExploreDate = Status("rangetodate");
for (i=LastValue(BarIndex());i>=0;i--) {
nCurDateNum = DN;
if (nCurDateNum == nExploreDate) {
nExploreBarIdx = i;
}
}
}

GraphXSpace=7;


PlotOHLC(Open, High, Low, Close,
"BIdx = " + BarIndex() +
"\n" + "O = " + O + "\n"+"H = "+ H + "\n"+"L = " + L
+ "\n"+"C ",
colorBlack, styleCandle);

if (bDraw) {
//Plot(MA(C, 21), "21 bar MA", colorAqua,styleLine+styleNoRescale+styleNoLabel);
//Plot(MA(C, 55), "55 bar MA", colorGreen,styleLine+styleNoRescale+styleNoLabel);
//Plot(MA(C, 233), "233 bar MA", colorDarkRed,styleLine+styleNoRescale+styleNoLabel);
}

aHPivs = H - H;
aLPivs = L - L;
aHPivHighs = H - H;
aLPivLows = L - L;
aHPivIdxs = H - H;
aLPivIdxs = L - L;
aAddedHPivs = H - H;
aAddedLPivs = L - L;
aLegVol = H - H;
aRetrcVol = H - H;

nHPivs = 0;
nLPivs = 0;

lastHPIdx = 0;
lastLPIdx = 0;
lastHPH = 0;
lastLPL = 0;
curPivBarIdx = 0;

aHHVBars = HHVBars(H, nBars);
aLLVBars = LLVBars(L, nBars);
aHHV = HHV(H, nBars);
aLLV = LLV(L, nBars);

nLastVisBar = LastValue(
Highest(IIf(Status("barvisible"), BarIndex(), 0)));

curBar = IIf(nlastVisBar > 0 AND bUseLastVis, nlastVisBar,
IIf(Status("action")==4 AND nExploreBarIdx > 0, nExploreBarIdx,
LastValue(BarIndex())));

curTrend = "";
if (aLLVBars[curBar] < aHHVBars[curBar])
curTrend = "D";
else
curTrend = "U";

if (curBar >= nNumBarsToScan) {
for (i=0; i<nNumBarsToScan; i++) {

curBar = IIf(nlastVisBar > 0 AND bUseLastVis,
nlastVisBar-i,
IIf(Status("action")==4 AND nExploreBarIdx > 0,
nExploreBarIdx-i,
LastValue(BarIndex())-i));

if (aLLVBars[curBar] < aHHVBars[curBar]) {

if (curTrend == "U") {
curTrend = "D";
curPivBarIdx = curBar - aLLVBars[curBar];
aLPivs[curPivBarIdx] = 1;
aLPivLows[nLPivs] = L[curPivBarIdx];
aLPivIdxs[nLPivs] = curPivBarIdx;
nLPivs++;
}
} else {
if (curTrend == "D") {
curTrend = "U";
curPivBarIdx = curBar - aHHVBars[curBar];
aHPivs[curPivBarIdx] = 1;
aHPivHighs[nHPivs] = H[curPivBarIdx];
aHPivIdxs[nHPivs] = curPivBarIdx;
nHPivs++;
}
}

}
}

curBar =
IIf(nlastVisBar > 0 AND bUseLastVis,nlastVisBar,
IIf(Status("action")==4 AND nExploreBarIdx > 0,
nExploreBarIdx,LastValue(BarIndex())));

if (nHPivs >= 2 AND nLPivs >= 2) {

lastLPIdx = aLPivIdxs[0];
lastLPL = aLPivLows[0];

lastHPIdx = aHPivIdxs[0];
lastHPH = aHPivHighs[0];

nLastHOrLPivIdx = Max(lastLPIdx, lastHPIdx);

nAddPivsRng = curBar - nLastHOrLPivIdx;
aLLVAfterLastPiv = LLV(L, nAddPivsRng);
nLLVAfterLastPiv = aLLVAfterLastPiv[curBar];
aLLVIdxAfterLastPiv = LLVBars(L, nAddPivsRng);
nLLVIdxAfterLastPiv = curBar - aLLVIdxAfterLastPiv[curBar];
aHHVAfterLastPiv = HHV(H, nAddPivsRng);
nHHVAfterLastPiv = aHHVAfterLastPiv[curBar];
aHHVIdxAfterLastPiv = HHVBars(H, nAddPivsRng);
nHHVIdxAfterLastPiv = curBar - aHHVIdxAfterLastPiv[curBar];



if (lastHPIdx > lastLPIdx) {


if (aHPivHighs[0] < aHPivHighs[1]) {

if (nLLVAfterLastPiv < aLPivLows[0] AND
(nLLVIdxAfterLastPiv - lastHPIdx - 1) >= nMinBarsBtwPivs
AND nLLVIdxAfterLastPiv != curBar ) {


aLPivs[nLLVIdxAfterLastPiv] = 1;
aAddedLPivs[nLLVIdxAfterLastPiv] = 1;


for (j=0; j<nLPivs; j++) {
aLPivLows[nLPivs-j] = aLPivLows[nLPivs-(j+1)];
aLPivIdxs[nLPivs-j] = aLPivIdxs[nLPivs-(j+1)];
}
aLPivLows[0] = nLLVAfterLastPiv;
aLPivIdxs[0] = nLLVIdxAfterLastPiv;
nLPivs++;


}


} else {

if (nLLVAfterLastPiv > aLPivLows[0] AND
(nLLVIdxAfterLastPiv - lastHPIdx - 1) >= nMinBarsBtwPivs
AND nLLVIdxAfterLastPiv != curBar ) {


aLPivs[nLLVIdxAfterLastPiv] = 1;
aAddedLPivs[nLLVIdxAfterLastPiv] = 1;

for (j=0; j<nLPivs; j++) {
aLPivLows[nLPivs-j] = aLPivLows[nLPivs-(j+1)];
aLPivIdxs[nLPivs-j] = aLPivIdxs[nLPivs-(j+1)];
}
aLPivLows[0] = nLLVAfterLastPiv;
aLPivIdxs[0] = nLLVIdxAfterLastPiv;
nLPivs++;


}

}


} else {

if (aHPivHighs[0] < aHPivHighs[1]) {

if (nHHVAfterLastPiv < aHPivHighs[0] AND
(nHHVIdxAfterLastPiv - lastLPIdx - 1) >= nMinBarsBtwPivs
AND nHHVIdxAfterLastPiv != curBar ) {


aHPivs[nHHVIdxAfterLastPiv] = 1;
aAddedHPivs[nHHVIdxAfterLastPiv] = 1;

for (j=0; j<nHPivs; j++) {
aHPivHighs[nHPivs-j] = aHPivHighs[nHPivs-(j+1)];
aHPivIdxs[nHPivs-j] = aHPivIdxs[nhPivs-(j+1)];
}
aHPivHighs[0] = nHHVAfterLastPiv;
aHPivIdxs[0] = nHHVIdxAfterLastPiv;
nHPivs++;


}


} else {

if (nHHVAfterLastPiv > aHPivHighs[0] AND
(nHHVIdxAfterLastPiv - lastLPIdx - 1) >= nMinBarsBtwPivs
AND nHHVIdxAfterLastPiv != curBar ) {


aHPivs[nHHVIdxAfterLastPiv] = 1;
aAddedHPivs[nHHVIdxAfterLastPiv] = 1;


for (j=0; j<nHPivs; j++) {
aHPivHighs[nHPivs-j] = aHPivHighs[nHPivs-(j+1)];
aHPivIdxs[nHPivs-j] = aHPivIdxs[nhPivs-(j+1)];
}
aHPivHighs[0] = nHHVAfterLastPiv;
aHPivIdxs[0] = nHHVIdxAfterLastPiv;
nHPivs++;


}

}

}

}



if (bDraw) {

// -- OK, let's plot the pivots using arrows
PlotShapes(IIf(aHPivs==1, shapeDownArrow, shapeNone), colorRed, 0, High, Offset=-15);
//PlotShapes(IIf(aAddedHPivs==1, shapeDownArrow, shapeNone), colorDarkRed, 0, High, Offset=-15);
PlotShapes(IIf(aLPivs==1, shapeUpArrow , shapeNone),colorGreen, 0, Low, Offset=-15);
//PlotShapes(IIf(aAddedLPivs==1, shapeUpArrow , shapeNone), colorDarkGreen, 0, Low, Offset=-15);
}

_SECTION_END();
 
the script that is needed to look at is this part from Gordon Rose ... :
try my way my works excellent in 5 min time frame and in longer too

Code:
//Module that generates buy/short signals
//Original work created by Abnash Singh abnash1978
//Digital filter logic is acknowledged to the creator Rajendran 
Yearabove=Param("From Year",2012,2000,2020,1);
Monthabove=Param("From Month",0,0,12,1);
Dateabove=Param("From Date",0,0,31,1);



y = Year(); 
m = Month(); 
d = Day(); 
r = Hour();
e = Minute();
dnum=(m+10)*100+d;
tnum=TimeNum()+100000;

 sby = SelectedValue( y );
 sbm = SelectedValue( m );
 sbd = SelectedValue( d );
 sbr = SelectedValue( r );
 sbe = SelectedValue( e );
 sdnum = SelectedValue( dnum );
 stnum = SelectedValue( tnum );
//=****************************Put Your Buy/Short logic below this line
_SECTION_BEGIN("_Butterworth Filter");


CutoffPeriod=15;
Shift=0; 
coef1=coef2=coef3=coef4=0;



 tempReal= atan(1.0);
 rad2Deg = 45/tempReal;
 deg2Rad = 1.0/ rad2Deg;
 pi = atan(1.0)* 4;
 a1 = exp(-sqrt(2) * pi / CutoffPeriod);
 b1 = 2 * a1 * cos(deg2Rad * sqrt(2) * 180 / CutoffPeriod);
 
coef2 = b1;
coef3 = -a1 * a1;
coef1 = (1 - b1 + a1 * a1) / 4;
mean=C;

TPBFilter[0]=Null;
up[0]=Null;
down[0]=Null;

 for(i = 1; i <BarCount; i++){
      if(i>3){
         TBFilter[i]=coef1 *(mean[i]+2.0*mean[i-1] +mean[i-2])+coef2*TBFilter[i-1]+coef3*TBFilter[i-2];
      }else{
         TBFilter[i]=mean[i];
      }
      if(TBFilter[i]>TBFilter[i-1]){
         up[i]=TBFilter[i];
         up[i-1]=TBFilter[i-1];
      }else{
         down[i]=TBFilter[i];
         down[i-1]=TBFilter[i-1];
      }
   }

 for(i = 0; i <4; i++)
 {
	TBFilter[i]=Null;
 }

Plot(TBFilter,"Signals with a Digital Filter",IIf(down==0,colorGreen,colorRed),styleDashed);

_SECTION_END();
_SECTION_BEGIN("_EMA");
nn=Param("Number of EMA periods",30,5,50,1);
usema=ParamList("MA|EMA|WMA","MA|EMA|WMA");
if (usema=="MA")
	EMA10=MA(C,nn);
else if (usema=="EMA")
	EMA10=EMA(C,nn);
else if (usema=="WMA")
	EMA10=WMA(C,nn);


Plot(EMA10,usema+WriteVal(nn,1.0),colorGreen);


Buy=Cross(tbfilter,emA10);
Short=Cross(emA10,tbfilter);

shapebuy = IIf(Buy,shapeUpArrow,Null);
shapeshort = IIf(Short,shapeDownArrow,Null);

PlotShapes( shapeshort, colorRed, 0, High+10);
PlotShapes( shapebuy, colorGreen, 0, Low-10);
_SECTION_END();

//=****************************End your Buy/SHort logic above this line

for (i=1;i<BarCount-1;i++)
{
	if ((Buy[i] OR Short[i]) AND y[i]>=Yearabove AND (Monthabove==0 OR m[i]>=Monthabove) AND (Dateabove==0 OR d[i]>=Dateabove))
	{
		Varname=Name()+"X"+y[i]+dnum[i]+tnum[i];
		if (Buy[i])StaticVarSet(Varname,1);
		if (Short[i])StaticVarSet(Varname,-2);
	}
}
good luck
 
thanks my friend test123 for sharing the above script very good job.

but I still need to know how to subtract the value of the last high from the previous low ... appreciate the help.
truly,
ztop
 
does anyone have an afl to chart a spread between any 2 futures contracts....for pair trading
eg:am talking about making candlestick charts of tatasteel-jswsteel, nifty-banknifty, pnb-bob..........
amibroker has a built in way of doing this....but its quite something else and does not produce candles with ohlc values, and also am not looking for ratio spreads like the nifty-banknifty ratio chart

any ideas anyone? it should be possible coz i've come across afl for iron condor spread charts in amibroker(http://www.marketcalls.in/amibroker/iron-condor-spread-amibroker-afl-code.html)
 

hmp

Well-Known Member
_SECTION_BEGIN("Gordon Rose");

nBars = Param("Number of bars", 12, 5, 40);
bTrace = Param("Include trace output", 1, 0, 1);
nNoPivsInSetup = Param("No. Pivs in Setup", 4, 3, 4, 1);
bShowTCZ = Param("Show TCZ", 1, 0, 1);
nMinBarsBtwPivs = Param("Min. number of bars btw. pivots", 1, 1, 10, 1);
nMinPctBtwPivs = Param("Min. percent diff. btw. pivots", .05, .04, .2, .01);
bLastBarCanBePiv = Param("Last bar can be a pivot", 1, 0, 1);
retrcTolerance = .01;
tczTolerance = .005;
nNumBarsToScan = 120;

nExploreBarIdx = 0;
nExploreDate = 0;
nCurDateNum = 0;
DN = DateNum();
DT = DateTime();

bTCZLong = False;
bTCZShort = False;
nAnchorPivIdx = 0;

ADX8 = ADX(8);

if(Status("action")==1) {
bDraw = True;
bUseLastVis = Param("Use last visible bar", 1, 0, 1);
} else {
bDraw = False;
bUseLastVis = False;
bTrace = False;
nExploreDate = Status("rangetodate");
for (i=LastValue(BarIndex());i>=0;i--) {
nCurDateNum = DN;
if (nCurDateNum == nExploreDate) {
nExploreBarIdx = i;
}
}
}

GraphXSpace=7;


PlotOHLC(Open, High, Low, Close,
"BIdx = " + BarIndex() +
"\n" + "O = " + O + "\n"+"H = "+ H + "\n"+"L = " + L
+ "\n"+"C ",
colorBlack, styleCandle);

if (bDraw) {
//Plot(MA(C, 21), "21 bar MA", colorAqua,styleLine+styleNoRescale+styleNoLabel);
//Plot(MA(C, 55), "55 bar MA", colorGreen,styleLine+styleNoRescale+styleNoLabel);
//Plot(MA(C, 233), "233 bar MA", colorDarkRed,styleLine+styleNoRescale+styleNoLabel);
}

aHPivs = H - H;
aLPivs = L - L;
aHPivHighs = H - H;
aLPivLows = L - L;
aHPivIdxs = H - H;
aLPivIdxs = L - L;
aAddedHPivs = H - H;
aAddedLPivs = L - L;
aLegVol = H - H;
aRetrcVol = H - H;

nHPivs = 0;
nLPivs = 0;

lastHPIdx = 0;
lastLPIdx = 0;
lastHPH = 0;
lastLPL = 0;
curPivBarIdx = 0;

aHHVBars = HHVBars(H, nBars);
aLLVBars = LLVBars(L, nBars);
aHHV = HHV(H, nBars);
aLLV = LLV(L, nBars);

nLastVisBar = LastValue(
Highest(IIf(Status("barvisible"), BarIndex(), 0)));

curBar = IIf(nlastVisBar > 0 AND bUseLastVis, nlastVisBar,
IIf(Status("action")==4 AND nExploreBarIdx > 0, nExploreBarIdx,
LastValue(BarIndex())));

curTrend = "";
if (aLLVBars[curBar] < aHHVBars[curBar])
curTrend = "D";
else
curTrend = "U";

if (curBar >= nNumBarsToScan) {
for (i=0; i<nNumBarsToScan; i++) {

curBar = IIf(nlastVisBar > 0 AND bUseLastVis,
nlastVisBar-i,
IIf(Status("action")==4 AND nExploreBarIdx > 0,
nExploreBarIdx-i,
LastValue(BarIndex())-i));

if (aLLVBars[curBar] < aHHVBars[curBar]) {

if (curTrend == "U") {
curTrend = "D";
curPivBarIdx = curBar - aLLVBars[curBar];
aLPivs[curPivBarIdx] = 1;
aLPivLows[nLPivs] = L[curPivBarIdx];
aLPivIdxs[nLPivs] = curPivBarIdx;
nLPivs++;
}
} else {
if (curTrend == "D") {
curTrend = "U";
curPivBarIdx = curBar - aHHVBars[curBar];
aHPivs[curPivBarIdx] = 1;
aHPivHighs[nHPivs] = H[curPivBarIdx];
aHPivIdxs[nHPivs] = curPivBarIdx;
nHPivs++;
}
}

}
}

curBar =
IIf(nlastVisBar > 0 AND bUseLastVis,nlastVisBar,
IIf(Status("action")==4 AND nExploreBarIdx > 0,
nExploreBarIdx,LastValue(BarIndex())));

if (nHPivs >= 2 AND nLPivs >= 2) {

lastLPIdx = aLPivIdxs[0];
lastLPL = aLPivLows[0];

lastHPIdx = aHPivIdxs[0];
lastHPH = aHPivHighs[0];

nLastHOrLPivIdx = Max(lastLPIdx, lastHPIdx);

nAddPivsRng = curBar - nLastHOrLPivIdx;
aLLVAfterLastPiv = LLV(L, nAddPivsRng);
nLLVAfterLastPiv = aLLVAfterLastPiv[curBar];
aLLVIdxAfterLastPiv = LLVBars(L, nAddPivsRng);
nLLVIdxAfterLastPiv = curBar - aLLVIdxAfterLastPiv[curBar];
aHHVAfterLastPiv = HHV(H, nAddPivsRng);
nHHVAfterLastPiv = aHHVAfterLastPiv[curBar];
aHHVIdxAfterLastPiv = HHVBars(H, nAddPivsRng);
nHHVIdxAfterLastPiv = curBar - aHHVIdxAfterLastPiv[curBar];



if (lastHPIdx > lastLPIdx) {


if (aHPivHighs[0] < aHPivHighs[1]) {

if (nLLVAfterLastPiv < aLPivLows[0] AND
(nLLVIdxAfterLastPiv - lastHPIdx - 1) >= nMinBarsBtwPivs
AND nLLVIdxAfterLastPiv != curBar ) {


aLPivs[nLLVIdxAfterLastPiv] = 1;
aAddedLPivs[nLLVIdxAfterLastPiv] = 1;


for (j=0; j<nLPivs; j++) {
aLPivLows[nLPivs-j] = aLPivLows[nLPivs-(j+1)];
aLPivIdxs[nLPivs-j] = aLPivIdxs[nLPivs-(j+1)];
}
aLPivLows[0] = nLLVAfterLastPiv;
aLPivIdxs[0] = nLLVIdxAfterLastPiv;
nLPivs++;


}


} else {

if (nLLVAfterLastPiv > aLPivLows[0] AND
(nLLVIdxAfterLastPiv - lastHPIdx - 1) >= nMinBarsBtwPivs
AND nLLVIdxAfterLastPiv != curBar ) {


aLPivs[nLLVIdxAfterLastPiv] = 1;
aAddedLPivs[nLLVIdxAfterLastPiv] = 1;

for (j=0; j<nLPivs; j++) {
aLPivLows[nLPivs-j] = aLPivLows[nLPivs-(j+1)];
aLPivIdxs[nLPivs-j] = aLPivIdxs[nLPivs-(j+1)];
}
aLPivLows[0] = nLLVAfterLastPiv;
aLPivIdxs[0] = nLLVIdxAfterLastPiv;
nLPivs++;


}

}


} else {

if (aHPivHighs[0] < aHPivHighs[1]) {

if (nHHVAfterLastPiv < aHPivHighs[0] AND
(nHHVIdxAfterLastPiv - lastLPIdx - 1) >= nMinBarsBtwPivs
AND nHHVIdxAfterLastPiv != curBar ) {


aHPivs[nHHVIdxAfterLastPiv] = 1;
aAddedHPivs[nHHVIdxAfterLastPiv] = 1;

for (j=0; j<nHPivs; j++) {
aHPivHighs[nHPivs-j] = aHPivHighs[nHPivs-(j+1)];
aHPivIdxs[nHPivs-j] = aHPivIdxs[nhPivs-(j+1)];
}
aHPivHighs[0] = nHHVAfterLastPiv;
aHPivIdxs[0] = nHHVIdxAfterLastPiv;
nHPivs++;


}


} else {

if (nHHVAfterLastPiv > aHPivHighs[0] AND
(nHHVIdxAfterLastPiv - lastLPIdx - 1) >= nMinBarsBtwPivs
AND nHHVIdxAfterLastPiv != curBar ) {


aHPivs[nHHVIdxAfterLastPiv] = 1;
aAddedHPivs[nHHVIdxAfterLastPiv] = 1;


for (j=0; j<nHPivs; j++) {
aHPivHighs[nHPivs-j] = aHPivHighs[nHPivs-(j+1)];
aHPivIdxs[nHPivs-j] = aHPivIdxs[nhPivs-(j+1)];
}
aHPivHighs[0] = nHHVAfterLastPiv;
aHPivIdxs[0] = nHHVIdxAfterLastPiv;
nHPivs++;


}

}

}

}



if (bDraw) {

// -- OK, let's plot the pivots using arrows
PlotShapes(IIf(aHPivs==1, shapeDownArrow, shapeNone), colorRed, 0, High, Offset=-15);
//PlotShapes(IIf(aAddedHPivs==1, shapeDownArrow, shapeNone), colorDarkRed, 0, High, Offset=-15);
PlotShapes(IIf(aLPivs==1, shapeUpArrow , shapeNone),colorGreen, 0, Low, Offset=-15);
//PlotShapes(IIf(aAddedLPivs==1, shapeUpArrow , shapeNone), colorDarkGreen, 0, Low, Offset=-15);
}

_SECTION_END();


AFL looks into future? arrows appear/disappear after 3/4 cndles.So no use.
 

amitrandive

Well-Known Member
does anyone have an afl to chart a spread between any 2 futures contracts....for pair trading
eg:am talking about making candlestick charts of tatasteel-jswsteel, nifty-banknifty, pnb-bob..........
amibroker has a built in way of doing this....but its quite something else and does not produce candles with ohlc values, and also am not looking for ratio spreads like the nifty-banknifty ratio chart

any ideas anyone? it should be possible coz i've come across afl for iron condor spread charts in amibroker(http://www.marketcalls.in/amibroker/iron-condor-spread-amibroker-afl-code.html)
Check this

http://www.traderji.com/amibroker/94761-arbitrage-trading-afl.html#post984803
 
Hello happyji,
I need a help from you to write AFL for the conditions below:

01.BUY/SELL/BUY COVER/SHORT COVER signal generations, while touching
manually drawn line in the price chart.

02.STOPLOSS- while touching manually drawn line.

Thanks in advance.:clap:
 

Similar threads