NexusFi: Find Your Edge


Home Menu

 





VIDEO TUTORIAL: How to create an advanced NinjaTrader Strategy


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one calhawk01 with 10 posts (1 thanks)
    2. looks_two Big Mike with 9 posts (193 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.4 thanks per post
    2. looks_two piersh with 8 thanks per post
    3. looks_3 zeller4 with 7 thanks per post
    4. looks_4 Cloudy with 1.5 thanks per post
    1. trending_up 85,359 views
    2. thumb_up 231 thanks given
    3. group 61 followers
    1. forum 67 posts
    2. attach_file 3 attachments




 
Search this Thread

VIDEO TUTORIAL: How to create an advanced NinjaTrader Strategy

  #41 (permalink)
 
NJAMC's Avatar
 NJAMC 
Atkinson, NH USA
Market Wizard
 
Experience: Intermediate
Platform: NinjaTrader 8/TensorFlow
Broker: NinjaTrader Brokerage
Trading: Futures, CL, ES, ZB
Posts: 1,970 since Dec 2010
Thanks Given: 3,037
Thanks Received: 2,394


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 NexusFi Trade Journal Reply With Quote

Can you help answer these questions
from other members on NexusFi?
My NT8 Volume Profile Split by Asian/Euro/Open
NinjaTrader
Request for MACD with option to use different MAs for fa …
NinjaTrader
ZombieSqueeze
Platforms and Indicators
NexusFi Journal Challenge - April 2024
Feedback and Announcements
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Retail Trading As An Industry
62 thanks
NexusFi site changelog and issues/problem reporting
47 thanks
Battlestations: Show us your trading desks!
35 thanks
GFIs1 1 DAX trade per day journal
32 thanks
What percentage per day is possible? [Poll]
31 thanks

  #42 (permalink)
 calhawk01 
baltimore marylnd
 
Experience: Beginner
Platform: ninja
Trading: es
Posts: 91 since May 2013
Thanks Given: 5
Thanks Received: 11


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
  #43 (permalink)
 calhawk01 
baltimore marylnd
 
Experience: Beginner
Platform: ninja
Trading: es
Posts: 91 since May 2013
Thanks Given: 5
Thanks Received: 11


 
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
  #44 (permalink)
 Cloudy 
desert CA
 
Experience: Intermediate
Platform: NT7, various
Broker: various, TDA
Trading: NQ,ES
Posts: 2,124 since Jul 2011
Thanks Given: 2,396
Thanks Received: 1,748

 
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 NexusFi Trade Journal Reply With Quote
  #45 (permalink)
 calhawk01 
baltimore marylnd
 
Experience: Beginner
Platform: ninja
Trading: es
Posts: 91 since May 2013
Thanks Given: 5
Thanks Received: 11

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://nexusfi.com/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:
  #46 (permalink)
 calhawk01 
baltimore marylnd
 
Experience: Beginner
Platform: ninja
Trading: es
Posts: 91 since May 2013
Thanks Given: 5
Thanks Received: 11

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://nexusfi.com/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
  #47 (permalink)
 
spinnybobo's Avatar
 spinnybobo 
Crete, IL/USA
 
Experience: Intermediate
Platform: NinjaTrader, Mt4
Broker: Tradestation/Tradestation, NinjaTrader, FXCM and Tallinex
Trading: ES, CL, EUR/USD, TF
Posts: 173 since Aug 2009
Thanks Given: 105
Thanks Received: 61


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://nexusfi.com/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://nexusfi.com/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 Reply With Quote
  #48 (permalink)
 calhawk01 
baltimore marylnd
 
Experience: Beginner
Platform: ninja
Trading: es
Posts: 91 since May 2013
Thanks Given: 5
Thanks Received: 11


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://nexusfi.com/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
  #49 (permalink)
 calhawk01 
baltimore marylnd
 
Experience: Beginner
Platform: ninja
Trading: es
Posts: 91 since May 2013
Thanks Given: 5
Thanks Received: 11

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
  #50 (permalink)
 
spinnybobo's Avatar
 spinnybobo 
Crete, IL/USA
 
Experience: Intermediate
Platform: NinjaTrader, Mt4
Broker: Tradestation/Tradestation, NinjaTrader, FXCM and Tallinex
Trading: ES, CL, EUR/USD, TF
Posts: 173 since Aug 2009
Thanks Given: 105
Thanks Received: 61


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://nexusfi.com/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 Reply With Quote





Last Updated on October 23, 2020


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
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.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts