Page 1 of 1

Strategy Not Closing All Positions

PostPosted: Wed Aug 17, 2016 11:40 am
by imxuf92
Hello all!

I have a strategy that enters and exists based on certain candle signals.

To close the position I'm using NetQtyFlag = "Y" parameter in the valuemap because in certain cases I may have multiple position open on the same side (see code below).

For some reason, the close request doesn't always work. The trade gets submitted and the async response is received with a "success" message, but the order's status reads as "C" (cancelled). This only happens SOMETIMES. Why would a "CM" order be cancelled? This is VERY frustrating since there is no reason as to what happened.

Any ideas?

(Additionally, it seems since Trading Station software update 01.15, many things have started acting inconsistently the backtester...)

Code: Select all

function FXCMTrader:submitTradeCloseRequest()

   local valuemap = core.valuemap();
   valuemap.Command = "CreateOrder";
   valuemap.OrderType = "CM";
   valuemap.OfferID = self.offerId;
   valuemap.AcctID = self.account;
    valuemap.TradeID = self.tradeInfo[1].tradeId;

   if (self.direction == 1) then
      valuemap.BuySell = "S";
   else
      valuemap.BuySell = "B";
   end
   valuemap.NetQtyFlag = "Y";  -- <-- Close all open trades for this instrument.
   
   
   local success, msg = terminal:execute(500, valuemap);
   if not (success) then
        trace("      Trade Close Submission Failure; Msg: " .. msg);
   else
        trace("      Trade Close Request Submitted; Msg: " .. msg);
   end

end



Re: Strategy Not Closing All Positions

PostPosted: Wed Aug 17, 2016 2:29 pm
by imxuf92
So after review, it appears that our strategy was trying to close a large position (4 standards) during non-volatile session hours, but lack of liquidity prevented the order from filling.

Has anyone identified a decent rule-set to handle such a scenario?

Re: Strategy Not Closing All Positions

PostPosted: Sat Oct 15, 2016 6:26 pm
by betwap
imxuf92 wrote:So after review, it appears that our strategy was trying to close a large position (4 standards) during non-volatile session hours, but lack of liquidity prevented the order from filling.

Has anyone identified a decent rule-set to handle such a scenario?


It's hard to give advice like that without knowing your entire environment. If you can count on your strategy to run until the order is resolved then subscribe to trade events. Read up at:

http://fxcodebase.com/wiki/index.php/Event_driven_strategy_programming_Trading_Events_Introduction

The most robust solution would be both to subscribe to trade events AND to plan for a reset of your strategy. Scan the orders/trades tables when your strategy loads and use a flag (like the async isbusy flag) at the top of your Update. Use the same flag in your trades update. Please share what you come up with.

Re: Strategy Not Closing All Positions

PostPosted: Tue Oct 18, 2016 8:14 am
by Apprentice
Try to use the approach from End of Turn_Live Execution Strategy Template.lua
viewtopic.php?f=28&t=2712