Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Incromprehensible SL

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

    Incromprehensible SL

    Hello,

    I dont understand why some trade is good, the SL and TP is correctly positioned but sometimes, with only 2 candle and 2 point my SL is touch .....

    This is the code i wrote for SL and TP and BE:

    Code:
    EnterShortLimit(1, Close[0]);
    SetProfitTarget(CalculationMode.Ticks, 80);
    SetStopLoss(CalculationMode.Ticks, 40);
    }
    if (Position.MarketPosition == MarketPosition.Flat)
    SetStopLoss(CalculationMode.Ticks, 80);
    {
    if((Close[0] < Position.AveragePrice + 10 * TickSize))
    {
    SetStopLoss(CalculationMode.Price, Position.AveragePrice + 0 * TickSize);

    #2
    Hello Sautarne,

    Thanks for your post.

    I do not see anything specific in the code you shared that would cause the behavior you are reporting.

    That said, I do see that you are calling Set methods after your Entry method. Set methods should be called before the entry is submitted since the Set methods prep NinjaTrader to submit target/stop upon the execution of the entry order. For example, see below.

    SetStopLoss(CalculationMode.Ticks, 10);
    EnterLong(totalContracts, "LONG");

    Ultimately, to understand why the script is behaving as it is, such as placing orders or not placing orders or drawing objects when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

    In the strategy add prints (outside of any conditions) that print the values of every variable used in every condition that places an order along with the time of that bar. Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

    Also, enable TraceOrders which will let us know if any orders are being ignored and not being submitted when the condition to place the orders is evaluating as true.

    Below is a link to a forum post that demonstrates how to use prints to understand behavior.
    https://ninjatrader.com/support/foru...121#post791121

    Please let me know if I may further assist
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Thank for your fast reply.

      I try to put my Set methods before the entry but this dont change nothing.

      For the "print" side, i read all the link you share but i dont understand all. I dont understand what is the utility of this ...

      I got only 4 EMA on my strategy. Its a simple strategy but i clearly dont know why this dont work.

      Code:
      EMA1 = EMA(Close, 10);
      EMA2 = EMA(Close, 20);
      EMA3 = EMA(Close, 50);
      EMA4 = EMA(Close, 100);
      EMA1.Plots[0].Brush = Brushes.Goldenrod;
      EMA2.Plots[0].Brush = Brushes.Goldenrod;
      EMA3.Plots[0].Brush = Brushes.Goldenrod;
      EMA4.Plots[0].Brush = Brushes.Goldenrod;
      AddChartIndicator(EMA1);
      AddChartIndicator(EMA2);
      AddChartIndicator(EMA3);
      AddChartIndicator(EMA4);
      
      }
      }
      
      protected override void OnBarUpdate()
      {
      if (BarsInProgress != 0)
      return;
      
      if (CurrentBars[0] < 1)
      return;
      
      // Set 1
      if (
      // achat
      ((EMA1[1] > EMA2[1])
      && (EMA2[1] > EMA3[1])
      && (EMA3[1] > EMA4[1])
      && (EMA1[0] > EMA2[0])
      && (EMA2[0] > EMA3[0])
      && (EMA3[0] > EMA4[0])
      && (Open[1] > EMA1[1])
      && (Close[1] < EMA2[1])
      && (Open[0] < EMA2[0])
      && (Close[0] > EMA1[0]))

      Comment


        #4
        Hello Sautarne,

        Thanks for your note.

        Unfortunately, in the support department at NinjaTrader it is against our policy to create, debug, or modify, code or logic for our clients. Further, we do not provide C# programming education services in our support. This is so that we can maintain a high level of service for all of our clients as well as our partners.

        You would need to add prints outside of the conditions that place orders which print out all the variables used to place the order. These prints could then be compared to see how your strategy is evaluating logic and if the conditions to place the order is becoming true or not.

        The prints should include the time of the bar and should print all values from all variables and all hard coded values in all conditions that must evaluate as true for this action to be triggered. It is very helpful to include labels and operators in the print to understand what is being compared in the condition sets.

        For example, if we had a script that called EnterLong() when EMA1 is greater than EMA2, the code for the print would look something like this.

        Code:
        Print("Time: " + Time[0] + " EMA1: " + EMA1[0] + " EMA2: " + EMA2[0]);
        if (EMA1[0] > EMA2[0])
            EnterLong();
        We could compare the prints values of EMA1 and EMA2 and the time of the bar to see if the condition is evaluating true and should place an order.

        You should also add prints to your script that print out the values used to place/move the stop loss and profit target orders and compare them to the current market price (Close[0]). A simple example of prints for this may look like this.


        Code:
        Print("Close: " + Close[0] + " Position.AveragePrice + 10Ticks: " + (Position.AveragePrice + 10 * TickSize));
        if((Close[0] < Position.AveragePrice + 10 * TickSize))
        {
            Print("SetStopLoss price: " + (Position.AveragePrice + 0 * TickSize));
            SetStopLoss(CalculationMode.Price, Position.AveragePrice + 0 * TickSize);
        }
        Let us know if we may assist further.
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment


          #5
          ok, i delete all my code for BE and its work fine... but i dont understand what wrong with this BE code ...

          Code:
          if (Position.MarketPosition == MarketPosition.Flat)
          SetStopLoss(CalculationMode.Ticks, 80);
          
          
          {
          if((Close[0] > Position.AveragePrice + 10 * TickSize))
          {
          SetStopLoss(CalculationMode.Price, Position.AveragePrice + 0 * TickSize);
          }
          }
          i just need to put my SL BE when the price is + 10 ticks about the price entry.

          Comment


            #6
            Hello Sautarne,

            Thanks for your note.

            I don't see anything in particular in the code you shared that would keep the strategy from moving the price of the stop to breakeven. That said, prints would need to be added to the script to compare how the strategy is behaving and calculating. If you do not see a print occur, this means that condition is not being hit.

            Please see this reference sample which demonstrates moving a stop loss to breakeven.

            SamplePriceModification: https://ninjatrader.com/support/help...of_stop_lo.htm

            Let us know if we may assist further.

            <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

            Comment


              #7
              I write it like that

              Code:
               protected override void OnBarUpdate()
              {
              if (CurrentBar < BarsRequiredToTrade)
              return;
              
              
              if (Position.MarketPosition == MarketPosition.Flat)
              {
              SetStopLoss(CalculationMode.Ticks, 40);
              }
              
              else if (Position.MarketPosition == MarketPosition.Long)
              {
              
              if (Close[0] > Position.AveragePrice + 10 * TickSize)
              {
              SetStopLoss(CalculationMode.Price, Position.AveragePrice);
              }
              }
              
              else if (Position.MarketPosition == MarketPosition.Short)
              {
              
              if (Close[0] < Position.AveragePrice - 10 * TickSize)
              {
              SetStopLoss(CalculationMode.Price, Position.AveragePrice);
              }
              }
              But this not work... i got again some position with a SL 5 tick or other thing... i dont understand why this SL change in each position loss... i go sleep now because my brain will explose. thank for your help.

              Comment


                #8
                Hello Sautarne,

                Thanks for your note.

                As previously stated in post #4, in the support department at NinjaTrader it is against our policy to create, debug, or modify, code or logic for our clients. Further, we do not provide C# programming education services in our support. This is so that we can maintain a high level of service for all of our clients as well as our partners.

                That said, I see you do not have debugging prints added to your script. To truly know what is causing the issue it would be necessary to use prints and debug by looking at all of the information the script is using for decisions.

                You would need to add prints to your script that print out every variable used for the conditions that place or move a stop loss order as well as the price that the stop loss is being submitted to. You would then need to compare the print output in a New > NinjaScript Output window to determine how the strategy is behaving and how the stop loss is being submitted or moved.

                See the forum thread linked in post #2 for detailed information about debugging using prints to understand how a strategy is behaving and placing orders.

                Also, review the SamplePriceModification reference sample in post #6 which demonstrates how to move a stop loss to breakeven.

                Let us know if we may assist further.
                <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by NullPointStrategies, Today, 05:17 AM
                0 responses
                39 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
                64 views
                0 likes
                Last Post NabilKhattabi  
                Started by Deep42, 03-06-2026, 12:28 AM
                0 responses
                41 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