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.


    https://ninjatrader.com/support/help...ries_class.htm


    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:
      http://ninjatrader.com/support/helpG...s_and_acc3.htm

      To set your profit loss to a data series you’ll want to reference the DataSeries section of the helpguide,
      http://ninjatrader.com/support/helpG...sic_syntax.htm

      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 corman, Today, 02:02 PM
      1 response
      7 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by Uregon, Today, 12:12 PM
      1 response
      7 views
      0 likes
      Last Post NinjaTrader_Kate  
      Started by kerplunk1899, Today, 10:03 AM
      1 response
      19 views
      0 likes
      Last Post NinjaTrader_Kate  
      Started by rexsole, Today, 10:20 AM
      1 response
      11 views
      0 likes
      Last Post NinjaTrader_RyanS  
      Started by pjsmith, Today, 03:01 AM
      6 responses
      18 views
      0 likes
      Last Post bltdavid  
      Working...
      X