Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Profit Targets & Stop Loss in Percentage doesn't work.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Profit Targets & Stop Loss in Percentage doesn't work.

    I was testing a simple strategy with candlestick pattern. Go long with a Morning Star, Go Short with an Evening Star with a Profit Target of 1% and a Stop Loss of 1%. The code generated by the strategy builder is shown below. Using ticks, the Stop Loss and Profit Target execute correctly, but using percentage, it doesn't execute the Stop Loss and Profit Target orders. See attached chart for the trades done.

    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class CandlestickStrategy : Strategy
    {
    private CandlestickPattern CandlestickPattern1;
    private CandlestickPattern CandlestickPattern2;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Candlestick Strategy";
    Name = "CandlestickStrategy";
    Calculate = Calculate.OnBarClose;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = false;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 0;
    StartBehavior = StartBehavior.WaitUntilFlat;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = true;
    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)
    {
    }
    else if (State == State.DataLoaded)
    {
    CandlestickPattern1 = CandlestickPattern(Close, ChartPattern.MorningStar, 0);
    CandlestickPattern2 = CandlestickPattern(Close, ChartPattern.EveningStar, 0);
    CandlestickPattern1.Plots[0].Brush = Brushes.Navy;
    AddChartIndicator(CandlestickPattern1);
    SetProfitTarget(@"", CalculationMode.Percent, 1);
    SetStopLoss(@"", CalculationMode.Percent, 1, false);
    }
    }

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 1)
    return;

    // Set 1
    if (CandlestickPattern1[0] == 1)
    {
    EnterLong(Convert.ToInt32(DefaultQuantity), @"Long");
    }

    // Set 2
    if (CandlestickPattern2[0] == 1)
    {
    EnterShort(Convert.ToInt32(DefaultQuantity), @"Short");
    }

    }
    }
    }

    #2
    Hello tjendra,

    As a tip, to export a NinjaTrader 8 NinjaScript so this can be shared and imported by the recipient do the following:
    1. Click Tools -> Export -> NinjaScript...
    2. Click the 'add' link -> check the box(es) for the script(s) and reference(s) you want to include
    3. Click the 'Export' button
    4. Enter a unique name for the file in the value for 'File name:'
    5. Choose a save location -> click Save
    6. Click OK to clear the export location message
    By default your exported file will be in the following location:
    • (My) Documents/NinjaTrader 8/bin/Custom/ExportNinjaScript/<export_file_name.zip>
    Below is a link to the help guide on Exporting NinjaScripts.
    http://ninjatrader.com/support/helpG...-us/export.htm


    For CalculationMode.Percent to get:
    5% use 0.05
    50% use 0.5
    500% use 5.0


    I would also encourage you to just look at the prices the orders are being submitted at with the values you are using on the Orders tab of the the Strategy Performance window.


    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks Chelsea!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by CarlTrading, 03-31-2026, 09:41 PM
      1 response
      47 views
      0 likes
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 04-01-2026, 02:41 AM
      0 responses
      23 views
      0 likes
      Last Post CarlTrading  
      Started by CaptainJack, 03-31-2026, 11:44 PM
      0 responses
      33 views
      1 like
      Last Post CaptainJack  
      Started by CarlTrading, 03-30-2026, 11:51 AM
      0 responses
      50 views
      0 likes
      Last Post CarlTrading  
      Started by CarlTrading, 03-30-2026, 11:48 AM
      0 responses
      42 views
      0 likes
      Last Post CarlTrading  
      Working...
      X