Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Break Even Stop Not Working

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

    Break Even Stop Not Working

    I am using the code below to test for price movement half-way to a profit target, and setting a new stoploss at the avg position price + (or minus for a short) 2 ticks. I set the stop loss value at the time the position is opened, not in the initialize section of code. But it isn't working. is there some limit on stop loss price change which is preventing this from working?

    Code:
                 //Long Break Even Stop
                if (Position.MarketPosition == MarketPosition.Long
                    && Position.Quantity == 1
    				&& Position.GetProfitLoss(Close[0], PerformanceUnit.Currency) >  Profit1 * 0.5)
                {
    				SetStopLoss("", CalculationMode.Price, Position.AvgPrice + 2 * TickSize, false);
    
                }
    
                // Short Break Even Stop
                if (Position.MarketPosition == MarketPosition.Short
                    && Position.Quantity == 1
    				&& Position.GetProfitLoss(Close[0], PerformanceUnit.Currency) > Profit1 * 0.5)
                {
    				SetStopLoss("", CalculationMode.Price, Position.AvgPrice - 2 * TickSize, false);
    
                }
    Thanks ahead of time,
    DaveN

    #2
    Hi DaveN,

    Set methods will use the last value set and submits an order that's triggered from the entry execution. Because Position.AvgPrice returns value of 0 when you're not in a position, your code will likely submit an invalid order.

    You'll want to reset to an initial fixed value when flat, and then your custom one based on price will change the value for it right after the position update is received.

    if (Position.MarketPosition == MarketPosition.Flat)
    SetStopLoss(CalculationMode.Ticks, 20);
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Break Even Not Working Continued

      I added the set stop when position flat as you supplied but it didn't change anything in my backtesting. I then looked at the code and I thought I had a problem mixing tick values with price values so I changed it to convert everything to price value. Still didn't make any difference. Here is what I added, and also how I changed the stop price to breakeven, but when I look at the output of trades after testing, I either stop out at full stop, close at profit, or close on an exit at close basis. I never see a break even stop out.
      Here's the code:

      Code:
      			if (Position.MarketPosition == MarketPosition.Flat)
      				SetStopLoss(CalculationMode.Ticks, Stop);
      			
      			double brk_even = Profit1 * 0.5 * TickSize;
      Code:
                  if (Position.MarketPosition == MarketPosition.Long
                      && Position.Quantity == 1
      				&& Position.GetProfitLoss(Close[0], PerformanceUnit.Currency) >  brk_even)
                  {
      				SetStopLoss("", CalculationMode.Price, Position.AvgPrice + brk_even , false);
                  }
      
                  // Short Break Even Stop
                  if (Position.MarketPosition == MarketPosition.Short
                      && Position.Quantity == 1
      				&& Position.GetProfitLoss(Close[0], PerformanceUnit.Currency) > brk_even)
                  {
      				SetStopLoss("", CalculationMode.Price, Position.AvgPrice - brk_even , false);
                  }

      Comment


        #4
        The best way to evaluate that this is doing what you expect is in real time. You can use either simulated data feed or market replay to simulate this. In a backtest you just don't get the tick by tick changes in price. If the initial stop loss can be filled (even on the entry bar) then it will.

        There is also a sample break-even stop loss strategy available here which you could use for ideas:
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          I reconfigured my code using the code from the SamplePriceModification example and nothing changed. Out of curiosity I tested the sample code. I never got a break-even stop out of it. either profit, full stop, or trade close. Are you sure it works, but it doesn't appear to on backtesting?
          DaveN

          Comment


            #6
            Yes, it works here. Have you tried with a simulated data feed, so you can use trend slider to force the profit trigger and then see the movement? Have you tried printing all values used in the modification condition? Are there any that aren't what you expect?

            If you want to do this in a backtest, it requires different technique than SetStopLoss(). You can use ExitLongStop(), ExitShortStop(), submitting to a smaller series. To get the same timing so they're submitted right after the entry order: Use the advanced handler OnExecution() as a trigger to submit your exit orders. To get a finer granularity, submit these orders to a smaller secondary series.
            Last edited by NinjaTrader_RyanM1; 04-10-2012, 05:36 PM.
            Ryan M.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by judysamnt7, 03-13-2023, 09:11 AM
            4 responses
            57 views
            0 likes
            Last Post DynamicTest  
            Started by ScottWalsh, Today, 06:52 PM
            4 responses
            36 views
            0 likes
            Last Post ScottWalsh  
            Started by olisav57, Today, 07:39 PM
            0 responses
            7 views
            0 likes
            Last Post olisav57  
            Started by trilliantrader, Today, 03:01 PM
            2 responses
            19 views
            0 likes
            Last Post helpwanted  
            Started by cre8able, Today, 07:24 PM
            0 responses
            9 views
            0 likes
            Last Post cre8able  
            Working...
            X