Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Swing Trading Strategy

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

    Swing Trading Strategy

    Hey there,

    I want to create a strategy based on the Swing Highs and Lows, but I only want it to respect those swing highs/lows if they are older than 100 bars on the 1 minute chart.

    I came up with this code so far but getting stuck as its not giving the results I was hoping for.

    Code:
                // Go Short
                if ((CrossAbove(High, Swing1.SwingHigh, 5))
                     && (VOL1[0] > 150)
                     && (High[1] > High[0])
                     && ((CurrentBar - Swing1.SwingHighBar(1,1,100)) > 100))
                {
                     BackBrushAll = Brushes.CornflowerBlue;
                     EnterShort(Convert.ToInt32(DefaultQuantity), "");
                }​
    Thanks in advance!

    #2
    Hello Lones,

    I believe the problem is that you are trying to subtract a BarsAgo from the CurrentBar which is an index. You are also using only 100 bars of a lookback, you may need to use more BarsAgo lookback as you move ahead of the swing in time.

    If you wanted to find the Index of the bar for the swing you would need to do:

    Code:
    int index = CurrentBar - Swing1.SwingHighBar(1,1,100);
    You can then use that index to know if its been 100 bars since the CurrentBar:

    Code:
    int totalBars = CurrentBar - index;
    A simple example would be if the CurrentBar is 250 and the last swing was 100 bars ago:

    250 - 100 = index 150
    250- 150 = total bars 100

    Code:
    int index = CurrentBar - Swing1.SwingHighBar(1,1,100);
    int totalBars = CurrentBar - index;​
    if ((CrossAbove(High, Swing1.SwingHigh, 5))
    && (VOL1[0] > 150)
    && (High[1] > High[0])
    && (totalBars > 100))
    {
    BackBrushAll = Brushes.CornflowerBlue;
    EnterShort(Convert.ToInt32(DefaultQuantity), "");
    }​​

    Comment


      #3
      Hello Jesse,

      thank you very much for that quick response!
      Indeed works now, but does it only consider the last recent swing high?

      Comment


        #4
        Hello Lones,

        The method to get the swing uses a lookback period to find the swings, the Instance you provided is which swing you wanted to look at. You used the first instance so its going to only see the most recent swing while looking backwards.

        (1,1,100)
        This is saying: start looking for swings starting at 1 bars ago, look for the first instance of a swing. Look for 100 bars.

        Comment


          #5
          Hello Jesse,

          thank you for the explanation!

          How could I say that I only want 1 trade per broken swing level? Right now it enters multiple times once a swing high is broken

          Comment


            #6
            Hello Lones,

            The easiest way to do that would be to add a bool variable. When your entry condition becomes true set the bool to true. As part of your entry condition check that the bool is false. That would make it so when the entry condition becomes true it disables the entry condition.

            that would look similar to:

            Code:
            private bool stopEntries; 
            
            protected override void OnBarUpdate()
            {
                 if(someConditionToResetVariables)
                 {
                     stopEntries = false;
                 }
            
                 if ((CrossAbove(High, Swing1.SwingHigh, 5))
                 && (VOL1[0] > 150)
                 && (High[1] > High[0])
                 && (totalBars > 100)
                 && stopEntries == false)
                 {
                      BackBrushAll = Brushes.CornflowerBlue;
                      EnterShort(Convert.ToInt32(DefaultQuantity), "");
                      stopEntries = true;
                 }​​ 
            
            }

            Comment


              #7
              Thank you once again!
              I have another question, should private List<Box> long_boxes = new List<Box>(); work when I have the "using NinjaTrader.Gui.Tools;" ?

              Comment


                #8
                Hello Lones,

                I am not sure I understand the question without more context.

                NinjaTrader.Gui.Tools is a namespace that contains most of the user interface controls. Box is not a type inside of NinjaTrader.Gui.Tools so the answer without context is no, Box does not need NinjaTrader.Gui.Tools but you likely need to leave that using statement in your script for other types that may be used.


                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by NullPointStrategies, Today, 05:17 AM
                0 responses
                39 views
                0 likes
                Last Post NullPointStrategies  
                Started by argusthome, 03-08-2026, 10:06 AM
                0 responses
                124 views
                0 likes
                Last Post argusthome  
                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                0 responses
                64 views
                0 likes
                Last Post NabilKhattabi  
                Started by Deep42, 03-06-2026, 12:28 AM
                0 responses
                41 views
                0 likes
                Last Post Deep42
                by Deep42
                 
                Started by TheRealMorford, 03-05-2026, 06:15 PM
                0 responses
                46 views
                0 likes
                Last Post TheRealMorford  
                Working...
                X