Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trailing Stop Loss

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

    Trailing Stop Loss

    Hello I am new to the platform and have developed a simple strategy to test.

    I cant find the code for a simple currency trailing stop loss.

    I currently have this:
    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CrossAbove(Low, sma, 1) && High[2] < sma.Value[2])
    EnterLong(coinAmount);

    if (Position.MarketPosition == MarketPosition.Long)
    {
    if (CrossBelow(High, sma, 1))
    ExitLong(coinAmount);
    }

    if (CrossBelow(High, sma, 1) && Low[1] < sma.Value[1])
    {
    EnterShort(coinAmount);
    }

    if (Position.MarketPosition == MarketPosition.Short)
    {
    if (CrossBelow(Low, sma, 1))
    ExitShort(coinAmount);
    }
    }

    but I want to trail the current price by 20$ and cant find how to do that?

    #2
    Hello jcdude89,

    You can set a trailing stop by using SetTrailStop however that cannot be used with ExitShort/ExitLong. You would also need to trail by a number of Ticks Pips or Percent from the price rather than a currency.
    https://ninjatrader.com/support/help...antedPositions

    https://ninjatrader.com/support/help...htsub=settrail

    There are some examples of trailing in the following forum post:
    https://ninjatrader.com/support/foru...269#post802269

    I look forward to being of further assistance.
    Last edited by NinjaTrader_Jesse; 12-28-2020, 12:57 PM.

    Comment


      #3
      I have added the following code but I am getting this sell that instantly hits the stop loss.

      What am I doing wrong

      protected override void OnBarUpdate()
      {
      if (BarsInProgress != 0)
      return;


      if (CrossAbove(smaFast, smaSlow, 1))
      EnterLong(coinAmount);

      if (Position.MarketPosition == MarketPosition.Long)
      {

      SetStopLoss(CalculationMode.Price, Close[0] - 2);
      }

      if (CrossBelow(smaFast, smaSlow, 1))
      EnterShort(coinAmount);

      if (Position.MarketPosition == MarketPosition.Short)
      {

      SetStopLoss(CalculationMode.Price, Close[0] + 2);
      }
      }

      Click image for larger version

Name:	Capture.PNG
Views:	288
Size:	9.1 KB
ID:	1134164

      Comment


        #4
        Hello jcdude89,

        You will want to make sure to call Set methods before the entry like this:

        Code:
        if (CrossAbove(smaFast, smaSlow, 1))
        {
            SetStopLoss(CalculationMode.Price, Close[0] - 2);
            EnterLong(coinAmount);
        }

        That prepares the target with the price, once the entry fills it uses that value.

        I look forward to being of further assistance.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Yesterday, 05:17 AM
        0 responses
        65 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        139 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        75 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        45 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        50 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X