Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Use Swing Indicator onBarClose within a Strategy with OnPriceChange

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

    Use Swing Indicator onBarClose within a Strategy with OnPriceChange

    Hi,
    I have a strategy that is using OnPriceChange() to place the Exit Orders based on some information from a Swing Indicator that needs to calculate OnBarClose(). Is there an easy way to modify the Swing Indicator so that it runs all its calculations on Bar Close? Or I need to modify the indicator so that every element that uses [0] now uses [1]? I tried to modify some of the values but it does not plot correctly anymore.

    High, Close and Low values are correct when I change this section but not the plotting.
    double high0 = !(Input is PriceSeries || Input is Bars) ? Input[0] : High[1];
    double low0 = !(Input is PriceSeries || Input is Bars) ? Input[0] : Low[1];
    double close0 = !(Input is PriceSeries || Input is Bars) ? Input[0] : Close[1];

    Thanks in advance,​

    #2
    Hi RSundays, thanks for posting. The Swing indicator can run OnEachTick or OnPriceChanged. If you want it to run OnBarClose exclusively, you would need to place all the calculation logic within the first tick e.g.

    if (IsFirstTickOfBar)
    {
    //calculate Swing here.
    }

    All hosted scripts will inherit the Calculate property of their host, so if your strategy runs OnPriceChange then the Swing will also run OnPriceChange. You can make a copy of the swing indicator by opening the source code>Right click>Save As. This will let you give it a new name and make a modifiable copy of Swing.

    Kind regards,
    -ChrisL

    Comment


      #3
      Thanks NinjaTrader_ChrisL for your quick response. Do I need to add it online in OnBarUpdate() or all other functions part of the Swing Indicator? And, do I not need to shift back 1 position all the Highs, Close and Lows to get the correct info? I have added the IsFirstTickOfBar in OnBarUpdate() and when I reload the script, everything that is on the chart already is good but the new swings are not being plotted correctly.

      Click image for larger version

Name:	image.png
Views:	117
Size:	6.8 KB
ID:	1218991
      Thanks,​

      Comment


        #4
        Hi RSundays, thanks for your reply. You will need to edit/compile/test and also use Prints in your code to test out different ideas. Any code that is in OnBarUpdate that you would want to run OnBarClose will need to go within the if (IsFirstTickOfBar) block condition. To see if you need to reference one bar back or 0 bars back, that would also take testing on your end using Print etc.

        Kind regards,
        -ChrisL

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Yesterday, 05:17 AM
        0 responses
        65 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        139 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        75 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        45 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        50 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X