Simple Coding Help - No Promise.

Hello Friends

I am starting this thread for helping anyone who requires small changes in AFLs or AFL coding done for small / simple requirements that can be clearly defined.

What does not go is conversion . . . from Amibroker to anything else . . . or from anything else to Amibroker . . .

Debugging requested for copy/paste AFLs, Please don't post huge AFLs with zillions of lines of code for debugging

I will not be available for any conversions and I am not interested in debugging some-one else's copy/pasted work and lastly . . .
Not interested in hacking/craking any ones work . . .

Only if have a clearly definable task to be coded I maybe able to help . . . No Promises but will try within my limitations . . .


As an example, recently one of our friends wanted a motivational message to be included in his Charts,
I enjoyed doing that, as i could easily empathize with why he wanted that . . .


Cheers

:) Happy
dear singh,

pls help me if u can . small correction for my own backtest

FOLLOWING NEEDS BELOW

BUY CONDITION: ALL BUY SIGNAL ONLY WILL TAKE HIGH+1 OF THE SIGNAL CANDLE .


SELL CONDITION: ALL SELL SIGNAL ONLY WILL TAKE LOW-1 OF THE SIGNAL CANDLE.




_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", colorBlack ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();



FirstTradeTime = 092500; // Earliest time to take a trade
LastTradeTime = 150000; // Latest time to take new trades
ExitAllPositionsTime = 152500; // Exit all trades

_SECTION_BEGIN("Advanced Trailstop System");



mult=Param("multi", 2.0, 0.1, 10, 0.05);
Aper=Param("ATR period", 7, 2, 100, 1 );
atrvalue=(mult*ATR(aper));

Bs[0]=0;//=L;
L[0]=0;
C[0]=0;

for( i = 9; i < BarCount; i++ )
{
if ( L[ i ] > L[ i - 1 ] AND L[ i ] > L[ i - 2 ] AND L[ i ] > L[ i - 3 ] AND L[ i ] > L[ i - 4 ] AND
L[ i ] > L[ i - 5 ] AND L[ i ] > L[ i - 6 ] AND L[ i ] > L[ i - 7 ] AND L[ i ] > L[ i - 8 ] AND
L[ i ] > L[ i - 9 ] AND C[ i ] > bs[ i - 1 ])
{
bs[ i ] = L[ i ] - ATRvalue[ i ] ;

}
else
{
if ( H[ i ] < H[ i - 1 ] AND H[ i ] < H[ i - 2 ] AND H[ i ] < H[ i - 3 ] AND H[ i ] < H[ i - 4 ] AND
H[ i ] < H[ i - 5 ] AND H[ i ] < H[ i - 6 ] AND H[ i ] < H[ i - 7 ] AND H[ i ] < H[ i - 8 ] AND
H[ i ] < H[ i - 9 ] AND C[ i ] < bs[ i - 1 ] )
{
bs[ i ] = H[ i ] + ATRvalue[ i ];

}
else
{
bs[ i ] = bs[ i - 1];
} }}

//====================================================
//Buy = Cross(EMA(C,per1),EMA(C,per2)) AND (TimeNum() >= FirstTradeTime AND TimeNum() <= LastTradeTime );
//Sell= Cross(EMA(C,per2),EMA(C,per1)) OR TimeNum() >= ExitAllPositionsTime;

//Buy=ExRem(Buy,Sell) ;
//Sell=ExRem(Sell,Buy);

Mycolor=IIf(C>bs,colorLime,colorRed);
bcolor=IIf(C>bs,colorBlue,colorBlue);
//PlotOHLC( Open, High, Low, Close, "", Mycolor, styleBar| styleThick );
Plot(bs,"ATS",bcolor,1|styleThick);
Buy=Cover=Cross(C,bs)AND (TimeNum() >= FirstTradeTime AND TimeNum() <= LastTradeTime );

Sell=Short=Cross(bs,C) OR TimeNum() >= ExitAllPositionsTime;

shape=Buy*shapeUpArrow + Sell*shapeDownArrow;
PlotShapes(shapeUpArrow*Buy, colorLime, 0, L, -95 );
PlotShapes(shapeDownArrow*Sell, colorRed, 0, H, -30 );
SellPrice=ValueWhen(Sell,C,1);
BuyPrice=ValueWhen(Buy,C,1);
Long=Flip(Buy,Sell);
Shrt=Flip(Sell,Buy );
_SECTION_END();
Filter= Buy OR Sell OR Short OR Cover;


Short=Sell AND (TimeNum() >= FirstTradeTime AND TimeNum() <= LastTradeTime );
Cover=Buy OR TimeNum() >= ExitAllPositionsTime;




//=================TITLE================================================================================================

_SECTION_BEGIN("Title");
if( Status("action") == actionIndicator )
(
Title = EncodeColor(colorWhite)+ "Advanced Trailstop System " + " - " + Name() + " - " + EncodeColor(colorRed)+ Interval(2) + EncodeColor(colorWhite) +
" - " + Date() +" - "+"\n" +EncodeColor(colorRed) +"Op-"+O+" "+"Hi-"+H+" "+"Lo-"+L+" "+
"Cl-"+C+" "+ "Vol= "+ WriteVal(V)+"\n"+
EncodeColor(colorLime)+
WriteIf (Buy , " GO LONG / Reverse Signal at "+C+" ","")+
WriteIf (Sell , " EXIT LONG / Reverse Signal at "+C+" ","")+"\n"+EncodeColor(colorWhite)+
WriteIf(Sell , "Total Profit/Loss for the Last Trade Rs."+(C-BuyPrice)+"","")+
WriteIf(Buy , "Total Profit/Loss for the Last trade Rs."+(SellPrice-C)+"","")+
WriteIf(Long AND NOT Buy, "Trade : Long - Entry price Rs."+(BuyPrice),"")+
WriteIf(shrt AND NOT Sell, "Trade : Short - Entry price Rs."+(SellPrice),"")+"\n"+
WriteIf(Long AND NOT Buy, "Current Profit/Loss Rs."+(C-BuyPrice)+"","")+
WriteIf(shrt AND NOT Sell, "Current Profit/Loss Rs."+(SellPrice-C)+"",""));
_SECTION_END();



SetPositionSize(1,spsShares);






//SetTradeDelays(1,1,1,1);
 

toughard

Well-Known Member
NOW trying to replace hourly OHLC with n bars

tried this----

NB=Param( "BARS", 12, 1, 55 );
HourH = HHV(H,NB); should be giving high from last 12 bars
HourL = LLV(L,NB); should be giving low from last 12 bars
HourC = Ref(C,NB); should be giving close of teh12th bar
HourO = Ref(O,-NB); should be giving open of 1st of these 12 bars


something wrong as I am not expert at these....

ANY EXPERTS ON BOARD PLEASE.......:eek:
 

amitrandive

Well-Known Member
NOW trying to replace hourly OHLC with n bars

tried this----

NB=Param( "BARS", 12, 1, 55 );
HourH = HHV(H,NB); should be giving high from last 12 bars
HourL = LLV(L,NB); should be giving low from last 12 bars
HourC = Ref(C,NB); should be giving close of teh12th bar
HourO = Ref(O,-NB); should be giving open of 1st of these 12 bars
I think you may want to try this code

Code:
Period = Param("Period", 25, 2, 100 ); 
bi = BarIndex(); 

procedure PlotLevel( array, nam, Color ) 
{ 
 x = SelectedValue( bi ); 
 y = SelectedValue( array ); 
 Plot( IIf( bi >= x AND bi < x + Period, y, Null ), 
       nam, Color, styleDashed ); 
 PlotText( nam, x + Period - 1, y, Color ); 
} 

pl = Ref( LLV( Low, Period ), -1 ); 
ph = Ref( HHV( High, Period ), -1 ); 
range = ph - pl; 
pc = Ref( Close, -1 ); 
pp=(pl+ph+pc)/3;

R2 = pp+range; 
R3 = ph+2*(pp-pl); 
R1 = 2*pp-pl; 
S1 =  2*pp-ph; 
S2 = pp-range; 
S3 = pl-2*(ph-pp);

Plot( C, Name() + " " + Date(), colorDefault, styleCandle ); 
PlotLevel(pp,"PivotPoint",colorYellow);
PlotLevel( R3, "R3", colorBlue ); 
PlotLevel( R2, "R2", colorBlue ); 
PlotLevel( R1, "R1", colorBlue ); 
PlotLevel( S1, "S1", colorRed ); 
PlotLevel( S2, "S2", colorRed ); 
PlotLevel( S3, "S3", colorRed );
 
Last edited:

Nehal_s143

Well-Known Member
Hi

I am got stuck in below two conditions badly, I tried solution given by Happy Sir and Trash sir in old post in several ways but got failed each time

please help to plot exit signal and sell signal as shown in snap





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() ); 
_SECTION_END();

_SECTION_BEGIN("SuperTrend Modi 15min");
tf=15;
tfrm=in1Minute*tf;
TimeFrameSet(tfrm);

kk = Optimize( "mult", Param( "mult", 2, 1, 6, 0.25 ), 1, 2, 0.25 );
Per = Optimize( "period", Param( "period", 10, 5, 50, 1 ), 5, 50, 1 );
sdfact = 2;
offset = 2;
tc = ParamList( "Channel Display", List = "No Channel|Channel|ChannelRT|Both
Channels", 1 );
ms = ParamToggle( "Trend", "Regular|Smoothed", 1 );

//x = Cum( 1 );
HaClose2 = ( O + H + L + C ) / 4;
HaOpen2 = AMA( Ref( HaClose2, -1 ), 0.5 );
HaHigh2 = Max( H, Max( HaClose2, HaOpen2 ) );
HaLow2 = Min( L, Min( HaClose2, HaOpen2 ) );

if ( ms == 0 )
{
nm = ( H - L );
J2 = ( O + H + L + C ) / 4;
}
else
{
nm = ( HaHigh2 - HaLow2 );
J2 = ( HaOpen2 + HaHigh2 + HaLow2 + HaClose2 ) / 4;
}

rfsctor = WMA( nm, Per );

revers = kk * rfsctor;
Trend = 1;
NW2[0] = 0;

for ( i = 1;i < BarCount;i++ )
{
if ( Trend[i-1] == 1 )
{
if ( J2[i] < NW2[i-1] )
{
Trend[i] = -1;
NW2[i] = J2[i] + Revers[i];
}
else
{
Trend[i] = 1;

if ( ( J2[i] - Revers[i] ) > NW2[i-1] )
{
NW2[i] = J2[i] - Revers[i];
}
else
{
NW2[i] = NW2[i-1];
}
}
}

if ( Trend[i-1] == -1 )
{
if ( J2[i] > NW2[i-1] )
{
Trend[i] = 1;
NW2[i] = J2[i] - Revers[i];
}
else
{
Trend[i] = -1;

if ( ( J2[i] + Revers[i] ) < NW2[i-1] )
{
NW2[i] = J2[i] + Revers[i];
}
else
{
NW2[i] = NW2[i-1];
}
}
}
}

cp = ( H + L ) / 2;

TimeFrameRestore();


Nwf2f = TimeFrameExpand( NW2, tfrm);
Jf2f = TimeFrameExpand( J2, tfrm);


mycNjtf=IIf(jf2f>NWf2f,colorBlue,
     IIf(jf2f<NWf2f,colorRed,31));

Plot( IIf( NWf2f < jf2f, NWf2f, Null ), "", ParamColor( "ColorTrailLong", colorBlue ), styleLine | styleLine );
Plot( IIf( NWf2f > jf2f, NWf2f, Null ), "", ParamColor( "ColorTrailShort", colorRed ), styleLine | styleLine );
 
_SECTION_END();

n = Param("Ratio 2 Adjust Band",2,0.1,8,0.1);
R = H-L;  eR = MA(R/n,20);

NWT=NWf2f - eR;
NWB= NWf2f + eR;

Plot(NWT, "",colorPink, styleLine | styleLine);
Plot(NWB, "",colorPink, styleLine | styleLine);

// touch 
touchExitShort = H > (NWf2f);
touchExitLong = L < (NWf2f);

Buy1 = jf2f>NWf2f;
Sell1 = touchExitLong;
Short1 = jf2f<NWf2f;
Cover1 = touchExitShort;

Buy1=ExRem(Buy1,Sell1);
Short1=ExRem(Short1,Cover1);
Sell1=ExRem(Sell1,Buy1);
Cover1=ExRem(Cover1,Short1);

PlotShapes(Buy1* shapeUpArrow,colorBlue, 0,L, Offset=-45); 
PlotShapes(Short1* shapeDownArrow,colorRed, 0,H, Offset=-45);
PlotShapes(Sell1*shapeHollowDownArrow,colorPink,0,L,-56);
PlotShapes(Cover1*shapeHollowUpArrow,colorSkyblue,0,H,-56);


Buy2 = jf2f>NWf2f AND Cross(C,NWT);
Sell2 = touchExitLong;;
Short2 = jf2f<NWf2f AND Cross(C,NWB);
Cover2 = touchExitShort;

Buy2=ExRem(Buy2,Sell2);
Short2=ExRem(Short2,Cover2);
Sell2=ExRem(Sell2,Buy2);
Cover2=ExRem(Cover2,Short2);

PlotShapes(Buy2* shapeUpArrow,colorGreen, 0,L, Offset=-51); 
PlotShapes(Short2* shapeDownArrow,colorOrange, 0,H, Offset=-51);
PlotShapes(Sell2*shapeHollowDownArrow,colorPink,0,L,-61);
PlotShapes(Cover2*shapeHollowUpArrow,colorSkyblue,0,H,-61);
 

pratapvb

Well-Known Member
Hi

I am got stuck in below two conditions badly, I tried solution given by Happy Sir and Trash sir in old post in several ways but got failed each time

please help to plot exit signal and sell signal as shown in snap





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() ); 
_SECTION_END();

_SECTION_BEGIN("SuperTrend Modi 15min");
tf=15;
tfrm=in1Minute*tf;
TimeFrameSet(tfrm);

kk = Optimize( "mult", Param( "mult", 2, 1, 6, 0.25 ), 1, 2, 0.25 );
Per = Optimize( "period", Param( "period", 10, 5, 50, 1 ), 5, 50, 1 );
sdfact = 2;
offset = 2;
tc = ParamList( "Channel Display", List = "No Channel|Channel|ChannelRT|Both
Channels", 1 );
ms = ParamToggle( "Trend", "Regular|Smoothed", 1 );

//x = Cum( 1 );
HaClose2 = ( O + H + L + C ) / 4;
HaOpen2 = AMA( Ref( HaClose2, -1 ), 0.5 );
HaHigh2 = Max( H, Max( HaClose2, HaOpen2 ) );
HaLow2 = Min( L, Min( HaClose2, HaOpen2 ) );

if ( ms == 0 )
{
nm = ( H - L );
J2 = ( O + H + L + C ) / 4;
}
else
{
nm = ( HaHigh2 - HaLow2 );
J2 = ( HaOpen2 + HaHigh2 + HaLow2 + HaClose2 ) / 4;
}

rfsctor = WMA( nm, Per );

revers = kk * rfsctor;
Trend = 1;
NW2[0] = 0;

for ( i = 1;i < BarCount;i++ )
{
if ( Trend[i-1] == 1 )
{
if ( J2[i] < NW2[i-1] )
{
Trend[i] = -1;
NW2[i] = J2[i] + Revers[i];
}
else
{
Trend[i] = 1;

if ( ( J2[i] - Revers[i] ) > NW2[i-1] )
{
NW2[i] = J2[i] - Revers[i];
}
else
{
NW2[i] = NW2[i-1];
}
}
}

if ( Trend[i-1] == -1 )
{
if ( J2[i] > NW2[i-1] )
{
Trend[i] = 1;
NW2[i] = J2[i] - Revers[i];
}
else
{
Trend[i] = -1;

if ( ( J2[i] + Revers[i] ) < NW2[i-1] )
{
NW2[i] = J2[i] + Revers[i];
}
else
{
NW2[i] = NW2[i-1];
}
}
}
}

cp = ( H + L ) / 2;

TimeFrameRestore();


Nwf2f = TimeFrameExpand( NW2, tfrm);
Jf2f = TimeFrameExpand( J2, tfrm);


mycNjtf=IIf(jf2f>NWf2f,colorBlue,
     IIf(jf2f<NWf2f,colorRed,31));

Plot( IIf( NWf2f < jf2f, NWf2f, Null ), "", ParamColor( "ColorTrailLong", colorBlue ), styleLine | styleLine );
Plot( IIf( NWf2f > jf2f, NWf2f, Null ), "", ParamColor( "ColorTrailShort", colorRed ), styleLine | styleLine );
 
_SECTION_END();

n = Param("Ratio 2 Adjust Band",2,0.1,8,0.1);
R = H-L;  eR = MA(R/n,20);

NWT=NWf2f - eR;
NWB= NWf2f + eR;

Plot(NWT, "",colorPink, styleLine | styleLine);
Plot(NWB, "",colorPink, styleLine | styleLine);

// touch 
touchExitShort = H > (NWf2f);
touchExitLong = L < (NWf2f);

Buy1 = jf2f>NWf2f;
Sell1 = touchExitLong;
Short1 = jf2f<NWf2f;
Cover1 = touchExitShort;

Buy1=ExRem(Buy1,Sell1);
Short1=ExRem(Short1,Cover1);
Sell1=ExRem(Sell1,Buy1);
Cover1=ExRem(Cover1,Short1);

PlotShapes(Buy1* shapeUpArrow,colorBlue, 0,L, Offset=-45); 
PlotShapes(Short1* shapeDownArrow,colorRed, 0,H, Offset=-45);
PlotShapes(Sell1*shapeHollowDownArrow,colorPink,0,L,-56);
PlotShapes(Cover1*shapeHollowUpArrow,colorSkyblue,0,H,-56);


Buy2 = jf2f>NWf2f AND Cross(C,NWT);
Sell2 = touchExitLong;;
Short2 = jf2f<NWf2f AND Cross(C,NWB);
Cover2 = touchExitShort;

Buy2=ExRem(Buy2,Sell2);
Short2=ExRem(Short2,Cover2);
Sell2=ExRem(Sell2,Buy2);
Cover2=ExRem(Cover2,Short2);

PlotShapes(Buy2* shapeUpArrow,colorGreen, 0,L, Offset=-51); 
PlotShapes(Short2* shapeDownArrow,colorOrange, 0,H, Offset=-51);
PlotShapes(Sell2*shapeHollowDownArrow,colorPink,0,L,-61);
PlotShapes(Cover2*shapeHollowUpArrow,colorSkyblue,0,H,-61);
My understanding is that you want to buy above bar that closes above upper pink, short below lower pink.....cover high above lower pink and sell low below upper pink. Correct?

Because if that is the case I don't think 2 sets of buy short etc are required
 
below is simple afl system. i wanted to filter some signals. buy , sell signals are coming on candle close. i need buy when price crosses buy signal bar high and sell when price crosses sell signal bar low.. please help..



_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() );
_SECTION_END();

_SECTION_BEGIN("Simple EMA Crossover Trading System");

EMA1 = EMA( C,20);
EMA2 = EMA( C,50);

/*Buy and Sell Condition */
Buy = Cross(EMA1,EMA2);
Sell = Cross(EMA2,EMA1);

/* Plot Buy and Sell Signal Arrows */
shape = Buy * shapeUpArrow + Sell * shapeDownArrow;
PlotShapes( shape, IIf( Buy, colorGreen, colorRed ), 0, IIf( Buy, Low, High ) );
GraphXSpace = 5;

/*Plot EMA lines and Candles */
Plot(EMA1,"EMA",colorRed);
Plot(EMA2,"EMA2",colorGreen);
Plot(C,"Close",colorRed,styleCandle);

_SECTION_END();
 

amitrandive

Well-Known Member
below is simple afl system. i wanted to filter some signals. buy , sell signals are coming on candle close. i need buy when price crosses buy signal bar high and sell when price crosses sell signal bar low.. please help..



_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 | .
.
.

Plot(C,"Close",colorRed,styleCandle);

_SECTION_END();
If you plot support and resistance lines,this might help.

http://www.traderji.com/amibroker/92019-price-crossing-level-alerts.html#post928173
 

amitrandive

Well-Known Member
thanks for help..i have just plotted that lines manually to show what i need. i just need to break of high of buy signal candle and break low of the candle of sell signal. i want to backtest some systems applying this filter. its simple thing but i am weak in coding..
Found and modified this one,try this now.
Code:
SetChartOptions(0,chartShowDates|chartShowArrows|chartLogarithmic|chartWrapTitle);
GraphXSpace = 5;
Plot(C,"",colorBlack,styleCandle);
x = Ref(H,-1);
Y = Ref(L,-1);
a=x+1;
b=y-1;
aa=EMA(C,20);
bb=EMA(C,50);
Buy = Cover = C>a AND aa>bb;
Sell = Short = C<b AND aa<bb;
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
PlotShapes(IIf(Buy,shapeUpArrow,shapeNone),colorBlue,0,L,-15);
PlotShapes(IIf(Sell,shapeDownArrow,shapeNone),colorRed,0,H,-15);
 
Last edited:

Similar threads