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

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.


    JesseNinjaTrader Customer Service

    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.


        JesseNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by JoMoon2024, Today, 06:56 AM
        0 responses
        6 views
        0 likes
        Last Post JoMoon2024  
        Started by Haiasi, 04-25-2024, 06:53 PM
        2 responses
        17 views
        0 likes
        Last Post Massinisa  
        Started by Creamers, Today, 05:32 AM
        0 responses
        5 views
        0 likes
        Last Post Creamers  
        Started by Segwin, 05-07-2018, 02:15 PM
        12 responses
        1,786 views
        0 likes
        Last Post Leafcutter  
        Started by poplagelu, Today, 05:00 AM
        0 responses
        3 views
        0 likes
        Last Post poplagelu  
        Working...
        X