• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.


No announcement yet.

Partner 728x90


Testing an indictors alert and placing market order

  • Filter
  • Time
  • Show
Clear All
new posts

    Testing an indictors alert and placing market order

    Not sure how to search for this in the forums. I have a 3rd party indicator that has two different values "AlertUp" and "AlertDown". Is there a sample NinjaScript to test for either of these conditions, and execute a market trade (long or short) based on an ATM strategy?

    Hello epete,

    While I likely not be able to find an example using the 3rd party indicator you are using, below is an example condition of checking the macd avg plot is greater than the macd macd plot.

    private MACD myMACD;

    myMACD = MACD(12, 26, 9);

    if (myMACD.Avg[0] > myMACD.MACD[0])
    // execute code

    Also, below is a link to a forum post with helpful information about getting started with NinjaScript.

    Included with NinjaTrader is the SampleAtmStrategy that demonstrates placing an order through Atm Strategy methods.
    Also, below is a link to the help guide.
    Chelsea B.NinjaTrader Customer Service


      I'm trying to do something, which I thought would be pretty easy to do.
      - I have a 3rd party indicator on my chart
      - I can see, in NinjaScript, the values that indicator exposes (i.e., by typing a period, which lists all the variables
      - I want to simply Print out one of those values, and later act upon other alerts that the indicator issues

      #region Using declarations
      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.ComponentModel.DataAnnotations;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Windows;
      using System.Windows.Input;
      using System.Windows.Media;
      using System.Xml.Serialization;
      using NinjaTrader.Cbi;
      using NinjaTrader.Gui;
      using NinjaTrader.Gui.Chart;
      using NinjaTrader.Gui.SuperDom;
      using NinjaTrader.Gui.Tools;
      using NinjaTrader.Data;
      using NinjaTrader.NinjaScript;
      using NinjaTrader.Core.FloatingPoint;
      using NinjaTrader.NinjaScript.Indicators;
      using NinjaTrader.NinjaScript.DrawingTools;

      //This namespace holds Strategies in this folder and is required. Do not change it.
      namespace NinjaTrader.NinjaScript.Strategies
      public class TestVSS : Strategy
      // private string OrderID;
      private int Count;

      private ncatVolumeSpreadScalper myIndicator;

      protected override void OnStateChange()
      if (State == State.SetDefaults)
      Description = @"Enter the description for your new custom Strategy here.";
      Name = "TestVSS";
      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.Day;
      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;
      // OrderID = string.Empty;
      Count = 0;
      else if (State == State.Configure)
      // AddDataSeries("ES 06-19", Data.BarsPeriodType.Tick, 300, Data.MarketDataType.Last);
      else if (State == State.DataLoaded)
      // myIndicator = ncatVolumeSpreadScalper(Close, @"Disabled", @"Disabled", @"Disabled", @"Disabled", 3, 150, 70, 25, 70, 200, 150, @"0", @"Minutes", 20, false, 20);

      protected override void OnBarUpdate()
      if (BarsInProgress != 0)

      if (CurrentBars[0] < 1)

      Count = Count + 1;
      Print("Testing Output");
      Print("Count = " + Count.ToString());
      // Print("Close = " + Close[0].ToString());
      // Print("OrderID=[" + ncatVolumeSpreadScalper.OrderId.ToString() + "]"); //- is a 'method', which is not valid in the given context
      // Print("OrderID=[" + ncatVolumeSpreadScalper.OrderId + "]"); //- is a 'method', which is not valid in the given context
      // Print("OrderID=[" + ncatVolumeSpreadScalper().OrderId[0] + "]"); //- no overlead for method 'ncatVolumeSpreadScalper' takes 0 arguments
      // Print("OrderID=[" + ncatVolumeSpreadScalper.OrderId + "]"); //- is a 'method', which is not valid in the given context
      Print("OrderID=[" + myIndicator.OrderId[0] + "]"); //-

      // Set 1
      // if (TestVSS1.TickHeight[0] == TestVSS1.TickHeight[0])


      In the output window, I get
      - a list of all my ATM strategies (which I find VERY odd) - that happens as soon as I open the Strategy window.

      Testing Output
      Count = 1
      Strategy "TestVSS": Error on calling "OnBarUpdate" method on bar 1: Object reference not set to an instance of an object.

      If I uncomment out that line "myIndicator = nCatVolumeSpreadScalper(...." NT8 aborts!


        Hello epete,

        Thank you for your reply.

        The error you are seeing: Object reference not set to an instance of an object. specifically relates to commenting out the line you mentioned. This means the variable you used is null, or myIndicator is null.

        When you uncomment the line you mentioned, you said that NT aborts, what you do mean by this? Do you mean the script is unable to start or the platform has some other problems occur?

        Because a custom indicator is being used, you may need to inquire with the creator of this item for assistance with how to correctly call it in NinjaScript. You are using the correct approach here in general regarding how you are assigning the variable and using it from OnBarUpdate.

        Alternatively, you could do a more simple test to see that what you are doing is correct and does work with other indicators:

        private Bollinger  myIndicator;

        myIndicator = Bollinger(1.5,12);


        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service


          If I uncomment that line, and try to add the Strategy to the chart, I get the following errors - and NT8 literally ends, stops, aborts, exits without warning.
          - Unhandled exception: Object reference not set to an instance of an object.
          - Stratey 'TestVSS': Error on calling 'OnStateChange' method. The calling thread must be STA, because many UI components require this.


            Hello epete,

            Thank you for providing the extra details here. In this case, you would still likely need to inquire with the author of this item to ensure the correct syntax is being used when calling it from code. The error mentioned may be due to something custom that is happening in the indicator which you would not be able to fix without being able to look at the code of the indicator. I would also suggest the more simple test as noted previously to confirm what you are doing in the script is correct, you should just see that instead of the error that the Bollinger indicator is printed. This can help narrow down the problem or isolate that it is a problem with the item being used.

            I look forward to being of further assistance.
            JesseNinjaTrader Customer Service


            Latest Posts


            Topics Statistics Last Post
            Started by mike09, Today, 07:37 PM
            0 responses
            1 view
            Last Post mike09
            by mike09
            Started by bebbus, Today, 04:03 PM
            2 responses
            Last Post bltdavid  
            Started by Unicorn_Catcher, Today, 04:06 PM
            1 response
            Last Post User1104  
            Started by punkiy2111, Today, 04:02 PM
            2 responses
            Last Post punkiy2111  
            Started by traderharley, Today, 03:32 PM
            1 response
            Last Post NinjaTrader_ChrisL