shellExec upstox python api script to place orders regularly

Greetings

I am runing the following AFL to place orders with Upstox Python Api script using shellExec
command for the symbol NIFTY18SEPFUt in 5 min Inter day data in Realtime.
It is placing orders under Bar replay.While in realtime it is not placing buy sell OCO orders.
no errors thrown.
my code is below

_SECTION_BEGIN("Price");
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", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();

_SECTION_BEGIN("Name");
function ParamOptimize( pname, defaultval, minv, maxv, step )
{
return Optimize( pname,
Param( pname, defaultval, minv, maxv, step ),
minv, maxv, step );
}
// on 5 min data refresh only
RequestTimedRefresh(300);

EntryBufferPct = ParamOptimize("Entry Buffer %", 0, 0, 2, 0.1);
NewDay = (Day()!= Ref(Day(), -1)) OR BarIndex() == 0;
EndDay=Ref(NewDay,-1);
HighestOfDay = HighestSince(NewDay,H,1);
LowestOfDay = LowestSince(NewDay,L,1);
realbh = ValueWhen(NewDay,HighestOfDay ,1) * (1 + (EntryBufferPct/100));
realbl = ValueWhen(NewDay,LowestOfDay ,1) * (1 - (EntryBufferPct/100));

res=realbh;
sup=realbl;
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);
supres_col=IIf(avn==1,colorGreen,colorRed);

Plot(supres,"",supres_col,styleStaircase|styleThick);
SetBarFillColor(supres_col);
Plot(C,"",supres_col,styleCandle);
// To avoid previous day signals

Buy=avn==1 AND Now(3)==LastValue(DateNum());
Short=avn==-1 AND Now(3)==LastValue(DateNum());

Buy=ExRem(Buy, Short OR endday);Short=ExRem(Short,Buy OR endday);
Sell=Short;
Cover=Buy;
Filter=Buy OR Short ;

PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorLime, 0,L, Offset=-45);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorRed, 0,H, Offset=-45);

//if (LastValue(DateNum()) == 091000)
//{
// Nz(StaticVarGet("BuyorderPlaced"))==False;
// Nz(StaticVarGet("SellorderPlaced"))==False;
//}
if ( LastValue(Buy) == True AND Nz(StaticVarGet("BuyorderPlaced"))==False)
{
AlertIF( Buy, "EMAIL", "Realtf buy alert on "+FullName(), 1 );
AlertIF( Buy, "", "Simple text alert", 1);
ShellExecute("C:\Users\Administrator\AppData\Local\Programs\Python\Python37\pyamibridgeupstoxbuyOCO.py", "", "" );
StaticVarSet("BuyorderPlaced",True);
//tradeType = "BUY";
//placeBracketOrder(AT_EXCHANGE, AT_SYMBOL, tradeType, AT_QUANTITY, buyPrice, TARGET, STOPLOSS, TRAILING_STOPLOSS, 1);
}

if ( LastValue(Short) == True AND Nz(StaticVarGet("SellorderPlaced"))==False)

{
AlertIF( Short, "EMAIL", "Realtf short alert on "+FullName(), 3 );
AlertIF( Short, "", "Simple text alert", 3 );
ShellExecute("C:\Users\Administrator\AppData\Local\Programs\Python\Python37\pyamibridgeupstoxsellOCO.py", "", "" );
StaticVarSet("SellorderPlaced",True);
//tradeType = "SELL";
//placeBracketOrder(AT_EXCHANGE, AT_SYMBOL, tradeType, AT_QUANTITY, sellPrice, TARGET, STOPLOSS, TRAILING_STOPLOSS, 1);
}

_SECTION_END();

The aim is place only one Buy and or Sell orders every day without my intervension.
kindly modify and guide me to make it work on every day.
thanking you.
V.Periasamy

Atleast edit your post and use code tags for the AFL, others have to read (and understand) code you know!

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", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();

_SECTION_BEGIN("Name");
function ParamOptimize( pname, defaultval, minv, maxv, step )
{
return Optimize( pname,
Param( pname, defaultval, minv, maxv, step ),
minv, maxv, step );
}
// on 5 min data refresh only
RequestTimedRefresh(300);

EntryBufferPct = ParamOptimize("Entry Buffer %", 0, 0, 2, 0.1);
NewDay = (Day()!= Ref(Day(), -1)) OR BarIndex() == 0;
EndDay=Ref(NewDay,-1);
HighestOfDay = HighestSince(NewDay,H,1);
LowestOfDay = LowestSince(NewDay,L,1);
realbh = ValueWhen(NewDay,HighestOfDay ,1) * (1 + (EntryBufferPct/100));
realbl = ValueWhen(NewDay,LowestOfDay ,1) * (1 - (EntryBufferPct/100));

res=realbh;
sup=realbl;
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);
supres_col=IIf(avn==1,colorGreen,colorRed);

Plot(supres,"",supres_col,styleStaircase|styleThick);
SetBarFillColor(supres_col);
Plot(C,"",supres_col,styleCandle);
// To avoid previous day signals

Buy=avn==1 AND Now(3)==LastValue(DateNum());
Short=avn==-1 AND Now(3)==LastValue(DateNum());

Buy=ExRem(Buy, Short OR endday);Short=ExRem(Short,Buy OR endday);
Sell=Short;
Cover=Buy;
Filter=Buy OR Short ;

PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorLime, 0,L, Offset=-45);
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorRed, 0,H, Offset=-45);

//if (LastValue(DateNum()) == 091000)
//{
//	Nz(StaticVarGet("BuyorderPlaced"))==False;
//	Nz(StaticVarGet("SellorderPlaced"))==False;
//}
if ( LastValue(Buy) == True AND Nz(StaticVarGet("BuyorderPlaced"))==False)
{
AlertIF( Buy, "EMAIL", "Realtf buy alert on "+FullName(), 1 );
AlertIF( Buy, "", "Simple text alert", 1);
ShellExecute("C:\Users\Administrator\AppData\Local\Programs\Python\Python37\pyamibridgeupstoxbuyOCO.py", "", "" );
StaticVarSet("BuyorderPlaced",True);
//tradeType = "BUY";
//placeBracketOrder(AT_EXCHANGE, AT_SYMBOL, tradeType, AT_QUANTITY, buyPrice, TARGET, STOPLOSS, TRAILING_STOPLOSS, 1);
}

if ( LastValue(Short) == True AND Nz(StaticVarGet("SellorderPlaced"))==False)

{
AlertIF( Short, "EMAIL", "Realtf short alert on "+FullName(), 3 );
AlertIF( Short, "", "Simple text alert", 3 );
ShellExecute("C:\Users\Administrator\AppData\Local\Programs\Python\Python37\pyamibridgeupstoxsellOCO.py", "", "" );
StaticVarSet("SellorderPlaced",True);
//tradeType = "SELL";
//placeBracketOrder(AT_EXCHANGE, AT_SYMBOL, tradeType, AT_QUANTITY, sellPrice, TARGET, STOPLOSS, TRAILING_STOPLOSS, 1);
}

_SECTION_END();

Will this code only generate interday Buy signal?

Buy=avn==1 AND Now(3)==LastValue(DateNum());
clarify?

For intraday Logic, its supposed to be done this way

Buy  = <some conditions> AND TimeNum() < 153000;
sell = <some conditions>  OR TimeNum() > 155000;

so if Exchange closes at 4PM,
then we can ensure that all fresh buys occur before 3:30PM and trade should square-off by 3:50PM,

So I Should not use this condition

Buy=avn==1 AND Now(3)==LastValue(DateNum());

use only Buy=avn==1
then it will execute the ShellExec Command. in my code.
Am I correct?.

Tell me the difference between Bar Replay and Scan alerts.

The Manual explains it better than I can.

2 Likes