Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

STOCHASTICS and MACD strategy problem

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

    STOCHASTICS and MACD strategy problem

    I'm trying to build a strategy with the following idea:

    When Stochastics (K) crosses above (D) (just the time of the cross), then a boolean variable is set as true. Then if there is a cross of MACD avobe its Average in less bars than 5 we open a long trade.

    The thing is that my code doesn't work fine and only take into account if both crosses happen at the same time and not with a difference of 5 bars.

    The code is the following:

    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Indicator;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Strategy;
    #endregion

    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
    /// <summary>
    /// Versión 5 del giro de las 1600
    /// </summary>
    [Description("Versión 5 del giro de las 1600")]
    public class GP16v5 : Strategy
    {
    #region Variables
    // Wizard generated variables
    private int barras = 5; // Default setting for Barras
    private int stop = 6; // Default setting for Stop
    private int profit = 4; // Default setting for Profit
    private int trades = 2; // Default setting for Trades
    private int tradesAnteriores = 0; // Default setting for TradesAnteriores
    // 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()
    {
    SetStopLoss("", CalculationMode.Ticks, Stop, false);
    SetProfitTarget("", CalculationMode.Ticks, Profit);

    CalculateOnBarClose = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {

    bool control=false;
    bool control2=false;
    int cont=100;

    if (CurrentBar <20) return;

    if(Stochastics(3,13,3).K[0]>Stochastics(3,13,3).D[0] && Stochastics(3,13,3).K[1]<=Stochastics(3,13,3).D[1])
    {
    control=true;
    cont=0;
    }
    if(MACD(4,10,4)[0]>MACD(4,10,4).Avg[0] && MACD(4,10,4)[1]<=MACD(4,10,4).Avg[1])
    {
    control2=true;
    }

    if(control==true && control2==true && cont<5)
    {
    EnterLong();
    control=false;
    control2=false;
    }
    cont++;

    }

    #region Properties
    [Description("")]
    [GridCategory("Parameters")]
    public int Barras
    {
    get { return barras; }
    set { barras = Math.Max(1, value); }
    }

    [Description("")]
    [GridCategory("Parameters")]
    public int Stop
    {
    get { return stop; }
    set { stop = Math.Max(1, value); }
    }

    [Description("")]
    [GridCategory("Parameters")]
    public int Profit
    {
    get { return profit; }
    set { profit = Math.Max(1, value); }
    }

    [Description("")]
    [GridCategory("Parameters")]
    public int Trades
    {
    get { return trades; }
    set { trades = Math.Max(1, value); }
    }

    [Description("")]
    [GridCategory("Parameters")]
    public int TradesAnteriores
    {
    get { return tradesAnteriores; }
    set { tradesAnteriores = Math.Max(0, value); }
    }
    #endregion
    }
    }

    #2
    Hello Sangui,

    One issue is that your bool flag and cont are assigned values in OnBarUpdate() with no other conditionals attached. This means it gets set as false and the counter reset with every bar. Declare these and assign an initial value in the Variables region, not OnBarUpdate().
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      You need to assign a counter in the block where control is first true, and then check the counter when control2 becomes true.

      Remember to reset the counter once you get your trade signal.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      630 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      364 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      105 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      566 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      568 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X