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

Strategy to print highest high, lowest low from intraday periods

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

    Strategy to print highest high, lowest low from intraday periods

    Hi,

    I am trying to write a strategy that will analyze the OHLC data for a contract and then output a file with all the values I’m interested in. Specifically, I want to analyze 3 months of minute data and have the strategy analyzer output the following values:

    Date
    Day of week
    RTH Open, High, Low, Close
    Overnight Open, High, Low, Close
    Time of RTH High and Low
    Time of ON High and Low
    Opening range Open, High, Low, Close

    I want to have one line per day containing all those values, each separated by a semicolon or comma. My plan was to then save the output window as a text file, open it in Excel, and then convert text to columns so I can analyze the data further.

    I have very little experience coding so I’ve spent quite a bit of time reading the forums to try to figure out the best way of doing this. So, here’s my questions:

    I read that using extensive Print() methods can be resource-intensive and may put Ninjatrader into a non-responsive state. I also saw that StreamWriter.IO is recommended for making txt files. If I’m only analyzing 3 months of data at a time (~60 days), is the StreamWriter.IO option still the right way to go, or is 60 days of data pretty manageable?

    It looks like much of the code for making this sort of strategy is already written in the indicator “SampleGetHighLowByTimeRange.cs” posted on this thread. Unfortunately, this is an indicator and I need to get it into a strategy. If I just used the Strategy Wizard to make a generic strategy and then copied the Indicator code into the strategy, would that work? Or is it a lot more complicated?

    Thanks

    Sniff

    #2
    Hello Sniff,

    In this case with the amount of data I believe you could likely go either way, it would really depend on your PC performance. 3 months of data is fairly manageable depending on the logic you will be executing.

    If I had to suggest one, it would likely be writing to file as this gives the benefit of being able to save the data for later use, and to open in excel would likely be easier. If you plan on doing optimizations this can be a problem with file in use errors though, you may need to add a unique prefix to the filename and create more than 1 file per optimization if you go this way.

    Regarding the indicator, that is also flexible on how you want to go about it. The idea of creating a simple strategy to give you a structure is a good idea, I often recommend doing this.You can unlock the code once you have something working that you like and then implement other logic that may be more complex.

    Depending on how the indicator works, it may be easier to call on the indicator from the strategy to retrieve values, or if it has simple logic it may be just as easy to implement the same logic(copy/paste). A lot of indicator syntax is identical and can be used in strategies and the other way around as well. Some items though are specific to indicators like Plotting.

    In this case the indicator from the thread has two plots HighestHigh and LowestLow, if these produce the values you need the strategy should be able to call upon the indicator to do comparisons. This indicator also seems to have simple logic which looks like most of which could be placed in a strategy aside from the Plots.

    If you run into errors or other problems you can always reply back or post new topics for specific issues and we can try to assist where possible.

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Thanks a lot for the detailed response.

      Yeah, it looks like pretty straightforward logic, though I think I found a fatal flaw with it (at least for my purposes). The indicator was written so one could find the highest high and lowest low over a user-inputted time range. It then counts the number of bars that would be in that range and finds the max/min values over that number of bars:

      double highestHigh = MAX(High, startBarsAgo - endBarsAgo + 1)[endBarsAgo];
      double lowestLow = MIN(Low, startBarsAgo - endBarsAgo + 1)[endBarsAgo];

      When I downloaded some of the data I was interested in, I saw that if price didn’t change (or maybe if there wasn’t any trades?) during a particular minute, there just wouldn’t be a bar for that minute. So, if there was 10 missing bars over a certain time-range, the MAX/MIN calculations would actually be based on all the bars in the specified range plus another 10 bars outside of that range.

      I think I have a work-around for that though. I was thinking if I use the following logic, it should calculate the the correct Max/Min no matter how many bars are missing (excuse the syntax below--I’m sure much of it is wrong but hopefully the logic is sound):

      If (Time[0] == rthStartTime)
      {
      rthHigh = High[0]
      rthLow = Low[0]
      rthHighTime = Time[0]
      rthLowTime = Time[0]
      }
      else
      {
      if (Time[0] > rthStartTime && Time[0] < rthEndTime)
      {
      if (High[0] > rthHigh)
      {
      rthHigh = High[0];
      rthHighTime = Time[0]
      }
      if (Low(0) < rthLow)
      {
      rthLow = Low[0]
      rthLowTime = Time[0]
      }
      }
      }

      ....and then I could have a statement that if it's the last minute of the day, print the highest high/lowest low of RTH, the times that they occurred, and a few other things.

      Does this seem like it might work?

      Thanks again,

      Sniff

      Comment


        #4
        Hello Sniff,

        Yes in theory this seems like it could work for your purposes based on your description. The real test would be to implement this and test it over various different periods of data or times known to have the problem you face. Once you do this, the result of the logic could tell you if it is the right logic to use.


        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Hi Jesse,

          Sorry, I've got the ultimate newb question here. I've looked around for an answer and found this and I thought I was doing exactly the same, but apparently not.

          My issue is I'm adding more user-defined variables to the code and adding the Get-Set statements down in the Properties section, but it keeps throwing errors (CS0102). Here's an example of what I have inserted:

          private int rthStartMin = 21; // Default setting for RTHStartMin

          And, in Properties....
          public int rthStartMin
          {
          get { return rthStartMin; }
          set { rthStartMin = Math.Max(0, value); }
          }

          I noticed that the variable name is often capitalized in the following line...
          public int rthStartMin
          ...but it doesn't seem to like it no matter if I capitalize it or not.

          Any ideas?

          Thanks.

          Comment


            #6
            Please disregard last post. Just saved and compiled again and it gave no errors.

            Thanks.

            Comment


              #7
              Hello Sniff,

              This error relates to the naming convention you used, generally in C# public properties are Capitalized where private variables are lowercase.

              The reason for the error is that you are double defining the name rthStartMin:

              Code:
              private int [B]rthStartMin [/B]= 21;
              
              public int [B]rthStartMin[/B]
              {
              get { return rthStartMin; }
              set { rthStartMin = Math.Max(0, value); }
              }

              The purpose of the public property is to both get and set the value of a private property in this case, there are other forms of {get;set} statements that dont require a private variable as well. You have Math.Max on the value though so you would need a private varaible in this case.

              The correct usage would be to capitalize the public variable:

              Code:
              private int [B]rthStartMin [/B]= 21;
              public int [B]RthStartMin[/B]
              {
              get { return rthStartMin; }
              set { rthStartMin = Math.Max(0, value); }
              }
              Now anywhere in code you want to use this value, you would use the public or capital version to take adantage of the Math.Max you have:

              Code:
              RthStartMin = 12; 
              
              if(RthStartMin == 12) { }
              I look forward to being of further assistance.
              JesseNinjaTrader Customer Service

              Comment


                #8
                Wow, okay, that's much clearer. My attempts to get it working by just changing things until it works is sometimes effective but I definitely prefer to understand WHY it works. Your explanation helps a lot. Thanks.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by DJ888, 04-16-2024, 06:09 PM
                6 responses
                18 views
                0 likes
                Last Post DJ888
                by DJ888
                 
                Started by Jon17, Today, 04:33 PM
                0 responses
                1 view
                0 likes
                Last Post Jon17
                by Jon17
                 
                Started by Javierw.ok, Today, 04:12 PM
                0 responses
                6 views
                0 likes
                Last Post Javierw.ok  
                Started by timmbbo, Today, 08:59 AM
                2 responses
                10 views
                0 likes
                Last Post bltdavid  
                Started by alifarahani, Today, 09:40 AM
                6 responses
                41 views
                0 likes
                Last Post alifarahani  
                Working...
                X