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:	120
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 CarlTrading, 03-31-2026, 09:41 PM
        1 response
        79 views
        1 like
        Last Post NinjaTrader_ChelseaB  
        Started by CarlTrading, 04-01-2026, 02:41 AM
        0 responses
        40 views
        0 likes
        Last Post CarlTrading  
        Started by CaptainJack, 03-31-2026, 11:44 PM
        0 responses
        63 views
        2 likes
        Last Post CaptainJack  
        Started by CarlTrading, 03-30-2026, 11:51 AM
        0 responses
        63 views
        0 likes
        Last Post CarlTrading  
        Started by CarlTrading, 03-30-2026, 11:48 AM
        0 responses
        54 views
        0 likes
        Last Post CarlTrading  
        Working...
        X