Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Initialize Time variable

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

    Initialize Time variable

    Hello,

    I want to 'initialize', if that's the right word, the private Time variable.

    Like when you set under Public class XXXX : Strategy
    {
    private bool MyBool = false;
    private int bse = 0;
    private double RefLevel = 0;

    And so on.

    I tried using:
    private DateTime MyVar = 0;

    But it gives me an error: Cannot implicitly convert type 'int' to 'System.DateTime'

    What would be the right way to 'initialize' this private variable of DateTime?

    #2
    I tried doing it this way as well:

    private DateTime MyVar = Convert.ToDateTime(0);

    But then it crashed my NinjaTrader with an error:

    "Unable to create instance of NinjaScript 'NinjaTrader.NinjaScript.Strategies.AI_200925'. Most likely either the implementation no longer exists, there is no default constructor defined, the default constructor...."

    And then I was only able to load NinjaTrader back again after deleting bin\Custom\NinjaTrader.Custom.dll

    Comment


      #3
      Hello UltraNIX,

      Thank you for your posts.

      The error message typically you are seeing means that an indicator or strategy that has been saved in a Workspace or Template has been modified or removed from the platform. To resolve this message, you could remove the indicator or strategy from the Workspace or Template followed by saving it.

      To add a DateTime variable in your script you would need to instantiate the variable first by creating a private DateTime variable. For example, private DateTime startTime;. Then in OnBarUpdate(), you could assign the variable a DateTime value. The example below demonstrates how you could assign a DateTime of 09:30 AM to the variable.

      protected override void OnBarUpdate()
      {
      startTime = new DateTime(Time[0].Year,Time[0].Month,Time[0].Day,9,30,0,0);
      }

      Please review the publicly available links below with more information about using DateTime in C#.

      MSDN - https://docs.microsoft.com/en-us/dot...ew=netcore-3.1

      C-Sharp Corner - https://www.c-sharpcorner.com/Upload...using-C-Sharp/

      Let us know if we may further assist.
      Brandon H.NinjaTrader Customer Service

      Comment


        #4
        I get an error, that 'startTime' does not exist in current context.

        Comment


          #5
          Hello UltraNIX,

          Thank you for that information.

          The error message you see could mean that the variable startTime is not being instantiated in your script.

          Are you instantiating the variable 'startTime' before OnStateChange()? (The code for this would look like this: private DateTime startTime

          You may view the attached script which demonstrates working with DateTime.

          Please let us know if we may further assist.
          Attached Files
          Brandon H.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by sdauteuil, Today, 08:23 AM
          1 response
          10 views
          0 likes
          Last Post NinjaTrader_PaulH  
          Started by tonynt, Today, 08:27 AM
          3 responses
          18 views
          0 likes
          Last Post NinjaTrader_Heath  
          Started by mht2020, Today, 07:08 AM
          1 response
          13 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Started by 7robert, Today, 07:07 AM
          4 responses
          38 views
          0 likes
          Last Post NinjaTrader_Zachary  
          Started by ppau2004, Today, 04:38 AM
          1 response
          14 views
          0 likes
          Last Post NinjaTrader_Jason  
          Working...
          X