![]() |
|
|||||||
| Strategy Development Support for the development of custom automated trading strategies using NinjaScript. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Member
Join Date: Aug 2008
Posts: 48
Thanks: 0
Thanked 0 times in 0 posts
|
Within a strategy I am working on, I use a trailing stop, let's say %2. I would like to try to change my strategy so that when this %2 stop is reached, I don't exit right then, but instead wait for a signal from another indicator. I would do this by having a flag change from false to true, and then work the indicator for an exit within that true condition.
One way I could do this would be with a custom indicator that performs the same trailing stop calculation but can change a flag value from False to True. Is there any other way? Any suggestions on making such a custom indicator? |
|
|
|
|
|
#2 |
|
NinjaTrader Customer Service
Join Date: May 2008
Location: Denver, CO
Posts: 3,157
Thanks: 0
Thanked 3 times in 3 posts
|
Hello,
You do not need a new indicator. You can do something like this: if(Position.GetProfitLoss(Close[0], PerformanceUnit.Percent) == some_percentage_here && Position.MarketPosition != MarketPosition.Flat) { has_reached_percentage = true; } Now you can use that true setting wherever you want within your code. You may want to also reset the has_reached_percentage like this: if(Position.MarketPosition == MarketPosition.Flat) { has_reached_percentage = false; } Here are some links that will help: http://www.ninjatrader-support.com/H...tPosition.html http://www.ninjatrader-support.com/H...rofitLoss.html
Ben
NinjaTrader Customer Service |
|
|
|
|
|
#3 |
|
Member
Join Date: Aug 2008
Posts: 48
Thanks: 0
Thanked 0 times in 0 posts
|
Ben I will look at those links. To be clear here, in your example code, could I use that as a trailing stop or is that just a %2 stop from the initial market position? I am a not so good coder yet.
And, currently as a form of position sizing, when I actually run a strategy live, I use "Order Properties, Account Size $10000". I think since I will be changing that account size amount depending on the stock, I would want to be coding with percentage changes and never with open profit and loss? |
|
|
|
|
|
#4 |
|
NinjaTrader Customer Service
Join Date: May 2008
Location: Denver, CO
Posts: 3,157
Thanks: 0
Thanked 3 times in 3 posts
|
Hello,
What I provided is just a condition and true/false toggle you could use to determine if your order has reached the % PnL you want. You will need to then add code to then determine if you want to actually exit the trade or some other action. It sounds like you will need to code your own logic to determine your trail stop.
Ben
NinjaTrader Customer Service |
|
|
|
|
|
#5 |
|
Member
Join Date: Aug 2008
Posts: 48
Thanks: 0
Thanked 0 times in 0 posts
|
Sorry to be vague here. What I am trying to get is how as a trade moves in my favor (let's say a short) the stop will get lower and lower. If the price reverses and then hits that new lower stop, I then want to have a flag be true (which I can code now from your previous help).
Can I have the actual trailing stop code that is used by for instance strategy generator? For instance if I could duplicate this code within my strategy as an onbarevent instead of calling it I would be all set. SetTrailStop("", CalculationMode.Percent, TrailingStop, true); |
|
|
|
|
|
#6 |
|
NinjaTrader Customer Service
Join Date: May 2008
Location: Denver, CO
Posts: 3,157
Thanks: 0
Thanked 3 times in 3 posts
|
Hello,
You will have to code it out yourself, sorry. This is not available.
Ben
NinjaTrader Customer Service |
|
|
|
|
|
#7 |
|
Member
Join Date: Aug 2008
Posts: 48
Thanks: 0
Thanked 0 times in 0 posts
|
So the code for this
SetTrailStop is not available? |
|
|
|
|
|
#8 |
|
NinjaTrader Customer Service
Join Date: May 2008
Location: Denver, CO
Posts: 3,157
Thanks: 0
Thanked 3 times in 3 posts
|
Hello,
That is correct. You will need to code it out yourself.
Ben
NinjaTrader Customer Service |
|
|
|
|
|
#9 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
|
sauer11155,
Here is some food for thought. Not tested, but it should help get you kicked off in how to code it yourself. Code:
// In Variables section
private double trailStop = 0;
private double prevValue = 0;
private bool justEntered = false;
// In OnBarUpdate() method
if (Close[0] > Open[0] && Position.MarketPosition == MarketPosition.Flat)
{
EnterLong();
justEntered = true;
}
if (Position.MarketPosition == MarketPosition.Long && justEntered == true)
{
trailStop = 0.98 * Position.AvgPrice;
prevValue = Position.AvgPrice;
justEntered = false;
Print("TRAIL STOP TRACKING: " + trailStop);
}
if (Position.MarketPosition == MarketPosition.Long)
{
if (Close[0] > Position.AvgPrice && Close[0] > prevValue)
{
trailStop = trailStop + (Close[0] - prevValue);
prevValue = Close[0];
Print("TRAIL STOP RAISED: " + trailStop + " " + prevValue);
}
Print(Time[0] + " " + Close[0] + " " + prevValue);
}
if (Close[0] <= trailStop)
{
Print("TRAIL STOP HIT: " + trailStop + " " + Close[0]);
// Trailing stop has been hit; do whatever you want here
trailStop = 0;
prevValue = 0;
}
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#10 |
|
Member
Join Date: Aug 2008
Posts: 48
Thanks: 0
Thanked 0 times in 0 posts
|
Thanks dude, I was overwhelmed lol. With what you gave me I have all weekend busy now, this should advance my coding some more. I gotta say I am getting far with this and it's been 1 month and 2 weeks.
|
|
|
|
|
|
#11 |
|
Member
Join Date: Aug 2008
Posts: 48
Thanks: 0
Thanked 0 times in 0 posts
|
I coded and have working a coded in onbarupdate trailing stop, thanks for the help in here. Here is the code, I will post this and another version in the scripts section:
#region Variables // Wizard generated variables private double trailStop = 0; // Default setting for TrailStop private double prevValue = 0; // Default setting for PrevValue private double trailStopPercentage = .02; private bool justEntered = false; // Default setting for JustEntered // User defined variables (add any user defined variables below) #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; } /// <summary> /// Called on each bar update event (incoming tick) /// </summary> protected override void OnBarUpdate() { if (Close[0] > Open[0] && Position.MarketPosition == MarketPosition.Flat) { EnterLong(); justEntered = true; Print(Time[0] + " Entered Long " + " symbol " + Instrument.FullName); } if (Position.MarketPosition == MarketPosition.Long && justEntered == true) { trailStop = Position.AvgPrice - (trailStopPercentage * Position.AvgPrice); prevValue = Position.AvgPrice; justEntered = false; Print(" TRAIL STOP TRACKING: " + trailStop + " symbol " + Instrument.FullName); } if (Position.MarketPosition == MarketPosition.Long) { if (High[0] > Position.AvgPrice && High[0] > prevValue) { trailStop = trailStop + (High[0] - prevValue); prevValue = High[0]; Print(" TRAIL STOP RAISED: " + trailStop + " PrevValue " + prevValue + " symbol " + Instrument.FullName); } Print(Time[0] + " High " + High[0] + " PrevValue " + prevValue + " symbol " + Instrument.FullName); } if (Low[0] <= trailStop) { Print(" TRAIL STOP HIT: " + trailStop + " " + Close[0] + " symbol " + Instrument.FullName); // Trailing stop has been hit; do whatever you want here ExitLong("",""); trailStop = 0; prevValue = 0; } } #region Properties [Description("")] [Category("Parameters")] public double TrailStop { get { return trailStop; } set { trailStop = Math.Max(0.00, value); } } [Description("")] [Category("Parameters")] public double PrevValue { get { return prevValue; } set { prevValue = Math.Max(0.00, value); } } [Description("")] [Category("Parameters")] public double TrailStopPercentage { get { return trailStopPercentage; } set { trailStopPercentage = Math.Max(0.00, value); } } [Description("")] [Category("Parameters")] public bool JustEntered { get { return justEntered; } set { justEntered = value; } } #endregion } } |
|
|
|
|
|
#12 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
|
Thanks for sharing sauer11155. I am sure people will benefit with this reference code.
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#13 | ||
|
Member
Join Date: Nov 2008
Location: Australia
Posts: 64
Thanks: 0
Thanked 0 times in 0 posts
|
sauer11155, thanks for your scripts.
Playing with StochasticCrosswTrailingStopExit raised some questions: 1. I thought that at the end of the code you forgot to reset TrailStop and PrevValue to 0 and JustEntered to false. So I changed Quote:
Quote:
But resetting JustEntered to false when MarketPosition == MarketPosition.Flat lowers my bcktesting results. Do you have any idea why adding JustEntered to false here would influence the result? I tried to follow the logic but can't see why. BTW Some minor details: 2. I think 2 of your variables are not used anywhere in the code 3. Do you have a reason to declare trailStop, prevValue, justEntered and exitFlag as PUBLIC in the properties ? Code:
{
#region Variables
// Wizard generated variables
private int periodD = 10; // Default setting for PeriodD
private int periodK = 20; // Default setting for PeriodK
private int smooth = 5; // Default setting for Smooth
private int topLine = 80; // Default setting for TopLine
private int bottomLine = 20; // Default setting for BottomLine
private int unrealizedPL = 1; // Default setting for UnrealizedPL
private bool aFlag1 = false; // Default setting for AFlag1
private double trailStop = 0; // Default setting for TrailStop
private double prevValue = 0; // Default setting for PrevValue
private double trailStopPercentage = .02;
private bool justEntered = false; // Default setting for JustEntered
private bool exitFlag = false;
private double safetyTrailingStop = 0.04; // Default setting forSafetyTrailingStop
private double profitTarget2 = 0.02; // Default setting for ProfitTarget2
// User defined variables (add any user defined variables below)
#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()
{
Add(Stochastics(PeriodD, PeriodK, Smooth));
Add(Stochastics(PeriodD, PeriodK, Smooth));
SetTrailStop("", CalculationMode.Percent, SafetyTrailingStop, true);
SetProfitTarget("", CalculationMode.Percent, ProfitTarget2);
CalculateOnBarClose = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Condition set 1
if (CrossAbove(Stochastics(PeriodD, PeriodK, Smooth).D, BottomLine, 1)
&& Position.MarketPosition == MarketPosition.Flat)
{
EnterLong(DefaultQuantity, "");
justEntered = true;
Print(Time[0] + " Entered Long " + " symbol " + Instrument.FullName);
}
if (Position.MarketPosition == MarketPosition.Long && justEntered == true)
{
trailStop = Position.AvgPrice - (trailStopPercentage * Position.AvgPrice);
prevValue = Position.AvgPrice;
justEntered = false;
Print(" TRAIL STOP TRACKING: " + trailStop + " symbol " + Instrument.FullName);
}
if (Position.MarketPosition == MarketPosition.Long)
{
if (High[0] > Position.AvgPrice && High[0] > prevValue)
{
trailStop = trailStop + (High[0] - prevValue);
prevValue = High[0];
Print(" TRAIL STOP RAISED: " + trailStop + " PrevValue " + prevValue + " symbol " + Instrument.FullName);
}
Print(Time[0] + " High " + High[0] + " PrevValue " + prevValue + " symbol " + Instrument.FullName);
}
if (Low[0] <= trailStop && (Position.MarketPosition == MarketPosition.Long))
{
Print(" TRAIL STOP HIT: " + trailStop + " " + Close[0] + " symbol " + Instrument.FullName);
// Trailing stop has been hit; do whatever you want here
DrawDot(Time[0].ToString() + "Buy", 0, Low[0] - TickSize, Color.Red);
ExitFlag = true;
}
if (ExitFlag == true)
{
if (CrossBelow(Stochastics(PeriodD, PeriodK, Smooth).D, TopLine, 1))
ExitLong("","");
}
if (Position.MarketPosition == MarketPosition.Flat)
{
ExitFlag = false;
}
}
#region Properties
[Description("")]
[Category("Parameters")]
public int PeriodD
{
get { return periodD; }
set { periodD = Math.Max(1, value); }
}
[Description("")]
[Category("Parameters")]
public int PeriodK
{
get { return periodK; }
set { periodK = Math.Max(1, value); }
}
[Description("")]
[Category("Parameters")]
public int Smooth
{
get { return smooth; }
set { smooth = Math.Max(1, value); }
}
[Description("")]
[Category("Parameters")]
public int TopLine
{
get { return topLine; }
set { topLine = Math.Max(1, value); }
}
[Description("")]
[Category("Parameters")]
public int BottomLine
{
get { return bottomLine; }
set { bottomLine = Math.Max(1, value); }
}
[Description("")]
[Category("Parameters")]
public int UnrealizedPL
{
get { return unrealizedPL; }
set { unrealizedPL = Math.Max(1, value); }
}
[Description("")]
[Category("Parameters")]
public bool AFlag1
{
get { return aFlag1; }
set { aFlag1 = value; }
}
[Description("")]
[Category("Parameters")]
public double TrailStop
{
get { return trailStop; }
set { trailStop = Math.Max(0.00, value); }
}
[Description("")]
[Category("Parameters")]
public double PrevValue
{
get { return prevValue; }
set { prevValue = Math.Max(0.00, value); }
}
[Description("")]
[Category("Parameters")]
public double TrailStopPercentage
{
get { return trailStopPercentage; }
set { trailStopPercentage = Math.Max(0.00, value); }
}
[Description("")]
[Category("Parameters")]
public bool JustEntered
{
get { return justEntered; }
set { justEntered = value; }
}
[Description("")]
[Category("Parameters")]
public bool ExitFlag
{
get { return exitFlag; }
set { exitFlag = value; }
}
[Description("")]
[Category("Parameters")]
public double SafetyTrailingStop
{
get { return safetyTrailingStop; }
set { safetyTrailingStop = Math.Max(0.00, value); }
}
[Description("")]
[Category("Parameters")]
public double ProfitTarget2
{
get { return profitTarget2; }
set { profitTarget2 = Math.Max(0.005, value); }
}
#endregion
}
|
||
|
|
|
|
|
#14 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
|
3. Variables are public in the Properties so you can access them and change them in the Strategy selection window. This effectively creates them as user defined parameters. As far as why flags are there I have not followed the code intimately so I would not know. Hope that helps.
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#15 |
|
Member
Join Date: Nov 2008
Location: Australia
Posts: 64
Thanks: 0
Thanked 0 times in 0 posts
|
Thanks Josh,
re 3: I should have formulated better. As far as I can see, these variables are set and overridden in the OnBarUpdate logic, so to me it does not make sense having them as Public and making the list in the optimizing windows larger then necessary. But I might miss something and it is there on purpose ... hence the question to Sauer. |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Going from a Stop Loss to a Trailing Stop within a strategy | Sidhartha | Strategy Development | 28 | 03-24-2009 03:11 PM |
| stop loss, trailing stop modification in NT6 | alfie | Strategy Development | 1 | 03-26-2008 03:46 PM |
| NT 6.5 - Trailing stop not trailing | PocoDiablo | Miscellaneous Support | 3 | 03-19-2008 01:46 PM |
| help with trailing stop | newguy05 | Strategy Development | 2 | 02-04-2008 10:54 PM |
| 3-1-1 trailing stop | daza | ATM Strategies (Discretionary Trading) | 6 | 10-28-2007 06:35 PM |