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

Target and Stoploss not work

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

    Target and Stoploss not work

    Hello,
    I followed many tutorials on internet, developping a simple strategy just for testing (EMA14 cross above/below EMA50 for example) with 10 ticks target or SL, but everytime I enable this strategy on a chart (ES/CL...), I can see the target and SL are not respected, I have not 10 ticks of target or SL, but sometimes 20-30-50 ticks..
    Please could you help me?
    Many Thanks!

    #2
    Without examining the strategy itself, it's difficult for someone to guess the problem.

    Comment


      #3
      Does it help?

      public class TESTPERSO : Strategy
      {
      private EMA EMA1;
      private EMA EMA2;

      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Description = @"Enter the description for your new custom Strategy here.";
      Name = "TESTPERSO";
      Calculate = Calculate.OnBarClose;
      EntriesPerDirection = 1;
      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)
      {
      }
      else if (State == State.DataLoaded)
      {
      EMA1 = EMA(Close, 14);
      EMA2 = EMA(Close, 50);
      EMA1.Plots[0].Brush = Brushes.Goldenrod;
      EMA2.Plots[0].Brush = Brushes.Goldenrod;
      AddChartIndicator(EMA1);
      AddChartIndicator(EMA2);
      SetProfitTarget(@"EnterShort", CalculationMode.Ticks, 20);
      SetProfitTarget(@"EnterLong", CalculationMode.Ticks, 20);
      SetStopLoss(@"EnterShort", CalculationMode.Ticks, 5, false);
      SetStopLoss(@"EnterLong", CalculationMode.Ticks, 5, false);
      }
      }

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

      if (CurrentBars[0] < 1)
      return;

      // Set 1
      if (CrossAbove(EMA1, EMA2, 1))
      {
      EnterLong(3, "");
      }

      // Set 2
      if (CrossBelow(EMA1, EMA2, 1))
      {
      EnterShort(4, "");
      }

      }
      }
      }

      Comment


        #4
        Hello Naouss,

        From the code it looks like you are not checking the market position so the strategy can reverse and not use the targets any time your opposite condition becomes true. I would suggest trying to limit the conditions to working while flat to see if that allows the targets to be hit.

        Last edited by NinjaTrader_Jesse; 02-06-2023, 08:05 AM.
        JesseNinjaTrader Customer Service

        Comment


          #5
          After looking at your code, it's quite obvious why your "SetProfitTarget" & "SetStopLoss" seem not working.
          Well they work normally, they just wait to close some specific Entry signals named "EnterLong" & "EnterShort".
          Since they don't "see" any named Entry signals with those specific names, they don't close your no-name ("") entries !

          Change your sets code as following:

          // Set 1
          if (CrossAbove(EMA1, EMA2, 1))
          {
          EnterLong(3, "EnterLong");
          }

          // Set 2
          if (CrossBelow(EMA1, EMA2, 1))
          {
          EnterShort(4, "EnterShort");
          }​

          I also think that your strategy would benefit from Calculate.OnEachTick instead of Calculate.OnBarClose.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by jeronymite, 04-12-2024, 04:26 PM
          3 responses
          40 views
          0 likes
          Last Post jeronymite  
          Started by bill2023, Today, 08:51 AM
          2 responses
          15 views
          0 likes
          Last Post bill2023  
          Started by sidlercom80, 10-28-2023, 08:49 AM
          167 responses
          2,260 views
          0 likes
          Last Post jeronymite  
          Started by warreng86, 11-10-2020, 02:04 PM
          7 responses
          1,362 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Started by Perr0Grande, Today, 08:16 PM
          0 responses
          5 views
          0 likes
          Last Post Perr0Grande  
          Working...
          X