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 ageeholdings, Today, 07:43 AM
            0 responses
            3 views
            0 likes
            Last Post ageeholdings  
            Started by pibrew, Today, 06:37 AM
            0 responses
            4 views
            0 likes
            Last Post pibrew
            by pibrew
             
            Started by rbeckmann05, Yesterday, 06:48 PM
            1 response
            14 views
            0 likes
            Last Post bltdavid  
            Started by llanqui, Today, 03:53 AM
            0 responses
            6 views
            0 likes
            Last Post llanqui
            by llanqui
             
            Started by burtoninlondon, Today, 12:38 AM
            0 responses
            12 views
            0 likes
            Last Post burtoninlondon  
            Working...
            X