This is execution module of BO which can be paste next to any Strategy ,Here’s what I am looking for:
once Buy Order Trigger:
When the strategy indicates a buy signal, a buy order should be placed. The order should be placed on the next candle’s opening price (delayed order).
Flexible Target, Stop Loss (SL), and Trailing Stop Loss:
I should be able to set the target and stop loss as a percentage (ranging from 1% to 50%) when starting the trade.
Exit the trade immediately whenever the target, SL, or trailing stop loss (TSL) is hit.
Here are the exit conditions for lot-wise exit:
Condition 1: If the price moves more than 100 points in a single candle, exit all 10 lots immediately.
Condition 2: If the price touches the SL or target, exit all remaining/open 10 lots.
Condition 3: a) If the price moves up by 30 points, exit 2 lots, and trail the remaining 8 lots.
b ) If the price moves up another 50 points, exit 3 more lots, and trail the remaining 5 lots.
c) Upon reaching an 80-point rise, exit 3 more lots, leaving 2 lots to trail with the TSL/target.
A trailing stop loss should be set in Rs (this should be flexible and updated live).
An old Execution module shared
///section begins for options; apply strategy code above this section
bsym= ParamStr("Base Symbol", "BANKNIFTY");
expiry= ParamStr("Expiry(dd-mm-yy)","26-09-2019");
strike= Param("Strike",0, -100000, 100000);
otype= ParamStr("Type CE or PE", "CE");
qt= Param("Quantity", 20, 1, 10000);
stag= ParamStr("Strategy Tag", "STG1");
otpt= ParamStr("OrdType|ProdType", "Market|Intraday");
per10 = Param( "Trade Entry From(HHMM)", 920, 900, 2300, 1 );
per11 = Param( "Trade Entry Upto(HHMM)", 1445, 900, 2300, 1 );
per12 = Param( "Trade Exit(HHMM)", 1515, 900, 2300, 100 );
pop= ParamToggle( "Percentage or Points", "Points|Percentage");
slp = Param( "StopLoss", 0, 0, 1000, 0.1 );
tsl= Param("Trail Stop", 0, 0, 1000, 0.1);
tgtp = Param( "Target", 0, 0, 1000, 0.1 );
delay= ParamToggle("Trade Intrabar?", "YES|Candle Completion");
dlong= ParamToggle("Disable Long?", "NO|YES");
dshort= ParamToggle("Disable Short?", "NO|YES");
RequestTimedRefresh( 1, onlyvisible = False ) ;
if(dlong){Buy=Sell=0;}
if(dshort){Short=Cover=0;}
dd= DaysSince1900();
d=prof= 0;
if(delay)
{Buy=Ref(Buy,-1); Sell=Ref(Sell,-1); Short= Ref(Short,-1); Cover= Ref(Cover,-1);}
intraex = (TimeNum() > per12 * 100);
intraen = ( TimeNum() <= per11 * 100 AND TimeNum() >= per10 * 100 );
if(LastValue(BarsSince(Buy)<BarsSince(Short)))
oprice= ValueWhen(Buy, O);
else
oprice= ValueWhen(Short, O);
osym= bsym + "|" + expiry + "|" + strike + "|" + otype;
instr= "OPTIDX";
qty= NumToStr(qt, 1.0, False);
Buy1 = Buy;
Sell1 = Sell;
Short1 = Short;
Cover1 = Cover;
Buy=Sell=Short=Cover=0;
bflag = sflag = sp=bp = 0;
slarr = tgtarr = qtarr= Null;
for ( i = 10; i < BarCount; i++ )
{
if ( ( Cover1[i] OR intraex[i]OR( H[i] > slarr[i-1] AND (sl>0 OR tsl>0) ) OR ( L[i] < tgtarr[i-1] AND tgt > 0 ) ) AND sflag )
{
Cover[i] = 1;
CoverPrice[i]= C[i];
sflag = 0;
d= dd[i]; prof= sp-C[i];
}
if ( ( Sell1[i] OR intraex[i] OR( L[i] < slarr[i-1] AND (sl>0 OR tsl>0) ) OR ( H[i] > tgtarr[i-1] AND tgt > 0 ) ) AND bflag )
{
Sell[i] = 1;
SellPrice[i]= C[i];
bflag = 0;
d= dd[i]; prof= C[i]- bp;
}
if ( Buy1[i] AND intraen[i] AND bflag == 0 )
{
Buy[i] = 1;
bflag = 1;
bp= C[i];
sl=slp; tgt= tgtp;
if(pop)
{sl= slp*bp/100;
tgt= tgtp*bp/100;}
if ( slp )
slarr[i] = bp-sl;
if ( tgtp )
tgtarr[i] = bp+tgt;
}
if ( bflag AND Buy[i]==0 )
{
slarr[i] = slarr[i-1];
tgtarr[i] = tgtarr[i-1];
if(tsl>0 AND pop)
slarr[i] = Max(slarr[i-1], H[i]*(1-tsl/100));
if(tsl>0 AND !pop)
slarr[i] = Max(slarr[i-1], H[i]-tsl);
}
if ( Short1[i] AND intraen[i] AND sflag == 0 )
{
Short[i] = 1;
sflag = 1;
Sp= C[i];
sl= slp; tgt= tgtp;
if(pop)
{sl= slp*Sp/100;
tgt= tgtp*Sp/100;}
if ( slp )
slarr[i] = sp + sl;
if ( tgtp )
tgtarr[i] = sp - tgt;
}
if ( sflag AND Short[i] == 0 )
{
slarr[i] = slarr[i-1];
tgtarr[i] = tgtarr[i-1];
if(tsl>0 AND pop)
slarr[i] = Min(slarr[i-1], L[i]*(1+tsl/100));
if(tsl>0 AND !pop)
slarr[i] = Min(slarr[i-1], L[i]+tsl);
}
}
Plot( slarr, "SL", colorViolet, styleThick );
Plot( tgtarr, "TGT", colorViolet, styleThick );
PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorViolet, 0, H, Offset = 15 );
PlotShapes( IIf( Short, shapeDownArrow, shapeNone ), colorViolet, 0, L, Offset = 15 );
PlotShapes( IIf( Cover, shapeStar, shapeNone ), colorViolet, 0, H, Offset = -25 );
PlotShapes( IIf( Sell, shapeStar, shapeNone ), colorViolet, 0, L, Offset = -25 );
sig = IIf( BarsSince( Buy ) < BarsSince( Short ), 1, 0 );
messageboard = ParamToggle( "Message Board", "Show|Hide", 1 );
if ( messageboard == 1 )
{
GfxSelectFont( "Tahoma", 13, 100 );
GfxSetBkMode( 1 );
GfxSetTextColor( colorWhite );
GfxSelectSolidBrush( colorDarkTeal ); // this is the box background color
pxHeight = Status( "pxchartheight" ) ;
xx = Status( "pxchartwidth" );
x = 5;
x2 = 450;
y = pxHeight;
GfxSelectPen( colorGreen, 1 ); // broader color
GfxRoundRect( x, y - 110, x2, y , 7, 7 ) ;
GfxTextOut( ( "Symbol "+ osym ), 13, y-140 );
GfxTextOut( ( "Last" + " Signal came " + ( BarsSince( Buy OR Short ) ) * Interval() / 60 + " mins ago" ), 13, y - 120 ) ; // The text format location
GfxTextOut( ( "" + WriteIf( sig == 1, "BUY @ " + ValueWhen(Buy,C) , "SHORT @ " + ValueWhen(Short,C) ) ), 13, y - 100 );
GfxTextOut( "Stop Loss : " + WriteIf(slp==0, "Not Activated", ""+slarr), 13, y - 80 );
GfxTextOut( "Target : " + WriteIf(tgtp==0, "Not Activated", ""+tgtarr), 13, y - 60 );
// GfxTextOut( ( "jhjh " ), 13, y-20 );
}
//section begins for auto trade
bp = NumToStr(bp, 1.2, False);
sp = NumToStr(sp, 1.2, False);
global algoji;
algoji = Name() + NumToStr( Interval() / 60, 1.0, False ) ;
procedure aStaticVarSet( SName, Svalue )
{
global algoji;
StaticVarSet( Sname + algoji, Svalue );
}
function aStaticVarGet( SName )
{
global algoji;
Var = StaticVarGet( Sname + algoji );
if ( IsNull( Var = StaticVarGet( Sname + algoji ) ) )
Var = 0;
return Var;
}
sym = osym;
//_TRACE("t"+t);
Checkdt=Nz(aStaticVarGet("lastdt"));
dt = LastValue( DateTime() );
Checkdtss=Nz(aStaticVarGet("lastdtss"));
dtss = LastValue( DateTime() );
Checkdtc=Nz(aStaticVarGet("lastdtc"));
dtc = LastValue( DateTime() );
Checkdts=Nz(aStaticVarGet("lastdts"));
dts = LastValue( DateTime() );
RTBuy = LastValue( Buy) AND Checkdt != dt;
RTShort = LastValue( Short) AND Checkdtss != dtss;
RTCover = LastValue( Cover) AND Checkdtc != dtc;
RTSell = LastValue( Sell) AND Checkdts != dts;
if ( RTCover )
{
aStaticVarSet("lastdtc",dtc );
StaticVarSet("counter", Nz(StaticVarGet("counter"))+1 );
_TRACE( "#"+Nz(StaticVarGet("counter"))+",SX,"+sym+",,," +bp +","+qty+","+instr+",,");
Algoji_Signal(NumToStr(Nz(StaticVarGet("counter")),0,False), "SX",sym,otpt,"",bp,qty,instr,stag);
}
if ( RTSell )
{
aStaticVarSet("lastdts",dts );
StaticVarSet("counter", Nz(StaticVarGet("counter"))+1 );
_TRACE( "#"+Nz(StaticVarGet("counter"))+",LX,"+sym+",,," +sp +","+qty+",,,");
Algoji_Signal(NumToStr(Nz(StaticVarGet("counter")),0,False), "LX",sym,otpt,"",sp,qty,instr,stag);
}
if ( RTBuy )
{
aStaticVarSet("lastdt",dt );
StaticVarSet("counter", Nz(StaticVarGet("counter"))+1 );
_TRACE( "#"+Nz(StaticVarGet("counter"))+",LE,"+sym+",,," +bp +","+qty+","+instr+",,");
Algoji_Signal(NumToStr(Nz(StaticVarGet("counter")),0,False), "LE",sym,otpt,"",bp,qty,instr,stag);
}
if ( RTShort )
{
aStaticVarSet("lastdtss",dtss );
StaticVarSet("counter", Nz(StaticVarGet("counter"))+1 );
sp= NumToStr(Close[BarCount-1],1.2, False);
_TRACE( "#"+Nz(StaticVarGet("counter"))+",SE,"+sym+",,," +sp +","+qty+","+instr+",,");
Algoji_Signal(NumToStr(Nz(StaticVarGet("counter")),0,False), "SE",sym,otpt,"",sp,qty,instr,stag);
}
/*
===============================================
Thanks in advance```