Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Protecting profits with trailing stoploss

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

    Protecting profits with trailing stoploss

    Hello,
    I have an automated trading strategy for ES and I am struggling with the following:

    Let's say I just entered a long trade

    - I want to set an initial stop loss of 12 ticks
    - Once the trade goes in my favor by 8 ticks, I'd like to move stop loss to breakeven price
    - After that once the strategy starts making 9 to 20 ticks of profit, I want to move the stop loss 4 ticks above my entry price (profit protection)
    - And at last if the strategy starts making 21 to 40 ticks of profit, I want to move the stop loss 12 ticks above my entry price

    I am not sure if I need to use SetStopLoss or SetTrailStop to achieve the above objectives.

    Currently this is what I have in terms of script under 'OnBarUpdate', but I am not able to see the strategy behaving the way it needs to.

    if (Position.MarketPosition == MarketPosition.Flat)
    {
    SetTrailStop(CalculationMode.Ticks, 12);
    }
    else if (Position.MarketPosition == MarketPosition.Long)
    {
    if (Close[0] >= (Position.AvgPrice + 8 * TickSize))
    {
    SetTrailStop(CalculationMode.Ticks, 0);
    }

    if (Close[0] >= (Position.AvgPrice + 9 * TickSize)
    && (Close[0] <= (Position.AvgPrice + 20 * TickSize)))
    {
    SetTrailStop(CalculationMode.Ticks, 4);
    }
    if (Close[0] >= (Position.AvgPrice + 21 * TickSize)
    && (Close[0] <= (Position.AvgPrice + 40 * TickSize)))
    {
    SetTrailStop(CalculationMode.Ticks, 12);
    }
    }

    I am not sure what am I doing wrong? Can someone please help to achieve the objectives I described above? Should I be using SetStopLoss instead of SetTrailStop? Is there anything wrong with the code above?

    Very much appreciate your help in advance.

    #2
    Hello pandyav,

    You may want to use "SetStopLoss()" if you want to keep your Stop a value that you define since "SetTrailStop" NinjaTrader will automatically move your Stop as the Market goes in your favor.

    We have an example of how this can be done at following thread that you may view.
    JCNinjaTrader Customer Service

    Comment


      #3
      Hi JC,
      Thank you for your reply. I used SetStopLoss as you suggested and even though it works, I don't think it is the most effective to protect unrealized profits. Please see the attached screenshot.

      As I think more about this, I don't think setting your stoploss based on Close[0] is the most ideal way as the price may already have moved against you to protect your profits.

      So, what I'd like to do is to base my stoploss based on the maximum level of unrealized profits for a single trade. I found some snippets of code on NT forum (http://www.ninjatrader.com/support/f...ead.php?t=4084) but it talks about profit for the overall strategy and I am more interested in managing an individual/current trade.

      So, my question is:
      Is there a way to identify the highest level of unrealized profits for an individual trade?

      Again, very much appreciate your help.
      Attached Files

      Comment


        #4
        Originally posted by pandyav View Post
        Hi JC,
        Thank you for your reply. I used SetStopLoss as you suggested and even though it works, I don't think it is the most effective to protect unrealized profits. Please see the attached screenshot.

        As I think more about this, I don't think setting your stoploss based on Close[0] is the most ideal way as the price may already have moved against you to protect your profits.

        So, what I'd like to do is to base my stoploss based on the maximum level of unrealized profits for a single trade. I found some snippets of code on NT forum (http://www.ninjatrader.com/support/f...ead.php?t=4084) but it talks about profit for the overall strategy and I am more interested in managing an individual/current trade.

        So, my question is:
        Is there a way to identify the highest level of unrealized profits for an individual trade?

        Again, very much appreciate your help.
        Not directly, but you just need to:
        1. Record the entry price at the time of entry,
        2. Record (for a long) the highest high reached after entry using a running method, as you are processing a stream of price data.
        3. Subtract the entryPrice from the HighestHigh.

        For a short, you want the lowestLow, which you would subtract from the entryPrice.

        Comment


          #5
          Thank you Koganam,
          That's what I need to do. The challenge is how can I do it. I know that to refer to the entry price I can use Position.AvgPrice function.

          But I am not sure how can one determine the highest high (for a long trade) or lowest low (for a short trade)?

          Any idea how this can be done?

          Again, very much appreciate your help and support.

          Comment


            #6
            Any thoughts on how can I figure out the highest level of profit once for a particular trade?

            Again appreciate any help you can provide,

            Comment


              #7
              Originally posted by pandyav View Post
              Any thoughts on how can I figure out the highest level of profit once for a particular trade?

              Again appreciate any help you can provide,
              Variables
              Code:
              private double HighestGain = 0;
              OnBarUpdate
              Code:
              double CurrentBarHighestGain = High[0] - Position.AvgPrice;
              HighestGain = Math.Max(HighestGain, CurrentBarHighestGain);
              for the longside.

              For the shortside,
              Code:
              double CurrentBarHighestGain = Position.AvgPrice - Low[0]; 
              //etc

              Comment


                #8
                Koganam,
                Thank you for providing the guidance on my issue. As a next step to my question below, I'd like to do the following now.

                I want to see the entry price, exit price, entry time, exit time, highest profit level of a trade and total profit/loss of every trade my strategy has taken in the past.

                I know I can get all of the above statistics except highest level of profit of individual trade from the strategy performance report. Do you know if there is a way to record the highest level of profit of an individual trade and transfer it to Excel/.CSV for further analysis?

                Again, very much appreciate your help and support. You suggestions have been very helpful.

                Comment


                  #9
                  Originally posted by pandyav View Post
                  Koganam,
                  Thank you for providing the guidance on my issue. As a next step to my question below, I'd like to do the following now.

                  I want to see the entry price, exit price, entry time, exit time, highest profit level of a trade and total profit/loss of every trade my strategy has taken in the past.

                  I know I can get all of the above statistics except highest level of profit of individual trade from the strategy performance report. Do you know if there is a way to record the highest level of profit of an individual trade and transfer it to Excel/.CSV for further analysis?

                  Again, very much appreciate your help and support. You suggestions have been very helpful.
                  I do not think that MFE is available for individual trades. You would have to write that kind of data to an external file, and import the data into Excel that way. If you look in the Samples subForum, there are examples of how to write to an external file.
                  Last edited by koganam; 12-21-2013, 06:30 PM.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by jaybedreamin, Today, 05:56 PM
                  0 responses
                  7 views
                  0 likes
                  Last Post jaybedreamin  
                  Started by DJ888, 04-16-2024, 06:09 PM
                  6 responses
                  18 views
                  0 likes
                  Last Post DJ888
                  by DJ888
                   
                  Started by Jon17, Today, 04:33 PM
                  0 responses
                  1 view
                  0 likes
                  Last Post Jon17
                  by Jon17
                   
                  Started by Javierw.ok, Today, 04:12 PM
                  0 responses
                  9 views
                  0 likes
                  Last Post Javierw.ok  
                  Started by timmbbo, Today, 08:59 AM
                  2 responses
                  10 views
                  0 likes
                  Last Post bltdavid  
                  Working...
                  X