Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Pop up window
Collapse
X
-
Hi Bertrand,
Thanks for replays.
I am trying to use trailing stop or Stop loss which follow previous Bar Low or low 2 bars ago.
Is this code correct?
protectedoverridevoid Initialize()
{
/* The following settings configure your strategy to execute only one entry for each uniquely named entry signal.
This can be configured in the Initalize() method or via the Strategy dialog window when running or backtesting a strategy */
EntriesPerDirection = 1;
EntryHandling = EntryHandling.UniqueEntries;
CalculateOnBarClose = true;
/* These Set methods will place Profit Target and Trail Stop orders for our entry orders.
Notice how the Profit Target order is only tied to our order named 'Long 1a'. This is the crucial step in achieving the following behavior.
If the price never reaches our Profit Target, both long positions will be closed via our Trail Stop.
If the price does hit our Profit Target, half of our position will be closed leaving the remainder to be closed by our Trail Stop. */
SetProfitTarget("Long 1a", CalculationMode.Ticks, 10);
SetTrailStop(CalculationMode.Ticks, Low[1]);
}
I just made change in red color at the end: Low[1]
thanks
Peter
Comment
-
Peter,
Simulated = false means the stop is not resting locally on your PC until triggered.
Using the trailstop in mode ticks with a price value does not make sense - you would need to call SetStopLoss() in the OnBarUpdate() with your price value to trail at -
Comment
-
Thanks Bertrand,
Do you think this can work?
elseif (Position.MarketPosition == MarketPosition.Long)
{
// Once the price is greater than entry price+50 ticks, set stop loss to breakeven
if (Close[0] > Close[1])
{
SetStopLoss(CalculationMode.Price, Position.Low[0]);
}
}
Thanks
Peter
Comment
-
Thanks Bertrand,
Do ypu know about example or thread which is dealing with using Low[] High[] in SL and TS?
Do you think this can work?
elseif (Position.MarketPosition == MarketPosition.Long)
{
// Once the price is greater than entry price+50 ticks, set stop loss to breakeven
if (Close[0] > Close[1])
{
SetStopLoss(CalculationMode.Price, Position.Low[0]);
}
}
Thanks
Peter
Comment
-
Peter, please see the previous reference sample link I gave you for how to create a dynamic trailing stop by calling SetStopLoss again to adjust your value when you would like to. Position.Low would not be a valid property, you would just set it to Low[0] then perhaps minus an offset so it does not get stopped out immediately if the low is tested.
Comment
-
Thanks Bertrand,
I have example you mention in front of me.
What I am confused about is " Position.AvgPrice".
When I check help I can see that I can declare
SetStopLoss(CalculationMode mode,double value)
In SamplePriceModification we use "Position.AvgPrice" .
I would like to use Low[1]. Do I insert just Low[1] or "Position.Low[1]"
Then whole statement would look like this
elseif (Position.MarketPosition == MarketPosition.Long)
{
// Once the price is greater than entry price+50 ticks, set stop loss to breakeven
if (Close[0] > Close[1])
{
SetStopLoss(CalculationMode.Price, Low[1]);
}
}
Actually I am trying to move SL to low of previous bar but not on Entry bar, but on every bar after entry bar.I could not find info which value I can use for "double value" inside the statement.
Thanks
Peter
Comment
-
You would use Low[1], the price bar low value of the last bar - Position.Low does not exist in NinjaScript. You can set the stop to any value that is of format double, this is just what the compiler would expect as value entered, so the statement would be ok in a programmatic sense - you still have to ensure you supply values that make sense in your trading context, you would need to start adjusting your stops then after entry so if BarsSinceEntry is > 0 - http://www.ninjatrader.com/support/h...sinceentry.htm
Comment
-
Hello Bertrand,
Thank you for help.
I have question about using Intrabar backtest.
Is it oossible to use Primary bars of One type and Secondary bars as different type ?
example 1: Primary bars type= Volume and Secondary is Tick bars type?
Example 2: Primary bars type= 1hour and Secondary is 30seconds bars type?
Can we presume that we can build up Primary, secodary, terciary types...
Primary: Volume500 Secondary Tick300 and Terciary 20Seconds ?
Thanks
Peter P.
Comment
-
Peter,
Yes, you can mix and match, but be aware that when you do so the bars are synced by their individual timestamps. This means just because the 500 volume bar closed and then you tried to check out what the 300 tick bar looked like at that exact moment, you will not be looking at the 300 tick bar in production, but rather the last known 300 tick bar since the 300 tick bar does not necessarily close at the same time as the 500 volume bar.
Hopefully that wasn't too confusing.Josh P.NinjaTrader Customer Service
Comment
-
Thanks Josh,
That was good explanation.
I have another question: Now I know that backtest is checking inside bar only if we program it to do it like in previous question.
What about Forward testing and Replay?
Do they behave the same as Backtesting or they act inside of the bar ?
Yhanks
peter
Comment
-
Peter88,
Forward testing and Market Replay will behave differently. For those, all you have to do is set CalculateOnBarClose = false and then it will behave exactly as it would as if every single tick were coming in (because it is). The processing of your strategy is driven on each and ever tick at that point in time.Josh P.NinjaTrader Customer Service
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by argusthome, 03-08-2026, 10:06 AM
|
0 responses
85 views
0 likes
|
Last Post
by argusthome
03-08-2026, 10:06 AM
|
||
|
Started by NabilKhattabi, 03-06-2026, 11:18 AM
|
0 responses
47 views
0 likes
|
Last Post
|
||
|
Started by Deep42, 03-06-2026, 12:28 AM
|
0 responses
29 views
0 likes
|
Last Post
by Deep42
03-06-2026, 12:28 AM
|
||
|
Started by TheRealMorford, 03-05-2026, 06:15 PM
|
0 responses
32 views
0 likes
|
Last Post
|
||
|
Started by Mindset, 02-28-2026, 06:16 AM
|
0 responses
67 views
0 likes
|
Last Post
by Mindset
02-28-2026, 06:16 AM
|

Comment