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

SetStopLoss/SetProfitTarget fail

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

    SetStopLoss/SetProfitTarget fail

    Hello

    SetStopLoss/SetProfitTarget fail in Strategy Analyzer at my end. They trigger only on bar close at a close price. But they work (seems on each tick which is expected behavior) on Market Replay. In Market replay I have Calculate = OnBarClose.
    I didn't find any description on this in Help.

    Sample strategy is here.

    //
    // Copyright (C) 2018, NinjaTrader LLC <www.ninjatrader.com>.
    // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
    //
    #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.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 SampleMACrossOver2 : Strategy
    {
    private SMA smaFast;
    private SMA smaSlow;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = NinjaTrader.Custom.Resource.NinjaScriptStrategyDes criptionSampleMACrossOver;
    Name = "SampleMACrossOver2";
    Fast = 10;
    Slow = 25;
    // This strategy has been designed to take advantage of performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = false;
    }
    else if (State == State.DataLoaded)
    {
    smaFast = SMA(Fast);
    smaSlow = SMA(Slow);

    smaFast.Plots[0].Brush = Brushes.Goldenrod;
    smaSlow.Plots[0].Brush = Brushes.SeaGreen;

    AddChartIndicator(smaFast);
    AddChartIndicator(smaSlow);
    }
    }

    protected override void OnBarUpdate()
    {
    if (CurrentBar < BarsRequiredToTrade)
    return;

    SetProfitTarget(CalculationMode.Ticks, 20);
    SetStopLoss(CalculationMode.Ticks, 6);

    if (CrossAbove(smaFast, smaSlow, 1))
    EnterLong();
    else if (CrossBelow(smaFast, smaSlow, 1))
    EnterShort();
    }

    #region Properties
    [Range(1, int.MaxValue), NinjaScriptProperty]
    [Display(ResourceType = typeof(Custom.Resource), Name = "Fast", GroupName = "NinjaScriptStrategyParameters", Order = 0)]
    public int Fast
    { get; set; }

    [Range(1, int.MaxValue), NinjaScriptProperty]
    [Display(ResourceType = typeof(Custom.Resource), Name = "Slow", GroupName = "NinjaScriptStrategyParameters", Order = 1)]
    public int Slow
    { get; set; }
    #endregion
    }
    }

    #2
    Hello ren37,

    Thank you for your post.

    Strategy Analyzer backtests run on historical data and therefore the calculations are done on the close of the bar. This includes the calculations for the Stop Loss and Profit Targets submitted through SetStopLoss and SetProfitTarget.

    Please refer to our Help Guide documentation on Historical Fill Processing for more details at the following link: https://ninjatrader.com/support/help...ical_fill_.htm

    Please let me know if you have any questions.

    Comment


      #3
      This is not true. Because I calculate indicators values on each tick during Analyzer backtesting with Tick replay perfectly.
      But seems your orders managed approach lives its own life and was not updated for years...
      Last edited by ren37; 03-16-2018, 09:24 AM.

      Comment


        #4
        What about ATMs?

        Comment


          #5
          Hello ren37,

          Thank you for your response.

          Tick Replay would allow for the calculations intra-bar but executions still occur on the bar close. You could add a secondary series that was smaller in order to submit orders to the smaller series to simulate intra-bar fills. Please see the reference sample at the following link for an example: https://ninjatrader.com/support/foru...ead.php?t=6652

          ATM strategies cannot be backtested.

          Please let me know if you have any questions.

          Comment


            #6
            Hello Patrick

            Checked. That doesn't help. I enter on bar close, but need to set stop loss which triggers with 1 tick granularity. But it still triggers on MAIN DATA SERIES bar close.

            From Help on SetStopLoss()
            Should you have multiple Bars objects of the same instrument while using SetStopLoss() in your strategy, you should only submit orders for this instrument to the first Bars context of that instrument. This is to ensure your order logic is processed correctly and any necessary order amendments are done properly.

            Any ideas?
            Last edited by ren37; 03-19-2018, 03:48 AM.

            Comment


              #7
              Hello ren37,

              Thank you for your response.

              The behavior of the SetStopLoss, SetTrailStop, and SetProfitTarget is that they will always process on the primary series. If you need to exit on the secondary series you need to call the Exit methods in the Managed Approach similar to how you use your Enter methods.

              Please visit the following link for more information: https://ninjatrader.com/support/help...d_approach.htm

              Please let me know if you have any questions.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by bortz, 11-06-2023, 08:04 AM
              47 responses
              1,605 views
              0 likes
              Last Post aligator  
              Started by jaybedreamin, Today, 05:56 PM
              0 responses
              8 views
              0 likes
              Last Post jaybedreamin  
              Started by DJ888, 04-16-2024, 06:09 PM
              6 responses
              18 views
              0 likes
              Last Post DJ888
              by DJ888
               
              Started by Jon17, Today, 04:33 PM
              0 responses
              4 views
              0 likes
              Last Post Jon17
              by Jon17
               
              Started by Javierw.ok, Today, 04:12 PM
              0 responses
              13 views
              0 likes
              Last Post Javierw.ok  
              Working...
              X