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 CarlTrading, 03-31-2026, 09:41 PM
            1 response
            157 views
            1 like
            Last Post NinjaTrader_ChelseaB  
            Started by CarlTrading, 04-01-2026, 02:41 AM
            0 responses
            91 views
            1 like
            Last Post CarlTrading  
            Started by CaptainJack, 03-31-2026, 11:44 PM
            0 responses
            143 views
            2 likes
            Last Post CaptainJack  
            Started by CarlTrading, 03-30-2026, 11:51 AM
            0 responses
            131 views
            1 like
            Last Post CarlTrading  
            Started by CarlTrading, 03-30-2026, 11:48 AM
            0 responses
            107 views
            0 likes
            Last Post CarlTrading  
            Working...
            X