Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Autotrail stoploss not executing

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

    Autotrail stoploss not executing

    I am having trouble with the stoploss modification to autotrail. I have downloaded SamplePriceModification.zip and compiled it based on my specs, but the modification never gets executed when running a strategy. It seems to work when running a backtest, not entirely sure though. Is there something I'm missing? Lets say I simply want to lock in 4 ticks profit once 13 ticks profit is reached, is the following correct?


    // Resets the stop loss to the original value when all positions are closed
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    SetStopLoss(CalculationMode.Ticks, stoplossticks);
    }

    // If a long position is open, allow for stop loss modification to breakeven
    else if (Position.MarketPosition == MarketPosition.Long)
    {
    // Once the price is greater than entry price + # ticks, set stop loss to breakeven
    if (Close[4] > Position.AvgPrice + 13 * TickSize)
    {
    SetStopLoss(CalculationMode.Price, Position.AvgPrice);
    }
    }

    // If a short position is open, allow for stop loss modification to breakeven
    else if (Position.MarketPosition == MarketPosition.Short)
    {
    // Once the price is less than entry price - # ticks, set stop loss to breakeven
    if (Close[-4] > Position.AvgPrice - 13 * TickSize)
    {
    SetStopLoss(CalculationMode.Price, Position.AvgPrice);
    }

    #2
    dargente,

    The problem with your code is when you access Close[]. You go Close[-4]. This does not work. The indexing is for barsAgo. 0 means the current bar. 1 means the previous bar. 2 means the bar after that and so on.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      ok figured it out thanks. besides the close[0] error, i was basing the modified stop loss on the current price rather than on avg price.
      Last edited by dargente; 11-27-2008, 08:44 AM. Reason: should be ok now

      Comment


        #4
        still problems

        it looks like the modified stop loss is being activated on the long side, but something is definitely wrong with the short side. or are the stoploss mods that i have for the long positions actually being applied to shorts as well? do i need to separately identify these? how can i allocate different stop loss orders for each long/short positions?
        Code:
                    [FONT=Times New Roman][SIZE=1]// Resets the stop loss to the original value when all positions are closed
                    if (Position.MarketPosition == MarketPosition.Flat)
                    {
                        SetStopLoss(CalculationMode.Ticks, stoplossticks);
                    }
                    
                    // If a long position is open, allow for stop loss modification to breakeven
                    else if (Position.MarketPosition == MarketPosition.Long)
                    {
                        // Once the price is greater than entry price + # ticks, set stop loss to breakeven
                        if (Close[0] > Position.AvgPrice + 10 * TickSize)
                        {
                            SetStopLoss(CalculationMode.Price, Position.AvgPrice * TickSize);
                        }
                        
                        if (Close[0] > Position.AvgPrice + 20 * TickSize)
                        {
                            SetStopLoss(CalculationMode.Price, Position.AvgPrice + 10 * TickSize);
                        }
        
                    // If a short position is open, allow for stop loss modification to breakeven
                    else if (Position.MarketPosition == MarketPosition.Short)
                        
         [/SIZE][/FONT] [CENTER][FONT=Times New Roman][SIZE=1]      // Once the price is less than entry price + # ticks, set stop loss to breakeven
        [/SIZE][/FONT] [/CENTER]
        [FONT=Times New Roman][SIZE=1]                if (Close[0] < Position.AvgPrice - 10 * TickSize)
                        {
                            SetStopLoss(CalculationMode.Price, Position.AvgPrice * TickSize);
                        } 
        
                         if (Close[0] < Position.AvgPrice - 20 * TickSize)
                        {
                            SetStopLoss(CalculationMode.Price, Position.AvgPrice - 10 * TickSize);[/SIZE][/FONT]
        Last edited by dargente; 11-27-2008, 01:25 PM.

        Comment


          #5
          Your hunch is correct. If you want separate stops for longs and shorts I suggest you use the fromEntrySignal parameter for the stop losses and tie them to specific long/short entries.
          Josh P.NinjaTrader Customer Service

          Comment


            #6
            retraceable stop loss orders

            not sure if with this code the stops are tied to specific longs or shorts, but the exits seem to be working correctly, except... my stop losses are retraceable, meaning depending on the OnBarClose my stop loss orders may move in any direction, instead of being locked in and only increasing as next target has been reached. its funny to see this happen when it has a beneficial effect, but i'd like to have more control over the exits to minimize loss. perhaps SetStopLoss is not the best solution? can you offer any insight as to how i can optimize this strategy? thanks

            update:
            think i need to use

            if (Position.MarketPosition == MarketPosition.Long)
            ExitLongStop(yourParametersHere);

            will look into that, just not sure how to configure it for multiple exits...

            Code:
            [FONT=Fixedsys][SIZE=1]
            protected override void Initialize()
                    {
                        EntriesPerDirection = 1;
                        EntryHandling = EntryHandling.AllEntries;
                        SetStopLoss(CalculationMode.Ticks, stoplossticks);
                        SetProfitTarget(CalculationMode.Ticks, profittargetticks);
            
                        CalculateOnBarClose = true;
                    }
            
                    /// <summary>
                    /// Called on each bar update event (incoming tick)
                    /// </summary>
                    protected override void OnBarUpdate()
                        
                    {
                            // Entry 1
                        if (Position.MarketPosition == MarketPosition.Flat)
                        if ...
                                EnterLong("Long Entry 1");
            
                        // Entry 2
                        if (Position.MarketPosition == MarketPosition.Flat)
                        if ...
                            EnterShort("Short Entry 1");
                        
                        // Entry 3
                        if (Position.MarketPosition == MarketPosition.Flat)
                            if ...
                                EnterLong("Long Entry 2");
            
                        // Entry 4
                        if (Position.MarketPosition == MarketPosition.Flat)
                        if ...
                            EnterShort("Short Entry 2");
            
                        // Exit 1
                        // Resets the stop loss to the original value when all positions are closed
                        if (Position.MarketPosition == MarketPosition.Flat)
                        {
                            SetStopLoss(CalculationMode.Ticks, stoplossticks);
                        }
                        
                        // If a long position is open, allow for stop loss modification to breakeven +-
                        if (Position.MarketPosition == MarketPosition.Long)
                            
                            // Once the price is greater than entry price + # ticks, set stop loss to breakeven
                            if (Close[0] > Position.AvgPrice + 10 * TickSize)
            
                            {
                                SetStopLoss(CalculationMode.Price, Position.AvgPrice - 5 * TickSize);
                            }
                            
                            // Once the price is greater than entry price + # ticks, set stop loss to breakeven +-
                            if (Close[0] > Position.AvgPrice + 20 * TickSize)
            
                            {
                                SetStopLoss(CalculationMode.Price, Position.AvgPrice + 10 * TickSize);
                            }
                        }
                            
                        // Exit 2
                        // Resets the stop loss to the original value when all positions are closed
                        if (Position.MarketPosition == MarketPosition.Flat)
                        {
                            SetStopLoss(CalculationMode.Ticks, stoplossticks);
                        }
                        
                        // If a short position is open, allow for stop loss modification to breakeven +-
                        else if (Position.MarketPosition == MarketPosition.Short)
                            
                            
                            // Once the price is greater than entry price + # ticks, set stop loss to breakeven
                            if (Close[0] < Position.AvgPrice - 10 * TickSize)
            
                            {
                                SetStopLoss(CalculationMode.Price, Position.AvgPrice + 5 * TickSize);
                            }
                            
                            
                            // Once the price is less than entry price + # ticks, set stop loss to breakeven +-
                            if (Close[0] < Position.AvgPrice - 20 * TickSize)
            
                            {
                                SetStopLoss(CalculationMode.Price, Position.AvgPrice - 10 * TickSize);
                            }
            [/SIZE][/FONT]
            Code:
                    [FONT=Fixedsys][SIZE=1]#region Properties
                    /// <summary>
                    /// </summary>
                    [Description("Numbers of ticks away from entry price for the Stop Loss order")]
                    [Category("Parameters")]
                    public int StopLossTicks
                    {
                        get { return stoplossticks; }
                        set { stoplossticks = Math.Max(0, value); }
                    }
                    
                    /// <summary>
                    /// </summary>
                    [Description("Number of ticks away from entry price for the Profit Target order")]
                    [Category("Parameters")]
                    public int ProfitTargetTicks
                    {
                        get { return profittargetticks; }
                        set { profittargetticks = Math.Max(0, value); }
                    }
                    
            
                    #endregion[/SIZE][/FONT]
            Last edited by dargente; 11-29-2008, 01:08 PM. Reason: toiling

            Comment


              #7
              The reason your stop orders are moving is because you keep resubmitting. Your condition is simply if you are long or if you are short. On each OnBarUpdate() in which that condition is true you end up resubmitting your stop loss at a different price. To prevent this I suggest you use some flag variables.

              Code:
              if (Position.MarketPosition == MarketPosition.Long && stopPlaced == false)
              {
                   SetStopLoss(...);
                   stopPlaced = true;
              }
              
              if (Position.MarketPosition == MarketPosition.Flat && stopPlaced == true)
              {
                   stopPlaced = false;
              }
              Something like that. You can change and tweak for it to fit your needs.
              Josh P.NinjaTrader Customer Service

              Comment


                #8
                hi josh,

                can you give a little more specifics on this; i'm having trouble figuring it out.

                Comment


                  #9
                  Your original condition was if you are long -> do something. This condition is evaluated to true on every single bar. This is why your stops keep moving because you keep changing it on every single bar. What you need to do is only submit it once and stop submitting it after that. That is what my prior post shows you how to do.
                  Josh P.NinjaTrader Customer Service

                  Comment


                    #10
                    i appreciate your help. the error message states:

                    The name 'stopPlaced' does not exist in the current context

                    Comment


                      #11
                      That is because you need to create it as a bool variable first.
                      Josh P.NinjaTrader Customer Service

                      Comment


                        #12
                        And how would you do that?

                        Comment


                          #13
                          In the variables section

                          private bool someVariable = false;
                          Josh P.NinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by ghoul, Today, 06:02 PM
                          3 responses
                          13 views
                          0 likes
                          Last Post NinjaTrader_Manfred  
                          Started by jeronymite, 04-12-2024, 04:26 PM
                          3 responses
                          44 views
                          0 likes
                          Last Post jeronymite  
                          Started by Barry Milan, Yesterday, 10:35 PM
                          7 responses
                          20 views
                          0 likes
                          Last Post NinjaTrader_Manfred  
                          Started by AttiM, 02-14-2024, 05:20 PM
                          10 responses
                          180 views
                          0 likes
                          Last Post jeronymite  
                          Started by DanielSanMartin, Yesterday, 02:37 PM
                          2 responses
                          13 views
                          0 likes
                          Last Post DanielSanMartin  
                          Working...
                          X