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

A Little Help For My Indicator

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

    A Little Help For My Indicator

    Hello,

    I have been working on a custom indicator for the smart money index. I am not a coder and am actually quite amazed that I have even made it this far with ninjascript. Anyway, I think I am at just about finished with the final step summing up the values for a running total. But that is where my brain is just fried and I would like a little help finishing the last step of a running total. I know this is probably easy for you coders, but a little help would be appreciated.

    All are welcome to it (if its worth anything to you). Also, if any of you coders want to take it and make it better, feel free. Just let me know so I can get a copy too I was hoping to add a little smoothing to it at some point, but I have to get it to work first.

    Thanks,

    Regards,

    Lee
    Attached Files

    #2
    Hello Lee,

    Is this last part the issue you are running into?
    Code:
    //double runningValue = (dailyValue[0]+dailyValue[-1]);
    //Print("The Running SMI Value is: " + runningValue);
    			
    // Set the plot values
    SMI.Set(dailyValue);
    If so, it would be because you are trying to access a negative index. You would want to use positive values here.
    Cal H.NinjaTrader Customer Service

    Comment


      #3
      Hi Cal,

      Thanks for the response. Yes that is the part. I've been working on it some more since yesterday, and I don't think that segment of the code will work for what I'm looking for. I'm trying to get a value to be set that is the close of the first day data to act as the initial value. Then the SMI running value would start from there and only sum for the end of the day. Not sure how to do this, so I will keep the trial and error approach going

      Comment


        #4
        HI,

        So I made a few changes, but can't seem to get the running value to sum correctly. It works for the first two days, so I'm not sure what I should do. Maybe my first approach was correct in trying to add an index to the dailyValue ?? Of course when I try to do that, it tells me that I can't add an index to this. How do I do that? And do you think this is the best approach in trying to get a running sum for the dailyvalue?

        Thanks,

        Lee
        Attached Files

        Comment


          #5
          Hello Lee,

          I am taking a look at your script and wanted to see if I could get a little bit of clarification on where in the script you are having trouble.

          I wanted to clarify that this is the correct calculation for the indicator.

          You are getting the range of open and close price in the morning, Get open bar at 9:30am, Get close bar at 9:59am
          Then
          Getting the range of open and close price in the evening, Get open bar 3:00pm, Get close bar 3:59pm

          Next

          Get the amount of change between the open and the close price for the morning range

          Then

          Get the amount of change between the close and the open price for the evening range

          Next

          Take the negative difference from the morning and add it to the difference from the evening
          Code:
          double dailyValue = (-openDailyChange+lastDailyChange);
          Finally take the output from above and add them to the close to plot the line

          The one portion of the code I see is having an issue is the dailyValue. It currently prints a 0, if I remove the negative sign before openDailyChange I start getting values.
          Code:
          double dailyValue = (-openDailyChange+lastDailyChange); // before
          double dailyValue = (openDailyChange+lastDailyChange); // after
          I also removed the
          Code:
          if(CurrentBar == 0)
          so I could see the line being plotted.

          That does affect the output of the SMI that is drawn when I change this.

          Please let me know if this is correct.
          JesseNinjaTrader Customer Service

          Comment


            #6
            Hi Jesse,

            Thanks for looking at this. I am not able to look at this right now, but I will try to explain as best as possible and verify tonight.

            For the most part, what you stated is correct. I get the morning delta and the afternoon delta and I add them to get the dailyValue. I am trying to follow the SMI formula [
            Today's SMI reading = yesterday's SMI – opening gain or loss + last hour change]. With that said, I was able to correctly calculate the dailyValue using the (-openDailyChange+lastDailyChange). I could remove the negative sign I would just have to reverse the delta for the morning session using intraday data. Are you using intraday or daily data? I have only been able to get it to work using intraday data...

            To throw a hitch in it, I was trying to use the first days close as the baseline starting value and then sum only the dailyValue from this point. The dailyValue is a single value for the day. I think this is why I was trying the if(CurrentBar == 0).

            For now, I would like to just be able to sum the dailyValues and follow this trend line. This is where I am having issues trying to sum only a single dailyValue through intraday data. I beleive I had the single value correct, just could not get it to sum the single dailyValue from day to day.

            Thanks again for all your help.

            Regards,

            Lee

            Comment


              #7
              Hello Lee,

              Thanks for clarifying that for me.

              It looks like the issue was in the math with the following statement.
              Code:
              double dailyValue = (-openDailyChange + lastDailyChange);
              The way you are trying to make the openDailyChange a negative is making it return 0.
              I have changed it to
              Code:
              double dailyValue = (openDailyChange - lastDailyChange);
              and have started receiving correct values. If you need it to be positive values then you would need to use Math.Abs()

              Also I removed the
              Code:
              if(CurrentBar == 0)
              statement so the SMI can be plotted on the chart.

              I did open it on both daily and intraday charts and it plots and returns values on both.

              I have attached the modified script.

              Please let me know if I may be of further assistance.
              Attached Files
              JesseNinjaTrader Customer Service

              Comment


                #8
                Hi Jesse,

                Thank you again for looking at this. I reviewed what you provided for the changes, but unfortunately its still does not seem to be calculating correctly. Here are a few comments I have.

                1). The change you made to the dailyValue [(openDailyChange - lastDailyChange);] in making it positive did not change the computation on my end other than to reverse the sign for the dailyValue. I actually want it be as it was originally scripted, and when I went back to the original [double dailyValue = (-openDailyChange + lastDailyChange);] it still worked fine, just had the opposite sign.

                2) From my end, the dailyValue is being calculated correctly, but the way the current script is written [ runningValue1 = (dailyValue+Close[0]); & runningValue2 = (dailyValue+runningValue1);], the running sum is for each bar of intraday data. But the SMI formula is only calculated for a single day. So I want to sum only one (1) value (dailyValue) for each day, not continuously sum for all the bars provided in the intraday range. Here is an example:

                Ex) dailyValue for 4/21 = -0.35 (one value only, regardless of intraday bars)
                dailyValue for 4/22 = -0.77 Running sum @ 4/22 = -.35 + (-.77) = -1.12
                dailyValue for 4/23 = 0.989 Running sum @ 4/23 = -1.12 + .989 = -0.131

                If I could get the indicator to do this, I would be very happy But ultimately I would also like to have the first day's calculation start with the close from the first day's data...

                Ex) dailyValue + Close for 4/21 = -0.35 + 187.04 = 186.69
                dailyValue for 4/22 = -0.77 Running sum @ 4/22 = 186.69 + (-.77) = 185.92
                dailyValue for 4/23 = 0.989 Running sum @ 4/23 = 185.92 + .989 = 186.91

                I believe the dailyValue is being calculated correctly as I am getting only one value regardless of the number of bars in the intraday range. But its the sum of these dailyValues from day to day that is the issue. I don't know how to program it to ignore all the intraday data and only use the one value for each day. Also, I tried the indicator with daily charts using only the free daily feeds from Yahoo and Google, and it did not work. This makes sense to me considering the formula requires intraday data to calculate the open and close delta. For me to get the indicator to work, I have to import historical intraday data. So I don't know if maybe the feed you are using provides different results than what I'm doing

                I hope this makes sense. Its frustrating to be so close but yet not be able to get it to work.

                Regards,

                Lee

                Comment


                  #9
                  Originally posted by lee612801 View Post
                  Hi Jesse,

                  Thank you again for looking at this. I reviewed what you provided for the changes, but unfortunately its still does not seem to be calculating correctly. Here are a few comments I have.

                  1). The change you made to the dailyValue [(openDailyChange - lastDailyChange);] in making it positive did not change the computation on my end other than to reverse the sign for the dailyValue. I actually want it be as it was originally scripted, and when I went back to the original [double dailyValue = (-openDailyChange + lastDailyChange);] it still worked fine, just had the opposite sign.

                  2) From my end, the dailyValue is being calculated correctly, but the way the current script is written [ runningValue1 = (dailyValue+Close[0]); & runningValue2 = (dailyValue+runningValue1);], the running sum is for each bar of intraday data. But the SMI formula is only calculated for a single day. So I want to sum only one (1) value (dailyValue) for each day, not continuously sum for all the bars provided in the intraday range. Here is an example:

                  Ex) dailyValue for 4/21 = -0.35 (one value only, regardless of intraday bars)
                  dailyValue for 4/22 = -0.77 Running sum @ 4/22 = -.35 + (-.77) = -1.12
                  dailyValue for 4/23 = 0.989 Running sum @ 4/23 = -1.12 + .989 = -0.131

                  If I could get the indicator to do this, I would be very happy But ultimately I would also like to have the first day's calculation start with the close from the first day's data...

                  Ex) dailyValue + Close for 4/21 = -0.35 + 187.04 = 186.69
                  dailyValue for 4/22 = -0.77 Running sum @ 4/22 = 186.69 + (-.77) = 185.92
                  dailyValue for 4/23 = 0.989 Running sum @ 4/23 = 185.92 + .989 = 186.91

                  I believe the dailyValue is being calculated correctly as I am getting only one value regardless of the number of bars in the intraday range. But its the sum of these dailyValues from day to day that is the issue. I don't know how to program it to ignore all the intraday data and only use the one value for each day. Also, I tried the indicator with daily charts using only the free daily feeds from Yahoo and Google, and it did not work. This makes sense to me considering the formula requires intraday data to calculate the open and close delta. For me to get the indicator to work, I have to import historical intraday data. So I don't know if maybe the feed you are using provides different results than what I'm doing

                  I hope this makes sense. Its frustrating to be so close but yet not be able to get it to work.

                  Regards,

                  Lee
                  So add it once, at the beginning of the next day?

                  Code:
                  if (Time[1].Date != Time[0].Date)
                  {
                      // It's a new day. Let us do stuff that we want to do only once here.
                      // If we are using calculate on bar close false, we will have to use FirstTickOfBar to ensure a once only calculation.
                  }
                  Last edited by koganam; 04-30-2014, 07:34 PM.

                  Comment


                    #10
                    Hi koganam,

                    Thank you for responding to my post. I tried your suggestion of adding "if (Time[1].Date != Time[0].Date)" to the code, but now I don't get anything but an "error on calling OnBarUpdate". Maybe I added it incorrectly, but I was hoping you can tell me where I am going wrong?

                    Thanks,

                    Lee
                    Attached Files

                    Comment


                      #11
                      Originally posted by lee612801 View Post
                      Hi koganam,

                      Thank you for responding to my post. I tried your suggestion of adding "if (Time[1].Date != Time[0].Date)" to the code, but now I don't get anything but an "error on calling OnBarUpdate". Maybe I added it incorrectly, but I was hoping you can tell me where I am going wrong?

                      Thanks,

                      Lee
                      What is the stuff that you want to do only once in any day period? Encase that, and only that, in the time filter block.

                      Comment


                        #12
                        Hi koganam,

                        Unfortunately, I am still learning ninjascript at this time and have minimal experience with it, which is why I am on this forum asking these questions from the experts. Maybe an example would be easier for me to understand. I don't know if you looked at the indicator I attached, but this is what I did:

                        protected override void OnBarUpdate()
                        {

                        if (Time[1].Date != Time[0].Date)

                        { all the stuff I want to do only once in any day period......
                        .................................................. ...............................
                        .................................................. ...............................
                        }
                        }

                        I'm not sure what I'm doing wrong, but I am not familiar with this function. When I do this, I get an error on calling OnBarUpdate...


                        Lee

                        Comment


                          #13
                          Originally posted by lee612801 View Post
                          Hi koganam,

                          Unfortunately, I am still learning ninjascript at this time and have minimal experience with it, which is why I am on this forum asking these questions from the experts. Maybe an example would be easier for me to understand. I don't know if you looked at the indicator I attached, but this is what I did:

                          protected override void OnBarUpdate()
                          {

                          if (Time[1].Date != Time[0].Date)

                          { all the stuff I want to do only once in any day period......
                          .................................................. ...............................
                          .................................................. ...............................
                          }
                          }

                          I'm not sure what I'm doing wrong, but I am not familiar with this function. When I do this, I get an error on calling OnBarUpdate...


                          Lee
                          We would be better able to help if you told us what the error is rather than just: "I get an error on calling OnBarUpdate."

                          However, looking at your code and scoping it, it seems to me that what you are asking is how to perform your summation once a day. The rest of the code, seeing as how it is calculating using intraday data, across time spans, will require processing more than once a day.

                          Here is a skeleton based on what you have written.

                          Code:
                          if (CurrentBar < 1) return;
                          if Time[1].Date != Time[0].Date
                          {
                          // calculate your running value here
                          }
                          //the rest of your stuff goes here
                          Unfortunately, even though your code is well commented, I do not understand what you are trying to do. You have a running value that you are not storing, and which has no recursion, and some values that you are calculating, but that is really all that I can see. Without an understanding of why you are summing some values, and the overall intent of the code, there is little more than I can say.

                          Comment


                            #14
                            Hi koganam,

                            Thanks again for the suggestion and I will try this asap! I will try give you an idea of the intent of this indicator. I want to calculate a single daily value regardless of the number of bars of intraday data based on the SMI formula (Today's SMI reading = yesterday's SMI – opening gain or loss + last hour change]). When I look at the output (for the version I had before I added the if (Time[1].Date != Time[0].Date)), it does show the same dailyValue for all the intraday bars. So I believe I am getting this correctly, but maybe I just need to store it as you suggested to be used for summing? Here is a numerical example of what I want:

                            Ex) dailyValue for 4/21 = -0.35 (one value only OR same value, regardless of intraday bars)
                            dailyValue for 4/22 = -0.77 Running sum @ 4/22 = -.35 + (-.77) = -1.12
                            dailyValue for 4/23 = 0.989 Running sum @ 4/23 = -1.12 + .989 = -0.131

                            If I could get the indicator to do this, I would be very happy But ultimately I would also like to have the first day's calculation start with the close from the first day's data...

                            Ex) dailyValue + Close for 4/21 = -0.35 + 187.04 = 186.69
                            dailyValue for 4/22 = -0.77 Running sum @ 4/22 = 186.69 + (-.77) = 185.92
                            dailyValue for 4/23 = 0.989 Running sum @ 4/23 = 185.92 + .989 = 186.91

                            Then I want to graph this running sum. Also, here is the exact error I get when using the if (Time[1].Date != Time[0].Date) :

                            (Error on calling 'OnBarUpdate' method for Indicator 'SmartMoneyIndicator' on bar 0. Bar Index needs to be greater/equal to 0.)

                            I hope this makes a little more sense. Keep in mind, what I am trying to do, and what I am able to code, are obviously not always the same Based on this, do you think your previous suggestion is still the correct approach for me to try?

                            Thanks again

                            Lee

                            Comment


                              #15
                              Hi koganam,

                              I added what you suggested and looks like it is working well for only doing what I want (calculating the dailyValue) only once a day! Now the most important question is how to get it to sum these values... When I check the output, it shows the dailyValue changing, but I would like a running sum of the dailyValue as follows:

                              Ex) dailyValue for 4/21 = -0.35
                              dailyValue for 4/22 = -0.77 Running sum @ 4/22 = -.35 + (-.77) = -1.12
                              dailyValue for 4/23 = 0.989 Running sum @ 4/23 = -1.12 + .989 = -0.131

                              So, do I need to store this dailyValue from each iteration? Should I use a Value.Set?? How would you suggest I do this and should it go outside the block of what you suggested before?

                              Example:
                              protected override void OnBarUpdate()
                              {

                              if (CurrentBar < 1) return;
                              if Time[1].Date != Time[0].Date

                              { dailyValue Calculation here(only once a day)......
                              .................................................. ...............................
                              .................................................. ...............................
                              }

                              Running Sum goes here???

                              }



                              Also, the only other issue with the new script is now it wont seem to plot any values when calculating only once a day...??? Not sure what this issue is, but I would like to take it one problem at a time and work the running sum.

                              Thank you again for all your help,

                              Lee

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by judysamnt7, 03-13-2023, 09:11 AM
                              4 responses
                              59 views
                              0 likes
                              Last Post DynamicTest  
                              Started by ScottWalsh, Today, 06:52 PM
                              4 responses
                              36 views
                              0 likes
                              Last Post ScottWalsh  
                              Started by olisav57, Today, 07:39 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post olisav57  
                              Started by trilliantrader, Today, 03:01 PM
                              2 responses
                              22 views
                              0 likes
                              Last Post helpwanted  
                              Started by cre8able, Today, 07:24 PM
                              0 responses
                              10 views
                              0 likes
                              Last Post cre8able  
                              Working...
                              X