Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Possible to use a Variable as an Indicator input?

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

    Possible to use a Variable as an Indicator input?

    Hi NT,

    Is it possible to use a Variable as an Indicator input? How would I do this if so?

    For example, I tried using the code below to set Variable1 as the input for an SMA indicator, but got an error message when I tried to compile it.

    What would I do differently?

    Code:
     protected override void OnBarUpdate()
            {
                     
                if (Position.MarketPosition == MarketPosition.Long)
                {
                    Variable1 = Position.GetProfitLoss(Close[0], PerformanceUnit.Currency);
                }
     
                if (SMA(Variable1, 7)[0] > SMA(Variable1, 14)[0])
                {
                    ExitLong("", "");
                }
            }
    Thanks

    #2
    You would need to use a dataseries, and pass that into the SMA instead.

    But you are probably going to run into issues until you have 14 calls in Position.GetProfitLoss through OnBarUpdate.





    Then will you reset it when the trades closes?

    Comment


      #3
      Hello Matheyas5,

      Thank you for writing in.

      Sledge is correct in that you need to pass the SMA a data series, thank you Sledge.
      Please see SMA Syntax in our help guide:


      To set your profit loss to a data series you’ll want to reference the DataSeries section of the helpguide,


      On Sledges second point with the 14 calls, you should have a check which ensures 14 bars have passed since you’re entry before the if statement comparing the SMA 7 period to SMA 14 period, for example if(BarsSinceEntry()<14) return;

      Regarding your exit condition, essentially your saying if the average PL over the last 7 periods is greater than the average PL of the last 14 periods, exit. After some testing, I’m fairly certain you would get the same exits if you just used SMA(Close, 7)[0] >SMA(Close,14)[0] which makes sense since PL is derived off Close. Thus you could eliminate the extra step of setting your PL to a data series and then passing that data series into the SMA.

      I’ve provided the code below I used for testing.

      Please let us know if you need further assistance.

      Under Region Variables,
      private DataSeries plSeries;
      Under Initialize,
      plSeries = new DataSeries(this);
      Under OnBarUpdate()
      If(Long Conditions)
      EnterLong();
      if (Position.MarketPosition == MarketPosition.Long)
      {
      plSeries[0] = Position.GetProfitLoss(Close[0], PerformanceUnit.Currency);
      }
      if(BarsSinceEntry()<14) return;

      if (SMA(plSeries, 7)[0] > SMA(plSeries, 14)[0])
      {
      ExitLong();
      }
      Alan P.NinjaTrader Customer Service

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      558 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      324 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      101 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      545 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      547 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X