Hello guys!
I am currently trying to adjust my algo for real-time trading and as most of the people, I also have issues with duplicate orders in one Bar. I already read the
"Preventing Repeat Orders and Whipsaws"
and am trying to use the sample code in the 2nd example i.e Clearing OrderIDs at the start of a new Bar.
However, obviously just copy and paste of that code does not do the trick so I am turning to you guys for advice in terms of what needs to be adjusted in the code in order for it to work properly.
RequestTimedRefresh( 1 );
ibc = GetTradingInterface("IB");
Transmit = ParamToggle("Transmit","OFF|ON",0);
BuyTrigger = ParamTrigger("Place Buy order","BUY");
SellTrigger = ParamTrigger("Place Sell order","SELL");
Reset = ParamTrigger("Reset All","RESET");
PrevTN = StaticVarGet("TimeNumber");
TN = LastValue(TimeNum());
NewBar = TN != PrevTN;
StaticVarSet("TimeNumber",TN);
BuyOrderID = StaticVarGetText("BuyOrderID");
SellOrderID = StaticVarGetText("SellOrderID");
BuyPending = ibc.IsOrderPending(BuyOrderID);
SellPending = ibc.IsOrderPending(SellOrderID);
if( NewBar )
{
if( NOT BuyPending ) StaticVarSetText("BuyOrderID","");
if( NOT SellPending ) StaticVarSetText("SellOrderID","");
SetChartBkColor( colorWhite );
}
if( BuyTrigger AND BuyOrderID == "" )
{
BuyOrderID= ibc.ModifyOrder( BuyOrderID, Name(), "Buy", 100, "MKT", 0, 0, "Day", Transmit);
StaticVarSetText("BuyOrderID",BuyOrderID);
SetChartBkColor( colorBrightGreen ) ;
}
else if( SellTrigger AND SellOrderID == "" )
{
SellORderID = ibc.ModifyOrder( SellORderID , Name(), "Sell", 100, "MKT", 0, 0, "Day", Transmit);
StaticVarSetText("SellOrderID",SellOrderID);
SetChartBkColor( colorRed ) ;
}
else if( Reset )
{
StaticVarSetText("BuyOrderID","");
if( BuyPending ) ibc.CancelOrder( BuyOrderID );
StaticVarSetText("SellOrderID","");
if( SellPending ) ibc.CancelOrder( SellOrderID );
ibc.CloseAllOpenPositions();
}
LastTWSMsg = ibc.getLastError( 0 );
BuyStatus = WriteIf( BuyOrderID != "", BuyOrderID+", Status: "+ibc.GetStatus( BuyORderID, True ),"");
SellStatus= WriteIf( SellOrderID != "", SellOrderID+", Status: "+ibc.GetStatus( SellORderID, True ),"");
LastBuyTime= Nz(StaticVarGet("LastBuyTime"));
LastSellTime= Nz(StaticVarGet("LastSellTime"));
Title = "\n"+
"Last TWS Error Msg: "+LastTWSMsg+"\n"+
" BuyOrderID: "+BuyStatus+"\n"+
" SellOrderID: "+SellStatus+"\n"+
"TWS Position Size: "+NumToStr(ibc.GetPositionSize( Name() ),1.0,False);
Plot(C,"Close",colorBlack,styleBar);