Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Setting up custom stop loss

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

    Setting up custom stop loss

    Hello,

    I am trying to make a custom stop loss that is different for different scenarios:

    In my current example:
    Scenario 1: a maximum stop loss of 300 ticks
    Scenario 2: a trailing stop loss of 10 ticks

    Since you can't do a TrailStop and an automatic StopLoss together... I am trying to pseudo hard-code it.

    Here is my code:



    if(currentprofit > 20)
    {

    if (currentprofit > maxprofit)
    {
    maxprofit = currentprofit;
    }


    if (currentprofit < (maxprofit - 10))
    {
    ExitShort(@"EntryShort");
    maxprofit = 0;
    }
    }

    if (currentprofit < -300)
    {
    ExitShort(@"EntryShort");​
    }


    So basically:
    If my currentprofit variable is larger than maxprofit, it will update maxprofit to the new profit value. If currentprofit dips below 10 ticks, it will exit the trade. If currentprofit never hits​ the +20 profit, it has a safety net at -300 ticks.

    The issue I am having is that max profit is always updating to current profit (i think because max profit is resetting to zero at every run of the code). I have it initialized as a "private double maxprofit = 0;" in the beginning of the strategy (before onstatechange()). Can anyone tell me how to retain this value of maxprofit through the next run of the code?

    Thank you!

    #2
    Hello Orion815,

    From the given code I see you are resetting the variable when you exit, if that condition keeps becoming true it will keep resetting the variable. The best way to go over this situation would be to add prints into your code to output the value at various points to see when it gets reset and how long it retains the value.

    Comment


      #3
      Hi Jesse,

      Thank you for your response.

      Yes, I want to reset the maxprofit variable at the end of every trade.

      However, it is not holding this value through each bar update. Is this possible with a variable for it to store the value at each bar update, and only update, as my code indicates, if currentprofit > maxprofit? Or should this be done with an array or data series?

      I tried doing the prints as you suggested, and it seems like it's holding the value until the end of each bar update.

      Thank you.

      Comment


        #4
        Hello Orion815,

        You mentioned in your first post that the variable is a private variable so that will retain a value over many bars so long as you are not resetting it. Because you used a private variable and the value is being reset to 0 that would indicate that your logic is not working as you expected and is resetting that variable. To know why that is happening you would need to use prints to trace how your conditions are working in relation to that variable.

        Comment


          #5
          is it possible to set up a custom trailing stoploss that follows the open price of the previous heiken ashi candle?

          Comment


            #6
            Hello conniejforextrader,

            Yes, if you are manually coding you can use the heiken ashi indicator and then submit an order with the price of your choosing.

            Comment


              #7
              Good evening,

              This script is for a trailing stop that trails the high of the previous heiken ashi candle in a sell and the low of the previous heiken ashi candle in a buy once the trade is 10 ticks in profit. It compiles but doesn't trail the trade. Can you see why it is not working?

              Last edited by conniejforextrader; 03-04-2025, 08:00 AM.

              Comment


                #8
                Hello conniejforextrader

                When making links make sure to put the full file path, the link you added is just leading to a website and not a file. Attaching the file directly to the post is the best option to avoid spam deletion.

                Comment


                  #9
                  Please see attached
                  Attached Files

                  Comment


                    #10
                    Hello conniejforextrader,

                    The code you posted looks correct however that will only happen once the condition is true so it won't immediately submit targets. You would have to use a condition that is true immediately if you wanted to submit targets. Then later you would have to toggle between conditions to trail. I would suggest looking at the following script which uses a variable as a state to change conditions:

                    Comment


                      #11
                      Thanks. I'll check it out.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by NullPointStrategies, Today, 05:17 AM
                      0 responses
                      43 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
                      65 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