Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Buy and Sell on every trade problem

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

    Buy and Sell on every trade problem

    Hello! I'm new to scripting and im trying to learn, Ive been through the videos and Im trying to build a MA crossover strategy. The problem is though that the strategy opens a buy and a sell for EVERY bar instead of when the MA's cross. I have been through other sample codes but cant find where I went wrong?

    Any help would be much appreciated!

    Heres an image to show what I mean





    And here is my code:


    #region Using declarations


    /// <summary>
    /// 5 sma crosses the 12 sma. Simple Bet both ways
    /// </summary>
    [Description("5 sma crosses the 12 sma. Simple Bet both ways")]
    public class SMA5overSMA12 : Strategy
    {
    #region Variables
    // Wizard generated variables
    private int stopLoss = 20; // Default setting for StopLoss
    private int betSize = 1; // Default setting for BetSize
    private int dema = 25; // Default setting for Dema
    private int ema = 60;// Default setting for Ema
    // 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()
    {
    CalculateOnBarClose = true;
    Add(DEMA(Dema));
    Add(EMA(Ema));
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    if(CrossAbove(DEMA(Dema),EMA(Ema),1));
    {
    EnterLong(BetSize);
    Print (Time[0] + ("Cross Above"));



    }
    if(CrossBelow(DEMA(Dema), EMA(Ema),1));
    {
    EnterShort(BetSize);
    Print(Time[0] + ("Cross Below"));
    }

    }


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

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

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

    [Description("")]
    [GridCategory("Parameters")]
    public int Ema
    {
    get { return ema; }
    set { ema = Math.Max(1, value); }
    }
    [Description("")]
    [GridCategory("Parameters")]
    public int Smooth
    {
    get { return smooth; }
    set { smooth = Math.Max(1, value); }
    }
    #endregion
    }
    }

    #2
    Hello,
    Please test this with using an "else if" statemtent instead of an "if" statement for the CrossBelow condition.
    Cody B.NinjaTrader Customer Service

    Comment


      #3
      Also EntriesPerDirection appears to be missing.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      633 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
      567 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