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 Hwop38, 05-04-2026, 07:02 PM
            0 responses
            182 views
            0 likes
            Last Post Hwop38
            by Hwop38
             
            Started by CaptainJack, 04-24-2026, 11:07 PM
            0 responses
            334 views
            0 likes
            Last Post CaptainJack  
            Started by Mindset, 04-21-2026, 06:46 AM
            0 responses
            259 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by M4ndoo, 04-20-2026, 05:21 PM
            0 responses
            358 views
            0 likes
            Last Post M4ndoo
            by M4ndoo
             
            Started by M4ndoo, 04-19-2026, 05:54 PM
            0 responses
            188 views
            0 likes
            Last Post M4ndoo
            by M4ndoo
             
            Working...
            X