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

Convert int to series double

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

    Convert int to series double

    Hi All,

    I am trying to use an int when trying to call for an EMA (Exponential Moving Average), however, I am getting two errors:
    - "The best overloaded method match for EMA...has overloaded arguments. (CS1502)
    - Argument1: cannot convert from int to double series (CS1503)

    private int Value;
    private EMA EMAL;
    .
    .
    .
    protected override void OnBarUpdate()
    {
    .
    .
    .
    EMAL = EMA(Value, Length);
    .
    .
    .
    }

    I have "Value" as an INT because I am adding +1, -1 etc...so is a calculate number that I am calculating thru my script.

    Let me know if anyone can help with this compiling error. Or provide any direction.



    #2
    Hello felixolmo,

    Thank you for your inquiry.

    The EMA would need to have a Series<double> as the first parameter, and you're not giving it a series of values, you're giving it a single calculated value. You can't calculate an EMA of a single calculated value - the average would just be that value.

    You could calculate your value and assign that value to a series on each bar and use that for the input for the EMA:
    private Series<double> myValues;

    Code:
    // in OnStateChange
    else if (State == State.DataLoaded)
    {
    myValues = new Series<double>(this, MaximumBarsLookBack.TwoHundredFiftySix);
    }
    
    protected override void OnBarUpdate()
    {
    // calculate Value
    .myValues[0] = Value;
    .
    EMAL = EMA(myValues, Length);
    
    }
    Please let us know if we may be of further assistance to you.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Hi Kate - Thanks for the suggestion. I believe I got it to work finally.

      I do have an additional question. thru that solution that you provided, I have the indicator working on the ES and the GC. However, when I load the indicator in the CL chart, for some reason is not working. is the same exact indicator.

      When I open the NinjaScript output. I see that is starts to run, gets into the "FOR", but it never comes out of the FOR.

      I am getting this error while inside the FOR: "Error on calling 'OnBarUpdate' method on bar 2: Object reference not set to an instance of an object."

      Again the same indicator is working on the ES and the GC, but not the CL. I never seen that happen before, that a working indicator doesn't work on only one chart.

      Comment


        #4
        Hello felixolmo,

        Thank you for your reply.

        Could you supply a code sample showing where it gets to on the CL but then seems to throw an error? If you can supply the indicator as an export, that would be even more helpful.

        Thanks in advance; I look forward to assisting you further.
        Kate W.NinjaTrader Customer Service

        Comment


          #5
          See attach.

          I believe it might have to do the how long the "FOR" goes. I was testing in Strategy analyzer on the ES 610 ticks and when I go past 14 (Length of the FOR), I get the same error.
          Attached Files

          Comment


            #6
            On the CL I was using 222 Ticks and GC I was on 122 Ticks...don't know if that matters.

            Comment


              #7
              Hello felixolmo,

              Thank you for your reply.

              You're hitting the error because you're not ensuring you have enough bars loaded to look back MOMLength number of bars.

              Replace this:

              if (CurrentBars[0] < 2)
              return;

              With this:

              if (CurrentBars[0] < MOMLength)
              return;

              And you shouldn't run into the error.

              Please let us know if we may be of further assistance to you.
              Kate W.NinjaTrader Customer Service

              Comment


                #8
                This is great Kate. Tested and works perfectly!

                Thank you so much. I think I have always had it with 2, and this is the only time I have got that error.

                Comment


                  #9
                  Originally posted by felixolmo View Post
                  See attach.

                  I believe it might have to do the how long the "FOR" goes. I was testing in Strategy analyzer on the ES 610 ticks and when I go past 14 (Length of the FOR), I get the same error.
                  Did you ever fix your mommentum script? Would you mind sharing?

                  Comment


                    #10
                    Originally posted by NinjaTrader_Kate View Post
                    Hello felixolmo,

                    Thank you for your reply.

                    You're hitting the error because you're not ensuring you have enough bars loaded to look back MOMLength number of bars.

                    Replace this:

                    if (CurrentBars[0] < 2)
                    return;

                    With this:

                    if (CurrentBars[0] < MOMLength)
                    return;

                    And you shouldn't run into the error.

                    Please let us know if we may be of further assistance to you.
                    Hi Kate,

                    Tried this script. The problem is when momlength at a very high number around 500 then the strategy waits for those bars to load. Any other options?
                    Last edited by rocketstock; 02-04-2022, 02:59 AM.

                    Comment


                      #11
                      Hello rocketstock,

                      Thank you for your reply.

                      If the strategy has a very large number of bars to look back, like if you specify the length to be 500 for MOMLength, the strategy will need to have at least that many bars loaded on the chart in order to calculate the indicator and there would not be a workaround for this - you simply cannot make calculations unless you have the necessary data.

                      Please let us know if we may be of further assistance to you.
                      Kate W.NinjaTrader Customer Service

                      Comment


                        #12
                        Thanks Kate. So in other words if system is restarted the strategy would wait for that much time to place a trade. It would not be able to use historical data to calculate MOM length if NT was restarted?
                        Last edited by rocketstock; 02-04-2022, 03:42 PM.

                        Comment


                          #13
                          Hello rocketstock,

                          Thank you for your reply.

                          This particular indicator will calculate on historical data, but you would need to have enough historical data loaded for it to be able to refer back that far. So you would need to make sure that the chart has a minimum of the same number of bars as the value for MOMLength in order for the indicator to plot. If you don't, it will wait until there are at least that many historical and formed-in-realtime bars to see the indicator plot. Same thing with strategies, if a strategy has to look back 500 bars to make calculations, it won't be able to until at least that many bars are loaded on the chart. Keep in mind you can adjust the number of days or bars to load in the Data Series window of the chart.

                          Please let us know if we may be of further assistance to you.
                          Kate W.NinjaTrader Customer Service

                          Comment


                            #14
                            Understood. Thats a great idea. Example I have 1000 period 3 min chart. the dataseries is currently 5 days default. So I imagine this should work? Reason I ask you differentiated between bars on chart for indicator and strategy. seems like you were implying both need to be handled differently. chart indicator data and strategy. Ot just setting data series to 5 days would take care of both indicator and strategy. It would load 5 days on restart

                            Comment


                              #15
                              Hello rocketstock,

                              Thank you for your reply.

                              The amount of data necessary to load on a chart would vary depending on the strategy or indicator in question and how far they need to look back at bars to function. My prior response is specific to the indicator in question in this thread as far as indicators go, and generalized for strategies.

                              Please let us know if we may be of further assistance to you.
                              Kate W.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by trilliantrader, 04-18-2024, 08:16 AM
                              5 responses
                              22 views
                              0 likes
                              Last Post trilliantrader  
                              Started by Davidtowleii, Today, 12:15 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post Davidtowleii  
                              Started by guillembm, Yesterday, 11:25 AM
                              2 responses
                              9 views
                              0 likes
                              Last Post guillembm  
                              Started by junkone, 04-21-2024, 07:17 AM
                              9 responses
                              68 views
                              0 likes
                              Last Post jeronymite  
                              Started by mgco4you, Yesterday, 09:46 PM
                              1 response
                              12 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Working...
                              X