Announcement

Collapse
No announcement yet.

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.
          BrettNinjaTrader Product Management

          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 Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            647 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            368 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            108 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            571 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            573 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X