Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Dynamic Stop Loss/Trailing Stop Loss

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

    Dynamic Stop Loss/Trailing Stop Loss

    Hello Everyone and thank you for help in advance. I am having some difficulty with creating a dynamic stop loss/trailing stop.

    Trade Management Information

    Once trade is in profit $104 the stop loss should move to breakeven
    when the trade is equal or greater to $580 then stop loss is moved into profit to secure $20
    lastly once unrealized profit is $1100 then stop loss is set to secure $700.

    my initial stop loss is $330

    6/10 the stop loss is moved to breakeven
    0/10 stop loss is moved to secure $20 after it reaches $580
    2/10 stop loss is moved to secure profit of $700 once $1100 unrealized profit is being reached.

    Here’s what I have so far


    }




    ManageTrades();

    }




    private void ManageTrades()

    {

    double currentProfitLoss = Position.GetUnrealizedProfitLoss(PerformanceUnit.C urrency, Close[0]);




    if (Position.MarketPosition == MarketPosition.Long)

    {

    if (currentProfitLoss >= ProfitTargetA)

    {

    double newStopPrice = Position.AveragePrice + ProfitTargetB;

    SetStopLoss(CalculationMode.Price, newStopPrice);

    }

    else if (currentProfitLoss >= MoveToBreakevenProfit)

    {

    double breakevenPrice = Position.AveragePrice;

    SetStopLoss(CalculationMode.Price, breakevenPrice);

    }

    }

    else if (Position.MarketPosition == MarketPosition.Short)

    {

    if (currentProfitLoss >= ProfitTargetA)

    {

    double newStopPrice = Position.AveragePrice - ProfitTargetB;

    SetStopLoss(CalculationMode.Price, newStopPrice);

    }

    else if (currentProfitLoss >= MoveToBreakevenProfit)

    {

    double breakevenPrice = Position.AveragePrice;

    SetStopLoss(CalculationMode.Price, breakevenPrice);

    }

    }

    }

    #2
    Hello J87dls18,

    Im not sure I follow what problem you are having here, Was the 6/10 0/10 2/10 meant to mean those events happened that many times out of ten?

    If not can you clarify what part you are asking for help with in relation to the code provided?

    Comment


      #3
      Yes, correct how many times those events happened out of 10.

      Here is a visual of stats from today's strategy analyzer. The highlighted part is my MFE, as you can see trades prior were moved to breakeven. When I was up $1200 in unrealized profit, my stop loss should have moved to secure $700 in profit.

      Click image for larger version

Name:	image.png
Views:	206
Size:	122.0 KB
ID:	1308745​​​
      Attached Files

      Comment


        #4
        Hello J87dls18,

        Thanks for clarifying. In that case you would need to add prints into your conditions to see if those conditions are becoming true or not. You are using else if conditions meaning that the conditions are linked together, if the first condition is true the second cannot become true. That may be related to seeing the movements only sometimes happening.

        ​​​​​​​

        Comment


          #5
          Current Profit/Loss: 1085
          Moved to breakeven for Short position at price: 19991.5
          Updated stop loss to secured profit level 1 for Short position at price: 19990.5
          Current Profit/Loss: 980
          Moved to breakeven for Short position at price: 19991.5
          Updated stop loss to secured profit level 1 for Short position at price: 19990.5
          Current Profit/Loss: 745
          Moved to breakeven for Short position at price: 19991.5
          Updated stop loss to secured profit level 1 for Short position at price: 19990.5
          Current Profit/Loss: 570
          Moved to breakeven for Short position at price: 19991.5​

          I created print in my code, It sets specific profit levels but it resets when unrealized profit changes.

          Once trade is in profit $104 the stop loss should move to breakeven
          when the trade is equal or greater to $580 then stop loss is moved into profit to secure $20
          lastly once unrealized profit is $1100 then stop loss is set to secure $700.​

          but if trade drops below instead of securing $20 it brings it back to "breakeven"

          Comment


            #6
            Hello J87dls18,

            You likely will need to add more logic to do the tasks you are wanting to do. As mentioned you are using if/else statements so one of those conditions will be true depending on the variables values. A breakeven should be a condition which can only become true once when the price has crossed a certain point so the order can be moved, that condition would need to be turned of using logic once that occurs so it does not become true again and readjust the order. For the other steps you want you would also need to do the same type of task if those movements should only happen once per trade. You can use a bool variable as a way to control turning on or off conditions.

            I would suggest to make your conditions into single if statements as a starting point so you can control each condition individually to avoid else logic. Using prints will be helpful for you to trace when each condition is becoming true to make sure that works initially. Based on that observation you can add other logic like bool variables to control the conditions that need to happen once. You would also need to later reset those variables, a good place to do that would be in a condition checking if the position is flat.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by NullPointStrategies, Today, 05:17 AM
            0 responses
            49 views
            0 likes
            Last Post NullPointStrategies  
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            126 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            67 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            42 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