Target, Stop loss issues

Hi Guys, following code is exiting me on the next bar, what I am trying to do is that 1) Put a % target and stoploss at hhv and llv at last five bars, please help understanding on where I am going wrong,

_SECTION_BEGIN("Intraday ORB Ssystem");

// Optimisation

// Optimisation engine

OptimizerSetEngine("spso");
OptimizerSetOption("Runs", 10 );
OptimizerSetOption("MaxEval", 50000 );

//Time optimisation

starttimehour = Optimize ( " starttimehour",09,1,15,1);
starttimemin = Optimize ( " starttimemin",30,1,60,1);

endtimehour = Optimize ( " endtimehour",09,1,15,1);
endtimemin = Optimize ( " endtimehour",30,1,60,1);

Squareofftimehour = Optimize ( " Squareofftimehour",09,1,15,1);
Squareofftimemin = Optimize ( " Squareofftimemin",30,1,60,1);

//ORBH & ORBL time multiplier

ORBHmult = Optimize("ORBHmult",60,1,60,1);
ORBlmult = Optimize("ORBlmult",60,1,60,1);

// Target mul

Targetbuymult = Optimize("Targetbuymult",0.2,0.2, 10, 0.1);
Targetshortmult = Optimize("Targetshortmult",0.2,0.2, 10, 0.1);

//Stop loss range

Stoplossbuyrange = Optimize("Stoplossbuyrange",5,1,100,1);
Stoplossshortrange = Optimize("Stoplossshortrange",5,1,100,1);

// Plot Candle Chart

_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g ", Ref(O,-1), Ref(H,-1), Ref(L,-1), Ref(C,-1)));

SetChartOptions(0 , chartShowArrows | chartShowDates);
Plot(Close,"Candle", colorDefault, styleCandle);

SetBarsRequired(-2,-2); //Turn Quick AFL Off

//Set postion

SetPositionSize(2*RoundLotSize,spsShares);

// Intraday Time specifications

starttime = ParamTime("Signal Start Time", "starttimehour:starttimemin");
endtime = ParamTime("Singal End Time", "endtimehour:endtimemin");
Squareofftime = ParamTime("Square Off Time", "Squareofftimehour:Squareofftimemin");

// Color Param

Color1 = ParamColor("ORBH", colorGreen);
Color2 = ParamColor("ORBL", colorRed);

// ORB - New day
newday = Day() != Ref(Day(),-1);

Plot(newday,"Newday",colorRed,styleHistogram | styleOwnScale);

//ORBH & ORBL calculation

orbh = ValueWhen(newday, TimeFrameGetPrice("H", ORBHmult*in1Minute));

orbl = ValueWhen(newday, TimeFrameGetPrice("L", ORBlmult*in1Minute));

// To plot post 10:30

orbh = IIf(TimeNum() <101500,Null,orbh);

orbl = IIf(TimeNum() <101500,Null,orbl);

// Plot ORBH & ORBL

Plot(orbh, "orbh", Color1);
Plot(orbl, "orbl", Color2);

// Entry Signals

SetTradeDelays(1,1,1,1);

Buy = Cross(Close,orbh) AND TimeNum() > starttime AND TimeNum() < endtime;
Short = Cross(orbl,Close) AND TimeNum() > starttime AND TimeNum() < endtime;

BuyPrice = Open;
shortprice = Open;

// exit signal

Targetbuy = (BuyPrice + (Targetbuymult * BuyPrice));

Targetshort = (ShortPrice - (Targetshortmult * ShortPrice));

Stoplossbuy = BuyPrice < (LLV(Ref(Buy,-1),Stoplossbuyrange));
Stoplossshort = ShortPrice < (HHV(Ref(Short,-1),Stoplossshortrange));

Sell = Targetbuy OR TimeNum() >= squareofftime OR Stoplossbuy;

Cover = Targetshort OR TimeNum() >= squareofftime OR Stoplossshort;

Sell = Open;
Cover = Open;

//Avoide duplicate trade

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

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

/* Plot Buy and Sell Signal Arrows */
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),colorGreen, 0,L, Offset=-45);
PlotShapes(IIf(cover, shapestar, shapeNone),colorRed, 0, H, Offset=40);

//Exploration

Filter = Buy OR Sell;

AddColumn(Close,"LTP",1.2);
AddColumn(ORBH,"ORBH",1.2);
AddColumn(ORBL,"ORBL",1.2);

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.