ShellExecute to avoid placing multiple orders


if (LastValue(Short))
{
//AlertIf( Short, "EMAIL", "Realtf Sell A sample alert on "+FullName(),3, 1+2 );
ShellExecute("C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37\\pyamibridgeupstoxsellOCO.py", "", "" );

}

The above code places multiple orders
How to place only one order?
Chart Refresh rate is 1 Sec.
interday time intravel 5Min

the orderpage is
![Capturemultiorder|625x500]
(upload://igttq00dlCu6TWEGij4pfprrEt4.png)Capturemultiorder

You should post entire code for the "Short" variable.

Anyway, faulty code will lead to issues.

Also, if you use LastValue(Short), this itself is a problem, for 5 minutes, your last value will be 1 incase signal is generated and if your chart parses it every second, you'll virtually fire 300 orders in a bar.

In short: you need to store STATE between executions

if (LastValue(Short) And Nz(StaticVarGet("OrderPlaced") ) == False )
{
 StaticVarSet("OrderPlaced", True ); // store STATE for subsequent executions
 ShellExecute("C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37\\pyamibridgeupstoxsellOCO.py", "", "" );
}

3 Likes

Not placing Email Alerts or shellexecute
I want to place only one buy or sell order everyday avoiding multiple orders.(Blue and Yellow triangles)
Suggest to improve it.!

Capturevp|690x296

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

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

Buy=avn==1 ;
Short=avn==-1 ;

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=-20); 
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorRed, 0,H, Offset=-20);

// trades limit
N = 1;

// identify new day
dn = DateNum();
newDay = dn != Ref( dn, -1 );
tradeCount1 = tradeCount2=Buyon =Sellon= 0;

for( i = 0; i < BarCount; i++ )
{
    // reset trade counter on the new day
    if( newDay[ i ] ) tradeCount1 = 0;
	 if( newDay[ i ] ) tradeCount2 = 0;
	 if( newDay[ i ] ) Buyon = 0;
	 if( newDay[ i ] ) Sellon = 0;
	

    // keep buy signal if there is no trade and trade count did not hit the limit
    if( Buy[ i ] AND tradeCount1 < N AND NOT Buyon )
    {
        Buyon = 1;
        TradeCount1++;
    }
    else
        Buy[ i ] = 0; // ignore other buy signals

    if ( Buyon AND Short[ i ] )
    {
        Buyon = 0; // reset onBuy flag on exit
    }


if( Short[ i ] AND tradeCount2 < N AND NOT Sellon )
  {
        Sellon = 1;
        TradeCount2++;	
	
   }
   else
        Short[i] = 0; // ignore other buy signals
    if ( Sellon AND Buy[i] )
    {
        Sellon = 0; // reset onBuy flag on exit
    }		 
}

if (LastValue(Buy) AND Nz(StaticVarGet("OrderPlacedbuy") ) == False )
{
	StaticVarSet("OrderPlacedbuy", True ); // store STATE for subsequent executions
	AlertIf( Buy, "EMAIL", "Realtf buy A sample alert on "+FullName(),1, 1+2 );
	ShellExecute("C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37\\pyamibridgeupstoxbuyOCO.py", "", "" );

}

if (LastValue(Short) AND Nz(StaticVarGet("OrderPlacedsell") ) == False )
{
 	StaticVarSet("OrderPlacedsell", True ); // store STATE for subsequent executions
	AlertIf( Short, "EMAIL", "Realtf Sell A sample alert on "+FullName(),3, 1+2 );
 	ShellExecute("C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37\\pyamibridgeupstoxsellOCO.py", "", "" );
	
}

		 PlotShapes( Buy*shapeUpTriangle, colorBlue, 0, Low, -40 );
		 PlotShapes( Short*shapeDownTriangle, colorYellow, 0, Low, -40 );


_SECTION_END();

Capturevp

ExRem also appled to remove excess signals

Firstly, I was reluctant yesterday pointing to Static variable because you hardly provided any info.

Now, that you are aware of StaticVarSet(), it appears you have not read the Docs on it.

Where in your code is the condition to reset the two variables
OrderPlacedbuy and OrderPlacedsell ?

Persist Is set to True, that means if you don't reset the variables, you will only get 1 buy and 1 sell for Life.

secondly,
Apply the same logic of static Variable to you buy and sell code, that is, put the whole buy and sell code in their respective IF statement so buy and sell will only execute as per your condition.

Thirdly, code a condition that satisfies what you want, if you want one trade per day, do it so. If you want one-pair of trade (intraday trade) then code accordingly.

Fourth, use StaticVarSetText() if you don't need an Array, that way you will have only one existing hardcoded STATE at any given time.

Also, read twice what Persist=True does in static variables.

2 Likes

I agree with you @travick There are more and more similar posts. They usually have at least a few things in common:

  1. Lack of all the necessary information in the initial post:

First post:

Second post:

  1. You won't find words like Thank you or any other signs of appreciation of the received help.

  2. Instead of Could you please tell me how to... there is only I want this, I want that...

In this case there is even an exclamation mark which is used when giving orders!


For the reasons above (I'm not referring to this specific case) I'm gradually learning not to respond and not to help users who don't appreciate other users time, don't use the word thank you, don't respond in any way after receiving help (they just disappear) or are not used to putting any effort into solving their own problems...

4 Likes

I highly appreciate travick's advice and follow the above guidelines in my next post.

Hi Milosz
Thanking you
I am poor in communication ,in future your suggestions
will be taken in account while postings.
with regards
v.periasamy

@veepsirtt - poor excuse. Saying thank you does not require M.Sc. in communications. Also it does not require special skills to take the time to describe what you really need in the very first post.
Remember - you come here to ask for help. If you want others to help you, you need to show that you did your homework first and at least precisely described what you need.

4 Likes

Hi
to get first signal of the day (either buy or short) ignore rest of all signals of that day
This is I want to achive ,finally after one week of searching the final code is here.
I used this Statements

isEndOfDay = Nz(TimeFrameExpand(1, inDaily, expandPoint));
Buy = ExRem(Buy, isEndOfDay);
Short = ExRem(Short, isEndOfDay);

Thank you all

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

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

Buy=avn==1 ;
Short=avn==-1 ;


isEndOfDay = Nz(TimeFrameExpand(1, inDaily, expandPoint));
Buy = ExRem(Buy, isEndOfDay);
Short = ExRem(Short, isEndOfDay);

Sell=Short;
Cover=Buy;
Filter=Buy OR Short   ;
if (LastValue(Buy) )
{
	AlertIf( Buy, "My alert"+FullName(), "BUY @ " + C, 1 );	
	ShellExecute("C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37\\pyamibridgeupstoxbuyOCO.py", "", "" );

}
if (LastValue(Short) )
{
	AlertIf( Short, "My alert"+FullName(), "SHORT @ " + C, 1 );	
	ShellExecute("C:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python37\\pyamibridgeupstoxsellOCO.py", "", "" );

}

PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorLime, 0,L, Offset=-20); 
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorRed, 0,H, Offset=-20);
  
PlotShapes( Buy*shapeUpTriangle, colorBlue, 0, Low, -40 );
PlotShapes( Short*shapeDownTriangle, colorYellow, 0, Low, -40 );


_SECTION_END()
3 Likes