Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

displacement for an indicator as a parameter.

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

    displacement for an indicator as a parameter.




    good day to all,




    i have been trying to code a strategy that will compare the value for an ema(14) to the value of the same ema(14) 14 bars ago. i haven't been able to create code so that the displacement can be modified and optimized as necessary, and i would also like to incorporate similar logic for an indicator to change colors depending on this evaluation.







    the most similar thread i could find was that one above, ¿are there are examples of how to accomplish this kind of expression?


    regards.


    #2
    Hello rtwave,

    Thank you for your post.

    When it comes to displacement for just a calculation in your script, you may use the barsAgo in your syntax to return the value from the current bar or the value from 14 bars ago. For example, if you wanted to set a condition that checks if the EMA(14) from the current bar is greater than the EMA(14) from 14 bars ago, that would look like this:
    Code:
    if (EMA(14)[0] > EMA(14)[14])
    {
    // do something
    }


    As for plotting them visually, you could add two plots to your strategy then set the value of the plots to the SMA of the current bar and the SMA from 14 bars ago as described in this post:
    Hello, I am trying to code an SMA/DMA Crossover Strategy and am having trouble figuring out how to plot the displaced SMA. As you can see from the attached


    When it comes to multi-colored plots (you mentioned an indicator that changes colors depending on the evaluation) we have a reference sample that demonstrates how to change plot colors based on if a value is rising or is falling:


    You could use those same ideas to set the plot colors based on certain conditions when you make comparisons between the two EMA values.

    Please let us know if we may be of further assistance.

    Comment


      #3


      NinjaTrader_Emily,



      thanks, i'm interested in creating code where the number of bars to be displaced will not be hard coded but will be an input that can be modified and optimized.



      if (EMA(14)[0] > EMA(14)[14])
      {
      // do something
      }​


      instead of the [14], i'm interested in having a public variable there.


      and something is definitely wrong, the indicators i have created which include a displacement will compile but then won't plot (even if the displacement is hard coded), so there must be some method to code this correctly.

      Comment


        #4
        Hello rtwave,

        Thank you for your reply.

        You could use inputs for the period as well as the displaced number of bars. To use an input value for the period, you could create an int input like SmaPeriod and then for the displace value, you could use an input like DisplaceValue as shown here:

        Code:
                private SMA mySMA, displacedSMA;
                protected override void OnStateChange()
                {
                    if (State == State.SetDefaults)
                    {
                        SmaPeriod                    = 14;
                        DisplaceValue                = 14;
                        AddPlot(Brushes.Orange, "SmaPlot");
                        AddPlot(Brushes.Orange, "DisplacedSmaPlot");
                    }
                    else if (State == State.DataLoaded)
                    {
                        mySMA = SMA(SmaPeriod);
                        displacedSMA = SMA(SmaPeriod);
                    }
                }
        
                protected override void OnBarUpdate()
                {
                    if (CurrentBar <= SmaPeriod + DisplaceValue)
                        return;
                    SmaPlot[0] = mySMA[0];
                    DisplacedSmaPlot[0] = displacedSMA[DisplaceValue];
                }​
        This sample adds the plots into State.SetDefaults and assigns the values to the plots in OnBarUpdate(). You mentioned that your indicators will compile but will not plot; do you receive an error on the Log tab of the Control Center? One common item is having to check that you have enough bars in the series you are accessing, which is why I added a CurrentBar check in the snippet. For more info, please see the tip page here:


        Please let me know if I may be of further assistance.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Yesterday, 05:17 AM
        0 responses
        62 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        134 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