How to code the following?

My buy signal is two consecutive bars with both close > open.

Buyprice is the 2nd bar close.

The profit target is 2 times the amount of (2nd bar close - 1st bar low).

The stop loss is 1st bar low minus one tick size.

As it is an intra-day signal, if the profit target is not hit, sold before the market close.

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

2 Likes

Dear awilson,

Thank you very much for your prompt reply.

After backtesting, I found that: (shown in the below image)

image

i) the position is hold after market close; and

ii) the target profit may exceed 2 times (due to consecutive close>open bars? what commands should be added to keep the result exactly as 2 times?)

Besides, I should be very grateful if you would explain how the following lines work as I would like to finish the short side and change the signal from 2 consecutive close>open bars to, say, 4 bars by myself with your illustration.

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

Thanks a lot.