Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to Entry rule that compares moving averages of other symbols

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

    How to Entry rule that compares moving averages of other symbols

    I want to add an entry rule that I only enter trades in an equity when the current SP500 price is above the SP500 100 day moving.

    Is there any way to achieve this?

    #2
    Hello Gathem,

    The NinjaTrader platform itself would not be able to restrict your ability to place a trade in this regard.

    While it should be possible to set up a NinjaScript that would alert you if the SP 500 price is above the 100-day moving average, e.g. with a message on the open chart, it would not be possible to restrict the placement of manual trades that way.

    If you are interested in learning more about how to program a script that would alert you, please let me know!
    Manfred F.NinjaTrader Customer Service

    Comment


      #3
      Sorry, let me add some context.

      I am using ninjascript to programmatically make trades.

      I am looking for a way to use ninjascript to get a moving average of a different symbol.

      The SMA function is relative to the instrument I am trading. But I want to get the SMA of a different instrument, for the decision criteria for my code to enter a trade.

      What I want to know is, how can I use ninjascript to get a moving average of the SP500 or DJIA when My default Instrument is neither of those indexes

      Comment


        #4
        I've set my Instrument in this case to AMZN

        This is the value I want. But it returns NULL
        Code:
        Print(Time[0] + ": " + Instrument.GetInstrument("^DJIA", true).MasterInstrument.FundamentalData.Day200MovingAverage)
        Does anyone have any ideas, on how to get this value?

        Comment


          #5
          Hello Gathem,

          Thank you for your replies.

          You could certainly do something like this by adding the required data series to your script using Add Data Series and then calling the MA indicator of your choice to calculate using that series to get the value.



          Going off your first post where you said you'd want to enter if the ^SP500 price is greater than a 20 day ^SP500 moving average, but send the entry to the primary instrument, here is an example of how you can do so:

          Code:
           public class ExamplePriceCrossesSMAofSecondarySeries : Strategy
          {
          // create a variable to hold an instance of the SMA
          private SMA SMA1;
          
          protected override void OnStateChange()
          {
          if (State == State.SetDefaults)
          {
          Description = @"Enter the description for your new custom Strategy here.";
          Name = "ExamplePriceCrossesSMAofSecondarySeries";
          Calculate = Calculate.OnBarClose;
          EntriesPerDirection = 1;
          EntryHandling = EntryHandling.AllEntries;
          IsExitOnSessionCloseStrategy = true;
          ExitOnSessionCloseSeconds = 30;
          IsFillLimitOnTouch = false;
          MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
          OrderFillResolution = OrderFillResolution.Standard;
          Slippage = 0;
          StartBehavior = StartBehavior.WaitUntilFlat;
          TimeInForce = TimeInForce.Gtc;
          TraceOrders = false;
          RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
          StopTargetHandling = StopTargetHandling.PerEntryExecution;
          BarsRequiredToTrade = 20;
          // Disable this property for performance gains in Strategy Analyzer optimizations
          // See the Help Guide for additional information
          IsInstantiatedOnEachOptimizationIteration = true;
          }
          else if (State == State.Configure)
          {
          // add an ^SP500 data series 
          AddDataSeries("^SP500", Data.BarsPeriodType.Minute, 1, Data.MarketDataType.Last);
          }
          else if (State == State.DataLoaded)
          {
          // assign our SMA to calculate on the close price of the secondary series
          SMA1 = SMA(Closes[1], 20);
          }
          }
          
          protected override void OnBarUpdate()
          {
          if (BarsInProgress != 0)
          return;
          
          if (CurrentBars[0] < 1
          || CurrentBars[1] < 0)
          return;
          
          // Set 1
          // check if the close price of the ^SP500 is greater than the current calculated 20 period SMA of the same
          if (Closes[1][0] > SMA1[0])
          {
          EnterLong(Convert.ToInt32(DefaultQuantity), "");
          }
          
          }
          }
          I should note here that you can set all this up in the Strategy Builder to make sure you get everything in the right spot.

          Please let us know if we may be of further assistance to you.
          Kate W.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by kujista, Today, 05:44 AM
          0 responses
          5 views
          0 likes
          Last Post kujista
          by kujista
           
          Started by ZenCortexCLICK, Today, 04:58 AM
          0 responses
          6 views
          0 likes
          Last Post ZenCortexCLICK  
          Started by sidlercom80, 10-28-2023, 08:49 AM
          172 responses
          2,281 views
          0 likes
          Last Post sidlercom80  
          Started by Irukandji, Yesterday, 02:53 AM
          2 responses
          18 views
          0 likes
          Last Post Irukandji  
          Started by adeelshahzad, Today, 03:54 AM
          0 responses
          8 views
          0 likes
          Last Post adeelshahzad  
          Working...
          X