Dear Seniors,
I am getting an Empty Buy/Sell signal at the first bar of the day. Please see the attached AFL and image. Also on certain days, the range line are getting angled. Please help to fix the buy bug.
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() );
sig = Null;
pos=Param("size",20,1,3000,1);
SetPositionSize(pos,spsShares);
iATR= Ref(MA(H-L,20),-1);
tslf= Param("TSL Factor",2,1, 10,0.5);
newday = Day() != Ref(Day(),-1); //check if new day or not
starttime = ValueWhen(newday,TimeNum());
IBendtime = starttime+1000;
minh = ValueWhen(newday,TimeFrameGetPrice("H",in5Minute*2));
minl = ValueWhen(newday,TimeFrameGetPrice("L",in5Minute*2));
TimeRange = ParamToggle("Time","No|Yes",1);
Starttime = Param("Starttime",090000,090000,151500,0);
Closetime = Param("Closetime",151500,090000,151500,0);
ExitToday = (Ref(DateNum(),1) > DateNum());
extime= TimeNum()>152500;
PH=TimeFrameGetPrice("H",inDaily,-1);
PL= TimeFrameGetPrice("L",inDaily,-1);
PC= TimeFrameGetPrice("C",inDaily,-1);
printf("%g",IBendtime);
DayOpen = TimeFrameGetPrice("O",inDaily);
DayHigh = TimeFrameGetPrice("H",inDaily);
DayLow = TimeFrameGetPrice("L",inDaily);
ORBH = IIf(TimeNum() < IBendtime, Null ,minh);
ORBL = IIf(TimeNum() < IBendtime, Null, minl);
FIBO = .618;
PREV_Day_Range= PH-PL;
Current_Day_Range=ORBH-ORBL;
FIBO_RANGE = ( PREV_Day_Range + Current_Day_Range ) * FIBO;
// Calcualte .618 range from yesterdays close..
Buy_Range_High = PC + FIBO_RANGE;
Sell_Range_Low = PC - FIBO_RANGE;
/// If todays price is gapup or gapbelow .618 range.
Condition1 = minh > Buy_Range_High;
Condition2= minl < Sell_Range_Low;
ExtraCondition = Condition1 OR Condition2;
Up = IIf(ExtraCondition, ORBH, Buy_Range_High);
DN = IIf(ExtraCondition, ORBL, Sell_Range_Low);
B1 = Cross(H,up) ;
//S1= C<=Dn ;
Sh1= Cross(dn,L);
//Co1= C>=Up;
//Plot( IIf(ExtraCondition, minh, Buy_Range_High), "Upper Break Out", colorYellow , styleDashed);
//Plot( IIf( ExtraCondition, minl, Sell_Range_Low), "Lower Break Down", colorYellow, styleDashed);
dtDayStartComp = TimeFrameCompress( DateTime(), inDaily, compressOpen );
dtDayStartExp = TimeFrameExpand( dtDayStartComp, inDaily, expandFirst );
dtDayEndComp = TimeFrameCompress( DateTime(), inDaily, compressLast );
dtDayEndExp = TimeFrameExpand( dtDayEndComp, inDaily, expandFirst );
DayCond = DateTime() >= dtDayStartExp AND DateTime() < dtDayEndExp;
//DayCond1 = DayCond AND (TimeNum() > 091000)
Plot( IIf( DayCond, UP, Null ), "Upper Breakout", colorYellow, styleDashed, Null, Null, 0 );
//Plot( IIf( DayCond, UP, Null ), "Upper Breakout", colorAqua, styleLine, Null, Null, 2 );
Plot( IIf( DayCond, DN, Null), "Lower Breakout", colorYellow, styleDashed, Null, Null, 0 );
//Plot( IIf( DayCond, DN, Null), "Lower Breakout", colorAqua, styleLine, Null, Null, 2 );
Buy = IIf(TimeRange==False,B1,B1 AND TimeNum() > Starttime AND TimeNum() < Closetime);
Short = IIf(Timerange==False,Sh1,Sh1 AND TimeNum() > Starttime AND TimeNum() < Closetime);
Cover = TimeNum() > Closetime;
Sell =TimeNum() > Closetime;
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
Short=ExRem(Short,Cover);
Cover=ExRem(Cover,Short);
BuyPrice=up;
ShortPrice=Dn;
dist = 1.5*ATR(10);
for( i = 0; i < BarCount; i++ )
{
if( Buy[i] ) PlotText( "Buyn@" + up[ i ], i, L[ i ]-dist[i], colorGreen );
if( Short[i] ) PlotText( "shortn@" + dn[ i ], i, H[ i ]+dist[i], colorRed, colorYellow );
}
//PlotShapes(IIf(SSL , shapeStar, shapeNone),colorBlue , 0,High,Offset=30);
tsl=tsls=0;
slarr=Null;
bflag=0;
sflag=0;
BP=SP=0;
BSL=SSL=0;
sl=Param("stoploss",0.4,0.3,0.6,0.01);
for( i = 1; i < BarCount; i++ )
{
if(Buy[i]==1)
{
tsl= C[i]-tslf*iatr[i];
bfalg=1;
BP=ValueWhen(Buy,C,1);
BSL = ValueWhen(Buy,BP-((BP/100)*sl),1);
}
if(tsl>0)
{
if(C[i]-tslf*iatr[i]>tsl) tsl= C[i]-tslf*iatr[i];
if (tsl[i]<BSL[i])
{
slarr[i]=BSL[i];
}
else
{
slarr[i]=tsl[i];
}
}
if((L[i]<slarr[i] AND C[i]<O[i]) OR extime[i] OR ExitToday[i] )
{
Sell[i]=1;
bflag=0;
tsl=0;
SellPrice=slarr[i];
}
if(Short[i]==1)
{
tsls= C[i]+tslf*iatr[i];
sflag=1;
SP=ValueWhen(Short,C,1);
SSL=ValueWhen(Short,SP+((SP/100)*sl),1);
}
if(tsls>0)
{
if(C[i]+tslf*iatr[i]<tsls) tsls= C[i]+tslf*iatr[i];
if(tsls[i]>SSL[i])
{
slarr[i]=SSL[i];
}
else
{
slarr[i]=tsls[i];
}
}
if((H[i]>slarr[i] AND C[i]>O[i]) OR extime[i] OR ExitToday[i])
{
Cover[i]=1;
sfalg=0;
tsls=0;
CoverPrice=slarr[i];
}
}
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
Short=ExRem(Short,Cover);
Cover=ExRem(Cover,Short);
Plot(slarr,"sl",colorAqua);
StopLoss=Param("stoploss",0.43,0.2,0.6,0.02);
ApplyStop(Type=0,Mode=1,Amount=StopLoss);
//Plot(RSI(11),"rsi",colorBlue,styleOwnScale);
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(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(Sell , shapeStar, shapeNone),colorRed , 0,High,Offset=20);
PlotShapes(IIf(Cover , shapeStar, shapeNone),colorGreen, 0,Low,Offset=-20);
Buy and sell signals can come only if the price crosses the range after the first 10 mins in the market.
Thanks in advance.
Regards,
Sumit