I have a very simple script that I'm putting together. Just so I can understand how to initiate an ATM from a ninjascript. All of my testing is being done on a market replay.
The ATM is supposed to be initiated when the bid price passes above a regression channel line. It actually works as expected. The problem is, it fires off dozens and dozens of trades as the price is bouncing around on that line.
So, I looked at the SampleAtmStrategy to limit it to a single entry. However, when I insert that code into my very simple script (that was spit out of the wizard), it doesn't do anything. Not a single order is executed. On the same market replay data.
In the output window, I Printed the orderId.Length and atmStrategyId.Length. They both start out as 32. Not zero, as I would expect. This seems to be the reason why nothing is happening. Although, I'm not entirely sure.
Can anyone send me in the right direction? I've put the code I'm using below. It is basically the SampleAtmStrategy with a change to what signals the trade.
#region Using declarations using System; using System.ComponentModel; using System.Diagnostics; using System.Drawing; using System.Drawing.Drawing2D; using System.Xml.Serialization; using NinjaTrader.Cbi; using NinjaTrader.Data; using NinjaTrader.Indicator; using NinjaTrader.Gui.Chart; using NinjaTrader.Strategy; #endregion // This namespace holds all strategies and is required. Do not change it. namespace NinjaTrader.Strategy { /// <summary> /// Testing ATM stuff /// </summary> [Description("Testing ATM stuff")] public class BuyCenterline : Strategy { #region Variables // Wizard generated variables private int periodLength = 20; // Default setting for PeriodLength // User defined variables (add any user defined variables below) private string atmStrategyId = string.Empty; private string orderId = string.Empty; #endregion /// <summary> /// This method is used to configure the strategy and is called once before any strategy method is called. /// </summary> protected override void Initialize() { CalculateOnBarClose = false; } /// <summary> /// Called on each bar update event (incoming tick) /// </summary> protected override void OnBarUpdate() { if (orderId.Length == 0 && atmStrategyId.Length == 0 && CrossAbove(GetCurrentBid(), RegressionChannel(PeriodLength, 4.5).Middle, 1)) { atmStrategyId = GetAtmStrategyUniqueId(); orderId = GetAtmStrategyUniqueId(); AtmStrategyCreate(Cbi.Action.Buy, OrderType.Limit, GetCurrentBid(), 0, TimeInForce.Day, orderId, "Centerline", atmStrategyId); } // Check for a pending entry order if (orderId.Length > 0) { string[] status = GetAtmStrategyEntryOrderStatus(orderId); // If the status call can't find the order specified, the return array length will be zero otherwise it will hold elements if (status.GetLength(0) > 0) { // Print out some information about the order to the output window Print("The entry order average fill price is: " + status[0]); Print("The entry order filled amount is: " + status[1]); Print("The entry order order state is: " + status[2]); // If the order state is terminal, reset the order id value if (status[2] == "Filled" || status[2] == "Cancelled" || status[2] == "Rejected") orderId = string.Empty; } } // If the strategy has terminated reset the strategy id else if (atmStrategyId.Length > 0 && GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat) atmStrategyId = string.Empty; if (atmStrategyId.Length > 0) { // You can change the stop price if (GetAtmStrategyMarketPosition(atmStrategyId) != MarketPosition.Flat) AtmStrategyChangeStopTarget(0, Low[0] - 3 * TickSize, "STOP1", atmStrategyId); // Print some information about the strategy to the output window Print("The current ATM Strategy position is: " + GetAtmStrategyMarketPosition(atmStrategyId)); Print("The current ATM Strategy position quantity is: " + GetAtmStrategyPositionQuantity(atmStrategyId)); Print("The current ATM Strategy average price is: " + GetAtmStrategyPositionAveragePrice(atmStrategyId)); Print("The current ATM Strategy Unrealized PnL is: " + GetAtmStrategyUnrealizedProfitLoss(atmStrategyId)); } } #region Properties [Description("Period length to create channel from.")] [Category("Parameters")] public int PeriodLength { get { return periodLength; } set { periodLength = Math.Max(1, value); } } #endregion } }
Comment