Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Auto close positions

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

    Auto close positions

    How do i set different Auto close position time for different instruments?

    I'm going to Tools-Options-Trading-Auto close position... I need to close my positions overnight or I loose my funding account. I want to close ES, CL, GC etc at 4:15pm ET and corn (ZC), wheat (ZW) at 2:15PM ET. How do i set a different close time for ZC, ZW??

    thank you

    #2
    Hello berya,

    Currently there is not an option to configure multiple auto close times within NinjaTrader.

    Christopher J.NinjaTrader Customer Service

    Comment


      #3
      Ok. thank you.

      Comment


        #4
        I had this same challenge and ultimately resolved it by coding logic that references the instrument specific trading hours sessions templates (Tools>Trading Hours>Properties>Sessions)

        Code:
        // For EOD position closeout and strategy termination utilizing NT instrument trading hours sessions templates
        private SessionIterator sessionIterator; // SessionIterator object required to work with trading hours/sessions data
        private DateTime instrumentCloseoutTime; // DateTime variable for calculated closeout time
        
        protected override void OnStateChange()
        {[INDENT]if (State == State.SetDefaults)[/INDENT][INDENT]{[/INDENT][INDENT=2]secondsPriorToEOD = 900; // Input via strategy UI or hardcode directly[/INDENT][INDENT]}[/INDENT][INDENT]else if (State == State.DataLoaded)
        {[/INDENT][INDENT=2]sessionIterator = new SessionIterator(Bars); // Instantiate sessionIterator once State has reached State.DataLoaded[/INDENT][INDENT]}[/INDENT]
         }
        
        protected override void OnBarUpdate()
        {[INDENT]// Set the instrument-specific current trading session closeout time at the beginning of each new session via use of IsFirstBarOfSession
        // Use of IsFirstBarOfSession minimizes computations from calling GetNextSession() which is computationally intensive
        if (Bars.IsFirstBarOfSession)
        {[/INDENT][INDENT=2]sessionIterator.GetNextSession(Time[0], true); // Calling GetNextSession() is required in order to properly reference ActualTradingDayEndLocal()[/INDENT][INDENT=2]instrumentCloseoutTime = sessionIterator.GetTradingDayEndLocal(sessionIterator.ActualTradingDayExchange).AddSeconds(-secondsPriorToEOD); // Note the (-) sign[/INDENT][INDENT]}
        
        if (BarsInProgress != 0)[/INDENT][INDENT=2]return;[/INDENT][INDENT]
        if (CurrentBar < BarsRequiredToTrade)[/INDENT][INDENT=2]return;[/INDENT][INDENT]
        // Subsequent use of CloseStrategy() will only function on real-time data
        if (State != State.Realtime)[/INDENT][INDENT=2]return;
        [/INDENT][INDENT]// EOD handling: Close all positions and disable strategy n seconds prior to EOD to comply with intraday margin requirements (for example)
        if (Times[0][0].CompareTo(instrumentCloseoutTime) >= 1) // CompareTo() allows the TimeSpan object Times[0][0] to be relationally compared to DateTime object. CompareTo() returns integer: -1, 0, 1 (less than, equal, greater than)[/INDENT][INDENT]{[/INDENT][INDENT=2]// Utilize CloseStrategy() to 'Disable' at the strategy level
        CloseStrategy("StrategyName");
        
        // Include pop-up alert for notification
        Log(Instrument.MasterInstrument.Name + ": EOD CLOSEOUT - Positions flattened, strategy disabled", LogLevel.Alert);[/INDENT][INDENT]}[/INDENT]
          }
        This logic will work for any instrument with the property "Trading Hours" defined (see attached InstrumentProperties.png).

        The logic will return the End Time column DateTime if it is flagged "EOD" (see attached TradingHoursTemplate.png).
        Attached Files

        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