Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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?



    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 funk10101, Today, 12:02 AM
      1 response
      10 views
      0 likes
      Last Post NinjaTrader_LuisH  
      Started by GLFX005, Today, 03:23 AM
      1 response
      6 views
      0 likes
      Last Post NinjaTrader_Erick  
      Started by nandhumca, Yesterday, 03:41 PM
      1 response
      12 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by The_Sec, Yesterday, 03:37 PM
      1 response
      11 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by vecnopus, Today, 06:15 AM
      0 responses
      1 view
      0 likes
      Last Post vecnopus  
      Working...
      X