Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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 mmckinnm, Today, 01:34 PM
      0 responses
      1 view
      0 likes
      Last Post mmckinnm  
      Started by f.saeidi, Today, 01:32 PM
      0 responses
      1 view
      0 likes
      Last Post f.saeidi  
      Started by traderqz, Today, 12:06 AM
      9 responses
      16 views
      0 likes
      Last Post NinjaTrader_Gaby  
      Started by kevinenergy, 02-17-2023, 12:42 PM
      117 responses
      2,766 views
      1 like
      Last Post jculp
      by jculp
       
      Started by Mongo, Today, 11:05 AM
      5 responses
      15 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Working...
      X