Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Swings and OHLC based strategy

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

    Swings and OHLC based strategy

    Hi there, I am trying to create a strategy that will enter based on crossover/touch of couple of standard indicators: Swing and Prior OHLC. The rules are:

    1. long entry on touch of the previous swing high, short entry on touch of previous swing low. I'm not sure what is look back period related in the wizard, are those look back bars? I input it on 100 value.

    2. Enter long or enter short on touch of Prior High or Prior Low or Prior close, depends from what direction the price approaching those lines from under(short) or over(long). I tried to create the first part of the strategy(entries on swings), but it does not seems to work at all.

    // <summary>
    /// Trade swings
    /// </summary>
    [Description("Trade swings")]
    public class SWings : Strategy
    {
    #region Variables
    // Wizard generated variables
    private int high = 1; // Default setting for High
    private int low = 1; // Default setting for Low
    // 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(Swing(5));
    Add(Swing(5));

    CalculateOnBarClose = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // Condition set 1
    if (CrossAbove(GetCurrentBid(), Swing(5).SwingLow, 100)
    && Position.MarketPosition == MarketPosition.Flat)
    {
    EnterShort(DefaultQuantity, "short");
    }

    // Condition set 2
    if (Position.MarketPosition == MarketPosition.Flat
    && CrossBelow(GetCurrentBid(), Swing(5).SwingHigh, 100))
    {
    EnterShort(DefaultQuantity, "short");
    }
    }

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

    [Description("Swing low")]
    [GridCategory("Parameters")]
    public int Low
    {
    get { return low; }
    set { low = Math.Max(1, value); }
    }
    #endregion
    }
    Attached Files

    #2
    Originally posted by meowflying View Post
    Hi there, I am trying to create a strategy that will enter based on crossover/touch of couple of standard indicators: Swing and Prior OHLC. The rules are:

    1. long entry on touch of the previous swing high, short entry on touch of previous swing low. I'm not sure what is look back period related in the wizard, are those look back bars? I input it on 100 value.

    2. Enter long or enter short on touch of Prior High or Prior Low or Prior close, depends from what direction the price approaching those lines from under(short) or over(long). I tried to create the first part of the strategy(entries on swings), but it does not seems to work at all.

    // <summary>
    /// Trade swings
    /// </summary>
    [Description("Trade swings")]
    public class SWings : Strategy
    {
    #region Variables
    // Wizard generated variables
    private int high = 1; // Default setting for High
    private int low = 1; // Default setting for Low
    // 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(Swing(5));
    Add(Swing(5));

    CalculateOnBarClose = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // Condition set 1
    if (CrossAbove(GetCurrentBid(), Swing(5).SwingLow, 100)
    && Position.MarketPosition == MarketPosition.Flat)
    {
    EnterShort(DefaultQuantity, "short");
    }

    // Condition set 2
    if (Position.MarketPosition == MarketPosition.Flat
    && CrossBelow(GetCurrentBid(), Swing(5).SwingHigh, 100))
    {
    EnterShort(DefaultQuantity, "short");
    }
    }

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

    [Description("Swing low")]
    [GridCategory("Parameters")]
    public int Low
    {
    get { return low; }
    set { low = Math.Max(1, value); }
    }
    #endregion
    }
    So what is the issue? You have described what you want to do. For what are you asking for help.

    Comment


      #3
      If you read carefully I wrote: " I tried to create the first part of the strategy(entries on swings), but it does not seems to work at all."

      Comment


        #4
        Originally posted by meowflying View Post
        If you read carefully I wrote: " I tried to create the first part of the strategy(entries on swings), but it does not seems to work at all."
        I see it now. My mistake. So you are not getting any entries? What does: "It does not work ..." mean in the context?

        Are there any errors in your log.

        Comment


          #5
          No errors, the strategy plotting swings as it should, but no entries. I guess the method I chose for entries: bid crossover of swing high or low is wrong, but I really inexperienced, thats why I am asking for help here.

          Comment


            #6
            Originally posted by meowflying View Post
            No errors, the strategy plotting swings as it should, but no entries. I guess the method I chose for entries: bid crossover of swing high or low is wrong, but I really inexperienced, thats why I am asking for help here.
            Looking at your code, it seems that it does not actually reflect what you are intending.

            Code:
            if (CrossAbove(GetCurrentBid(), Swing(5).SwingLow, 100)
                            && Position.MarketPosition == MarketPosition.Flat)
                        {
                            EnterShort(DefaultQuantity, "short");
                        }
            attempts to say when price crosses above the SwingLow to go short, which is at variance with what you describe. That is a matter of the logic.

            However, when I look at the Swing() code, I see that the Plots are being Reset, so you may actually be having invalid values when you try to use it. I would suggest that you manually get the relevant Swing value and examine the cross manually.

            Code:
            int LastSwingHighBarsAgo = Swing(5).SwingHighBar(0, 1, 100); //or however many bars you wish to lookback
            double LastSwingHighValue = High[LastSwingHighBarsAgo];
            if (Close[1] <= LastSwingHighValue 
            && Close[0] > LastSwingHighValue //implies a cross above
            && Position.MarketPosition == MarketPosition.Flat)
            {
                            EnterLong(DefaultQuantity, "long");
                        }
            The code looked straightforward, so I have not tested it. Try using this alone in OBU, and see if you get a trade entered. If not, holler back, and I will run a test on it. Remember, that you are going to need exit conditions for this. Of course if you are coding a "Stop & Reverse" system, then just follow the logic and code the short side entry.

            All the best.
            Last edited by koganam; 08-27-2014, 10:09 AM.

            Comment


              #7
              Hi meowflying,

              Thanks for your post.

              You are currently using variables that are already being used by the NinjaScript class.

              High and Low are both special variables that you cannot use.

              Try changing the High and Low variable names.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Originally posted by NinjaTrader_ChelseaB View Post
                Hi meowflying,

                Thanks for your post.

                You are currently using variables that are already being used by the NinjaScript class.

                High and Low are both special variables that you cannot use.

                Try changing the High and Low variable names.
                I did change it, got one entry just after starting the strategy(not by the rules). But no more...

                Comment


                  #9
                  Originally posted by meowflying View Post
                  I did change it, got one entry just after starting the strategy(not by the rules). But no more...
                  Have you coded an exit for use after entering the trade?

                  Comment


                    #10
                    Originally posted by koganam View Post
                    Have you coded an exit for use after entering the trade?
                    It doesnt matter right now, easy done in wizard, I done it already for another strategy before. I need the entries to get executed first and thats not happening.

                    Comment


                      #11
                      Hello meowflying,

                      This is likely due to the Entries per direction.

                      What do you have set for Entries per direction?

                      http://www.ninjatrader.com/support/h...rdirection.htm

                      http://www.ninjatrader.com/support/h...ryhandling.htm
                      Chelsea B.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                      0 responses
                      666 views
                      0 likes
                      Last Post Geovanny Suaza  
                      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                      0 responses
                      377 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by Mindset, 02-09-2026, 11:44 AM
                      0 responses
                      110 views
                      0 likes
                      Last Post Mindset
                      by Mindset
                       
                      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                      0 responses
                      575 views
                      1 like
                      Last Post Geovanny Suaza  
                      Started by RFrosty, 01-28-2026, 06:49 PM
                      0 responses
                      580 views
                      1 like
                      Last Post RFrosty
                      by RFrosty
                       
                      Working...
                      X