Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

AtmStrategyCreate too many trades

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

    AtmStrategyCreate too many trades

    I did a search for someone with the same problem. And I did indeed find one. However, the tech support suggested looking at the SampleAtmStrategy. I did that too. But, that didn't seem to help.

    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.

    Code:
    #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
        }
    }

    #2
    lookOutBelow, are there any errors in the logs (right-most tab of Control Center)? The function GetCurrentBid() only works on real-time data, are you trying to backtest it? If so, you'll have to input another price parameter to allow it to backtest correctly.
    AustinNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Austin View Post
      lookOutBelow, are there any errors in the logs (right-most tab of Control Center)? The function GetCurrentBid() only works on real-time data, are you trying to backtest it? If so, you'll have to input another price parameter to allow it to backtest correctly.
      No, there are no errors in the log.

      To answer your other question, I am not backtesting. But running it on Market Replay data.

      Thanks

      Comment


        #4
        lookOutBelow, thank you for the response, I will have someone respond on Monday.
        AustinNinjaTrader Customer Service

        Comment


          #5
          lookOutBelow, you will need to start at a point where everything is working correctly and add on complexity one layer at a time until it "breaks". I suggest using plenty of Print() statements to verify everything you are looking at.
          AustinNinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by cre8able, 02-11-2023, 05:43 PM
          3 responses
          235 views
          0 likes
          Last Post rhubear
          by rhubear
           
          Started by frslvr, 04-11-2024, 07:26 AM
          8 responses
          113 views
          1 like
          Last Post NinjaTrader_BrandonH  
          Started by stafe, 04-15-2024, 08:34 PM
          10 responses
          45 views
          0 likes
          Last Post stafe
          by stafe
           
          Started by rocketman7, Today, 09:41 AM
          3 responses
          11 views
          0 likes
          Last Post NinjaTrader_Jesse  
          Started by traderqz, Today, 09:44 AM
          2 responses
          10 views
          0 likes
          Last Post NinjaTrader_Gaby  
          Working...
          X