Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

1. Transfer SetStopLoss to SetTrailStop 2. Adding boolean statement after x price

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

    1. Transfer SetStopLoss to SetTrailStop 2. Adding boolean statement after x price

    Question 1.

    How do I repeat a Raise Stop Loss statement? I understand the SetTrailStop() will do the trick but:

    1. I'm using a SetStopLoss() order for the entry (@ -6 ticks from entry)

    2. You can't use a both a SetTrailStop() & SetStopLoss together &

    3. I'm trying to raise the initial SetStopLoss() at 6 ticks, then raise it to: 1 tick, then 5 ticks, then change the code to repeat by 5 ticks instead of adding unnecessary lines of codes every 5 tick as example below
    - Unless I change the code to a SetTrailStop?

    Code:
    // Resets the stop loss to the original value when all positions are closed
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    // Set stop loss to x point in case of entry reversals
    SetStopLoss("LongEntry", CalculationMode.Ticks, 6, false);
    }
    
    else if (Position.MarketPosition == MarketPosition.Long)
    {
    
    // if price is over entry by 5 ticks (1.25 points), raise Stop Loss to 1 tick
    if (Close[0] >= Position.AveragePrice + (5 * TickSize))
    
    {
    SetStopLoss("LongEntry", CalculationMode.Ticks, -1, false);
    }
    
    // if price is over entry by 9 ticks (2.25 points), raise Stop Loss to 1.25 points (5 ticks)
    if (Close[0] >= Position.AveragePrice + (9 * TickSize))
    
    
    // I imagine this is where you change it to a SetTrailStop? But then how do you repeat the code that's based on the 9 tick rule?
    {
    SetStopLoss("LongEntry", CalculationMode.Ticks, -5, false);
    }
    }
    
    // First Statement - Going Long
    if (CrossAbove(EMA1, SMA1, 1))
    {
    // Enter Long
    EnterLong(Convert.ToInt32(DefaultQuantity), "LongEntry");
    }

    Question 2.

    In addition to the code above, I want the available option to exit if price is >= from entry of 5 ticks (So price has the option of either exiting by the stop loss or a Crossunder)

    I know this code doesn't compile correctly, but to further explain what I'm trying to accomplish

    Code:
    // Something that combines these statements together
    if (Close[0] >= Position.AveragePrice, EnterLong("LongEntry");
    
    if (Close[0] >= Position.AveragePrice + (5 * TickSize))
    {
    
    // If the Crossunder appears when price is over entry by 5+ ticks, allow it
    if (CrossBelow(EMA1, SMA1, 1)) = true;
    {
    ExitLong(Convert.ToInt32(DefaultQuantity), "LongEntry");
    
    }
    
    }
    Would this new line of code be before the code stated above? Or would it be within the 5 tick rule statement?

    Thanks in advance.
    Last edited by sentient254; 01-26-2021, 07:52 AM.

    #2
    Hello sentient254,

    Thanks for your post and welcome to the NinjaTrader forums!

    You are correct, you cannot use a SetTrailStop and a SetStopLoss on the same order. You cannot switch from one to the other on the same order.

    To answer question 1, as you increment the stoploss, also increment the trigger price. For example, create a double called Trigger_Price and set it to be the first level of Position.AveragePrice +5 *TickSize; When you hit the trigger price then adjust the stop and then set your new trigger price, for example, Trigger_Price += 5 *TickSize. Likewise, you can create a double for the stop price and likewise can increment it using the += operator.

    For question 2, you would just combine the CrossAbove and the price check, something like this:

    if (Close[0] >= Position.AveragePrice + (5 * TickSize) && CrossBelow(EMA1, SMA1, 1) )
    {
    ExitLong(Convert.ToInt32(DefaultQuantity), "LongEntry");
    }

    You will need to be careful that you do not issue ExitLong at the same time that the SetStopLoss fires as you could end up with an unintended short position. It would be a good practice to ensure that your triggers for the stoploss and for this exit are significantly different in price so they do not occur at the same time

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Today, 05:17 AM
    0 responses
    52 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    130 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    70 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    44 views
    0 likes
    Last Post Deep42
    by Deep42
     
    Started by TheRealMorford, 03-05-2026, 06:15 PM
    0 responses
    48 views
    0 likes
    Last Post TheRealMorford  
    Working...
    X