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

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 PaulMohn, Today, 12:36 PM
      1 response
      14 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by yertle, Yesterday, 08:38 AM
      8 responses
      36 views
      0 likes
      Last Post ryjoga
      by ryjoga
       
      Started by rdtdale, Today, 01:02 PM
      1 response
      6 views
      0 likes
      Last Post NinjaTrader_LuisH  
      Started by alifarahani, Today, 09:40 AM
      3 responses
      17 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by RookieTrader, Today, 09:37 AM
      4 responses
      20 views
      0 likes
      Last Post RookieTrader  
      Working...
      X