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

Cannot Implicitly convert type "int" to "NinjaTraderNinjaScriptIndicators.SMA"

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

    Cannot Implicitly convert type "int" to "NinjaTraderNinjaScriptIndicators.SMA"

    If I declare:

    private SMA sMAPeriod;

    and then:

    sMAPeriod = 200;

    and then:

    if (Close[0] > sMAPeriod [0]))// cross up
    {
    EnterLong( getQty(), "LongEntry" );

    why I;m getting that error? I just want to buy if the close of the last bar is above the 200 period SMA.

    Thanks

    #2
    Hello futuros,

    This is because you are trying to assign 200 to an indicator, this is not how you would supply a period to the indicator.

    If you want to make an SMA with a variable for the Period please use the SampleMACrossOver strategy as a reference. The sample strategy shows adding an SMA and checking a crossover. Your code would need to look identical to that file and how the Period is being supplied to the indicator.

    The SMA specifically would have its period set like the following:

    SMA(200)

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

    Comment


      #3
      Hi Jesse,

      so, I dont need this declaration? private SMA sMAPeriod;

      and just:

      if (Close[0] > SMA(200) [0]))
      {
      EnterLong( getQty(), "LongEntry" );
      }

      Thanks

      Comment


        #4
        Hello futuros,

        Correct, that would be valid. I will explain my prior post in more detail.

        The main point to take away from my prior response would be that
        Code:
        sMAPeriod = 200;
        is not valid. you have defined sMAPeriod as type SMA but you tried to assign an INT to it. It was expecting you to use the syntax:

        Code:
        sMAPeriod = SMA(200);
        Then later you would have used:

        Code:
        if (Close[0] > sMAPeriod[0]))
        {
        EnterLong( getQty(), "LongEntry" );
        }
        Both ways are valid, you just have to form the syntax correct if you plan to use a variable.

        if you also want the 200 to be configured when applying the indicator you can see the SampleMACrossOver strategy and how it makes use of the indicators/property. That uses variables like you were originally and could be copied if you wanted to do that.

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

        Comment


          #5
          Thanks a lot Jesse, now my strategy compiles perfectly but now I got this error when I go to Strategies to insert it into my chart: Object reference not set to an instance of an object, how can I fix this error?

          Comment


            #6
            Hello futuros,

            The error means something that you are using is null, if you are using the indicator as a variable that would happen if you missed assigning the indicator to the variable:
            sMAPeriod = SMA(200);

            Can you provide a sample of your updated code?


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

            Comment


              #7
              Here you go:

              namespace NinjaTrader.NinjaScript.Strategies
              {
              public class MACDStrategy : MartingaleBase
              {
              public enum StopType { TrailingStop, StandardStop, PeakStop }

              private Series<double> upperBand;
              private Series<double> lowerBand;

              private MACD macd;
              private StdDev sd;
              private SMA sMAPeriod;


              Order stopOrder = null;
              Order profitOrder = null;

              protected override void OnStateChange()
              {
              base.OnStateChange();

              if (State == State.SetDefaults)
              {
              Description = @"Strategy for MACD";
              Name = "MACDStrategy";
              Calculate = Calculate.OnBarClose;
              EntriesPerDirection = 1;
              EntryHandling = EntryHandling.AllEntries;
              IsExitOnSessionCloseStrategy = true;
              ExitOnSessionCloseSeconds = 30;
              IsFillLimitOnTouch = false;
              MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
              OrderFillResolution = OrderFillResolution.Standard;
              Slippage = 0;
              StartBehavior = StartBehavior.WaitUntilFlat;
              TimeInForce = TimeInForce.Gtc;
              TraceOrders = false;
              RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
              StopTargetHandling = StopTargetHandling.PerEntryExecution;
              BarsRequiredToTrade = 20;
              // Disable this property for performance gains in Strategy Analyzer optimizations
              // See the Help Guide for additional information
              IsInstantiatedOnEachOptimizationIteration = true;

              ProfitTargetTicks = 15;
              StopLossTicks = 10;
              UseStopType = StopType.StandardStop;
              PeakStopOffset = 1;
              MacdFastPeriod = 12;
              MacdSlowPeriod = 26;
              MacdBandPeriod = 10;
              MacdBandStdDev = 1.0;
              sMAPeriod = SMA(200);

              BeginTime = 93000;
              EndTime = 163000;

              Comment


                #8
                Hi Jesse, I've posted the code two hours ago but it shows like pending, did you receive it?

                Comment


                  #9
                  Hello futuros,

                  Yes some posts require approval, it may take some time for it to show up but it was received.

                  It looks like some of the post was cut off, can you either attach the .cs file from the folder Documents\NinjaTrader 8\bin\Custom\Strategies or re edit your post to contain the whole code?

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

                  Comment


                    #10
                    Hi Jesse, I really don't want to share publicly the whole .cs file, can I send it directly to you by email?

                    Comment


                      #11
                      Hello futuros,

                      If you don't want to share the whole file that would be a good situation to make a reduced sample that you can share. As we are talking about only a small part of the script I would not want to see your proprietary logic and only what we are talking about. The sample you provided excluded what we are talking about so it was not specifically helpful.
                      You can alternatively send in a file to our platform support at ninjatrader.com address but if it is a large complex file we may still ask for a reduced sample.


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

                      Comment


                        #12
                        Hi Jesse, here are the snippets you're asking, I hope you can give me a clue how to fix this error, thanks

                        private MACD macd;
                        private StdDev sd;
                        private SMA sMAPeriod;
                        sMAPeriod = SMA(200);
                        {
                        if((macd[0] > upperBand[0]) && (macd[1] <= upperBand[1])
                        && (Close[0] > sMAPeriod[0]))// cross up
                        {
                        EnterLong( getQty(), "LongEntry" );
                        }
                        else if((macd[0] < lowerBand[0]) && (macd[1] >= lowerBand[1])
                        && (Close[0] < sMAPeriod[0]))// cross down
                        {
                        EnterShort( getQty(), "ShortEntry" );
                        }
                        }

                        Comment


                          #13
                          Hello futuros,

                          Unfortunately this is out of context from the rest of the script, I cant really tell if you made the code right from this. The error may be coming from any of the variables you have defined I cannot tell if you have inserted this code in the right place. You are missing the indicator instantiation for the StdDev and macd in what you provided, was that left out or is that missing in general?

                          If you can make a sample of the code you are having trouble with and attach it as a whole script so I can see the whole picture that would be helpful.

                          Alternatively this is an error which is very common in C# and is not specific to NinjaScript. You can debug this code yourself to locate the error by finding out what is null. You can insert prints between lines of code to find where the execution stops or also print the object in a comparison with null. Here is a public resource which goes into some depth about the error you are seeing: https://stackoverflow.com/questions/...ow-do-i-fix-it

                          In what you provided I would likely suggest checking the smaPeriod variable if that is null and also your other variables you have shown like macd or lowerBand.

                          Code:
                          Print("is sMAPeriod  null? " + (sMAPeriod == null));
                          The same type of print could be used for any object to test if it has a value or not, you should see true if this object is null meaning it cannot be used.


                          I look forward to being of further assistance.



                          JesseNinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by alifarahani, Today, 09:40 AM
                          4 responses
                          20 views
                          0 likes
                          Last Post alifarahani  
                          Started by gentlebenthebear, Today, 01:30 AM
                          3 responses
                          16 views
                          0 likes
                          Last Post NinjaTrader_Jesse  
                          Started by PhillT, Today, 02:16 PM
                          2 responses
                          7 views
                          0 likes
                          Last Post PhillT
                          by PhillT
                           
                          Started by Kaledus, Today, 01:29 PM
                          3 responses
                          11 views
                          0 likes
                          Last Post NinjaTrader_Jesse  
                          Started by frankthearm, Yesterday, 09:08 AM
                          14 responses
                          47 views
                          0 likes
                          Last Post NinjaTrader_Clayton  
                          Working...
                          X