I want to create the same trailing stop for both of my entries from my scaling out long positions "Long1" and "Long2". Instead of specifying a tick size, I want the value of my trailing stop to be something like (PriorDayOHLC().PriorHigh[0] - PriorDayOHLC().PriorLow[0]). How can I create a trailing stop that calls upon a value from another indicator?
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Custom Tick Sizes for TrailStop
Collapse
X
-
Custom Tick Sizes for TrailStop
I'm pretty new to coding, but I'm trying to teach myself by reading topics here and watching educational videos. I tried searching for my problem but I wasn't successful.
I want to create the same trailing stop for both of my entries from my scaling out long positions "Long1" and "Long2". Instead of specifying a tick size, I want the value of my trailing stop to be something like (PriorDayOHLC().PriorHigh[0] - PriorDayOHLC().PriorLow[0]). How can I create a trailing stop that calls upon a value from another indicator?Tags: None
-
Hi IFT20,
Thank you for posting and welcome to the Forum!
Unfortunately, you can only use Tick or Percentage for the trailing stop.
You can create a StopLoss and dynamically change the value of the StopLoss via the code.
Here is a reference sample that has sample code of how to achieve this.
http://www.ninjatrader.com/support/f...ead.php?t=3222
Let me know if I can be of further assistance.Cal H.NinjaTrader Customer Service
-
Thanks for the quick reply Cal!
Just to clarify, it would not be possible to assign something for the value of (PriorDayOHLC().PriorHigh[0] - PriorDayOHLC().PriorLow[0]) and use it as a tick size for the trailing stop?
In the meantime, I'll try to work with the SetStopLoss and see if I can achieve the desired results. Thanks for the help!
Comment
-
IFT20,
You can try using that input as the value of the trailing stop. It should work since they are both a double value.
I would use this overload method however...
Code:SetTrailStop(CalculationMode.Ticks, PriorDayOHLC().PriorHigh[0] - PriorDayOHLC().PriorLow[0]);
Cal H.NinjaTrader Customer Service
Comment
-
I'm trying to implement this trailing stop to my strategy, but I'm running into a problem. The code compiles fine, but it won't load in the strategy analyzer. The output window tells me it has "Failed to call method 'Initialize' for strategy "Strategy1"... Object reference not set to an instance of an object."
The code I put under Initialize() is
Code:SetTrailStop("Long1a", CalculationMode.Ticks, (PriorDayOHLC().PriorHigh[0] - PriorDayOHLC().PriorLow[0]), false);
Comment
-
IFT20,
You will need to use this code in the OnBarUpdate method since there is no Bar data being loaded in the initialize and therefore causes the error.
You can dynamically have it being placed in the OnBarUpdate, just ensure that you reset the values when you are back to a Flat position.Cal H.NinjaTrader Customer Service
Comment
-
I've moved it under the OnBarUpdate, and I'm trying to reset the values when I am flat, but I'm messing something up and the trailing stops are not working.
Code:protected override void OnBarUpdate() { if (Position.MarketPosition == MarketPosition.Flat) { SetTrailStop("Long1b", CalculationMode.Ticks, 5, false); SetTrailStop("Short1b", CalculationMode.Ticks, 5, false); } else if (Position.MarketPosition == MarketPosition.Long) { if (Close[0] >= 60) { SetTrailStop("Long1b", CalculationMode.Ticks, (PriorDayOHLC().PriorHigh[0] - PriorDayOHLC().PriorLow[0]), false); } } else if (Position.MarketPosition == MarketPosition.Short) { if (Close[0] <= 30) { SetTrailStop("Short1b", CalculationMode.Ticks, (PriorDayOHLC().PriorHigh[0] - PriorDayOHLC().PriorLow[0]), false); } }
Comment
-
IFT20,
In your code, while that would work, however you the trail stop is being reset on each bar update so long as those conditions are true.
You can use the a switch variable that when the SetTrailStop his set it switches the value so you don't have the constant reset
E.G.
Code:protected override void OnBarUpdate() { double switchset = 0; if (Position.MarketPosition == MarketPosition.Flat && switchset == 0) { SetTrailStop("Long1b", CalculationMode.Ticks, 5, false); SetTrailStop("Short1b", CalculationMode.Ticks, 5, false); switchset = 1; } else if (Position.MarketPosition == MarketPosition.Long && switchset == 1) { if (Close[0] >= 60) { SetTrailStop("Long1b", CalculationMode.Ticks, (PriorDayOHLC().PriorHigh[0] - PriorDayOHLC().PriorLow[0]), false); switchset = 0; } } else if (Position.MarketPosition == MarketPosition.Short && switchset == 1) { if (Close[0] <= 30) { SetTrailStop("Short1b", CalculationMode.Ticks, (PriorDayOHLC().PriorHigh[0] - PriorDayOHLC().PriorLow[0]), false); switchset = 0; } }
Cal H.NinjaTrader Customer Service
Comment
-
Thank you so much Cal. Like I said, I am new to coding and you've been really helpful. I understand how you used the setswitch and it is a useful tool, but I want to go back to my original inquiry.
I oversimplified my code in my previous post to state that the original trailstop would be 5 ticks, but in actuality I want it to be (PriorDayOHLC().PriorHigh[0] - PriorDayOHLC().PriorLow[0]) ticks.
Code:SetTrailStop("Long1b", CalculationMode.Ticks, (PriorDayOHLC().PriorHigh[0] - PriorDayOHLC().PriorLow[0]), false);
Last edited by IFT20; 08-22-2013, 05:00 PM.
Comment
-
IFT20,
The trail stop for NinjaScript will work the second you have favor in your market.
While this is similar to the ATM trail stop, there are some differences such as; there is no profit trigger, meaning the trail stop will start trailing your market the second there is a tick in your favor and will continue to trail each tick in your favor.
When the market goes against then the trailing stops.
You may see this in backtest results where the stop loss is the same as your entry price cause the market went in favor and then got stopped out later on.
If these are the not the results you are seeing, would you be willing to share your strategy and any screen shots?Cal H.NinjaTrader Customer Service
Comment
-
My positions are stopping out immediately at the entry point, regardless of favor in the market (or any price movement at all).
Code:protected override void Initialize() { EntriesPerDirection = 1; EntryHandling = EntryHandling.UniqueEntries; CalculateOnBarClose = false; } protected override void OnBarUpdate() { SetTrailStop("Long1b", CalculationMode.Ticks, (PriorDayOHLC().PriorHigh[0] - PriorDayOHLC().PriorLow[0]), false); SetProfitTarget("Long1a", CalculationMode.Ticks, 20); SetStopLoss("Long1a", CalculationMode.Ticks, 20, false); if (CrossAbove(EMA(7), EMA(15), 1)) { EnterLong(60, "Long1a"); EnterLong(40, "Long1b"); } } #region Properties [Description("")] [GridCategory("Parameters")] public int MyInput0 { get { return myInput0; } set { myInput0 = Math.Max(1, value); } } #endregion } }
Last edited by IFT20; 08-23-2013, 08:11 AM.
Comment
-
IFT20,
Can you print out the values of the trail stop that you are trying to place?
Insert this code line after the setprofit target -
Code:Print(PriorDayOHLC().PriorHigh[0] - PriorDayOHLC().PriorLow[0]);
When the values print, are they the values you want?Cal H.NinjaTrader Customer Service
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by Davidtowleii, Today, 12:15 AM
|
0 responses
3 views
0 likes
|
Last Post
by Davidtowleii
Today, 12:15 AM
|
||
Started by guillembm, Yesterday, 11:25 AM
|
2 responses
9 views
0 likes
|
Last Post
by guillembm
Yesterday, 11:58 PM
|
||
Started by junkone, 04-21-2024, 07:17 AM
|
9 responses
68 views
0 likes
|
Last Post
by jeronymite
Yesterday, 11:51 PM
|
||
Started by trilliantrader, 04-18-2024, 08:16 AM
|
4 responses
20 views
0 likes
|
Last Post Yesterday, 10:33 PM | ||
Started by mgco4you, Yesterday, 09:46 PM
|
1 response
12 views
0 likes
|
Last Post Yesterday, 09:56 PM |
Comment