Intraday trade re-entry based on a follow-up strategy

I am trying out a strategy wherein the system generates a BUY signal when the main entry condition is met. Once exited from the trade, by stop-loss or with pre-set profit level, I need the system to generate a subsequent BUY signal, if a new entry condition is met, within a stipulated time and ONLY if LTP is above the price at which trade was last exited.

The below given code is a modification of the solution given in a similar thread (Re entering trade - #5 by mike). Now, the system is generating a subsequent entry signal, but only once per day. The rest of the TRUE conditions of the second entry logic is getting filtered out.

I have avoided ApplyStop as the same script need to be used for live automated trades. I had done my level best but am unable to make it execute a sequel signal to first subsequent trade after exiting the main trade.

Please help.

SetTradeDelays( 0, 0, 0, 0 );

OptimizerSetEngine( "spso" );


SetBacktestMode( backtestRegularRaw );

SetOption( "AllowSameBarExit", False );

TickSize = 0.01;


my_prefix = "Magna{chartid}";

#pragma enable_static_decl(my_prefix)

StaticVarRemove( "*" );


static _onLong;


Plot( C, "Price", colorDefault, styleCandle );


BuyPrice = SellPrice = ShortPrice = CoverPrice = Open;

entryMax = TimeNum() < 141000;
sqOffLong = TimeNum() > 151000;

BuyCond_A =   _onLong == 0  AND entryMax AND MFI( 14 ) > 70 AND ADX( 14 ) > 25 AND Close > Open;


Buy =  Ref( BuyCond_A, -1 );

Sell = sqOffLong;

Buy = ExRem( Buy, Sell );

boughtPrice = ValueWhen( Cross( Buy, 0 ), BuyPrice );

maxProfExitLong = boughtPrice + 60;
maxLossExitLong = boughtPrice - 30;


hhSinceBuy = HighestSince( Cross( Buy, 0 ), High );
llSinceBuy = LowestSince( Cross( Buy, 0 ), Low );

Sell = hhSinceBuy >= maxProfExitLong OR llSinceBuy < maxLossExitLong  OR sqOffLong;

exLongBy_SL 		=   Flip( Sell, Buy );
exPrcLongBy_SL		=	ValueWhen( Cross( Sell, 0 ), O ) ;
timeExLongBy_SL		=	ValueWhen( Cross( Sell, 0 ), DateTime() ) ;


timeDiffLongEx 		= 	 DateTimeDiff( DateTime(), timeExLongBy_SL );
isOpenReEntry 		= 	 timeDiffLongEx < 12000;
isPriceAboveExPrc 	= 	 High > exPrcLongBy_SL;


AddColumn( exLongBy_SL, "exLongBy_SL", 1.2, colorDefault, IIf( exLongBy_SL, colorRose, colorDefault ) );

BuyCond_B 			= 	_onLong == 0 AND entryMax AND exLongBy_SL AND timeDiffLongEx AND isPriceAboveExPrc 
                        AND MFI( 14 ) > 20 AND ADX( 14 ) > 10 AND Close > Open;



Buy = IIf( ( NOT _onLong AND Ref( BuyCond_A, -1 ) ), Ref( BuyCond_A, -1 ),
           IIf( ( NOT _onLong AND Ref( BuyCond_B, -1 ) ), Ref( BuyCond_B, -1 ), Buy ) );

boughtPrice = ValueWhen( Cross( Buy, 0 ), BuyPrice );

maxProfExitLong = boughtPrice + 30;
maxLossExitLong = boughtPrice - 20;


hhSinceBuy = HighestSince( Cross( Buy, 0 ), High );
llSinceBuy = LowestSince( Cross( Buy, 0 ), Low );

Sell = hhSinceBuy >= maxProfExitLong OR llSinceBuy < maxLossExitLong  OR sqOffLong;

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

_onLong = Flip( Buy, Sell );


AddColumn( exLongBy_SL, "exLongBy_SL", 1.2, colorDefault, IIf( exLongBy_SL, colorRose, colorDefault ) );

PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorBrightGreen, 0, O, -10 );
PlotShapes( IIf( Buy, shapeSmallUpTriangle, shapeNone ), colorBrightGreen, 0, L, -40 );

PlotShapes( IIf( Sell, shapeSmalldownTriangle, shapeNone ), ColorRGB( 255, 0, 255 ), 0, H, -30 );
PlotShapes( IIf( Sell, shapedownArrow, shapeNone ), ColorRGB( 255, 0, 255 ), 0, O, -10 );

Filter =  Buy OR Sell  OR  buyCond_A OR buyCond_B;

AddColumn( Buy, "Buy", 1.2, colorDefault, IIf( Buy, colorBrightGreen, colorDefault ) );
AddColumn( Sell, "Sell", 1.2, colorDefault, IIf( Sell, colorYellow, colorDefault ) );


AddColumn( _onLong, "_onLong", 1.2, colorDefault, IIf( _onLong, colorAqua, colorDefault ) );

AddColumn( buyCond_A, "_buyCond1A", 1.2, colorDefault, IIf( buyCond_A, colorGreen, colorDefault ) );
AddColumn( buyCond_B, "_buyCond1B", 1.2, colorDefault, IIf( buyCond_B, colorPaleTurquoise, colorDefault ) );

Else, if there is a more prudent way, how to script it? What needed is to enter again into a trade after exiting from the main trade once SL hit or profit reached. The subsequent trade to be of same direction, if a new set of rules ( relaxed) are satisfied. LTP to be above previously exited price AND time within a set limit. This need to be repeated as long as the conditions persist intra-day.

For what it is worth, #pragma is pre-processor command and has to have literal string, not variable inside.

Thank you Tomasz.

Accordingly, amended the code, as below.


SetTradeDelays( 0, 0, 0, 0 );

OptimizerSetEngine( "spso" );


SetBacktestMode( backtestRegularRaw );

SetOption( "AllowSameBarExit", False );

TickSize = 0.01;



#pragma enable_static_decl("Magna{chartid}")

StaticVarRemove( "*" );


static _onLong;


Plot( C, "Price", colorDefault, styleCandle );


BuyPrice = SellPrice = ShortPrice = CoverPrice = Open;

entryMax = TimeNum() < 141000;
sqOffLong = TimeNum() > 151000;

BuyCond_A =   _onLong == 0  AND entryMax AND MFI( 14 ) > 70 AND ADX( 14 ) > 25 AND Close > Open;


Buy =  Ref( BuyCond_A, -1 );

Sell = sqOffLong;

Buy = ExRem( Buy, Sell );

boughtPrice = ValueWhen( Cross( Buy, 0 ), BuyPrice );

maxProfExitLong = boughtPrice + 60;
maxLossExitLong = boughtPrice - 30;


hhSinceBuy = HighestSince( Cross( Buy, 0 ), High );
llSinceBuy = LowestSince( Cross( Buy, 0 ), Low );

Sell = hhSinceBuy >= maxProfExitLong OR llSinceBuy < maxLossExitLong  OR sqOffLong;

exLongBy_SL 		=   Flip( Sell, Buy );
exPrcLongBy_SL		=	ValueWhen( Cross( Sell, 0 ), O ) ;
timeExLongBy_SL		=	ValueWhen( Cross( Sell, 0 ), DateTime() ) ;


timeDiffLongEx 		= 	 DateTimeDiff( DateTime(), timeExLongBy_SL );
isOpenReEntry 		= 	 timeDiffLongEx < 12000;
isPriceAboveExPrc 	= 	 High > exPrcLongBy_SL;


AddColumn( exLongBy_SL, "exLongBy_SL", 1.2, colorDefault, IIf( exLongBy_SL, colorRose, colorDefault ) );

BuyCond_B 			= 	_onLong == 0 AND entryMax AND exLongBy_SL AND timeDiffLongEx AND isPriceAboveExPrc 
                        AND MFI( 14 ) > 20 AND ADX( 14 ) > 10 AND Close > Open;



Buy = IIf( ( NOT _onLong AND Ref( BuyCond_A, -1 ) ), Ref( BuyCond_A, -1 ),
           IIf( ( NOT _onLong AND Ref( BuyCond_B, -1 ) ), Ref( BuyCond_B, -1 ), Buy ) );

boughtPrice = ValueWhen( Cross( Buy, 0 ), BuyPrice );

maxProfExitLong = boughtPrice + 30;
maxLossExitLong = boughtPrice - 20;


hhSinceBuy = HighestSince( Cross( Buy, 0 ), High );
llSinceBuy = LowestSince( Cross( Buy, 0 ), Low );

Sell = hhSinceBuy >= maxProfExitLong OR llSinceBuy < maxLossExitLong  OR sqOffLong;

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

_onLong = Flip( Buy, Sell );


AddColumn( exLongBy_SL, "exLongBy_SL", 1.2, colorDefault, IIf( exLongBy_SL, colorRose, colorDefault ) );

PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorBrightGreen, 0, O, -10 );
PlotShapes( IIf( Buy, shapeSmallUpTriangle, shapeNone ), colorBrightGreen, 0, L, -40 );

PlotShapes( IIf( Sell, shapeSmalldownTriangle, shapeNone ), ColorRGB( 255, 0, 255 ), 0, H, -30 );
PlotShapes( IIf( Sell, shapedownArrow, shapeNone ), ColorRGB( 255, 0, 255 ), 0, O, -10 );

Filter =  Buy OR Sell  OR  buyCond_A OR buyCond_B;

AddColumn( Buy, "Buy", 1.2, colorDefault, IIf( Buy, colorBrightGreen, colorDefault ) );
AddColumn( Sell, "Sell", 1.2, colorDefault, IIf( Sell, colorYellow, colorDefault ) );


AddColumn( _onLong, "_onLong", 1.2, colorDefault, IIf( _onLong, colorAqua, colorDefault ) );

AddColumn( buyCond_A, "_buyCond1A", 1.2, colorDefault, IIf( buyCond_A, colorGreen, colorDefault ) );
AddColumn( buyCond_B, "_buyCond1B", 1.2, colorDefault, IIf( buyCond_B, colorPaleTurquoise, colorDefault ) );

Even after changing the #pragma literal string, the result is the same. Only one follow-up trade is happening per day.

To get better understanding of what is happening in your code and how functions work, use advice given here: How do I debug my formula?

You need to watch the values in the variables that you generate in your own code to see what actually happens in contrast to what you think is happening.

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