Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Script is closing position as soon as it opens it

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

    Script is closing position as soon as it opens it

    I'm a newbie here - I'm trying to write a script to look back N bars, and if they are all red, then go long, or if they are all green, go short. I also set a trailing stop of 1.5 points. It compiles okay, but it appears that the trades are closed immediately. i.e. Go long at 4500, then closed at 4500.

    This is what I have.

    Code:
    #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>
        /// After n bars up, go short.  After n bars down, go long.  Testing whether 5-6 red bars is a good time for a swing trade in the opposite direction.
        /// </summary>
        [Description("After n bars up, go short.  After n bars down, go long.  Testing whether 5-6 red bars is a good time for a swing trade in the opposite direction.")]
        public class nBarsOpposite : Strategy
        {
            #region Variables
            // Wizard generated variables
            private int nbars = 5; // Default setting for Nbars
            // 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()
            {
                SetTrailStop("", CalculationMode.Ticks, 1.5, true);
    
                CalculateOnBarClose = true;
            }
    
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
                // Condition set 1
                if (NBarsDown(3, true, true, true)[0] == 1)
                {
                    EnterLong(DefaultQuantity, "");
                }
    
                // Condition set 2
                if (NBarsUp(3, true, true, true)[0] == 1)
                {
                    EnterShort(DefaultQuantity, "");
                }
            }
    		
    
            #region Properties
            [Description("How many bars to look back")]
            [GridCategory("Parameters")]
            public int Nbars
            {
                get { return nbars; }
                set { nbars = Math.Max(1, value); }
            }
            #endregion
        }
    }

    #2
    Hello onemorelot,

    Thanks for your post and welcome to the forums!

    In review of the line: SetTrailStop("", CalculationMode.Ticks, 1.5, true); you are setting the mode to be ticks and telling it to trail by 1.5 ticks.
    Please see: http://ninjatrader.com/support/helpG...ttrailstop.htm

    I don't know what instrument you are using but if it were the ES then 1 point = 4 ticks so a 1.5 point stop would need to be specified as 6 ticks.
    Paul H.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by bortz, 11-06-2023, 08:04 AM
    47 responses
    1,607 views
    0 likes
    Last Post aligator  
    Started by jaybedreamin, Today, 05:56 PM
    0 responses
    9 views
    0 likes
    Last Post jaybedreamin  
    Started by DJ888, 04-16-2024, 06:09 PM
    6 responses
    19 views
    0 likes
    Last Post DJ888
    by DJ888
     
    Started by Jon17, Today, 04:33 PM
    0 responses
    6 views
    0 likes
    Last Post Jon17
    by Jon17
     
    Started by Javierw.ok, Today, 04:12 PM
    0 responses
    15 views
    0 likes
    Last Post Javierw.ok  
    Working...
    X