Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Multi TF MACD crossover

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

    Multi TF MACD crossover

    Hi, I'm having a slight problem coding a strategy for a MACD crossover in 2 different timeframes. I used the wizard and help pages and I'm almost there,but....
    I wanted to take a Long position when the MACD fast line crossed the slow line in the Primary timeframe but only when the 15 min MACD fast line is also above the slow line..ie the Primary dataseries (eg 5min) and 15 are both up and the 5 has just crossed.
    I have enclosed my coding below.BarsArray[1] refers to the 15min dataseries.The program checker won't let me save the strategy as it sees errors at the end of this line:

    "if (MACD(BarsArray[1],12, 26, 9)) > (MACD(BarsArray[1],12,26,9).Avg, 1))"

    Any help would be appreciated,
    Thank you,
    John
    --------------------------
    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
    /// <summary>
    /// long enrty & exit ONLY
    /// </summary>
    [Description("long enrty & exit ONLY")]
    public class JHMACDXoverLONG : Strategy
    {
    #region Variables
    // Wizard generated variables
    private int myInput0 = 1; // Default setting for MyInput0
    // User defined variables (add any user defined variables below)
    #endregion

    /// <summary>
    /// This method is used to configure the strategy and is called once before any strategy method is called.
    /// </summary>
    protected override void Initialize()
    {
    Add(PeriodType.Minute, 15);
    CalculateOnBarClose = false;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    if (MACD(BarsArray[1],12, 26, 9)) > (MACD(BarsArray[1],12,26,9).Avg, 1))

    // Condition set 1
    if (CrossAbove(MACD(12, 26, 9), MACD(12, 26, 9).Avg, 1))
    {
    EnterLong(DefaultQuantity, "");
    }

    // Condition set 2
    if (CrossBelow(MACD(12, 26, 9), MACD(12, 26, 9).Avg, 1))
    {
    ExitLong("", "");
    }
    }

    #region Properties
    [Description("")]
    [Category("Parameters")]
    public int MyInput0
    {
    get { return myInput0; }
    set { myInput0 = Math.Max(1, value); }
    }
    #endregion
    }
    }

    #2
    Not sure what you wanted to code but this code

    if (MACD(BarsArray[1],12, 26, 9)[0] > MACD(BarsArray[1],12,26,9).Avg[0])

    ... compiles fine. You may need to tweak it according your requirements.

    Comment


      #3
      Thanks Dierk, the strategy compiles now. The results are a bit weird so I need to check out what 've done!
      John

      Comment


        #4
        My guess is that you might need to check BarsInProgress to make sure it's 1 before doing your comparisons of the MACD calculations of BarsArray[1].

        I suggest that you look at the following two threads:





        Good luck.

        Comment


          #5
          Thanks for the post KBJ. I've read the post links you gave and will look at my strategy again this weekend,
          Thanks,
          John

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by RubenCazorla, Today, 09:07 AM
          2 responses
          11 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by i019945nj, 12-14-2023, 06:41 AM
          7 responses
          81 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by timmbbo, 07-05-2023, 10:21 PM
          4 responses
          158 views
          0 likes
          Last Post NinjaTrader_Gaby  
          Started by tkaboris, Today, 08:01 AM
          1 response
          7 views
          0 likes
          Last Post NinjaTrader_Gaby  
          Started by Lumbeezl, 01-11-2022, 06:50 PM
          31 responses
          818 views
          1 like
          Last Post NinjaTrader_Adrian  
          Working...
          X