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

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.
    JesseNinjaTrader Customer Service

    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:	226
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.
        JesseNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by junkone, Today, 11:37 AM
        0 responses
        1 view
        0 likes
        Last Post junkone
        by junkone
         
        Started by quantismo, 04-17-2024, 05:13 PM
        5 responses
        34 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by proptrade13, Today, 11:06 AM
        1 response
        6 views
        0 likes
        Last Post NinjaTrader_Clayton  
        Started by love2code2trade, 04-17-2024, 01:45 PM
        4 responses
        34 views
        0 likes
        Last Post love2code2trade  
        Started by cls71, Today, 04:45 AM
        2 responses
        10 views
        0 likes
        Last Post eDanny
        by eDanny
         
        Working...
        X