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

Testing an indictors alert and placing market order

Collapse
X
 
  • 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?

    #2
    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

    Comment


      #3
      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;
      #endregion

      //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)
      return;

      if (CurrentBars[0] < 1)
      return;

      ClearOutputWindow();
      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!

      Comment


        #4
        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:


        Code:
        private Bollinger  myIndicator;

        Code:
        myIndicator = Bollinger(1.5,12);

        Code:
        Print(myIndicator.Upper[0]);


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

        Comment


          #5
          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.

          Comment


            #6
            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

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Barry Milan, Today, 10:35 PM
            0 responses
            2 views
            0 likes
            Last Post Barry Milan  
            Started by DJ888, Yesterday, 06:09 PM
            2 responses
            9 views
            0 likes
            Last Post DJ888
            by DJ888
             
            Started by jeronymite, 04-12-2024, 04:26 PM
            3 responses
            40 views
            0 likes
            Last Post jeronymite  
            Started by bill2023, Today, 08:51 AM
            2 responses
            16 views
            0 likes
            Last Post bill2023  
            Started by sidlercom80, 10-28-2023, 08:49 AM
            167 responses
            2,260 views
            0 likes
            Last Post jeronymite  
            Working...
            X