Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
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 for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
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 :/
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.
Cloudy, thank you for trying. Still no luck though :/
here is the complete code to the strategy:
#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
}
}
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:
#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
}
}
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
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:
#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.
#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?
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.
#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
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?
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
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.
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.
#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); }
}
}
}