Chart getting distorted after plotting SL line :(

Dear all,

When i am trying to plot the SL and buy and sell arrow, the chart is getting distorted..

image

I am not sure what i am doing in the afl..

_SECTION_BEGIN( "Sumit code v1" );

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

StartTime = ParamTime( "Start time", "11:40:00");
endtime = ParamTime( "End time", "5:00:00" );
square_off_time = ParamTime( "Square off time", "5:00:00" );


Buy = False;
Sell = False;
Short = False;
Cover = False;

newday = Day() != Ref( Day(), -1 );

starttime = ValueWhen( newday, TimeNum() );
orb = "10min";


//Go to 10 Min time fram
if( orb == "10min" )
{
    IBendtime = starttime + 1000;
    minh = ValueWhen( newday, TimeFrameGetPrice( "H", in5Minute * 3 ) );
    minl = ValueWhen( newday, TimeFrameGetPrice( "L", in5Minute * 3 ) );
}
//*************************************************************************************************************************************
// Current Day 10 min high /low
current_day_10_min_high = IIf( TimeNum() < IBendtime, Null , minh );
current_day_10_min_low = IIf( TimeNum() < IBendtime, Null, minl );
//Previous day high low close
prev_day_close = ValueWhen( newday, TimeFrameGetPrice( "C", inDaily, -1 ) );
prev_day_High = ValueWhen( newday, TimeFrameGetPrice( "H", inDaily, -1 ) );
prev_day_LOW = ValueWhen( newday, TimeFrameGetPrice( "L", inDaily, -1 ) );
prev_day_open = TimeFrameGetPrice( "O", inDaily, 0 );
printf( "\n prev_day_close = %.2f", prev_day_close );
printf( "\n prev_day_High = %.2f", prev_day_High );
printf( "\n prev_day_LOW = %.2f", prev_day_LOW );
printf( "\n cur_day_10h = %.2f \n", current_day_10_min_high );
printf( "\nscur_day_10l = %.2f \n", current_day_10_min_low );

//*************************************************************************************************************************************

// Plotting buy and sell value....

FIBO = .618;
PREV_Day_Range = prev_day_High - prev_day_LOW;
Current_Day_Range = current_day_10_min_high - current_day_10_min_low;
printf( "\n PREV_Day_Range = %.2f", PREV_Day_Range );
printf( "\n Current_Day_Range = %.2f", Current_Day_Range );

FIBO_RANGE = ( PREV_Day_Range + Current_Day_Range ) * FIBO;
// Calcualte .618 range from yesterdays close..
Buy_Range_High = prev_day_close + FIBO_RANGE;
Sell_Range_Low = prev_day_close - FIBO_RANGE;
/// If todays price is gapup or gapbelow  .618 range.
Condition1 = current_day_10_min_high > Buy_Range_High;
Condition2= current_day_10_min_low < Sell_Range_Low;
ExtraCondition =  Condition1 OR Condition2;

Plot( IIf(ExtraCondition, current_day_10_min_high, Buy_Range_High), "Upper Break Out", colorBlue , styleDashed);
Plot( IIf( ExtraCondition, current_day_10_min_low, Sell_Range_Low), "Lower Break Down", colorBlue, styleDashed);

//*************************************************************************************************************************************
//SETTING Buy AND Sell CONDITIONS 

BuyPrice = IIf(ExtraCondition, current_day_10_min_high, Buy_Range_High);
printf( "\n Buy Price = %.2f", BuyPrice );
sellPrice = IIf(ExtraCondition, current_day_10_min_Low, Sell_Range_Low);
printf( "\n sell Price = %.2f", sellPrice );

ExtraCondition_buystoploss = current_day_10_min_high - (current_day_10_min_high * (0.5/100));
ExtraCondition_Sellstoploss = current_day_10_min_low + (current_day_10_min_low * (0.5/100));

NormalCondition_buystoploss = Buy_Range_High - (Buy_Range_High * (0.5/100));
NormalCondition_Sellstoploss = Sell_Range_Low + (Sell_Range_Low * (0.5/100));

BuySL = IIf(ExtraCondition,ExtraCondition_buystoploss,NormalCondition_buystoploss);
SellSL = IIf(ExtraCondition, ExtraCondition_Sellstoploss, NormalCondition_Sellstoploss);

printf( "\n Buy Sl = %.2f", BuySL );
printf( "\n Sell SL = %.2f", SellSL );

Buy =  IIf(ExtraCondition, current_day_10_min_high, Buy_Range_High) AND TimeNum() > StartTime AND TimeNum() < endtime;
Short =  IIf(ExtraCondition, current_day_10_min_Low, Sell_Range_Low) AND TimeNum() > StartTime AND TimeNum() < endtime;

Cover = Buy OR C > SellSL OR TimeNum() > square_off_time;
Sell =  Short OR C < BuySL OR TimeNum() > square_off_time;

Plot( BuySL, "Buy SL", colorYellow , styleDashed);
Plot( SellSL, "Sell SL", colorYellow, styleDashed);


PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorBlue , 0, Low, -30 );
PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorBlue, 0, High, -10 );
PlotShapes( IIf( Cover , shapeUpArrow, shapeNone ), colorRed, 0, Low, -10 );
PlotShapes( IIf( Short, shapeDownArrow, shapeNone ), colorRed, 0, High, -30 );



 


_SECTION_END();


Can someone please help.

Thanks in advance.

Modify BuySL and SellSL to

Plot( ValueWhen(!IsNull(current_day_10_min_high), BuySL), "Buy SL", colorYellow , styleDashed);
Plot( ValueWhen(!IsNull(current_day_10_min_low), SellSL), "Sell SL", colorYellow, styleDashed);

So,

_SECTION_BEGIN( "Sumit code v1" );

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

StartTime = ParamTime( "Start time", "11:40:00");
endtime = ParamTime( "End time", "5:00:00" );
square_off_time = ParamTime( "Square off time", "5:00:00" );


Buy = False;
Sell = False;
Short = False;
Cover = False;

newday = Day() != Ref( Day(), -1 );

starttime = ValueWhen( newday, TimeNum() );
orb = "10min";


//Go to 10 Min time fram
if( orb == "10min" )
{
    IBendtime = starttime + 1000;
    minh = ValueWhen( newday, TimeFrameGetPrice( "H", in5Minute * 3 ) );
    minl = ValueWhen( newday, TimeFrameGetPrice( "L", in5Minute * 3 ) );
}
//*************************************************************************************************************************************
// Current Day 10 min high /low
current_day_10_min_high = IIf( TimeNum() < IBendtime, Null , minh );
current_day_10_min_low = IIf( TimeNum() < IBendtime, Null, minl );
//Previous day high low close
prev_day_close = ValueWhen( newday, TimeFrameGetPrice( "C", inDaily, -1 ) );
prev_day_High = ValueWhen( newday, TimeFrameGetPrice( "H", inDaily, -1 ) );
prev_day_LOW = ValueWhen( newday, TimeFrameGetPrice( "L", inDaily, -1 ) );
prev_day_open = TimeFrameGetPrice( "O", inDaily, 0 );
printf( "\n prev_day_close = %.2f", prev_day_close );
printf( "\n prev_day_High = %.2f", prev_day_High );
printf( "\n prev_day_LOW = %.2f", prev_day_LOW );
printf( "\n cur_day_10h = %.2f \n", current_day_10_min_high );
printf( "\nscur_day_10l = %.2f \n", current_day_10_min_low );

//*************************************************************************************************************************************

// Plotting buy and sell value....

FIBO = .618;
PREV_Day_Range = prev_day_High - prev_day_LOW;
Current_Day_Range = current_day_10_min_high - current_day_10_min_low;
printf( "\n PREV_Day_Range = %.2f", PREV_Day_Range );
printf( "\n Current_Day_Range = %.2f", Current_Day_Range );

FIBO_RANGE = ( PREV_Day_Range + Current_Day_Range ) * FIBO;
// Calcualte .618 range from yesterdays close..
Buy_Range_High = prev_day_close + FIBO_RANGE;
Sell_Range_Low = prev_day_close - FIBO_RANGE;
/// If todays price is gapup or gapbelow  .618 range.
Condition1 = current_day_10_min_high > Buy_Range_High;
Condition2= current_day_10_min_low < Sell_Range_Low;
ExtraCondition = Condition1 OR Condition2;

Plot( IIf(ExtraCondition, current_day_10_min_high, Buy_Range_High), "Upper Break Out", colorBlue , styleDashed);
Plot( IIf( ExtraCondition, current_day_10_min_low, Sell_Range_Low), "Lower Break Down", colorBlue, styleDashed);

//*************************************************************************************************************************************
//SETTING Buy AND Sell CONDITIONS 

BuyPrice = IIf(ExtraCondition, current_day_10_min_high, Buy_Range_High);
printf( "\n Buy Price = %.2f", BuyPrice );
sellPrice = IIf(ExtraCondition, current_day_10_min_Low, Sell_Range_Low);
printf( "\n sell Price = %.2f", sellPrice );

ExtraCondition_buystoploss = current_day_10_min_high - (current_day_10_min_high * (0.5/100));
ExtraCondition_Sellstoploss = current_day_10_min_low + (current_day_10_min_low * (0.5/100));

NormalCondition_buystoploss = Buy_Range_High - (Buy_Range_High * (0.5/100));
NormalCondition_Sellstoploss = Sell_Range_Low + (Sell_Range_Low * (0.5/100));

BuySL = IIf(ExtraCondition,ExtraCondition_buystoploss,NormalCondition_buystoploss);
SellSL = IIf(ExtraCondition, ExtraCondition_Sellstoploss, NormalCondition_Sellstoploss);

printf( "\n Buy Sl = %.2f", BuySL );
printf( "\n Sell SL = %.2f", SellSL );

Buy =  IIf(ExtraCondition, current_day_10_min_high, Buy_Range_High) AND TimeNum() > StartTime AND TimeNum() < endtime;
Short =  IIf(ExtraCondition, current_day_10_min_Low, Sell_Range_Low) AND TimeNum() > StartTime AND TimeNum() < endtime;

Cover = Buy OR C > SellSL OR TimeNum() > square_off_time;
Sell =  Short OR C < BuySL OR TimeNum() > square_off_time;

Plot( ValueWhen(!IsNull(current_day_10_min_high),BuySL), "Buy SL", colorYellow , styleDashed);
Plot( ValueWhen(!IsNull(current_day_10_min_low),SellSL), "Sell SL", colorYellow, styleDashed);


PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorBlue , 0, Low, -30 );
PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorBlue, 0, High, -10 );
PlotShapes( IIf( Cover , shapeUpArrow, shapeNone ), colorRed, 0, Low, -10 );
PlotShapes( IIf( Short, shapeDownArrow, shapeNone ), colorRed, 0, High, -30 )
1 Like

Thanks a ton Fxshrat. Stop line plotted like a charm..