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

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 algospoke, Yesterday, 06:40 PM
          2 responses
          23 views
          0 likes
          Last Post algospoke  
          Started by ghoul, Today, 06:02 PM
          3 responses
          15 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Started by jeronymite, 04-12-2024, 04:26 PM
          3 responses
          45 views
          0 likes
          Last Post jeronymite  
          Started by Barry Milan, Yesterday, 10:35 PM
          7 responses
          22 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Started by AttiM, 02-14-2024, 05:20 PM
          10 responses
          181 views
          0 likes
          Last Post jeronymite  
          Working...
          X