Not tested…
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() );
//My buy signal is two consecutive bars with both close > open.
BuySignal = Close > Open;
Buy = BuySignal AND Ref(BuySignal, -1);
//Buyprice is the 2nd bar close.
BuyPrice = Close;
//As it is an intra-day signal, if the profit target is not hit, sold before the market close.
CloseTrades = ParamTime("CloseTrades","16:30:00");
CloseTrades = TimeNum() >= CloseTrades;
BuyLoss = 0;
BuyProfit = 0;
pBuyLoss = Null;
pBuyProfit = Null;
longshort = 0; // -1 Short, 1 Long
Sell = Short = Cover = False;
dbg = 0;
for(i=3; i<BarCount; i++) {
test=0;
if( longshort == 1 ) {
pBuyLoss[i] = BuyLoss;
pBuyProfit[i] = BuyProfit;
}
if( Buy[i] ) {
if( longshort == 0 ) {
BuyLoss = Low[i-1] - TickSize;
BuyProfit = Close[i] + 2 * (Close[i] - Low[i-1]);
longshort = 1;
} else {
Buy[i] = False;
}
}
if( NOT Buy[i] ) {
if( longshort == 1 ) {
if(Low[i] <= BuyLoss ) {
Sell[i] = True;
SellPrice[i] = Min(Open[i], BuyLoss);
} else if(High[i] >= BuyProfit ) {
Sell[i] = True;
SellPrice[i] = Max(Open[i], BuyProfit);
} else if(CloseTrades[i]) {
Sell[i] = True;
SellPrice[i] = Close[i];
}
if( Sell[i] ) {
longshort = 0;
BuyLoss = 0;
BuyProfit = 0;
}
}
}
dbg[i] = test;
}
if( Status("action") == actionIndicator ) {
Plot(dbg, "\ndbg", colorGreen, styleNoLine| styleNoRescale);
Plot(pBuyLoss, "\nLongLoss", colorGreen, styleDots | styleNoLine| styleNoRescale);
Plot(pBuyProfit, "LongGain", colorGreen, styleDashed | styleNoRescale);
PlotShapes( IIf(Buy, shapeUpArrow, shapeNone), IIf(Buy==sigScaleOut, colorBrightGreen, colorGreen), 0, Low, -29 );
PlotShapes( IIf(Short, shapeDownArrow, shapeNone), IIf(Short==sigScaleOut, colorBrightGreen, colorRed), 0, High, -29);
PlotShapes( IIf(Sell AND NOT Short, shapeDownArrow, shapeNone), colorBlue, 0, High, -29);
PlotShapes( IIf(Cover AND NOT Buy, shapeUpArrow, shapeNone), colorBlue, 0, Low, -29);
}