Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Close all open positions?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Close all open positions?

    I'm coding a new Forex strategy and am running into trouble closing all open positions after adding position sizes (instead of the DefaultQuantity).

    My call to exit used to be like this:

    Code:
    exitOrder = ExitLong(1, DefaultQuantity, "Exit Long Position", LONG_POSITION);
    However now I am using dynamic sizes and how can I be sure how many fills I really had when my entry may be e.g. 2454567 units. I could hang on that to that value but as I'm not sure that everything got filled I would need a way to check how many positions are actually filled so that I can close them.

    I have noticed that there is an ExitLong message without the quantity and have attempted to use that one. However, it's closing it one by one. Here is an example with 5 positions opened and then closed five times by 1 position size:

    ###### Short Entry at: 1.32165 #########
    Local time: 6/7/2013 3:59:00 PM
    New York: 6/7/2013 3:59:00 PM Eastern Daylight Time
    New York session is active.
    London: 6/7/2013 8:59:00 PM GMT Daylight Time
    London session is inactive.
    Tokyo: 6/8/2013 4:59:00 AM Tokyo Standard Time
    Tokyo session is inactive.
    Instrument: $EURUSD Default
    Close: 1.32165
    Theoretical Entry Price: 1.32165
    *Current Bid*: 1.32165
    Current Ask: 1.32165
    B/A Spread: 0 ticks.
    Short entry order has been submitted: Order='NT-00017/Sim101' Name='Short Position' State=PendingSubmit Instrument='$EURUSD' Action=SellShort Limit price=0 Stop price=0 Quantity=5 Strategy='CrazyIvan' Type=Market Tif=Gtc Oco='' Filled=0 Fill price=0 Token='23f50da514284977baa3cbb50f47e716' Gtd='12/1/2099 12:00:00 AM'
    Resetting triggers...
    ##################################
    Order: SellShort was filled
    EXECUTED at BarNumberOfFill (adjusted): 223
    CurrentBars: 222
    Time on 0 series: 6/7/2013 3:30:00 PM
    Appending order update to message buffer: SIGNALS: FH-S.

    TRADE: SellShort at 1.32165.

    Global buffer in onOrderUpdate(): SIGNALS: FH-S.

    TRADE: SellShort at 1.32165.

    Order has been filled - adding stop.

    6/7/2013 3:59:00 PM Entered internal PlaceOrder() method at 6/7/2013 3:59:00 PM: BarsInProgress=1 Action=BuyToCover OrderType=Stop Quantity=5 LimitPrice=0 StopPrice=1.3224'5 SignalName='Stop loss' FromEntrySignal='Short Position'
    ###### Short Stop: 1.32245 #########
    Local time: 6/7/2013 3:59:00 PM
    New York: 6/7/2013 3:59:00 PM Eastern Daylight Time
    New York session is active.
    London: 6/7/2013 8:59:00 PM GMT Daylight Time
    London session is inactive.
    Tokyo: 6/8/2013 4:59:00 AM Tokyo Standard Time
    Tokyo session is inactive.
    Instrument: $EURUSD Default
    Close: 1.32165
    Average Fill Price: 1.32165
    Theoretical Entry Price: 1.32165
    ISL in full ticks: 8
    Real stop loss in handles: 0.0008
    Setting short stop to 1.32245
    Execution: Execution='NT-00015' Instrument='$EURUSD' Account='Sim101' Name='Short Position' Exchange=Default Price=1.32165 Quantity=5 Market position=Short Commission=0 Order='NT-00017' Time='6/7/2013 3:59:00 PM' Multiplier=1E-05
    Short stop: 1.32245
    Stop order: Order='NT-00018/Sim101' Name='Stop loss' State=PendingSubmit Instrument='$EURUSD' Action=BuyToCover Limit price=0 Stop price=1.32245 Quantity=5 Strategy='CrazyIvan' Type=Stop Tif=Gtc Oco='' Filled=0 Fill price=0 Token='61534281f635486d921dca0b64f463a7' Gtd='12/1/2099 12:00:00 AM'
    Stop order Action: BuyToCover
    Stop order Price: 1.32245
    Current Bid: 1.32165
    *Current Ask*: 1.32165
    B/A Spread: 0 ticks.
    ##################################
    Buffer inside onExecution:

    STOP: BuyToCover at 1.32245.

    RISK: 8 pips.

    Stop has been set
    FRIDAY EOW CLOSING TIME
    Date: Friday, 6/7/2013 3:59:00 PM
    Time: 155900
    New York: 6/7/2013 3:59:00 PM Eastern Daylight Time
    6/7/2013 3:59:00 PM Entered internal PlaceOrder() method at 6/7/2013 3:59:00 PM: BarsInProgress=1 Action=BuyToCover OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Exit Short Position' FromEntrySignal='Short Position'
    Order: BuyToCover was filled
    EXECUTED at BarNumberOfFill (adjusted): 223
    CurrentBars: 222
    Time on 0 series: 6/7/2013 3:30:00 PM
    6/7/2013 3:59:00 PM
    Close: 1.32165
    Current Bid: 1.32165
    Current Ask: 1.32165
    B/A Spread: 0 ticks.
    Appending order update to message buffer: EXIT REMINDER: BuyToCover at 1.32165 and clear all stops.

    Global buffer in onOrderUpdate(): EXIT REMINDER: BuyToCover at 1.32165 and clear all stops.

    FRIDAY EOW CLOSING TIME
    Date: Friday, 6/7/2013 3:59:00 PM
    Time: 155900
    New York: 6/7/2013 3:59:00 PM Eastern Daylight Time
    6/7/2013 3:59:00 PM Entered internal PlaceOrder() method at 6/7/2013 3:59:00 PM: BarsInProgress=1 Action=BuyToCover OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Exit Short Position' FromEntrySignal='Short Position'
    Order: BuyToCover was filled
    EXECUTED at BarNumberOfFill (adjusted): 223
    CurrentBars: 222
    Time on 0 series: 6/7/2013 3:30:00 PM
    6/7/2013 3:59:00 PM
    Close: 1.32165
    Current Bid: 1.32165
    Current Ask: 1.32165
    B/A Spread: 0 ticks.
    Appending order update to message buffer: EXIT REMINDER: BuyToCover at 1.32165 and clear all stops.

    Global buffer in onOrderUpdate(): EXIT REMINDER: BuyToCover at 1.32165 and clear all stops.

    FRIDAY EOW CLOSING TIME
    Date: Friday, 6/7/2013 3:59:00 PM
    Time: 155900
    New York: 6/7/2013 3:59:00 PM Eastern Daylight Time
    6/7/2013 3:59:00 PM Entered internal PlaceOrder() method at 6/7/2013 3:59:00 PM: BarsInProgress=1 Action=BuyToCover OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Exit Short Position' FromEntrySignal='Short Position'
    Order: BuyToCover was filled
    EXECUTED at BarNumberOfFill (adjusted): 223
    CurrentBars: 222
    Time on 0 series: 6/7/2013 3:30:00 PM
    6/7/2013 3:59:00 PM
    Close: 1.32165
    Current Bid: 1.32165
    Current Ask: 1.32165
    B/A Spread: 0 ticks.
    Appending order update to message buffer: EXIT REMINDER: BuyToCover at 1.32165 and clear all stops.

    Global buffer in onOrderUpdate(): EXIT REMINDER: BuyToCover at 1.32165 and clear all stops.

    FRIDAY EOW CLOSING TIME
    Date: Friday, 6/7/2013 3:59:00 PM
    Time: 155900
    New York: 6/7/2013 3:59:00 PM Eastern Daylight Time
    6/7/2013 3:59:00 PM Entered internal PlaceOrder() method at 6/7/2013 3:59:00 PM: BarsInProgress=1 Action=BuyToCover OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Exit Short Position' FromEntrySignal='Short Position'
    Order: BuyToCover was filled
    EXECUTED at BarNumberOfFill (adjusted): 223
    CurrentBars: 222
    Time on 0 series: 6/7/2013 3:30:00 PM
    6/7/2013 3:59:00 PM
    Close: 1.32165
    Current Bid: 1.32165
    Current Ask: 1.32165
    B/A Spread: 0 ticks.
    Appending order update to message buffer: EXIT REMINDER: BuyToCover at 1.32165 and clear all stops.

    Global buffer in onOrderUpdate(): EXIT REMINDER: BuyToCover at 1.32165 and clear all stops.

    FRIDAY EOW CLOSING TIME
    Date: Friday, 6/7/2013 3:59:01 PM
    Time: 155901
    New York: 6/7/2013 3:59:01 PM Eastern Daylight Time
    6/7/2013 3:59:01 PM Entered internal PlaceOrder() method at 6/7/2013 3:59:01 PM: BarsInProgress=1 Action=BuyToCover OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='Exit Short Position' FromEntrySignal='Short Position'
    6/7/2013 3:59:01 PM Cancelled pending exit order, since associated position is closed: Order='NT-00018/Sim101' Name='Stop loss' State=Working Instrument='$EURUSD' Action=BuyToCover Limit price=0 Stop price=1.32245 Quantity=5 Strategy='CrazyIvan' Type=Stop Tif=Gtc Oco='' Filled=0 Fill price=0 Token='61534281f635486d921dca0b64f463a7' Gtd='12/1/2099 12:00:00 AM'
    Order: BuyToCover was filled
    EXECUTED at BarNumberOfFill (adjusted): 223
    CurrentBars: 222
    Time on 0 series: 6/7/2013 3:30:00 PM
    6/7/2013 3:59:01 PM
    Close: 1.32165
    Current Bid: 1.32165
    Current Ask: 1.32165
    B/A Spread: 0 ticks.
    Appending order update to message buffer: EXIT REMINDER: BuyToCover at 1.32165 and clear all stops.

    Global buffer in onOrderUpdate(): EXIT REMINDER: BuyToCover at 1.32165 and clear all stops.
    Obviously something's wrong here - it should just execute one order with a quantity of 5. Any help would be appreciated.
    Last edited by molecool; 07-15-2013, 11:24 AM.

    #2
    Hi Molecool,

    Thank you for posting.

    You can try using Position.Quantity, This will return the number of positions you have in your strategy. You would be able to take this return number and apply it to the number of all open positions you would like to close.

    http://www.ninjatrader.com/support/h...l?quantity.htm

    Let me know if I can be of further assistance.
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      I think I just figured out my problem

      I just figured out what the problem is. I had simply removed the quantity assuming that the signature was similar. However the method I was using previously was this:

      Code:
      ExitLong(int barsInProgressIndex, int quantity, string signalName, string fromEntrySignal)
      And as such an example would be:

      Code:
      int quantity = 5;
      exitOrder = ExitShort(1, quantity, "Exit Short Position", SHORT_POSITION);
      As you can see I'm referencing the index, then the quantity. I need the index as I am using a multi-instrument strategy, it needs to execute against my tick series.

      Now if I simply remove the quantity I get the wrong method signature:

      Code:
      ExitLong(int quantity, string signalName, string fromEntrySignal)
      And as such it reads it as needing to close one position only.

      Code:
      int quantity = 5;
      exitOrder = ExitShort(1, "Exit Short Position", SHORT_POSITION);
      So it all makes sense now but I need a solution.

      Unfortunately you guys have not provided a method that allows me to exit on a second bar series without a quantity. What to do??

      Comment


        #4
        Originally posted by NinjaTrader_Cal View Post
        Hi Molecool,

        Thank you for posting.

        You can try using Position.Quantity, This will return the number of positions you have in your strategy. You would be able to take this return number and apply it to the number of all open positions you would like to close.

        http://www.ninjatrader.com/support/h...l?quantity.htm

        Let me know if I can be of further assistance.
        Thank you for responding but please see below - I just provided more details after figuring out what happened (see below). I have followed your advice and it's apparently now working fine :-)

        Thanks!
        Last edited by molecool; 07-15-2013, 11:40 AM.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        634 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        364 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        105 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        567 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        568 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X