Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

improvements to a sample sma ema hma strategy

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

    improvements to a sample sma ema hma strategy

    this is a strategy that is publicly available on the bmt forum. it is intended to work as a coding exercise so beginners can put together a simple strategy. i followed the video step by step, typed everything down and it was a fairly instructive exercise (i realized you have to define functions at least two damn times for a damned strategy to work. i also realized ninjatrader code is severely case sensitive, which i find to be idiotic).


    well, this strategy does compile and it would indeed execute, but i would like to know how to make a series of improvements to this strategy and it is not apparent how to best proceed.


    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("samplesmaemahma")]
        public class samplesmaemahma : Strategy
        {
            #region Variables
            // Wizard generated variables
            private int myInput0 = 1; // Default setting for MyInput0
            // User defined variables (add any user defined variables below)
    		
    		
    		private int smalength = 200;
    		private int emalength = 200;
    		private int hmalength = 200;
    		
    		private int target1 = 12;
    		private int target2 = 12;
    		private int target3 = 12;
    		
    		private int stop = 12;
    		
    		private bool be2 = false;
    		private bool be3 = false;
    		
    		
    		
    		
    		
    		
            #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 = true;
    			EntryHandling = EntryHandling.UniqueEntries;
            }
    
    		private void GoLong()
    		{	
    			SetStopLoss("target1", CalculationMode.Price, Close[0] - (Stop*TickSize), false);
    			SetStopLoss("target2", CalculationMode.Price, Close[0] - (Stop*TickSize), false);
    			SetStopLoss("target3", CalculationMode.Price, Close[0] - (Stop*TickSize), false);
    			
    			
    			SetProfitTarget("target1", CalculationMode.Price, Close[0] + (Target1*TickSize));
    			SetProfitTarget("target2", CalculationMode.Price, Close[0] + ((Target1+Target2)*TickSize));
    			SetProfitTarget("target3", CalculationMode.Price, Close[0] + ((Target1+Target2+Target3)*TickSize));
    			
    			EnterLong("target1");
    			EnterLong("target2");
    			EnterLong("target3");
    		}
    		
    		
    		private void GoShort()
    		{	
    			SetStopLoss("target1", CalculationMode.Price, Close[0] + (Stop*TickSize), false);
    			SetStopLoss("target2", CalculationMode.Price, Close[0] + (Stop*TickSize), false);
    			SetStopLoss("target3", CalculationMode.Price, Close[0] + (Stop*TickSize), false);
    			
    			
    			SetProfitTarget("target1", CalculationMode.Price, Close[0] - (Target1*TickSize));
    			SetProfitTarget("target2", CalculationMode.Price, Close[0] - ((Target1+Target2)*TickSize));
    			SetProfitTarget("target3", CalculationMode.Price, Close[0] - ((Target1+Target2+Target3)*TickSize));
    			
    			EnterShort("target1");
    			EnterShort("target2");
    			EnterShort("target3");
    		}
    		
    		private void ManageOrders()
    		{	
    			if (Position.MarketPosition == MarketPosition.Long)
    				
    				{
    					
    					if (BE2 && High[0] > Position.AvgPrice + (Target1*TickSize))
    					SetStopLoss("target2", CalculationMode.Price, Position.AvgPrice, false);
    			
    		
    					if (BE3 && High[0] > Position.AvgPrice + ((Target1+Target2)*TickSize))
    					SetStopLoss("target3", CalculationMode.Price, Position.AvgPrice, false);
    					
    				}
    				
    				
    			if (Position.MarketPosition == MarketPosition.Short)
    				
    				{
    					
    					if (BE2 && Low[0] < Position.AvgPrice - (Target1*TickSize))
    					SetStopLoss("target2", CalculationMode.Price, Position.AvgPrice, false);
    			
    		
    					if (BE3 && Low[0] < Position.AvgPrice - ((Target1+Target2)*TickSize))
    					SetStopLoss("target3", CalculationMode.Price, Position.AvgPrice, false);
    					
    				}
    		
    		}
            protected override void OnBarUpdate()
            {
    			EntryHandling = EntryHandling.UniqueEntries;
    		
    			SMA		smav = SMA(smalength);
    			EMA		emav = EMA(smalength);
    			HMA		hmav = HMA(smalength);
    			
    			
    			ManageOrders();
    			
    			
    			if (Position.MarketPosition != MarketPosition.Flat) return;
    			
    			
    			if (Rising(smav) && Rising(emav) && Rising(hmav))
    				GoLong();
    			
    			
    			else if (Falling(smav) && Falling(emav) && Falling(hmav))
    				GoShort();
    			
    			
            }
    
            #region Properties
            [Description("")]
            [GridCategory("Parameters")]
            public int SMAlength
            {
                get { return smalength; }
                set { smalength = Math.Max(1, value); }
            }
    		[Description("")]
            [GridCategory("Parameters")]
            public int EMAlength
            {
                get { return emalength; }
                set { emalength = Math.Max(1, value); }
            }
    		[Description("")]
            [GridCategory("Parameters")]
            public int HMAlength
            {
                get { return hmalength; }
                set { hmalength = Math.Max(1, value); }
            }
    		[Description("")]
            [GridCategory("Parameters")]
            public int Target1
            {
                get { return target1; }
                set { target1 = Math.Max(1, value); }
            }
    		[Description("")]
            [GridCategory("Parameters")]
            public int Target2
            {
                get { return target2; }
                set { target2 = Math.Max(1, value); }
            }
    		[Description("")]
            [GridCategory("Parameters")]
            public int Target3
            {
                get { return target3; }
                set { target3 = Math.Max(1, value); }
            }
    		[Description("")]
            [GridCategory("Parameters")]
            public int Stop
            {
                get { return stop; }
                set { stop = Math.Max(1, value); }
            }
    		[Description("")]
            [GridCategory("Parameters")]
            public bool BE2
            {
                get { return be2; }
                set { be2 = value; }
            }
    		[Description("")]
            [GridCategory("Parameters")]
            public bool BE3
            {
                get { return be3; }
                set { be3 = value; }
            }
            #endregion
        }
    }

    1) i can't see where the number of contracts for every possible entry / order is determined. how could i make the strategy take 2 contracts on every order? how could we make the strategy take 2 contracts for entry 1, 4 contracts for entry 2 and 8 contracts for entry 3?


    2) another thing i would like to know is how to change this strategy which apparently works with market orders to now use limit orders? is it possible to use limit "join" and stop limit "join" (buy at the bid, sell at the ask) orders on every order the strategy generates? what about "hit / take" limit and stop limit (buy at the ask, sell at the bid) orders?
    Last edited by rtwave; 03-19-2016, 02:43 PM. Reason: yes indeed

    #2
    Hello rtwave,

    Thank you for writing in.

    NinjaScript is an extension of the Microsoft C# programming language, which is a case-sensitive language.

    I would suggest taking a look at this link from our help guide for some of the C# basics: https://ninjatrader.com/support/help...g_concepts.htm

    You can specify the amount of contracts to enter by modifying the EnterLong() and EnterShort() method calls to utilize the overload that takes in a quantity value:
    EnterLong()
    EnterShort()

    You can submit limit or stop limit orders by utilizing EnterLongLimit()/EnterLongStopLimit() and EnterShortLimit()/EnterShortStopLimit():
    EnterLongLimit()
    EnterLongStopLimit()
    EnterShortLimit()
    EnterShortStopLimit()

    You can utilize the GetCurrentBid() and GetCurrentAsk() methods to obtain the current bid and ask prices and use those values when submitting your limit and stop limit orders:
    GetCurrentBid()
    GetCurrentAsk()

    Further, the following link is to our help guide with an alphabetical reference list to all supported methods, properties, and objects that are used in NinjaScript.
    Alphabetical Reference
    Zachary G.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Today, 05:17 AM
    0 responses
    48 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    126 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    66 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    42 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    46 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X