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

Using a class to represent 2 columns

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

    Using a class to represent 2 columns

    Hi guys,

    I'm not sure if I'm approaching this correctly or not and thought it was worth a shot at asking this question here.

    My goal is to reference a list or array to pull back timestamps and another string. Both stored as strings currently.

    When googling around it seems that the best practice for multiple columns is not using an array but creating a class that will represent a row with two columns. This is where I'm getting that from.

    Assuming this is correct (it may not be for Ninja I have no clue) I have this code in the namespace NinjaTrader.NinjaScript.Strategies before the strategy class:

    Code:
    public class tradeTimeStampsClass
    {
         public string tradeTimeStamp { get; set; }
         public string tradeDirection { get; set; }
    }
    Then in OnBarUpdate() I have this:

    Code:
    List<tradeTimeStampsClass> _trades = new List<tradeTimeStampsClass>();
    
    _trades.Add(new tradeTimeStampsClass {tradeTimeStamp = "1/28/2022 9:01:17", tradeDirection = "S"});
    So now I would be under the impression that I have a list called _trades, which has two columns: tradeTimeStamp and tradeDirection.

    What I'm trying to do is get this new two-column list to play nice with the loop that I had that compared the timestamp in the list to the current time like this:

    Code:
    //loop through the timestamp list
    foreach(string stamp in _trades)
    {
         //parse date
         if (DateTime.TryParse(stamp, out timeStampParsed))
              Print("Parsed successfully: " + timeStampParsed);
         else
              Print("Unable to understand DateTime input.");
         //compare timestamp to time window. if we're in the time window okToTrade = true;
         if(Time[0] <= timeStampParsed.AddSeconds(10) && Time[0] >= timeStampParsed.AddSeconds(-10))
         {
              Print(Time[0]);
              Print("WE SHOULD BE GETTING INTO A TRADE HERE");
              okToTrade= true;
         }
    }
    The last part gives this error: Cannot convert type 'NinjaTrader.NinjaScript.Strategies.tradeTimeStamp sClass' to 'string'.

    I'm not sure if this is out of the scope of the type of question we can ask on here but I felt like it was worth a shot because I'm spinning my wheels here.

    #2
    Hello WalterSkinner,

    That would certainly be something you could do.
    I'm not sure how many of these values you need. If only a few values are needed to be saved, a set of variables might be less work.

    However, below is a link to an example that has a custom class saved in a dictionary you might find helpful.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      I've only got about 50 or so in the dataset I'm working with now but that number could grow to be around a thousand.

      What I've done instead of a list or dictionary is I just made an array. So much easier so far, and at least I'm getting the correct output I believe.

      Here's what I've done so far in case anyone comes across this and is curious:

      Code:
      string[,] arrayOfTimeStampsAndDirection = new string[,]
      {
           {"1/28/2022 9:01:17", "S"},
           //i cut off a bunch of rows for brevity
      };
      Then:
      Code:
      for(int i = 0; i < arrayOfTimeStampsAndDirection.Length/2; i++)
      {
           //Print(arrayOfTimeStampsAndDirection[i,0]);
           //parse
           if (DateTime.TryParse(arrayOfTimeStampsAndDirection[i,0], out timeStampParsed))
                Print("Parsed successfully:\t" + timeStampParsed);
           else
                Print("Unable to understand DateTime input.");
           //check if now is the trade and if it is do something
                if(Time[0] <= timeStampParsed.AddSeconds(10) && Time[0] >= timeStampParsed.AddSeconds(-10))
           {
           Print(Time[0]);
           Print("WE SHOULD BE GETTING INTO A TRADE HERE");
           okToTrade= true;
      }
      }

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Skifree, Today, 03:41 AM
      1 response
      2 views
      0 likes
      Last Post Skifree
      by Skifree
       
      Started by usazencort, Today, 01:16 AM
      0 responses
      1 view
      0 likes
      Last Post usazencort  
      Started by kaywai, 09-01-2023, 08:44 PM
      5 responses
      603 views
      0 likes
      Last Post NinjaTrader_Jason  
      Started by xiinteractive, 04-09-2024, 08:08 AM
      6 responses
      23 views
      0 likes
      Last Post xiinteractive  
      Started by Pattontje, Yesterday, 02:10 PM
      2 responses
      23 views
      0 likes
      Last Post Pattontje  
      Working...
      X