Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Condition with exitlong result not happening

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

    Condition with exitlong result not happening

    I have this condition as a static stop loss to reduce the amount of losses but it seems that something is not working as it never prints output when i am backtesting. Is there something wrong with the code ? The code is located in the onbarupdate.

    if (Position.MarketPosition == MarketPosition.Long && Close[0] <= 0.9 * Position.AveragePrice)
    {
    Print(String.Format("Long Exit - 10% Loss | Bar: {0} | Entry: {1:F2} | Current: {2:F2} | Loss: {3:F2}%",
    CurrentBar,
    Position.AveragePrice,
    Close[0],
    ((Close[0] - Position.AveragePrice) / Position.AveragePrice) * 100));

    ExitLong();
    return;

    }
    if (Position.MarketPosition == MarketPosition.Short && Close[0] >= 1.1 * Position.AveragePrice) {
    Print(String.Format("Short Exit - 10% Loss | Bar: {0} | Entry: {1:F2} | Current: {2:F2} | Loss: {3:F2}%",
    CurrentBar,
    Position.AveragePrice,
    Close[0],
    ((Position.AveragePrice - Close[0]) / Position.AveragePrice) * 100));
    ExitShort();
    return;​


    #2
    What is the intended logic?
    I looked up ES prices and hypothetically speaking, if the average price was 6018.00 for a long position, then 0.9 * that would mean the stop loss would be at 5416.20, which is quite a large stoploss.

    Perhaps this is the reason for the lack of output? In any case, understanding your intentions with the code could help with understanding why it isn't working. If the logic is fine, it could be a small detail like a ; somewhere that doesn't lead to compiling errors but ruin the code.
    Last edited by rockmanx00; 12-29-2024, 06:33 PM.

    Comment


      #3
      The script implements a trading strategy designed to exit positions when a 10% loss threshold is reached, either for long or short trades. It first prints debugging information, including the current bar number, market position, average price, close price, and the calculated loss threshold. For a long position, it checks if the current close price is less than or equal to 90% of the current position in the trade, signaling a 10% loss, and exits the position if the condition is met. Similarly, for a short position, it verifies if the close price is greater than or equal to 110% of the current position of the trade (indicating a 10% loss) and exits the short position while logging the details. This logic ensures that trades are exited promptly when losses reach the predefined threshold.

      Comment


        #4
        Ok. So, if you were to enter in on ES at 6018.00, your SL would be at 5416.25, which means your SL is 601.75 points, making your SL $30k. Is that the intent?

        Comment


          #5
          This is what i ment to do.

          if (Position.MarketPosition == MarketPosition.Long)
          {
          if (Close[0] <= 0.9989536557 * Position.AveragePrice)
          {
          ExitLong();
          }
          }
          else if (Position.MarketPosition == MarketPosition.Short)
          if (Close[0] >= 1.001046344 * Position.AveragePrice)
          {
          ExitShort();​

          I managed now ! much thanks !

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by CarlTrading, 03-31-2026, 09:41 PM
          1 response
          47 views
          0 likes
          Last Post NinjaTrader_ChelseaB  
          Started by CarlTrading, 04-01-2026, 02:41 AM
          0 responses
          23 views
          0 likes
          Last Post CarlTrading  
          Started by CaptainJack, 03-31-2026, 11:44 PM
          0 responses
          33 views
          1 like
          Last Post CaptainJack  
          Started by CarlTrading, 03-30-2026, 11:51 AM
          0 responses
          50 views
          0 likes
          Last Post CarlTrading  
          Started by CarlTrading, 03-30-2026, 11:48 AM
          0 responses
          42 views
          0 likes
          Last Post CarlTrading  
          Working...
          X