Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Moving stop losses question.

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

    Moving stop losses question.

    E.G.
    if(Close[0] > Position.AvgPrice + 50 * TickSize)

    I know that this will test to see if price is above +50 ticks THEN will do whatever code is underneath...in the thread I got it from, it essentially set a stop loss with about 30 ticks at that point.


    However, I have a couple questions.

    1) Can I use something more immediate than Close[0]? Can I use "Bid[0]" or something similar?

    2) What if I have 2 or 3 of the if statements above...what keeps the condition from actually moving the stop loss backwards as the condition changes?

    IE
    the example above moves to 30 ticks...but what if during that time...Close[0] moves back below 50 ticks...if I put one at Close{0} > 30 (put stop loss at +15)...NOW this one is true...will it reset my stop loss now that the +50 is no longer true?

    How should I work around that? Let's say at entry I set a stop loss at 15 ticks back. At +10 I set my stop loss 5 ticks back from entry. At 20 ticks I set my stop loss at 5 ticks from entry...etc. It's not always the same amount, nor is it necessarily that consistent to use the SetTrailStop().

    #2
    Hello Darth_Trader,
    Thank you for writing in.
    1. If you would like to use the bid or ask prices you can use the OnMarketData() method. It might look something like this:
    Code:
    protected override void OnMarketData(MarketDataEventArgs e)
    {
        if(e.MarketDataType == MarketDataType.Bid && e.Price > Position.AvgPrice + 50 * TickSize)
        {
            //Do Something
        }
    }
    The OnMarketData() method has access to intrabar data even when CalculateOnBarClose is set to true. For more information on the OnMarketData() method, please see our help guide here: http://ninjatrader.com/support/helpG...b=onmarketdata

    If you simply want to use the last traded price, you can set CalculateOnBarClose=false; in your Initialize() method and continue using OnBarUpdate(). For example:
    Code:
    protected override void Initialize()
    {
        CalculateOnBarClose = false;
    }
    protected override void OnBarUpdate()
    {
        if(Close[0] > Position.AvgPrice + 50 * TickSize)
        {
            //Do Something
        }
    }
    2. Yes it will continue moving your stop loss anytime that condition is true.
    At +10 I set my stop loss 5 ticks back from entry. At 20 ticks I set my stop loss at 5 ticks from entry
    This part of what you are saying would not require any additional code because the stop loss is adjusted to the same value in relation to the entry price. So it could be +5 ticks or +100 ticks but the stop loss would always be 5 ticks below the entry price.

    Here is an example for OnBarUpdate() based on what you were saying:
    Code:
    protected override void Initialize()
    {
    SetStopLoss(CalculationMode.Ticks, 15); //Set initial stop loss
    }
    protected override void OnBarUpdate()
    {
        if(Close[0] > Position.AvgPrice + 10 * TickSize && Close[0] <= Position.AvgPrice + 20 * TickSize )
        {
            SetStopLoss(Position.AvgPrice - 5 * TickSize);
        }
        else if(Close[0] > Position.AvgPrice + 20 * TickSize)
        {
            SetStopLoss(Position.AvgPrice - 5 * TickSize); 
        }
    }
    NOTE (when dynamically modifying the stop loss): You should always reset the stop loss price/offset value when your strategy is flat, otherwise the last price/offset value set will be used to generate your stop loss order on your next open position.
    Please see our help guide for more information on the SetStopLoss() method: http://ninjatrader.com/support/helpG...ub=setstoploss

    Please let us know if we may be of further assistance.
    Michael M.NinjaTrader Quality Assurance

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by mmenigma, Today, 02:22 PM
    1 response
    3 views
    0 likes
    Last Post NinjaTrader_Jesse  
    Started by frankthearm, Today, 09:08 AM
    9 responses
    35 views
    0 likes
    Last Post NinjaTrader_Clayton  
    Started by NRITV, Today, 01:15 PM
    2 responses
    9 views
    0 likes
    Last Post NRITV
    by NRITV
     
    Started by maybeimnotrader, Yesterday, 05:46 PM
    5 responses
    26 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by quantismo, Yesterday, 05:13 PM
    2 responses
    21 views
    0 likes
    Last Post quantismo  
    Working...
    X