Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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 AaronKoRn, Yesterday, 09:49 PM
      0 responses
      11 views
      0 likes
      Last Post AaronKoRn  
      Started by carnitron, Yesterday, 08:42 PM
      0 responses
      10 views
      0 likes
      Last Post carnitron  
      Started by strategist007, Yesterday, 07:51 PM
      0 responses
      12 views
      0 likes
      Last Post strategist007  
      Started by StockTrader88, 03-06-2021, 08:58 AM
      44 responses
      3,982 views
      3 likes
      Last Post jhudas88  
      Started by rbeckmann05, Yesterday, 06:48 PM
      0 responses
      10 views
      0 likes
      Last Post rbeckmann05  
      Working...
      X