i have seen several of the other posts that concern this issue. But in my case the error is produced after I have closed the position and attempt to open a new order in opposite direction.
This is the basic approach:
1. Open limit entry order (with its set of StopLoss/TakeProfit orders)
2. limit order gets filled and now we have an open order with StopLoss/TakeProfit orders
3. new signal in opposite direction appears
4. close open position
5. open new limit entry order in opposite direction with its own StopLoss/TakeProfit orders (this is where the IGNORE error happens).
BTW all the trades that failed were tested in the replay connection and in that case the error is not happening. LOL my customer keeps informing me that the opposite order things is not working and when I show them this working every time in the replay connection, they are completely baffled, lol.
I looked at the Order Handling Rules and this one catches my attention:
-The strategy position is flat and an order submitted by an enter method (EnterLongLimit() for example) is active and the order is used to open a position in the opposite direction
I have no idea what the author meant to communicate with this, at first impression it says to me that even with no open opsitions nor orders that on attempting to place any EnterXXXLimi or StopMarket that the request will be ignored... LOL makes no sense, right?
So, based on that this is the idea I will try. Instead of just closing the position, I will have the strat cancel the StopLoss/TakeProfit orders before it closes it. This should then leave no trace of any open positions or orders and allow the new limit entry to be executed with its own StopLoss/TakeProfit orders.
But who knows, according to the rule since the account will be flat a new order in the opposite direction will be ignored, right? LOL... If this does not work and there is no other solution, I guess the next step is the unmanaged route or just have the strat do all of of the StopLoss/TakeProfit itselft instead of using the ninja SET functions. Hmm we'll see....
Here's a stripped down version of the code is it handles this situation, now . Hopefully the comments wll make it clear as to what is happening.
//signal can contain the values OP_BUY , OP_SELL or -1 (yea I'm a MetaQuotes buff LOL) signal=getSignal(); //Get the signal from our super duper classified approach!! if(signal==-1) return; //Aww no new signal return.. do nothing //close opened position on any new signal if(Position.MarketPosition!=MarketPosition.Flat) { //Check if we have an open position if(Position.MarketPosition==MarketPosition.Long) ExitLong(); //BTW there is only one open position at any given time, so no need to identify which position is being dealt with if(Position.MarketPosition==MarketPosition.Short) ExitShort(); } openLimitTrade(signal); //Ok lets opn a limit order with a new signal! void openLimitTrade(int op){ bool isStopOperation=false; //Flag for later use, we make sure that current price is at correct level for limit order //entryDelta is set by user it can only 0 or greater limitprice=(op==OP_BUY?Close[2]+entryDelta*TickSize:Close[2]-entryDelta*TickSize); limitprice=Math.Round(limitprice,digits); //digits is global, properly calculated for the current instrument digits after decimal //Quick Check of limitprice versus current price if(op==OP_BUY && limitprice>Open[0]) isStopOperation=true; if(op==OP_SELL && limitprice<Open[0]) isStopOperation=true; //User sets StopLoss and TakeProfit in inputs section if(StopLoss>0) SetStopLoss("dT_"+Time[1].ToString("yyyy.MM.dd HH:mm"), CalculationMode.Ticks, StopLoss,false); if(TakeProfit>0) SetProfitTarget("dT_"+Time[1].ToString("yyyy.MM.dd HH:mm"), CalculationMode.Ticks, TakeProfit); if(op==OP_BUY) if(!isStopOperation) { EnterLongLimit(0,true,contractSize,limitprice,"dT_"+Time[1].ToString("yyyy.MM.dd HH:mm")); } else EnterLongStopMarket(0,true,contractSize,limitprice,"dT_"+Time[1].ToString("yyyy.MM.dd HH:mm")); if(op==OP_SELL) if(!isStopOperation) { EnterShortLimit(0,true,contractSize,limitprice,"dT_" + Time[1].ToString("yyyy.MM.dd HH:mm")); } else EnterShortStopMarket(0,true,contractSize,limitprice,"dT_"+Time[1].ToString("yyyy.MM.dd HH:mm")); //User sets alert in input section and ah yes,, lets use telegram to get signals sent to our phones :). if(alert) telegram("opentrade",op,isStopOperation,token,chanid,message); }
AND .. This is the new approach we will try this next week when the market opens.
//signal can contain the values OP_BUY or OP_SELL (yea I'm a MetaQuotes buff LOL) signal=getSignal(); //Get the signal from our super duper classified approach!! if(signal==-1) return; //Aww no new signal return.. do nothing //close opened position on any new signal if(Position.MarketPosition!=MarketPosition.Flat) { //Check if we have an open position if(Position.MarketPosition==MarketPosition.Long) { closeStopTakeOrders(); ExitLong(); } if(Position.MarketPosition==MarketPosition.Short) { closeStopTakeOrders(); ExitShort(); } } openLimitTrade(signal); //Ok lets opn a limit order with a new signal! void closeStopTakeOrders() { //takeprofitOrder and stoplossOrder will of course be assigned when a new imit emtry order is placed CancelOrder(stoplossOrder); CancelOrder(takeprofitOrder); } void openLimitTrade(int op){ //NO CHANGE TO CODE }
Any advice is greatly appreciated,
Jess
Comment