• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Closing all positions from within an addon

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

    Closing all positions from within an addon

    Hello, I am using the AddOn framework sample to develop my own addon.


    I want it to close all positions, regarless of how they were opened, at 3:55 (=9:55 PM in my timezone).

    I have this code in a timer event handler that ticks every second:

    if (accountSelector.SelectedAccount != null)
    {
    DateTime thisDateTime = new DateTime(2017, 05, 25) + new TimeSpan(21, 30, 00); //TODO: Remove hardoding
    if ((DateTime.Now > (thisDateTime)) && (accountSelector.SelectedAccount.Name == "MYACCOUNTNUMBER"))
    {
    //if the end of trading is approaching (5 minutes), flatten everything
    foreach (Position thisPosition in accountSelector.SelectedAccount.Positions)
    {
    if (thisPosition.MarketPosition != MarketPosition.Flat)
    {
    // do something.
    thisPosition.Close();
    }
    }
    }
    }


    It does trigger and closes the position, but it then triggers many more times, I am guessing that it is triggers until the position closes, but then, as the new positions that are to close the one we're trying to close are opening, it triggers over and over...

    There must be a smarter and smoother way than that... Please show me, I am eager to slap my forhead and call myself dumb.

    Thank you,

    Libor

    #2
    Hello,

    Thank you for the post.

    Have you tried the Flatten syntax?

    http://ninjatrader.com/support/helpG...ghtsub=flatten

    This would be the suggested way to flatten the account for an instrument or instruments. Using a timer, you would likely want to ensure this only gets called once at the time you want to flatten, and then ensure the positions were closed afterward using the events of the account.

    Using a timer and the existing approach, you would likely need to wait for a specified time and then make a duplicate of the items in the collection as a class level variable. You could then use the timer to close the orders that you put into the variable as this would not be updated with new changes related to closing the positions. You would also likely need to use logic to ensure that the positions are closed and assign your variables to null much like you would in a strategy. You would also need to use logic from preventing the timer logic from happening more than once and then follow up with the account events to make sure there was no failure.

    You can find an example that shows subscribing to the account events here: http://ninjatrader.com/support/helpG...ghtsub=account


    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Thank you Jesse for the quick and complete answer. It all makes sense. Forehead slapped, and I have called myself dumb, too, as announced.

      Best,

      LIbor

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Irukandji, Today, 09:43 AM
      0 responses
      1 view
      0 likes
      Last Post Irukandji  
      Started by TazoTodua, Today, 09:06 AM
      0 responses
      2 views
      0 likes
      Last Post TazoTodua  
      Started by sambo81, Yesterday, 06:57 PM
      1 response
      19 views
      0 likes
      Last Post NinjaTrader_ThomasC  
      Started by tgn55, Today, 08:34 AM
      0 responses
      4 views
      0 likes
      Last Post tgn55
      by tgn55
       
      Started by TazoTodua, Today, 06:00 AM
      0 responses
      9 views
      0 likes
      Last Post TazoTodua  
      Working...
      X