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 NullPointStrategies, Today, 05:17 AM
      0 responses
      44 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      124 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      65 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      42 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      46 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X