Strategy Not Closing All Positions

Section for discussions related to indicators, use of indicators, and building of trading stategies using indicators.

Moderator: admin

Strategy Not Closing All Positions

Postby imxuf92 » Wed Aug 17, 2016 11:40 am

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


imxuf92
 
Posts: 26
Joined: Sat Jan 23, 2016 6:55 pm

Re: Strategy Not Closing All Positions

Postby imxuf92 » Wed Aug 17, 2016 2:29 pm

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?
imxuf92
 
Posts: 26
Joined: Sat Jan 23, 2016 6:55 pm

Re: Strategy Not Closing All Positions

Postby betwap » Sat Oct 15, 2016 6:26 pm

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.
User avatar
betwap
 
Posts: 7
Joined: Tue Oct 04, 2016 10:24 pm

Re: Strategy Not Closing All Positions

Postby Apprentice » Tue Oct 18, 2016 8:14 am

Try to use the approach from End of Turn_Live Execution Strategy Template.lua
viewtopic.php?f=28&t=2712
User avatar
Apprentice
FXCodeBase: Confirmed User
 
Posts: 36341
Joined: Thu Dec 31, 2009 11:59 am
Location: Zagreb, Croatia


Return to Discussions

Who is online

Users browsing this forum: No registered users and 5 guests