Simple Coding Help - No Promise.

Hi Happy_Singh Ji,

my AFL is not showing the BUY or SHORT signal after the close of the first candle of the day i.e if i am using 5mn chart & from previous day the ATR trailing stop-loss indicator is LONG & then after the opening bell & after close of first candle (5m or 7m) if ATR trailing stop-loss is still in green a BUY signal should have been show.... amibroker is not picking up the signal.... can you help with his AFL...

in the attached 5m charts of BankNIfty at the close of first 5m candle the indicator is SHORT & the amibroker/AFL should have triggered a new fresh SHORT for the day.... but it is not picking up the signal.... what could be wrong...
ATR-trailing_1.jpg


_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) Vol " +WriteVal( V, 1.0 ) +" {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 )) ));


barcolor = IIf(C >= O,colorGreen,colorRed);
SetBarFillColor(IIf(C > O,ParamColor("Candle UP Color", colorGreen),IIf(C <= O,ParamColor("Candle Down Color", colorRed),colorLightGrey)));
PlotOHLC( O, H, L, C, "", barcolor, styleCandle );
_SECTION_END();


_SECTION_BEGIN("SuperTrend");

SetTradeDelays(1,1,1,1);

function ParamOptimize( pname, defaultval, minv, maxv, step )
{
return Optimize( pname,
Param( pname, defaultval, minv, maxv, step ),
minv, maxv, step );
}

StopATRPeriod = ParamOptimize("ATR period", 21, 13, 34,1 );
StopATRFactor = ParamOptimize("ATR multiple", 3.4, 2.1, 5, 0.1 );


HL = H - L;
MAHL = 1.5 * MA( HL, StopATRPeriod );
HiLo = IIf( HL < MAHL, HL, MAHL );
H1 = Ref( H, -1 );
L1 = Ref( L, -1 );
C1 = Ref( C, -1 );
Href = IIf( L <= H1, H - C1, ( H - C1 ) - ( L - H1 ) / 2 );
Lref = IIf( H >= L1, C1 - L, ( C1 - L ) - ( L1 - H ) / 2 );

diff1 = Max( HiLo, HRef );
diff2 = Max( diff1, LRef );

ATRmod = Wilders( diff2, StopATRPeriod );

sup = C - StopATRFactor * ATRmod ;
res = C + StopATRFactor * ATRmod ;
//}

// calculate trailing stop line
trailARRAY = Null;
trailstop = 0;
for( i = 1; i < BarCount; i++ )
{
//if( Started[ i ] == 0 ) continue;

if( C[ i ] > trailstop AND C[ i - 1 ] > trailstop )
trailstop = Max( trailstop, sup[ i ] );
else
if( C[ i ] < trailstop AND C[ i - 1 ] < trailstop )
trailstop = Min( trailstop, res[ i ] );
else
trailstop = IIf( C[ i ] > trailstop, sup[ i ], res[ i ] );

trailARRAY[ i ] = trailstop;
}


Buy_ATR_TSL = Cross( C, trailArray );
Sell_ATR_TSL = Cross( trailArray, C );

Short_ATR_TSL = Sell_ATR_TSL;
Cover_ATR_TSL = Buy_ATR_TSL;


tn = TimeNum();
startTime = 91500; // start in HHMMSS format
endTime = 150559; // end in HHMMSS format
endTime2 = 151400; // end2 in HHMMSS format for session exit
timeOK = tn >= startTime AND tn <= endTime;

Buy = BUY_ATR_TSL AND timeOK;
regular_sell = Sell_ATR_TSL AND timeOK;
session_exit = Cross(tn, endTime2);
Sell = regular_sell OR session_exit;

Short = Short_ATR_TSL AND timeOK;
regular_buy = Buy_ATR_TSL AND timeOK;
session_exit = Cross(tn, endTime2);
Cover = regular_buy OR session_exit;

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

cover=ExRem(cover,short);
short=ExRem(short,cover);

Filter=Buy OR Sell;

BuyPrice=ValueWhen(BUY,C);
SellPrice=ValueWhen(SELL,C);
ShortPrice=ValueWhen(SHORT,C);
CoverPrice=ValueWhen(Cover,C);

TRAILCOLOR =IIf(C>trailARRAY,colorDarkGreen ,colorRed);
Plot( trailARRAY,"trailing stop level", TRAILCOLOR, styleStaircase | styleThick );

PlotShapes( IIf( Buy, shapeSquare, shapeNone ), colorGreen, 0, L, Offset = -40 );
PlotShapes( IIf( Buy, shapeSquare, shapeNone ), colorLime, 0, L, Offset = -50 );
PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorWhite, 0, L, Offset = -45 );
PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorRed, 0, H, Offset = -65 );
PlotShapes( IIf( Short, shapeSquare, shapeNone ), colorRed, 0, H, Offset = 40 );
PlotShapes( IIf( Short, shapeSquare, shapeNone ), colorOrange, 0, H, Offset = 50 );
PlotShapes( IIf( Short, shapeDownArrow, shapeNone ), colorWhite, 0, H, Offset = -45 );
PlotShapes( IIf( Cover, shapeUpArrow, shapeNone ), colorBlue, 0, L, Offset = -65 );


AddColumn(C,"Close");
AddColumn(trailArray,"trailArray");
AddColumn(trailstop,"trailstop");

_SECTION_END();
 

trash

Well-Known Member
Plot(OBV(),"obv",colorWhite,styleThick); O_B_V = 0; // replace this line with next line to get exact OBV figures //O_B_V[0] = V[0]; for (i = 1; i < BarCount; i++) { if(C > C[i-1]) O_B_V = O_B_V[i-1] + V; else if(C < C[i-1]) O_B_V = O_B_V[i-1] - V; else O_B_V = O_B_V[i-1]; } Plot (O_B_V,"OBV2",colorGreen, styleThick);
It is obvious once again that you do not have slightest clue on how to apply AFL properly.
Contrary to that more clue on stealing AmiBroker.
What a tool.
 

john302928

Well-Known Member
Hi
I run the below code by setting the backtesting time frame to Monthly as shown in the image . but its not producing BEPL in the result on 8 jan 2021. Could anyone please let me know why. I am trying to find out red candle stocks. the below code supposed to produce BEPL in the result since BEPL is red candle today. But its not producing it. I would like to know why. Thanks in advance

1610113043257.png

TimeFrameSet( inDaily ); // switch now to daily

Dailyredcandle = Close < Open;

TimeFrameRestore(); // restore time frame to original


Filter = Dailyredcandle;
 
I am new to AFL coding. Experts here kindly help me out with the following which despite spending hours I could not make it work.

So following is my requirement:

I want to find out the highest price reached until the end of day AFTER buy signal is generated.

After market hours, I want to run the explorer and analyze those stocks.

However, the highest price being displayed is incorrect. See screenshot. Kindly help !!!!

CODE:

Code:
Buy = Cross( MACD(), Signal() );
Short = Cross( Signal(), MACD() );

Cover = Sell = TimeNum()>151500 OR 0;

Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
Short = ExRem(Short,Cover);
Cover = ExRem(Cover,Short);

Filter = Buy OR Short;

AddColumn(IIf(Buy,HighestSince(Buy, High),Null),"Highest",1.2);

AddColumn(IIf(Short,LowestSince(Short, Low),Null),"Lowest",1.2);
screenshot.png
 

VJAY

Well-Known Member
.....
 
Last edited:

Romeo1998

Well-Known Member
I am new to AFL coding. Experts here kindly help me out with the following which despite spending hours I could not make it work.

So following is my requirement:

I want to find out the highest price reached until the end of day AFTER buy signal is generated.

After market hours, I want to run the explorer and analyze those stocks.

However, the highest price being displayed is incorrect. See screenshot. Kindly help !!!!

CODE:

Code:
Buy = Cross( MACD(), Signal() );
Short = Cross( Signal(), MACD() );

Cover = Sell = TimeNum()>151500 OR 0;

Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
Short = ExRem(Short,Cover);
Cover = ExRem(Cover,Short);

Filter = Buy OR Short;

AddColumn(IIf(Buy,HighestSince(Buy, High),Null),"Highest",1.2);

AddColumn(IIf(Short,LowestSince(Short, Low),Null),"Lowest",1.2);
View attachment 45225
good luck :)
Code:
AddColumn(HighestSince(sell, High),"Highest",1.2);
AddColumn(LowestSince(cover, Low),"Lowest",1.2);
 

john302928

Well-Known Member
Hi
I run the below code by setting the backtesting time frame to Monthly as shown in the image . but its not producing BEPL in the result on 8 jan 2021. Could anyone please let me know why. I am trying to find out red candle stocks. the below code supposed to produce BEPL in the result since BEPL is red candle today. But its not producing it. I would like to know why. Thanks in advance

View attachment 45224
TimeFrameSet( inDaily ); // switch now to daily

Dailyredcandle = Close < Open;

TimeFrameRestore(); // restore time frame to original


Filter = Dailyredcandle;
@Romeo1998 @timepass or anyone please help me on this query. thanks
 

john302928

Well-Known Member
if u r exploring the current day for a red day, then use Daily in Periodicity and in Range "1 recent day". good luck :)
Code:
Filter = Close < Open;
AddColumn(c,"Close");
No, I have to explore in monthly time frame so i set the backtesting to Monthly. But some of the conditions need to be satisfied in daily time frame.
So when i checked by setting the the backtester periodicity to Monthly view and explored this red candle in daily time frame its not producing correct results.
 

Similar threads