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 bortz, 11-06-2023, 08:04 AM
        47 responses
        1,602 views
        0 likes
        Last Post aligator  
        Started by jaybedreamin, Today, 05:56 PM
        0 responses
        8 views
        0 likes
        Last Post jaybedreamin  
        Started by DJ888, 04-16-2024, 06:09 PM
        6 responses
        18 views
        0 likes
        Last Post DJ888
        by DJ888
         
        Started by Jon17, Today, 04:33 PM
        0 responses
        4 views
        0 likes
        Last Post Jon17
        by Jon17
         
        Started by Javierw.ok, Today, 04:12 PM
        0 responses
        12 views
        0 likes
        Last Post Javierw.ok  
        Working...
        X