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

AtmStrategy Issues

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

    AtmStrategy Issues

    Hello,

    Long time lurker, first time poster. I am developing a NT strategy using an AtmStrategy but it is not acting the way I want it to. I have attached the script. I want it to basically:

    A) Kill the AtmEntryOrder after 1 bar if it does not fill - currently it does not
    B) Kill any open AtmStrategies and positions after 3PM - currently it does not
    C) When I specify to reverse positions, it does not.

    Thanks for your help.

    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>
        /// Enter the description of your strategy here
        /// </summary>
        [Description("Enter the description of your strategy here")]
        public class StochScalpRANGE12 : Strategy
        {
            #region Variables
            // Wizard generated variables
            private string    orderId                = string.Empty;
            private string    atmStrategyId        = string.Empty;
            private int     BarsPast_OE         = 1;
            private         DataSeries             barcount;
            #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()
            {
                barcount = new DataSeries (this) ;
    
            }
    
            private void GoLong()
            {
                if (orderId != null)
                {
                    string[] entryOrder = GetAtmStrategyEntryOrderStatus(orderId);
                    if (GetAtmStrategyMarketPosition(orderId) != MarketPosition.Flat)
                        return;
                    
                    if (entryOrder.Length > 0)
                    {                   
                        if (entryOrder[2].ToString() != "Filled")
                        return;
                    } 
                    
                
                }
                orderId = GetAtmStrategyUniqueId();
                atmStrategyId = GetAtmStrategyUniqueId();
                AtmStrategyCreate(Cbi.OrderAction.Buy, OrderType.Limit, Low[0], 0, TimeInForce.Day, orderId, "CL-Ichi", atmStrategyId);
                barcount.Set(CurrentBars[0]);
            }
            
            private void GoShort()
            {
                if (orderId != null)
                {
                    string[] entryOrder = GetAtmStrategyEntryOrderStatus(orderId);               
                    if (GetAtmStrategyMarketPosition(orderId) != MarketPosition.Flat)
                        return;
    
                    if (entryOrder.Length > 0)
                   { 
                        if (entryOrder[2].ToString() != "Filled")
                        return;
                   }
                    
                }
           
                orderId    = GetAtmStrategyUniqueId();
                atmStrategyId = GetAtmStrategyUniqueId();
                AtmStrategyCreate(Cbi.OrderAction.SellShort, OrderType.Limit, High[0], 0, TimeInForce.Day, orderId, "CL-Ichi", atmStrategyId);
                barcount.Set(CurrentBars[0]);
            }
            
            private void ExitLong()
            {
                // check to see if not flat, close all open orders
                AtmStrategyClose(orderId);
            } 
                
            private void ExitShort()
            {
                // check to see if not flat, close all open orders
                AtmStrategyClose(orderId);
            }
                
            
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
              {
                    
                if (ToTime(Time[0]) >= ToTime(15, 0, 0))
                {
                AtmStrategyClose(orderId);
                } 
                
    //            Cancels Limit order LONG
                if ((CurrentBars[0] + BarsPast_OE)  > barcount[0] && GetAtmStrategyMarketPosition(orderId) == MarketPosition.Long)
                {
                AtmStrategyCancelEntryOrder(orderId);
                orderId = string.Empty;
                Print("Cancelled LONG ENTRY Order from strategy");
                }
                
    //            Cancels Limit order SHORT
                if ((CurrentBars[0] + BarsPast_OE)  > barcount[0] && GetAtmStrategyMarketPosition(orderId) == MarketPosition.Short)
                {
                AtmStrategyCancelEntryOrder(orderId);
                orderId = string.Empty;
                Print("Cancelled SHORT ENTRY Order from strategy");
                }
                                    
                if ((ToTime(Time[0]) > ToTime(5, 30, 0)) && (ToTime(Time[0]) < ToTime(15, 0, 0)))
                {
                            
                    // Condition set 1
                    if (CrossAbove(MACD(10, 22, 5), MACD(10, 22, 5).Avg, 1))
                    {
                        if (Position.MarketPosition == MarketPosition.Flat)
                        {
                            GoLong();
                        }
                        
                        if (Position.MarketPosition == MarketPosition.Short)
                        {
                            ExitShort();
                            GoLong();
                        }
                    }
                    
                    // Condition set 2
                    if (CrossBelow(MACD(10, 22, 5), MACD(10, 22, 5).Avg, 1))
                    {
                        if (Position.MarketPosition == MarketPosition.Flat)
                        {
                            GoShort();
                        }
                        
                        if (Position.MarketPosition == MarketPosition.Long)
                        {
                            ExitLong();
                            GoShort();
                        }
                    }
                        
                }
                
            }
    
    
             #region Properties
            
                #endregion
    }
    }

    #2
    Hello,

    Everything seems to be set up logically in your code. Have you done a bit of debugging by adding prints at the points in which these actions should occur? This may help to pinpoint exactly where the code is having trouble. Also, are you seeing any errors showing up in the Output Window or the Log tab of the Control Center while the strategy is running?

    I look forward to your reply.
    Dave I.NinjaTrader Product Management

    Comment


      #3
      Sure,

      So the output window says this

      **NT** Submitting order with strategy 'StochScalpRANGE12/32fa0ce14438448fb4be48d2155722f1'
      **NT** GetAtmStrategyMarketPosition() method error: AtmStrategyId '4ef0caf2603f4510b2b0d95ba51324f4' does not exist

      and

      Cancelled LONG ENTRY Order from strategy
      **NT** AtmStrategyCancelEntryOrder() method error: Missing orderId parameter
      Last edited by rdavido; 05-12-2015, 05:39 PM.

      Comment


        #4
        Hello rdavido,

        Thank you for your response.

        I would recommend implementing separate orderIds and atmStrategyIds for going long and short. I have created an example of how to create a NinjaScript strategy that reverses with an ATM strategy, please review this example at the following link: http://www.ninjatrader.com/support/f...d=5&linkid=615

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by aussugardefender, Today, 01:07 AM
        0 responses
        3 views
        0 likes
        Last Post aussugardefender  
        Started by pvincent, 06-23-2022, 12:53 PM
        14 responses
        238 views
        0 likes
        Last Post Nyman
        by Nyman
         
        Started by TraderG23, 12-08-2023, 07:56 AM
        9 responses
        384 views
        1 like
        Last Post Gavini
        by Gavini
         
        Started by oviejo, Today, 12:28 AM
        0 responses
        4 views
        0 likes
        Last Post oviejo
        by oviejo
         
        Started by pechtri, 06-22-2023, 02:31 AM
        10 responses
        125 views
        0 likes
        Last Post Leeroy_Jenkins  
        Working...
        X