// Downloaded From https://www.WiseStockTrader.com
//Coded by Rajandran R
//Website - www.marketcalls.in
//Date - 15-Nov-2015


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

_SECTION_BEGIN("Camarilla Pivot as Trailing Stop");


SetPositionSize(150,spsShares);

//---- pivot points
DayH = TimeFrameGetPrice("H", inDaily, -1);		// yesterdays high
DayL = TimeFrameGetPrice("L", inDaily, -1);		//				low
DayC = TimeFrameGetPrice("C", inDaily, -1);		//				close
DayO = TimeFrameGetPrice("O", inDaily);			// current day open

// camarilla pivots
if ( True )
{
R = DayH - DayL;	  // range
PP = (DayH + DayL + DayO + DayO) / 4 ;
R4 = (R * 1.1/2)+DayC;
R3 = (R * 1.1/4)+DayC;
S3 = DayC-(R * 1.1/4);
S4 = DayC- (R * 1.1/2);
}

Plot(PP, "",colorYellow,styleLine);

Buysignal = Cross(C,PP);
Shortsignal = Cross(PP,C);

ibsignal = Flip(buysignal,shortsignal); // Flip can be used for holding contion true till the time the other condition true
issignal = Flip(shortsignal,buysignal);


buysignal = ExRem( buysignal, shortsignal );

Buy = Ref(buysignal,-1);
Short = Ref(Shortsignal,-1);


BuyLimitPrice = ValueWhen(BuySignal, H);
ShortLimitPrice = ValueWhen(shortsignal,L);

Buy = ibsignal and H>buylimitprice;
Short = issignal and L<shortlimitprice;


ibuy = Flip(Buy,Short);
isell = Flip(Short,Buy);

sellsignal = Cross(PP,C) AND ibuy;
coversignal = Cross(C,PP) AND isell;

Sell = Ref(sellsignal,-1);
Cover = Ref(coversignal,-1);

SellLimitPrice = ValueWhen(Cross(PP,C),L);
CoverLimitPrice = valuewhen(Cross(C,PP),H);



Sell = ibuy AND L<SellLimitPrice;
Cover = isell AND H>coverLimitPrice;



Buy = ExRem(Buy,short);
Short = ExRem(Short,Buy);

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




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



_SECTION_END();