# Need help on order palce and EOD

#### Muroyal

##### New Member
I have AFL which have a target and stop loss. kindly help me …
2. 1 quantity should exit when stop loss or taarget hit and then the quntity should be again 1;
3. if stop_loss or target is not hit then it quantity should be 2 so that it will cover 1 QTY and palce 1 QTY on next buy are short trade.
4. EOD if the trade is open it should cover or sell.
Here is the AFL.

//Title = Date() + “, Op=” + Open + “, Hi=” + High + “, Lo=” + Low + “, LTP=” + Close + “, Change= " + SelectedValue( ROC( C, 1 ) ) + “%” + “\n Todays High=" + DayHigh + ", Todays Low=” + DayLow + " © PRIME SYSTEM” ;
prev=AMA2(C,1,0);
d=IIf(C>Ref(Max(Max(H,Ref(H,-20)),Max(Ref(H,-10),Ref(H,-15))),-1),Min(Min(L,Ref(L,-20)),Min(Ref(L,-10),Ref(L,-15))),
IIf(C<Ref(Min(Min(L,Ref(L,-20)),Min(Ref(L,-10),Ref(L,-15))),-1),Max(Max(H,Ref(H,-20)),Max(Ref(H,-10),Ref(H,-15))),PREV));
a=Cross(Close,d);
b=Cross(d,Close);
s=state>Ref(state,-1);
ss=state<Ref(state,-1);
sss=state==Ref(state,-1);
col=IIf(state == 1 ,51,IIf(state ==0,4,1));
Plot(C,"",Col,64);
_SECTION_BEGIN(“swing1”);
no=20;
res=HHV(H,no);
sup=LLV(L,no);
avd=IIf(C>Ref(res,-1),1,IIf(C<Ref(sup,-1),-1,0));
avn=ValueWhen(avd!=0,avd,1);
supres=IIf(avn==1,sup,res);
a=Cross(C,supres);
b=Cross(supres,C);
style = a * styleStaircase + b * styleStaircase;
PlotShapes(a,style, IIf(a,colorGreen,colorRed), 0, IIf(a,Low,High));
Plot(supres,“Swing”,colorYellow,styleStaircase);
_SECTION_END();
_SECTION_BEGIN(“trend”);
uptrend=PDI(20)>MDI(10)AND Signal(29)<MACD(13);
downtrend=MDI(10)>PDI(20)AND Signal(29)>MACD(13);
Plot( 2, /* defines the height of the ribbon in percent of pane width /“ribbon”,
IIf( uptrend, colorGreen, IIf( downtrend, colorRed, 0 )), /
choose color */
styleOwnScale|styleArea|styleNoLabel, -0.5, 100 );
_SECTION_END();
Sell = ss;
*/
Buy = Sell = Short = Cover = 0;
Buy = (s AND a AND uptrend) OR s ;
Short =( ss AND b AND downtrend) OR ss ;
//Sell = (ss AND b AND downtrend) OR ss ;
//Cover = (s AND a AND uptrend) OR s ;
//plot volume graph
//SetChartBkGradientFill( ParamColor(“BgTop”, ColorRGB( 172,172,172 )),ParamColor(“BgBottom”, ColorRGB( 102,136,187 )),ParamColor(“titleblock”,ColorRGB( 255,255,255 )));
_SECTION_BEGIN(“Raghee Horner’s EMA’s”);
EMA1 = EMA(H,34);
EMA2 = EMA(L,34);
EMA3 = EMA(C,34);
Plot( EMA1, “EMA1” ,ParamColor( “Color1”, colorRed ), ParamStyle(“Style”, styleLine|styleThick) | styleNoRescale );
Plot( EMA2, “EMA2” ,ParamColor( “Color2”, colorGreen ), ParamStyle(“Style”, styleLine|styleThick) | styleNoRescale );
Plot( EMA3, “EMA3” ,ParamColor( “Color3”, colorBlue ), ParamStyle(“Style”, styleLine|styleThick) | styleNoRescale );
_SECTION_END();
_SECTION_BEGIN(“Supertrend”);
procedure calcTrend_proc(ATR_Period,tr,ATR_Multiplier,TrendMode,CalcPrice)
{
global buffer_line_down;
global buffer_line_up;
buffer_line_down = Null;
buffer_line_up = Null;
PHASE_NONE = 0;
PHASE_SELL = -1;
phase=PHASE_NONE;
band_upper = 0;band_lower = 0;
for(i = ATR_Period + 1; i < BarCount; i++)
{
band_upper = CalcPrice + ATR_Multiplier * tr;
band_lower = CalcPrice - ATR_Multiplier * tr;
if(phase==PHASE_NONE)
{
buffer_line_up = CalcPrice;
buffer_line_down = CalcPrice;
}
{
buffer_line_up = band_lower;
buffer_line_up[i-1] = buffer_line_down[i-1];
}
if(phase!=PHASE_SELL && Close<buffer_line_up[i-1] && !IsEmpty(buffer_line_up[i-1]))
{
phase = PHASE_SELL;
buffer_line_down = band_upper;
buffer_line_down[i-1] = buffer_line_up[i-1];
}
if(phase==PHASE_BUY && ((TrendMode==0 && !IsEmpty(buffer_line_up[i-2])) || TrendMode==1) )
{
if(band_lower>buffer_line_up[i-1])
{
buffer_line_up = band_lower;
}
else
{
buffer_line_up = buffer_line_up[i-1];
}
}
if(phase==PHASE_SELL && ((TrendMode==0 && !IsEmpty(buffer_line_down[i-2])) || TrendMode==1) )
{
if(band_upper<buffer_line_down[i-1])
{
buffer_line_down = band_upper;
}
else
{
buffer_line_down = buffer_line_down[i-1];
}
}
}
}
TrendMode = ParamToggle(“TrendMode”,“Off|On”,1);
ATR_Multiplier = Param(“ATR_Multiplier”,2,0.1,10,0.1);
ATR_Period = Param( “ATR_Period”,5,1,20,1);
tr = ATR(ATR_Period);
CalcPrice = (H+L)/2;
calcTrend_proc(ATR_Period,tr,ATR_Multiplier,TrendMode,CalcPrice);
//Plot(buffer_line_up,"\ntu",ColorRGB(28,134,238),styleThick);
//Plot(buffer_line_down,"\ntd",ColorRGB(205,51,51),styleThick);
//Plot( 2,"",IIf(buffer_line_up,colorGreen,colorBlack),styleOwnScale|styleArea|styleNoLabel, -0.5, 100 );
//Plot( 4,"",IIf(buffer_line_down,colorRed,colorBlack),styleOwnScale|styleArea|styleNoLabel, -0.5, 100 );
_SECTION_END();
if(shouldSquareOff() == 1) {
squareOffPosition();
}
else {
if ( LastValue(Buy) == True AND isFirstOrder() == 0 )
{
“regular”, 0, 0, 0);
Quantity=2;
}
if ( LastValue(Short) == True AND isFirstOrder() == 0)
{

placeOrderAdvancedNew(AT_EXCHANGE, AT_INSTRUMENT, Name(), "SELL", AT_PRODUCT_TYPE, AT_ORDER_TYPE, 1,0, defaultTriggerPrice(), defaultDisclosedQuantity(), AT_OPTION_TYPE, AT_STRIKE_PRICE, AT_EXPIRY, defaultClientId(), defaultValidity(), defaultTraderType(), defaultMarketProtectionPct(), defaultStrategyId(), defaultComments(),

“regular”, 0, 0, 0);
Quantity=2;
}
if ( LastValue(Buy) == True )
{
“regular”, 0, 0, 0);
Quantity=2;
}
if ( LastValue(Short) == True )
{

“regular”, 0, 0, 0);
Quantity=2;
}
}
StopLevel = Param( “Stop Loss (%)”, 0.25, 0.01, 10, 0.01 );
TgtLevel = Param( “Profit target (%)”, 0.5, 0.01, 10, 0.01 );
ApplyStop( stopTypeLoss, stopModePercent, StopLevel, True );
ApplyStop( stopTypeProfit, stopModePercent, TgtLevel, True );
Equity( 1, 0 );
sellcond2=Sell==2; covercond2=Cover==2;
sellcond3=Sell==3; covercond3=Cover==3;
Sell=ExRem(Sell,Cover);
Cover=ExRem(Cover,Sell);
if ( LastValue(covercond2) ==True OR LastValue(covercond3) ==True)
{
“regular”, 0, 0, 0);
//AT_QUANTITY = Param(“Order quantity (lot size)”, 1, 0, 300000, 10);
Quantity=1;
}
if ( LastValue(sellcond2)==True OR LastValue(sellcond3) ==True)
{
placeOrderAdvancedNew(AT_EXCHANGE, AT_INSTRUMENT, Name(), "SELL", AT_PRODUCT_TYPE, AT_ORDER_TYPE, 1,0, defaultTriggerPrice(), defaultDisclosedQuantity(), AT_OPTION_TYPE, AT_STRIKE_PRICE, AT_EXPIRY, defaultClientId(), defaultValidity(), defaultTraderType(), defaultMarketProtectionPct(), defaultStrategyId(), defaultComments(),

“regular”, 0, 0, 0);
//AT_QUANTITY = Param(“Order quantity (lot size)”, 1, 0, 300000, 10);
Quantity=1;
}

//PlotShapes(IIf(sellcond1,shapeSmallDownTriangle,shapeNone),colorOrange,0,H,-25);
PlotShapes(IIf(sellcond3,shapeHollowDownArrow,shapeNone),colorOrange,0,H,-15);
PlotShapes(IIf(sellcond2,shapeStar,shapeNone),colorOrange,0,H,-15);
//PlotShapes(IIf(exit,shapeStar,shapeNone),colorOrange,0,H,-15);
///PlotShapes(IIf(covercond1,shapeSmallUpTriangle,shapeNone),colorLime,0,L,-25);
PlotShapes(IIf(covercond3,shapeHollowUpArrow,shapeNone),colorAqua,0,L,-25);
PlotShapes(IIf(covercond2,shapeStar,shapeNone),colorAqua,0,L,-25);
//PlotShapes(IIf(exit,shapeStar,shapeNone),colorAqua,0,L,-25);
WriteIf( Sell == 1, “EOD exit”,WriteIf( Sell == 3, “Profit Tgt”,WriteIf( Sell == 2, “Max stop”, “” ) ));
WriteIf( Cover == 1, “EOD exit”,WriteIf( Cover == 3, “Profit Tgt”,WriteIf( Cover == 2, “Max stop”, “” ) ));
dist = ATR(18);
for (i=0; i<BarCount; i++) {
if ( Cover )
PlotText(“Cover:” + O[ i ], i, H[ i ]+3*dist, colorGreen);
if ( Sell )
PlotText(“Cover:” + O[ i ], i, H[ i ]+3*dist, colorGreen);
}
PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), 43, layer = 0,yposition = Low, offset = -20);
PlotShapes( IIf( Short, shapeDownArrow, shapeNone ), colorRed, layer = 0, yposition = High, offset = -20); //shapeSmallCircle
//WriteIf(s,“EXIT all Short positions\nif trading long positions, enter long Now-\nOR at the market price on tomorrow’s Open with stop=”+EncodeColor(4)+WriteVal(L+.75ATR(5),1.4)+" ,","");
//WriteIf(ss,“exit all long positions today with a Market On Close (MOC) order\nOR at the market price on tomorrow’s Open with stop=”+EncodeColor(4)+WriteVal(Ref(H+.75
ATR(5), -1),1.4)+",","");
//WriteIf( sss ,“No trading signals today.”,"") ;