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

Code to detect cross above of Stochastics.K/Stochastics.D, MACD.Value/MACD.Avg?

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

    Code to detect cross above of Stochastics.K/Stochastics.D, MACD.Value/MACD.Avg?

    Hi,

    I use following code to test CrossAbove of Stochastics.K/Stochastics.D and MACD.Value/MACD.Avg, but it does not work because the condition logic always produces FALSE.


    private MACD mACD;
    private Stochastics stochastics;


    mACD = MACD(12, 26, 9);
    stochastics = Stochastics(3, 14, 7);


    if (CrossAbove(mACD.Value, mACD.Avg, 1))
    {
    ......
    }
    if (CrossAbove(stochastics.K, stochastics.D, 1))
    {
    ......
    }

    Would you please tell me the correct code to do this?

    Thanks,
    weiwei

    #2
    weiwei, I am currently not able to get this working either. I'll continue to look into this and let you know what I find.

    EDIT: As a quick side note, I believe you have mixed up the smoothing period and the D period values, but it doesn't yet function correctly after switching the numbers back.
    AustinNinjaTrader Customer Service

    Comment


      #3
      weiwei, some of the code needs to be moved to OnStartUp():
      Code:
      // in variables
      private Stochastics stoch_ind;
      
      protected override void OnStartUp()
      {
      stoch_ind = Stochastics(7, 14, 3);
      macd_ind = MACD(...)
      }
      protected override void OnBarUpdate()
      {
         if (CrossAbove(stoch_ind.K, stoch_ind.D, 1))
            // crossabove, do something
      }
      AustinNinjaTrader Customer Service

      Comment


        #4
        Hi Austin,

        2 more questions.

        1.Should I always assign indicators to variables within OnStartUp()?
        2.If I use same strategy to trade multiple stocks by adding them in Initialize(), then in OnBarUpdate(), will each BarsInProgress has its own value of these variables?

        Thanks,
        weiwei

        Comment


          #5
          Hello,

          1) No need to do this all the time in OnStartUp()

          2) OnBarUpdate would have the correct variable and data for the BarsInProgress in use, you are correct here.

          Comment


            #6
            Thanks! I moved indicator assignment code back to OnBarUpdate(), because it does not work for multiple stock trading if putting this code within OnStartUp().

            weiwei

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by rhyminkevin, Today, 04:58 PM
            3 responses
            47 views
            0 likes
            Last Post Anfedport  
            Started by iceman2018, Today, 05:07 PM
            0 responses
            5 views
            0 likes
            Last Post iceman2018  
            Started by lightsun47, Today, 03:51 PM
            0 responses
            7 views
            0 likes
            Last Post lightsun47  
            Started by 00nevest, Today, 02:27 PM
            1 response
            14 views
            0 likes
            Last Post 00nevest  
            Started by futtrader, 04-21-2024, 01:50 AM
            4 responses
            50 views
            0 likes
            Last Post futtrader  
            Working...
            X