Announcement

Collapse
No announcement yet.

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.

        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 NullPointStrategies, Yesterday, 05:17 AM
          0 responses
          64 views
          0 likes
          Last Post NullPointStrategies  
          Started by argusthome, 03-08-2026, 10:06 AM
          0 responses
          139 views
          0 likes
          Last Post argusthome  
          Started by NabilKhattabi, 03-06-2026, 11:18 AM
          0 responses
          75 views
          0 likes
          Last Post NabilKhattabi  
          Started by Deep42, 03-06-2026, 12:28 AM
          0 responses
          45 views
          0 likes
          Last Post Deep42
          by Deep42
           
          Started by TheRealMorford, 03-05-2026, 06:15 PM
          0 responses
          50 views
          0 likes
          Last Post TheRealMorford  
          Working...
          X