Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Time conversion

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

    Time conversion

    This is the log error

    DB,Error on executing DB command: System.ArgumentException: The conversion could not be completed because the supplied DateTime did not have the Kind property set correctly. For example when the Kind property is DateTimeKind.Local the source time zone must be TimeZoneInfo.Local. Parameter name: sourceTimeZone at System.TimeZoneInfo.ConvertTime(DateTime dateTime TimeZoneInfo sourceTimeZone TimeZoneInfo destinationTimeZone TimeZoneInfoOptions flags CachedData cachedData) at NinjaTrader.Cbi.Order.DbAdd() at NinjaTrader.Cbi.DB.DBThread(),

    In this segment of code- the only component of time, I manage around the margin windows. But i'm getting this error and I'm not sure how it needs to be resolved?

    Code:
    if (Instrument.MasterInstrument.InstrumentType == InstrumentType.Future)
                {
    
                    // Define variables for central time start and end bands in CT (3:45 - 4:00pm) but go a little earlier
    
                    // Define the time zones for Central Time (CT) and Pacific Time (PT)
                    TimeZoneInfo centralTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
                    TimeZoneInfo pacificTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
    
                    // Get the current date and time in Central Time (CT)
                    DateTime currentDateTimeCT = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, centralTimeZone);
    
                    // Calculate the current date and time in Pacific Time (PT)
                    DateTime currentDateTimePT = TimeZoneInfo.ConvertTime(currentDateTimeCT, pacificTimeZone);
    
                    // Combine the current date and time in Pacific Time (PT) with the specified times
                    DateTime centralTimeStartPT = currentDateTimePT.Date.Add(centralTimeStartCT);
                    DateTime centralTimeEndPT = currentDateTimePT.Date.Add(centralTimeEndCT);
                    DateTime today5PMPT = currentDateTimePT.Date.Add(centralTime5PMCT);
    
                    // Check if the current time falls within the trading hours in Pacific Time (PT)
                    if (currentDateTimePT >= centralTimeStartPT && currentDateTimePT <= centralTimeEndPT && Position.MarketPosition == MarketPosition.Flat)
                    {
                        BackBrush = Brushes.DarkSlateBlue;
                        return;
                    }
    
                    // If not flat, exit and end
                    if (currentDateTimePT >= centralTimeStartPT && currentDateTimePT <= centralTimeEndPT && Position.MarketPosition != MarketPosition.Flat)
                    {
                        double initMargin = Account.Get(AccountItem.InitialMargin, Currency.UsDollar);
                        double netLiq = Account.Get(AccountItem.NetLiquidation, Currency.UsDollar);
    
                        if (Position.MarketPosition == MarketPosition.Long)
                        {
                            // ExitLong();
                            DateTime resultDateTime = DateTime.Now.Add(today5PMPT.TimeOfDay);
                            CancelOrders(OrderState.Filled, 0, resultDateTime, false);
                            BackBrush = Brushes.SlateGray;
                            return;
                        }
    
                       else if (Position.MarketPosition == MarketPosition.Short)
                        {
                            // ExitShort();
                            DateTime resultDateTime = DateTime.Now.Add(today5PMPT.TimeOfDay);
                            CancelOrders(OrderState.Filled, 0, resultDateTime, false);
                            BackBrush = Brushes.SlateGray;
                            return;
                        }
                    }
                }​

    #2
    Hello Skifree,

    From the given code I don't see what the problem would be, have you tried to comment out each of the lines to find which specific line is causing the error or if that code is related?

    I do see that you have a method called CancelOrders in addition to using ExitLong and ExitShort. If you are going to submit an exit order but have existing orders that need cancelled you need to do the cancellation first and wait for those orders to be confirmed cancelled in OnOrderUpdate. Once you confirm the orders were cancelled you could submit market exit orders.


    Comment


      #3
      Hi Jesse,

      Yes the cancel order closes out everything including unfilled orders.

      I haven't beenable to debug this because I only see the errors live, not in backtesting.

      How is timezone managed. Is it all based on my timezone /chart setting or is any function tied to specific zones like utc?

      Comment


        #4
        Hello Skifree,

        From the given code I don't see what you would be doing to generate that error. Are you certain this logic is related to the error?

        The error mentions at NinjaTrader.Cbi.Order.DbAdd() which would likely relate to orders. If you are trying to cancel orders at the same time as exiting it may cause that type of error. You need to ensure you are waiting for fills to happen or cancels to happen before doing the next action.


        Comment

        Latest Posts

        Collapse

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