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

How do I submit an order in NinjaScript to use a selected ATM Strategy?

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

    How do I submit an order in NinjaScript to use a selected ATM Strategy?

    I have an ATM Strategy that has a target, stop loss and moves the stop loss after a certain amount of ticks in profit. I want to be able to use a strategy created with NinjaScript to run that ATM Strategy when the strategy triggers an order. How do I go about doing that?

    #2
    Actually, it seems like the "Algorithmic Trading with NinjaTrader" course on Udemy has a section on this. It seems to be working so far.

    Comment


      #3
      Here is a template in case anyone is interested.

      Code:
      #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 _ATMStrat : Strategy
      {
      #region Variables
      
      private string atmStrategyId;
      private string atmStrategyOrderId;
      private bool isAtmStrategyCreated = false;
      private bool entryLong;
      private bool entryShort;
      
      #endregion
      
      #region Properties
      
      [NinjaScriptProperty]
      [Display(Name ="ATM Template Name", Description="The name of the custom ATM Strategy.", Order = 1, GroupName = "ATM Strat")]
      public string ATMTemplateName {get; set;}
      
      #endregion
      
      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = @"Enter the description for your new custom Strategy here.";
      Name = "_ATMStrat";
      Calculate = Calculate.OnEachTick;
      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;
      
      ATMTemplateName = "ATMStratName";
      }
      else if (State == State.Configure)
      {
      }
      }
      
      protected override void OnBarUpdate()
      {
      if (CurrentBar < BarsRequiredToTrade)
      return;
      
      if (State == State.Historical)
      return;
      
      // Checking for ATM Strategy will fail if not in real time.
      if (State != State.Realtime)
      return;
      
      entryLong = Close[0] > Close[1];
      entryShort = Close[0] < Close[1];
      
      if (AtmIsFlat())
      {
      if (entryLong)
      {
      atmStrategyId = GetAtmStrategyUniqueId();
      atmStrategyOrderId = GetAtmStrategyUniqueId();
      
      AtmStrategyCreate(OrderAction.Buy, OrderType.Market, 0, 0, TimeInForce.Day, atmStrategyOrderId, ATMTemplateName, atmStrategyId,
      (atmCallbackErrorCode, atmCallbackId) => {
      if (atmCallbackId == atmStrategyId)
      {
      if (atmCallbackErrorCode == Cbi.ErrorCode.NoError)
      {
      isAtmStrategyCreated = true;
      }
      }
      });
      }
      else if (entryShort)
      {
      atmStrategyId = GetAtmStrategyUniqueId();
      atmStrategyOrderId = GetAtmStrategyUniqueId();
      
      AtmStrategyCreate(OrderAction.SellShort, OrderType.Market, 0, 0, TimeInForce.Day, atmStrategyOrderId, ATMTemplateName, atmStrategyId,
      (atmCallbackErrorCode, atmCallbackId) => {
      if (atmCallbackId == atmStrategyId)
      {
      if (atmCallbackErrorCode == Cbi.ErrorCode.NoError)
      {
      isAtmStrategyCreated = true;
      }
      }
      });
      }
      }
      
      if (isAtmStrategyCreated)
      {
      // Do stuff...
      }
      
      if (!isAtmStrategyCreated)
      {
      // Do stuff for failed ATM Strategy
      }
      }
      
      private bool AtmIsFlat()
      {
      if (atmStrategyId == null)
      return true;
      
      return GetAtmStrategyMarketPosition(atmStrategyId) == MarketPosition.Flat;
      }
      }
      }

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by maybeimnotrader, Today, 05:46 PM
      0 responses
      6 views
      0 likes
      Last Post maybeimnotrader  
      Started by quantismo, Today, 05:13 PM
      0 responses
      6 views
      0 likes
      Last Post quantismo  
      Started by AttiM, 02-14-2024, 05:20 PM
      8 responses
      166 views
      0 likes
      Last Post jeronymite  
      Started by cre8able, Today, 04:22 PM
      0 responses
      8 views
      0 likes
      Last Post cre8able  
      Started by RichStudent, Today, 04:21 PM
      0 responses
      5 views
      0 likes
      Last Post RichStudent  
      Working...
      X