VIDEO TUTORIAL: How to create an advanced NinjaTrader Strategy - futures io
futures io futures trading



VIDEO TUTORIAL: How to create an advanced NinjaTrader Strategy


Discussion in NinjaTrader

Updated by afshinmoshrefi
      Top Posters
    1. looks_one calhawk01 with 10 posts (1 thanks)
    2. looks_two Big Mike with 9 posts (189 thanks)
    3. looks_3 stephenszpak with 6 posts (0 thanks)
    4. looks_4 spinnybobo with 6 posts (2 thanks)
      Best Posters
    1. looks_one Big Mike with 21.0 thanks per post
    2. looks_two zeller4 with 7.0 thanks per post
    3. looks_3 piersh with 7.0 thanks per post
    4. looks_4 Cloudy with 1.5 thanks per post
    1. trending_up 68,571 views
    2. thumb_up 222 thanks given
    3. group 59 followers
    1. forum 64 replies
    2. attach_file 3 attachments




Welcome to futures io: the largest futures trading community on the planet, with well over 100,000 members
  • Genuine reviews from real traders, not fake reviews from stealth vendors
  • Quality education from leading professional traders
  • We are a friendly, helpful, and positive community
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts
  • We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community.  It's free and simple.

-- Big Mike, Site Administrator

(If you already have an account, login at the top of the page)

 
Search this Thread
 

VIDEO TUTORIAL: How to create an advanced NinjaTrader Strategy

(login for full post details)
  #41 (permalink)
Atkinson, NH USA
 
Experience: Intermediate
Platform: NinjaTrader 8/TensorFlow
Broker: NinjaTrader Brokerage
Trading: Futures, CL, ES, ZB
 
NJAMC's Avatar
 
Posts: 1,963 since Dec 2010
Thanks: 3,031 given, 2,375 received


calhawk01 View Post
Thank you for the reply.

Now I have:

 
Code
		private void GoLong()
		{
			SetStopLoss("target1", CalculationMode.Price, Close[0] - (Stop*TickSize), false);
			
			SetProfitTarget("target1", CalculationMode.Price, Close[0] + (Target1*TickSize));
			
			EnterLong("target1");

		}


		
		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);
			
			}
This still does not seem to work. It exits at profit target1

Hi,

I couldn't find a description of what you were trying to do, but that seems correct with a single target.

Can you describe what it is you are trying to do?

Nil per os
-NJAMC [Generic Programmer]

LOM WIKI: NT-Local-Order-Manager-LOM-Guide
Artificial Bee Colony Optimization
Visit my futures io Trade Journal Reply With Quote

Can you help answer these questions
from other members on futures io?
QDEL
Stocks and ETFs
Degiro Negative Balance Advice
Traders Hideout
Dollar crash in 2021
Currencies
3x Fangdango
Traders Hideout
Ninjatrader 8, NT8, autotrading strategy stability
NinjaTrader
 
Best Threads (Most Thanked)
in the last 7 days on futures io
VWAP for stock index futures trading?
41 thanks
Coronavirus COVID-19
20 thanks
Price Action Kewltech Style
19 thanks
Emini 4 Tick Scalp still valid?
13 thanks
Is back testing a waste of time?
13 thanks
 
(login for full post details)
  #42 (permalink)
baltimore marylnd
 
Experience: Beginner
Platform: ninja
Trading: es
 
Posts: 91 since May 2013
Thanks: 5 given, 11 received


NJAMC View Post
Hi,

I couldn't find a description of what you were trying to do, but that seems correct with a single target.

Can you describe what it is you are trying to do?

hi NJAMC, apolgs for the late reply [went on a vaca ]

what i'm trying to do is:

buy one contract when my buy conditions are true, set my initial stoploss 12 ticks away, and have three targets, once target1 is hit, I want to move my initial stoploss to my purchase price, once target2 is hit, i want to move my stoploss to target1 and once target 3 is hit, i want to exit. i thought that is what this script did but it's a bit different :/

Reply With Quote
 
(login for full post details)
  #43 (permalink)
baltimore marylnd
 
Experience: Beginner
Platform: ninja
Trading: es
 
Posts: 91 since May 2013
Thanks: 5 given, 11 received


 
Code
		private void GoLong()
		{
			SetStopLoss("target1", CalculationMode.Price, Close[0] - (Stop*TickSize), false);
			
			SetProfitTarget("target1", CalculationMode.Price, Close[0] + ((Target1+Target2+Target3)*TickSize));
		
			EnterLong("target1");
			
		}

		private void ManageOrders()
		{

			if (Position.MarketPosition == MarketPosition.Long)
			{
				if (High[0] > Position.AvgPrice + (Target1*TickSize))
					SetStopLoss("target1", CalculationMode.Price, Position.AvgPrice, false);
				
				if (High[0] > Position.AvgPrice + ((Target1+Target2)*TickSize))
					SetStopLoss("target1", CalculationMode.Price, Position.AvgPrice + (Target1*TickSize), false);
				if (High[0] > Position.AvgPrice + ((Target1+Target2+Target3)*TickSize))
					SetStopLoss("target1", CalculationMode.Price, Position.AvgPrice + ((Target1+Target2)*TickSize), false);
				
			}
when t1 is hit, it moves to stoploss to average price, but when t2 is hit, the stoploss isn't moved to t1

Reply With Quote
 
(login for full post details)
  #44 (permalink)
desert CA
 
Experience: Intermediate
Platform: NT7, TOS, CQG-trader
Broker: AMP, DT, TDA
Trading: CL,NQ,YM
 
Posts: 2,130 since Jul 2011
Thanks: 2,368 given, 1,710 received

 
Code
		private void GoLong()
		{
			SetStopLoss("target1", CalculationMode.Price, Close[0] - (Stop*TickSize), false);
			
			SetProfitTarget("target1", CalculationMode.Price, Close[0] + ((Target1+Target2+Target3)*TickSize));
		
			EnterLong("target1");
			
		}

		private void ManageOrders()
		{

			if (Position.MarketPosition == MarketPosition.Long)
			{
				if (High[0] > Position.AvgPrice + ((Target1+Target2+Target3)*TickSize))
					SetStopLoss("target1", CalculationMode.Price, Position.AvgPrice + ((Target1+Target2)*TickSize), false);
                                else if (High[0] > Position.AvgPrice + ((Target1+Target2)*TickSize))
					SetStopLoss("target1", CalculationMode.Price, Position.AvgPrice + (Target1*TickSize), false);
                                else if (High[0] > Position.AvgPrice + (Target1*TickSize))
					SetStopLoss("target1", CalculationMode.Price, Position.AvgPrice, false);
				
			}
                }

Just an idea. Reversing the order of the statements and using "else" so that "SetStopLoss" is not set multiple times in the loop. I don't have a strategy setup currently to test it.

Visit my futures io Trade Journal Reply With Quote
 
(login for full post details)
  #45 (permalink)
baltimore marylnd
 
Experience: Beginner
Platform: ninja
Trading: es
 
Posts: 91 since May 2013
Thanks: 5 given, 11 received

Cloudy, thank you for trying. Still no luck though :/

here is the complete code to the strategy:

 
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>
    /// Big Mike - 1/26/10 - https://futures.io/beginners-introductions-tutorials/2512-video-tutorial-how-create-advanced-ninjatrader-strategy.html
    /// </summary>
    [Description("Make me millions Jan 27 2010")]
    public class TargetES930 : Strategy
    {
        //#region Variables
		
		private int		target1			= 12;
		private int		target2			= 10;
		private int		target3			= 24;
		
		private int		stop			= 12;
		
		private bool	be2				= true;
		private bool	be3				= true;
		
		
		//#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);
			
			SetProfitTarget("target1", CalculationMode.Price, Close[0] + ((Target1+Target2+Target3)*TickSize));
		
			EnterLong("target1");
			
		}

		private void ManageOrders()
		{

			if (Position.MarketPosition == MarketPosition.Long)
			{
				if (High[0] > Position.AvgPrice + ((Target1+Target2+Target3)*TickSize))
					SetStopLoss("target1", CalculationMode.Price, Position.AvgPrice + ((Target1+Target2)*TickSize), false);
                                else if (High[0] > Position.AvgPrice + ((Target1+Target2)*TickSize))
					SetStopLoss("target1", CalculationMode.Price, Position.AvgPrice + (Target1*TickSize), false);
                                else if (High[0] > Position.AvgPrice + (Target1*TickSize))
					SetStopLoss("target1", CalculationMode.Price, Position.AvgPrice, false);
				
			}
                
			
		}

        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
protected override void OnBarUpdate()
{
EntryHandling = EntryHandling.UniqueEntries;

ManageOrders();

if (CrossBelow(CCI(14), 250, 1))
EnterLong();
}

        //#region Properties
		[Description("")]
        [Category("Parameters")]
        public int Target1
        {
            get { return target1; }
            set { target1 = Math.Max(1, value); }
        }
		[Description("")]
        [Category("Parameters")]
        public int Target2
        {
            get { return target2; }
            set { target2 = Math.Max(1, value); }
        }
		[Description("")]
        [Category("Parameters")]
        public int Target3
        {
            get { return target3; }
            set { target3 = Math.Max(1, value); }
        }
		[Description("")]
        [Category("Parameters")]
        public int Stop
        {
            get { return stop; }
            set { stop = Math.Max(1, value); }
        }
		[Description("")]
        [Category("Parameters")]
        public bool BE2
        {
            get { return be2; }
            set { be2 = value; }
        }
		[Description("")]
        [Category("Parameters")]
        public bool BE3
        {
            get { return be3; }
            set { be3 = value; }
        }
		
		
        //#endregion
    }
}

Reply With Quote
The following user says Thank You to calhawk01 for this post:
 
(login for full post details)
  #46 (permalink)
baltimore marylnd
 
Experience: Beginner
Platform: ninja
Trading: es
 
Posts: 91 since May 2013
Thanks: 5 given, 11 received

Alright i'm going to give up on this unless someone helps me lol

I've tried to play around with this for the past several days but it does not seem to work properly

Current code I have, does not seem to call the "manage order();" properly.

It enters the positions, but the stoploss only changes ONCE when target1 is hit, the stoploss moves to avg price, but when target2 is hit, stoploss is never moved to target1 and so on..

Thanks in advance for looking at the code:

 
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>
    /// Big Mike - 1/26/10 - https://futures.io/beginners-introductions-tutorials/2512-video-tutorial-how-create-advanced-ninjatrader-strategy.html
    /// </summary>
    [Description("Make me millions Jan 27 2010")]
    public class TargetES930 : Strategy
    {
        //#region Variables
		
		private int		target1			= 12;
		private int		target2			= 10;
		private int		target3			= 24;
		
		private int		stop			= 12;
		
		private bool	be2				= true;
		private bool	be3				= true;
		
		
		//#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;
			EntryHandling		= EntryHandling.UniqueEntries;
			
		}
		
		private void GoLong()
		{
			SetStopLoss("target1", CalculationMode.Price, Close[0] - (Stop*TickSize), false);
			SetProfitTarget("target1", CalculationMode.Price, Close[0] + (Target2*TickSize));	
		}

		private void ManageOrders()
		{

			if (Position.MarketPosition == MarketPosition.Long)
			{
				if (High[0] > Position.AvgPrice + (Target1*TickSize))
					SetStopLoss("target1", CalculationMode.Price, Position.AvgPrice, false);
				if (High[0] > Position.AvgPrice + ((Target1+Target2)*TickSize))
					SetStopLoss("target2", CalculationMode.Price, Position.AvgPrice + (Target1*TickSize), false);
				if (High[0] > Position.AvgPrice + ((Target1+Target2+Target3)*TickSize))
					SetStopLoss("target3", CalculationMode.Price, Position.AvgPrice + ((Target1+Target2)*TickSize), false);
			
			}
                
			
		}

        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {

            EntryHandling		= EntryHandling.UniqueEntries;
			
			GoLong();
			ManageOrders();
			
			if (Position.MarketPosition != MarketPosition.Flat) return;
			
			if (........ENTER ANY TRADING LOGIC.................))
				EnterLong("target1");
				GoLong();
			if (ToTime(Time[0]) == ToTime(15, 56, 0))
				ExitLong("", "");			
        }

        //#region Properties
		[Description("")]
        [Category("Parameters")]
        public int Target1
        {
            get { return target1; }
            set { target1 = Math.Max(1, value); }
        }
		[Description("")]
        [Category("Parameters")]
        public int Target2
        {
            get { return target2; }
            set { target2 = Math.Max(1, value); }
        }
		[Description("")]
        [Category("Parameters")]
        public int Target3
        {
            get { return target3; }
            set { target3 = Math.Max(1, value); }
        }
		[Description("")]
        [Category("Parameters")]
        public int Stop
        {
            get { return stop; }
            set { stop = Math.Max(1, value); }
        }
		[Description("")]
        [Category("Parameters")]
        public bool BE2
        {
            get { return be2; }
            set { be2 = value; }
        }
		[Description("")]
        [Category("Parameters")]
        public bool BE3
        {
            get { return be3; }
            set { be3 = value; }
        }
		
		
        //#endregion
    }
}

Reply With Quote
 
(login for full post details)
  #47 (permalink)
Crete, IL/USA
 
Experience: Intermediate
Platform: NinjaTrader, Mt4
Broker: Tradestation/Tradestation, NinjaTrader, FXCM and Tallinex
Trading: ES, CL, EUR/USD, TF
 
spinnybobo's Avatar
 
Posts: 181 since Aug 2009
Thanks: 103 given, 56 received


calhawk01 View Post
Alright i'm going to give up on this unless someone helps me lol

I've tried to play around with this for the past several days but it does not seem to work properly

Current code I have, does not seem to call the "manage order();" properly.

It enters the positions, but the stoploss only changes ONCE when target1 is hit, the stoploss moves to avg price, but when target2 is hit, stoploss is never moved to target1 and so on..

Thanks in advance for looking at the code:

 
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>
    /// Big Mike - 1/26/10 - https://futures.io/beginners-introductions-tutorials/2512-video-tutorial-how-create-advanced-ninjatrader-strategy.html
    /// </summary>
    [Description("Make me millions Jan 27 2010")]
    public class TargetES930 : Strategy
    {
        //#region Variables
		
		private int		target1			= 12;
		private int		target2			= 10;
		private int		target3			= 24;
		
		private int		stop			= 12;
		
		private bool	be2				= true;
		private bool	be3				= true;
		
		
		//#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;
			EntryHandling		= EntryHandling.UniqueEntries;
			
		}
		
		private void GoLong()
		{
			SetStopLoss("target1", CalculationMode.Price, Close[0] - (Stop*TickSize), false);
			SetProfitTarget("target1", CalculationMode.Price, Close[0] + (Target2*TickSize));	
		}

		private void ManageOrders()
		{

			if (Position.MarketPosition == MarketPosition.Long)
			{
				if (High[0] > Position.AvgPrice + (Target1*TickSize))
					SetStopLoss("target1", CalculationMode.Price, Position.AvgPrice, false);
				if (High[0] > Position.AvgPrice + ((Target1+Target2)*TickSize))
					SetStopLoss("target2", CalculationMode.Price, Position.AvgPrice + (Target1*TickSize), false);
				if (High[0] > Position.AvgPrice + ((Target1+Target2+Target3)*TickSize))
					SetStopLoss("target3", CalculationMode.Price, Position.AvgPrice + ((Target1+Target2)*TickSize), false);
			
			}
                
			
		}

        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {

            EntryHandling		= EntryHandling.UniqueEntries;
			
			GoLong();
			ManageOrders();
			
			if (Position.MarketPosition != MarketPosition.Flat) return;
			
			if (........ENTER ANY TRADING LOGIC.................))
				EnterLong("target1");
				GoLong();
			if (ToTime(Time[0]) == ToTime(15, 56, 0))
				ExitLong("", "");			
        }

        //#region Properties
		[Description("")]
        [Category("Parameters")]
        public int Target1
        {
            get { return target1; }
            set { target1 = Math.Max(1, value); }
        }
		[Description("")]
        [Category("Parameters")]
        public int Target2
        {
            get { return target2; }
            set { target2 = Math.Max(1, value); }
        }
		[Description("")]
        [Category("Parameters")]
        public int Target3
        {
            get { return target3; }
            set { target3 = Math.Max(1, value); }
        }
		[Description("")]
        [Category("Parameters")]
        public int Stop
        {
            get { return stop; }
            set { stop = Math.Max(1, value); }
        }
		[Description("")]
        [Category("Parameters")]
        public bool BE2
        {
            get { return be2; }
            set { be2 = value; }
        }
		[Description("")]
        [Category("Parameters")]
        public bool BE3
        {
            get { return be3; }
            set { be3 = value; }
        }
		
		
        //#endregion
    }
}

Hey Calhawk01

it looks like in the method GoLong() you only have SetProfitTarget for "target1" and did not set it for "target2 and "target3. If you don't set them, then they don't exist.
or you are setting them the manageorders and bugs are then happening. Make sure when you first GoLong, you have Stops and all Profit Targets set. then when they are set, you can properly call manageorders. otherwise weird things happen and hard to trace. make a step by step list, then program it that way. It will take care of most of your problems.

actually, now that I look at it, if you are using 3 positions you need to call SetStopLoss 3 times.
here is the code you should start with as a template. It has been stripped down. If not using the optimizer, then you don't need B/E, if you are not going short, then you don't need short logic.

 
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>
    /// Big Mike - 1/26/10 - https://futures.io/beginners-introductions-tutorials/2512-video-tutorial-how-create-advanced-ninjatrader-strategy.html
    /// </summary>
    [Description("Make me millions Jan 27 2010")] //modified by Spencer Davis as a simple template 05/30/2013.  
    public class MyMoneyMaker_BigMike : Strategy
    {
        
		
		private int		target1			= 12;
		private int		target2			= 10;
		private int		target3			= 24;
		private int		stop			= 12;
		
		
		
        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 ManageOrders()
		{
			if (Position.MarketPosition == MarketPosition.Long)
			{
				//Enter Trade management logic here for stop tightening
			}
		}

        protected override void OnBarUpdate()
        {
            EntryHandling		= EntryHandling.UniqueEntries;
			
			ManageOrders();
			
			if (Position.MarketPosition != MarketPosition.Flat) return;
			
			//if ( enter reason for going long here ) 
			//	GoLong();
			
        }

		[Description("")]
        [Category("Parameters")]
        public int Target1
        {
            get { return target1; }
            set { target1 = Math.Max(1, value); }
        }
		[Description("")]
        [Category("Parameters")]
        public int Target2
        {
            get { return target2; }
            set { target2 = Math.Max(1, value); }
        }
		[Description("")]
        [Category("Parameters")]
        public int Target3
        {
            get { return target3; }
            set { target3 = Math.Max(1, value); }
        }
		[Description("")]
        [Category("Parameters")]
        public int Stop
        {
            get { return stop; }
            set { stop = Math.Max(1, value); }
        }
    }
}
hope this helps.
can you please describe your management strategy?

Spencer

Follow me on Twitter Visit my Facebook Reply With Quote
 
(login for full post details)
  #48 (permalink)
baltimore marylnd
 
Experience: Beginner
Platform: ninja
Trading: es
 
Posts: 91 since May 2013
Thanks: 5 given, 11 received


spinnybobo View Post
Hey Calhawk01

it looks like in the method GoLong() you only have SetProfitTarget for "target1" and did not set it for "target2 and "target3. If you don't set them, then they don't exist.
or you are setting them the manageorders and bugs are then happening. Make sure when you first GoLong, you have Stops and all Profit Targets set. then when they are set, you can properly call manageorders. otherwise weird things happen and hard to trace. make a step by step list, then program it that way. It will take care of most of your problems.

actually, now that I look at it, if you are using 3 positions you need to call SetStopLoss 3 times.
here is the code you should start with as a template. It has been stripped down. If not using the optimizer, then you don't need B/E, if you are not going short, then you don't need short logic.

 
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>
    /// Big Mike - 1/26/10 - https://futures.io/beginners-introductions-tutorials/2512-video-tutorial-how-create-advanced-ninjatrader-strategy.html
    /// </summary>
    [Description("Make me millions Jan 27 2010")] //modified by Spencer Davis as a simple template 05/30/2013.  
    public class MyMoneyMaker_BigMike : Strategy
    {
        
		
		private int		target1			= 12;
		private int		target2			= 10;
		private int		target3			= 24;
		private int		stop			= 12;
		
		
		
        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 ManageOrders()
		{
			if (Position.MarketPosition == MarketPosition.Long)
			{
				//Enter Trade management logic here for stop tightening
			}
		}

        protected override void OnBarUpdate()
        {
            EntryHandling		= EntryHandling.UniqueEntries;
			
			ManageOrders();
			
			if (Position.MarketPosition != MarketPosition.Flat) return;
			
			//if ( enter reason for going long here ) 
			//	GoLong();
			
        }

		[Description("")]
        [Category("Parameters")]
        public int Target1
        {
            get { return target1; }
            set { target1 = Math.Max(1, value); }
        }
		[Description("")]
        [Category("Parameters")]
        public int Target2
        {
            get { return target2; }
            set { target2 = Math.Max(1, value); }
        }
		[Description("")]
        [Category("Parameters")]
        public int Target3
        {
            get { return target3; }
            set { target3 = Math.Max(1, value); }
        }
		[Description("")]
        [Category("Parameters")]
        public int Stop
        {
            get { return stop; }
            set { stop = Math.Max(1, value); }
        }
    }
}
hope this helps.
can you please describe your management strategy?

Spencer

Hello Spencer, the above code enters three different positions. that's what i'm trying to change. the original code posted by bigmike also enters three different positions. I want to enter one position (one contract) with three different profit targets and stoplosses that move once profit targets are hit. initial stoploss 12 ticks, if target1 is hit then stoploss= avg price, if target2 is hit, then stoploss=target1, if target3 is hit, then stoploss= target2

 
Code
		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");
			
		}
the above creates three different positions/ buys three contracts

i figured the below would set the stoploss and target1 when the trade is first entered, but once target1 is hit, ManageOrder() would kick in and move the stoplosses. Ya know?

 
Code

 
Code
		private void GoLong()
		{
			SetStopLoss("target1", CalculationMode.Price, Close[0] - (Stop*TickSize), false);

			
			SetProfitTarget("target1", CalculationMode.Price, Close[0] + (Target1*TickSize));

			
			EnterLong("target1");

			
		}

Reply With Quote
 
(login for full post details)
  #49 (permalink)
baltimore marylnd
 
Experience: Beginner
Platform: ninja
Trading: es
 
Posts: 91 since May 2013
Thanks: 5 given, 11 received

anyone? maybe I can paypal you some cash?? i really dont feel like dropping hundreds of dollars to one of the indicator creating companies! b/c i feel like i already have the correct code.. just needs some additional coding or move some parts around. need:

buy ONE contract when trade logic becomes true with the following trade management settings: initial stoploss 12 ticks, if target1 is hit then stoploss= avg price, if target2 is hit, then stoploss=target1, if target3 is hit, then stoploss= target2

thank you in advance for taking the time

Reply With Quote
 
(login for full post details)
  #50 (permalink)
Crete, IL/USA
 
Experience: Intermediate
Platform: NinjaTrader, Mt4
Broker: Tradestation/Tradestation, NinjaTrader, FXCM and Tallinex
Trading: ES, CL, EUR/USD, TF
 
spinnybobo's Avatar
 
Posts: 181 since Aug 2009
Thanks: 103 given, 56 received


Hey,

Ok, I know what you mean now. So, I think in order to access the entry price, you need to create an IOrder object.

 
Code
private IOrder _entry = null;

//then you can do this
_entry = EnterLong("tag you want to give");

//then later you can do this
if (High[0] >= (target1 * TickSize))
SetStopLoss(CaculationMode.Price, (_entry.AvgFillPrice);
I did not try it out yet. Look at Scott Shodsons Ninja Trader webinars. He uses IOrders a lot.
He has 2 webinars. They are very good.

 
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>
    /// Big Mike - 1/26/10 - https://futures.io/beginners-introductions-tutorials/2512-video-tutorial-how-create-advanced-ninjatrader-strategy.html
    /// </summary>
    [Description("Make me millions Jan 27 2010")] //modified by Spencer Davis as a simple template 05/30/2013.  
    public class MyMoneyMaker_BigMike : Strategy
    {
        
		
		private int		_target1			= 12;
		private int		_target2			= 10;
		private int		_target3			= 24;
		private int		_stop			= 12;
		
		private IOrder _entry = null;
		
        protected override void Initialize()
        {
            CalculateOnBarClose = true;
			EntryHandling		= EntryHandling.UniqueEntries;
        }
		
		private void GoLong()
		{
			SetStopLoss("Enter_Position", CalculationMode.Price, Close[0] - (Stop*TickSize), false);

			_entry = EnterLong("Enter_Position");
		}
		
		private void ManageOrders()
		{
			if (Position.MarketPosition == MarketPosition.Long)
			{
				if(High[0] >= (_target1 * TickSize))
					SetStopLoss(CalculationMode.Price, _entry.AvgFillPrice);
				else if (High[0] >= (_target2 * TickSize))
					SetStopLoss(CalculationMode.Price, (_entry.AvgFillPrice + (_target1 * TickSize)));
				else if (High[0] >= (_target3 * TickSize))
					SetStopLoss(CalculationMode.Price, (_entry.AvgFillPrice + (_target2 * TickSize)));
			}
		}

        protected override void OnBarUpdate()
        {
            EntryHandling		= EntryHandling.UniqueEntries;
			ManageOrders();
			
			if (Position.MarketPosition != MarketPosition.Flat) return;
			
			//if ( enter reason for going long here ) 
			//	GoLong();
			
        }

		[Description("")]
        [Category("Parameters")]
        public int Target1
        {
            get { return _target1; }
            set { _target1 = Math.Max(1, value); }
        }
		[Description("")]
        [Category("Parameters")]
        public int Target2
        {
            get { return _target2; }
            set { _target2 = Math.Max(1, value); }
        }
		[Description("")]
        [Category("Parameters")]
        public int Target3
        {
            get { return _target3; }
            set { _target3 = Math.Max(1, value); }
        }
		[Description("")]
        [Category("Parameters")]
        public int Stop
        {
            get { return _stop; }
            set { _stop = Math.Max(1, value); }
        }
    }
}
hope this helps

I will code something later and test it.
Spencer

Follow me on Twitter Visit my Facebook Reply With Quote


futures io Trading Community Platforms and Indicators NinjaTrader > VIDEO TUTORIAL: How to create an advanced NinjaTrader Strategy


January 26, 2020


Upcoming Webinars and Events
 

An Afternoon With futures io member TropicalTrader

Elite only
     



Copyright © 2020 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, +507 833-9432, info@futures.io
All information is for educational use only and is not investment advice.
There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
no new posts