Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trailing stop and Target on Idicator Help

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

    Trailing stop and Target on Idicator Help

    Hi,

    I’m having issues with a trailing stop and target on a indicator. I know there are several posts about this but I can’t find anything to fix the issues I’m having.

    If I set the target on a indicator and hard stop no problem it works great. if I set the trailing stop with a hard target it works great the problem I can’t get both of them to work together. I can get one to work but not the other.

    Here is the code for the Target


    protectedoverridevoid OnExecution(IExecution iexec){
    double entryPrice = iexec.Price;
    double tickSize = Instrument.MasterInstrument.TickSize;


    if (iexec.MarketPosition == MarketPosition.Long){
    double stopPrice = entryPrice - stop*tickSize;
    ExitLongLimit(0, true, iexec.Order.Quantity, ExitPrice[0], "Sweet Exit",iexec.Order.FromEntrySignal);
    ExitLongStop(0, true, iexec.Order.Quantity, stopPrice, "Sad exit", iexec.Order.FromEntrySignal);


    }



    else if (iexec.MarketPosition == MarketPosition.Short) {
    double stopPrice = entryPrice + stop*tickSize;
    ExitShortStop(0, true, iexec.Order.Quantity, stopPrice, "Sad exit", iexec.Order.FromEntrySignal);
    ExitShortStop(0, true, iexec.Order.Quantity, TrailExit [0], "Sad exit", iexec.Order.FromEntrySignal);
    ExitShortLimit(0, true, iexec.Order.Quantity, ExitPrice[0], "Sweet Exit", iexec.Order.FromEntrySignal);

    }




    Here is the code for the Trailing stop


    protectedoverridevoid OnBarUpdate()
    {

    #region Breakeven and Trailing Stops
    // Resets the stop loss to the original value when all positions are closed
    switch (Position.MarketPosition)
    {
    case MarketPosition.Flat:
    SetStopLoss(CalculationMode.Ticks, stopLossTicks);
    previousPrice = 0;
    break;
    case MarketPosition.Long:
    // Once the price is greater than entry price+ breakEvenTicks ticks, set stop loss to breakeven
    if (Close[0] > Position.AvgPrice + breakEvenTicks * TickSize
    && previousPrice == 0)
    {
    initialBreakEven = Position.AvgPrice + plusBreakEven * TickSize;
    SetStopLoss(CalculationMode.Price, initialBreakEven);
    previousPrice = Position.AvgPrice;
    PrintWithTimeStamp("previousPrice = "+previousPrice);
    }
    // Once at breakeven wait till trailProfitTrigger is reached before advancing stoploss by trailStepTicks size step
    elseif (previousPrice != 0////StopLoss is at breakeven
    && GetCurrentAsk() > previousPrice + trailProfitTrigger * TickSize
    )
    {
    newPrice = previousPrice + trailStepTicks * TickSize;
    SetStopLoss(CalculationMode.Price, newPrice);
    previousPrice = newPrice;
    PrintWithTimeStamp("previousPrice = "+previousPrice);
    }
    break;
    case MarketPosition.Short:
    // Once the price is Less than entry price - breakEvenTicks ticks, set stop loss to breakeven
    if (Close[0] < Position.AvgPrice - breakEvenTicks * TickSize
    && previousPrice == 0)
    {
    initialBreakEven = Position.AvgPrice - plusBreakEven * TickSize;
    SetStopLoss(CalculationMode.Price, initialBreakEven);
    previousPrice = Position.AvgPrice;
    PrintWithTimeStamp("previousPrice = "+previousPrice);
    }
    // Once at breakeven wait till trailProfitTrigger is reached before advancing stoploss by trailStepTicks size step
    elseif (previousPrice != 0////StopLoss is at breakeven
    && GetCurrentAsk() < previousPrice - trailProfitTrigger * TickSize
    )
    {
    newPrice = previousPrice - trailStepTicks * TickSize;
    SetStopLoss(CalculationMode.Price, newPrice);
    previousPrice = newPrice;
    PrintWithTimeStamp("previousPrice = "+previousPrice);
    }



    break;






    default:
    //autoStopA900Traded = false;
    break;
    }



    What do I need to do to fix this issue?

    #2
    Still having issues with this.

    Comment


      #3
      EvilHog, you're attempting to combine the Set() with the Exit() methods here - so likely you are running into the Internal Order Handling rules here in our managed approach -

      http://www.ninjatrader.com/support/h...d_approach.htm (botton section here).

      What I would suggest is running the script with TradeOrders enabled via the Initialize() method to better understand what's exactly happening when it 'breaks', this approach would also note ignored orders due to the rules mentioned above in your debug output window.

      BertrandNinjaTrader Customer Service

      Comment


        #4
        Bertrand,

        Thanks for the info but due to my very limited coding knowledge I still have no Idea what’s going on but I want to take a guess to see if I’m in the ball park.

        I did a very short back test (3 trades) to look at the output window like you suggested here are the results from 1 of them.

        1/31/2013 9:34:00 AM Entered internal PlaceOrder() method at 1/31/2013 9:34:00 AM: BarsInProgress=0 Action=Buy OrderType=Market Quantity=1 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal=''
        1/31/2013 9:34:00 AM Entered internal PlaceOrder() method at 1/31/2013 9:34:00 AM: BarsInProgress=0 Action=Sell OrderType=Limit Quantity=1 LimitPrice=97.53 StopPrice=0 SignalName='Sweet Exit' FromEntrySignal=''
        1/31/2013 12:11:00 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=97.22 Currency=0 Simulated=False
        1/31/2013 12:11:00 PM Amended stop order: Order='NT-00004/Backtest' Name='Stop loss' State=Working Instrument='CL ##-##' Action=Sell Limit price=0 Stop price=97.22 Quantity=1 Strategy='CTD' Type=Stop Tif=Day Oco='NT-00002-199' Filled=0 Fill price=0 Token='45210082b7dd46bdb4bd1f551bc5aefd' Gtd='12/1/2099 12:00:00 AM'
        1/31/2013 12:11:00 PM previousPrice = 97.2

        1/31/2013 12:49:00 PM Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Price Value=97.7 Currency=0 Simulated=False
        1/31/2013 12:49:00 PM Amended stop order: Order='NT-00004/Backtest' Name='Stop loss' State=Working Instrument='CL ##-##' Action=Sell Limit price=0 Stop price=97.7 Quantity=1 Strategy='CTD' Type=Stop Tif=Day Oco='NT-00002-199' Filled=0 Fill price=0 Token='45210082b7dd46bdb4bd1f551bc5aefd' Gtd='12/1/2099 12:00:00 AM'
        1/31/2013 12:49:00 PM previousPrice = 97.7



        My “Sweet Exit” (target) was set at 9:34 and at 12:11 when my trailing stop was moved it removed my target. Looking at my charts my target (97.53 S1) should have been it at 12:11 but it got out at 12:50

        Is this correct? If so I don’t understand how to fix this problem.


        Here is the Entry conditions

        if condition
        {
        EnterLong(DefaultQuantity, "");
        ExitPrice.Set(DPivots.S1[0]);
        }
        Attached Files

        Comment


          #5
          Hello,

          As Bertrand mentioned you will want to fully read up on the internal handling at the bottom here. He's pointing out that your mixing of Exit() and Set() are likely conflicting with each other: http://www.ninjatrader.com/support/h...d_approach.htm

          Instead of using SetStopLoss() to manage your exit you will probably want to use IOrder to be able to manage your exit order along the way.

          Here is a good example for getting started with using OnOrderUpdate() which also demonstrates Modifying stop-loss order to breakeven after a certain amount in profit
          The OnOrderUpdate() and OnExecution() methods are reserved for experienced programmers. Instead of using Set() methods to submit stop-loss and profit target orders, you can submit and update them manually through the use of IOrder and IExecution objects in the OnOrderUpdate() and OnExecution() methods. The OnOrderUpdate()


          Please let me know if I can be of further assistance.
          LanceNinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by helpwanted, Today, 03:06 AM
          1 response
          11 views
          0 likes
          Last Post sarafuenonly123  
          Started by Brevo, Today, 01:45 AM
          0 responses
          9 views
          0 likes
          Last Post Brevo
          by Brevo
           
          Started by aussugardefender, Today, 01:07 AM
          0 responses
          5 views
          0 likes
          Last Post aussugardefender  
          Started by pvincent, 06-23-2022, 12:53 PM
          14 responses
          242 views
          0 likes
          Last Post Nyman
          by Nyman
           
          Started by TraderG23, 12-08-2023, 07:56 AM
          9 responses
          387 views
          1 like
          Last Post Gavini
          by Gavini
           
          Working...
          X