Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
How do you set Strategy builder to scale out at profit targets?
Collapse
X
-
Hello JTizz,
The CPU resource utilization can be viewed in the NinjaScript Utilization monitor.
The Calculate setting can be viewed in the Strategy window in the parameters in the Setup section, or the default used in the Strategy Analyzer is set in the script code in OnStateChange when State is State.SetDefaults.
Added series are in the code in OnStateChange() in State.Configure.
OnMarketData() and OnMarketDepth() can be added anywhere within the scope of the class. (If this is a script you wrote, and didn't add these method overrides, likely they are not in the code)
The screenshot is showing that there aren't any scripts that at the moment you opened the utilization monitor that are using a high amount of resources.
Are you currently experiencing the behavior when that screenshot was taken?Chelsea B.NinjaTrader Customer Service
Comment
-
Hello JTizz,
Atm Strategy methods cannot be used in the Strategy Builder as these are advanced tools.
Below are links to the help guide.
Set methods cannot be updated when using the Strategy Builder as these are set in State.Configure.
However, it is possible to use exit methods and change the price of these for custom exit movement behavior.
Below are links to examples in the Strategy Builder and examples that are in unlocked scripts.
Chelsea B.NinjaTrader Customer Service
Comment
-
Hello NinjaTrader_ChelseaBOriginally posted by NinjaTrader_ChelseaB View PostHello Community,
This is a commonly requested example for the Strategy Builder so I've made a similar script still locked for editing with the strategy builder.
ScaleOutBuilderExample_NT8.zip
[ATTACH]n1221604[/ATTACH]
Why is the 3rd order never submitted in this version?
EnterLong(Convert.ToInt32(DefaultQuantity), @"longEntry3");
How to get it to submit any number of orders?
Solution:
if (State == State.SetDefaults)
{
EntriesPerDirection = 3;
Code:#region Using declarations using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using System.Windows.Media; using System.Xml.Serialization; using NinjaTrader.Cbi; using NinjaTrader.Gui; using NinjaTrader.Gui.Chart; using NinjaTrader.Gui.SuperDom; using NinjaTrader.Gui.Tools; using NinjaTrader.Data; using NinjaTrader.NinjaScript; using NinjaTrader.Core.FloatingPoint; using NinjaTrader.NinjaScript.Indicators; using NinjaTrader.NinjaScript.DrawingTools; #endregion //This namespace holds Strategies in this folder and is required. Do not change it. namespace NinjaTrader.NinjaScript.Strategies { public class ScaleOutBuilderExample : Strategy { protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @""; Name = "ScaleOutBuilderExample"; Calculate = Calculate.OnBarClose; EntriesPerDirection = 2; EntryHandling = EntryHandling.AllEntries; IsExitOnSessionCloseStrategy = true; ExitOnSessionCloseSeconds = 30; IsFillLimitOnTouch = false; MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix; OrderFillResolution = OrderFillResolution.Standard; Slippage = 0; StartBehavior = StartBehavior.ImmediatelySubmitSynchronizeAccount; TimeInForce = TimeInForce.Gtc; TraceOrders = false; RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose; StopTargetHandling = StopTargetHandling.PerEntryExecution; BarsRequiredToTrade = 20; // Disable this property for performance gains in Strategy Analyzer optimizations // See the Help Guide for additional information IsInstantiatedOnEachOptimizationIteration = true; } else if (State == State.Configure) { SetTrailStop(@"longEntry2", CalculationMode.Ticks, 8, false); } } protected override void OnBarUpdate() { if (BarsInProgress != 0) return; if (CurrentBars[0] < 1) return; // Set 1 if (Position.MarketPosition == MarketPosition.Flat) { EnterLong(Convert.ToInt32(DefaultQuantity), @"longEntry1"); EnterLong(Convert.ToInt32(DefaultQuantity), @"longEntry2"); EnterLong(Convert.ToInt32(DefaultQuantity), @"longEntry3"); //EnterLong(Convert.ToInt32(DefaultQuantity), @"longEntry4"); //EnterLong(Convert.ToInt32(DefaultQuantity), @"longEntry5"); //EnterLong(Convert.ToInt32(DefaultQuantity), @"longEntry6"); //EnterLong(Convert.ToInt32(DefaultQuantity), @"longEntry7"); } // Set 2 if ((Position.MarketPosition != MarketPosition.Flat) && (BarsSinceEntryExecution(0, "", 0) == 2)) { ExitLong(Convert.ToInt32(DefaultQuantity), @"longExit1", @"longEntry1"); ExitLong(Convert.ToInt32(DefaultQuantity), @"longExit3", @"longEntry3"); } } } }
This other version too does not submit the 3rd order:
Code:#region Using declarations using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using System.Windows.Media; using System.Xml.Serialization; using NinjaTrader.Cbi; using NinjaTrader.Gui; using NinjaTrader.Gui.Chart; using NinjaTrader.Gui.SuperDom; using NinjaTrader.Gui.Tools; using NinjaTrader.Data; using NinjaTrader.NinjaScript; using NinjaTrader.Core.FloatingPoint; using NinjaTrader.NinjaScript.Indicators; using NinjaTrader.NinjaScript.DrawingTools; #endregion //This namespace holds Strategies in this folder and is required. Do not change it. namespace NinjaTrader.NinjaScript.Strategies { public class ScaleOutBuilderExample : Strategy { protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @""; Name = "ScaleOutBuilderExample"; Calculate = Calculate.OnBarClose; EntriesPerDirection = 2; EntryHandling = EntryHandling.AllEntries; IsExitOnSessionCloseStrategy = true; ExitOnSessionCloseSeconds = 30; IsFillLimitOnTouch = false; MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix; OrderFillResolution = OrderFillResolution.Standard; Slippage = 0; StartBehavior = StartBehavior.ImmediatelySubmitSynchronizeAccount; TimeInForce = TimeInForce.Gtc; TraceOrders = false; RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose; StopTargetHandling = StopTargetHandling.PerEntryExecution; BarsRequiredToTrade = 20; // Disable this property for performance gains in Strategy Analyzer optimizations // See the Help Guide for additional information IsInstantiatedOnEachOptimizationIteration = true; } else if (State == State.Configure) { SetTrailStop(@"longEntry2", CalculationMode.Ticks, 8, false); } } protected override void OnBarUpdate() { if (BarsInProgress != 0) return; if (CurrentBars[0] < 1) return; // Set 1 if (Position.MarketPosition == MarketPosition.Flat) { for (int index = 1; index < 4; index++) { EnterLong(Convert.ToInt32(DefaultQuantity), @"longEntry"+index); } } // Set 2 if ((Position.MarketPosition != MarketPosition.Flat) && (BarsSinceEntryExecution(0, "", 0) == 2)) { ExitLong(Convert.ToInt32(DefaultQuantity), @"longExit1", @"longEntry1"); ExitLong(Convert.ToInt32(DefaultQuantity), @"longExit3", @"longEntry3"); } } } }Last edited by PaulMohn; 07-27-2024, 09:30 AM.
Comment
-
Solution:
if (State == State.SetDefaults)
{
EntriesPerDirection = 3;
Time Category Message
27/07/2024 10:51:04 Execution Execution='ea50c6a975e549c79cc83f59ca7c6014' Instrument='NQ 09-24' Account='Sim101' Exchange=Default Price=19208 Quantity=1 Market position=Short Operation=Operation_Add Order='c67e8c5b8da64318b88845191cfffd1c' Time='7/27/2024 10:51 AM'
Time Category Message
27/07/2024 10:51:04 Order Order='c67e8c5b8da64318b88845191cfffd1c/Sim101' Name='Trail stop' New state='Filled' Instrument='NQ 09-24' Action='Sell' Limit price=0 Stop price=19208 Quantity=1 Type='Stop Market' Time in force=GTC Oco='' Filled=1 Fill price=19208 Error='No error' Native error=''
Time Category Message
27/07/2024 10:51:04 Order Order='c67e8c5b8da64318b88845191cfffd1c/Sim101' Name='Trail stop' New state='Working' Instrument='NQ 09-24' Action='Sell' Limit price=0 Stop price=19208 Quantity=1 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
Time Category Message
27/07/2024 10:49:42 Order Order='c67e8c5b8da64318b88845191cfffd1c/Sim101' Name='Trail stop' New state='Accepted' Instrument='NQ 09-24' Action='Sell' Limit price=0 Stop price=19208 Quantity=1 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
Time Category Message
27/07/2024 10:49:42 Order Order='c67e8c5b8da64318b88845191cfffd1c/Sim101' Name='Trail stop' New state='Change submitted' Instrument='NQ 09-24' Action='Sell' Limit price=0 Stop price=19208 Quantity=1 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
Time Category Message
27/07/2024 10:49:35 Order Order='c67e8c5b8da64318b88845191cfffd1c/Sim101' Name='Trail stop' New state='Accepted' Instrument='NQ 09-24' Action='Sell' Limit price=0 Stop price=19207 Quantity=1 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
Time Category Message
27/07/2024 10:49:35 Order Order='c67e8c5b8da64318b88845191cfffd1c/Sim101' Name='Trail stop' New state='Change submitted' Instrument='NQ 09-24' Action='Sell' Limit price=0 Stop price=19207 Quantity=1 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
Time Category Message
27/07/2024 10:49:26 Order Order='c67e8c5b8da64318b88845191cfffd1c/Sim101' Name='Trail stop' New state='Accepted' Instrument='NQ 09-24' Action='Sell' Limit price=0 Stop price=19206.25 Quantity=1 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
Time Category Message
27/07/2024 10:49:26 Position Instrument='NQ 09-24' Account='Sim101' Average price=19208.25 Quantity=2 Market position=Long Operation=Update
Time Category Message
27/07/2024 10:49:26 Order Order='c67e8c5b8da64318b88845191cfffd1c/Sim101' Name='Trail stop' New state='Submitted' Instrument='NQ 09-24' Action='Sell' Limit price=0 Stop price=19206.25 Quantity=1 Type='Stop Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
Time Category Message
27/07/2024 10:49:26 Execution Execution='c0df2580255c4a6280f2a84389a9f315' Instrument='NQ 09-24' Account='Sim101' Exchange=Default Price=19208.25 Quantity=1 Market position=Long Operation=Operation_Add Order='df082677da8f413999d18e19d209558b' Time='7/27/2024 10:49 AM'
Time Category Message
27/07/2024 10:49:26 NinjaScript NinjaScript strategy 'ScaleOutBuilderExample/333554126' submitting order
Time Category Message
27/07/2024 10:49:26 Order Order='df082677da8f413999d18e19d209558b/Sim101' Name='longEntry2' New state='Filled' Instrument='NQ 09-24' Action='Buy' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=GTC Oco='' Filled=1 Fill price=19208.25 Error='No error' Native error=''
Time Category Message
27/07/2024 10:49:26 Position Instrument='NQ 09-24' Account='Sim101' Average price=19208.25 Quantity=1 Market position=Long Operation=Operation_Add
Time Category Message
27/07/2024 10:49:26 Execution Execution='add45875dd1c427b98d6ba2bcf8a6ddf' Instrument='NQ 09-24' Account='Sim101' Exchange=Default Price=19208.25 Quantity=1 Market position=Long Operation=Operation_Add Order='262ceeaaec76441ca8c41d2b25a333b3' Time='7/27/2024 10:49 AM'
Time Category Message
27/07/2024 10:49:26 Order Order='262ceeaaec76441ca8c41d2b25a333b3/Sim101' Name='longEntry1' New state='Filled' Instrument='NQ 09-24' Action='Buy' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=GTC Oco='' Filled=1 Fill price=19208.25 Error='No error' Native error=''
Time Category Message
27/07/2024 10:49:26 Order Order='df082677da8f413999d18e19d209558b/Sim101' Name='longEntry2' New state='Working' Instrument='NQ 09-24' Action='Buy' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
Time Category Message
27/07/2024 10:49:26 Order Order='262ceeaaec76441ca8c41d2b25a333b3/Sim101' Name='longEntry1' New state='Working' Instrument='NQ 09-24' Action='Buy' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
Time Category Message
27/07/2024 10:49:26 Order Order='df082677da8f413999d18e19d209558b/Sim101' Name='longEntry2' New state='Accepted' Instrument='NQ 09-24' Action='Buy' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
Time Category Message
27/07/2024 10:49:25 Order Order='262ceeaaec76441ca8c41d2b25a333b3/Sim101' Name='longEntry1' New state='Accepted' Instrument='NQ 09-24' Action='Buy' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
Time Category Message
27/07/2024 10:49:25 Order Order='df082677da8f413999d18e19d209558b/Sim101' Name='longEntry2' New state='Submitted' Instrument='NQ 09-24' Action='Buy' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
Time Category Message
27/07/2024 10:49:25 NinjaScript NinjaScript strategy 'ScaleOutBuilderExample/333554126' submitting order
Time Category Message
27/07/2024 10:49:25 Order Order='262ceeaaec76441ca8c41d2b25a333b3/Sim101' Name='longEntry1' New state='Submitted' Instrument='NQ 09-24' Action='Buy' Limit price=0 Stop price=0 Quantity=1 Type='Market' Time in force=GTC Oco='' Filled=0 Fill price=0 Error='No error' Native error=''
Time Category Message
27/07/2024 10:49:25 NinjaScript NinjaScript strategy 'ScaleOutBuilderExample/333554126' submitting order
Last edited by PaulMohn; 07-27-2024, 09:29 AM.
Comment
-
Code:#region Using declarations using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using System.Windows.Media; using System.Xml.Serialization; using NinjaTrader.Cbi; using NinjaTrader.Gui; using NinjaTrader.Gui.Chart; using NinjaTrader.Gui.SuperDom; using NinjaTrader.Gui.Tools; using NinjaTrader.Data; using NinjaTrader.NinjaScript; using NinjaTrader.Core.FloatingPoint; using NinjaTrader.NinjaScript.Indicators; using NinjaTrader.NinjaScript.DrawingTools; #endregion //This namespace holds Strategies in this folder and is required. Do not change it. namespace NinjaTrader.NinjaScript.Strategies { public class MultiTargetStopTest : Strategy { protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Enter the description for your new custom Strategy here."; Name = "MultiTargetStopTest"; Calculate = Calculate.OnBarClose; EntriesPerDirection = 3; EntryHandling = EntryHandling.AllEntries; IsExitOnSessionCloseStrategy = true; ExitOnSessionCloseSeconds = 30; IsFillLimitOnTouch = false; MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix; OrderFillResolution = OrderFillResolution.Standard; Slippage = 0; StartBehavior = StartBehavior.WaitUntilFlat; TimeInForce = TimeInForce.Gtc; TraceOrders = false; RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose; StopTargetHandling = StopTargetHandling.PerEntryExecution; BarsRequiredToTrade = 20; // Disable this property for performance gains in Strategy Analyzer optimizations // See the Help Guide for additional information IsInstantiatedOnEachOptimizationIteration = true; } else if (State == State.Configure) { } } protected override void OnBarUpdate() { if(State == State.Historical) return; if (Position.MarketPosition == MarketPosition.Flat) { // Reset Stops and Targets when we are flat before we enter a new position. SetProfitTarget("LongEntry1", CalculationMode.Ticks, 20); SetProfitTarget("LongEntry2", CalculationMode.Ticks, 20); SetProfitTarget("LongEntry3", CalculationMode.Ticks, 20); SetStopLoss("LongEntry1", CalculationMode.Ticks, 10, false); SetStopLoss("LongEntry2", CalculationMode.Ticks, 10, false); SetStopLoss("LongEntry3", CalculationMode.Ticks, 10, false); SetProfitTarget("ShortEntry1", CalculationMode.Ticks, 20); SetProfitTarget("ShortEntry2", CalculationMode.Ticks, 20); SetProfitTarget("ShortEntry3", CalculationMode.Ticks, 20); SetStopLoss("ShortEntry1", CalculationMode.Ticks, 10, false); SetStopLoss("ShortEntry2", CalculationMode.Ticks, 10, false); SetStopLoss("ShortEntry3", CalculationMode.Ticks, 10, false); if(Close[0] > Open[0]) { EnterShort("ShortEntry1"); EnterShort("ShortEntry2"); EnterShort("ShortEntry3"); } if(Open[0] > Close[0]) { EnterLong("LongEntry1"); EnterLong("LongEntry2"); EnterLong("LongEntry3"); } } if (Position.MarketPosition == MarketPosition.Long) { if(Close[0] >= Position.AveragePrice + 10 * TickSize) { SetStopLoss("LongEntry1", CalculationMode.Price, Position.AveragePrice + 5 * TickSize, false); SetStopLoss("LongEntry2", CalculationMode.Price, Position.AveragePrice + 5 * TickSize, false); SetStopLoss("LongEntry3", CalculationMode.Price, Position.AveragePrice + 5 * TickSize, false); } else if(Close[0] >= Position.AveragePrice + 5 * TickSize) { SetStopLoss("LongEntry1", CalculationMode.Price, Position.AveragePrice, false); SetStopLoss("LongEntry2", CalculationMode.Price, Position.AveragePrice, false); SetStopLoss("LongEntry3", CalculationMode.Price, Position.AveragePrice, false); } } if (Position.MarketPosition == MarketPosition.Short) { if (Close[0] <= Position.AveragePrice - 10 * TickSize) { SetStopLoss("ShortEntry1", CalculationMode.Price, Position.AveragePrice - 5 * TickSize, false); SetStopLoss("ShortEntry2", CalculationMode.Price, Position.AveragePrice - 5 * TickSize, false); SetStopLoss("ShortEntry3", CalculationMode.Price, Position.AveragePrice - 5 * TickSize, false); } else if (Close[0] <= Position.AveragePrice - 5 * TickSize) { SetStopLoss("ShortEntry1", CalculationMode.Price, Position.AveragePrice, false); SetStopLoss("ShortEntry2", CalculationMode.Price, Position.AveragePrice, false); SetStopLoss("ShortEntry3", CalculationMode.Price, Position.AveragePrice, false); } } } } }
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by argusthome, 03-08-2026, 10:06 AM
|
0 responses
116 views
0 likes
|
Last Post
by argusthome
03-08-2026, 10:06 AM
|
||
|
Started by NabilKhattabi, 03-06-2026, 11:18 AM
|
0 responses
61 views
0 likes
|
Last Post
|
||
|
Started by Deep42, 03-06-2026, 12:28 AM
|
0 responses
40 views
0 likes
|
Last Post
by Deep42
03-06-2026, 12:28 AM
|
||
|
Started by TheRealMorford, 03-05-2026, 06:15 PM
|
0 responses
44 views
0 likes
|
Last Post
|
||
|
Started by Mindset, 02-28-2026, 06:16 AM
|
0 responses
82 views
0 likes
|
Last Post
by Mindset
02-28-2026, 06:16 AM
|

Comment