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

Set stop to breakeven after profit

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

    Set stop to breakeven after profit

    I'm attempting to do an immediate stop and reverse on a parabolicSAR with code that sets the stop to breakeven. I copied the code from the SamplePriceModification.cs file on another help item and am using that. But what occurs now is that after a winning long trade I close the position, go short immediately and get immediately stopped out (instantly) on the backtesting of the strategy. If I comment out the code for moving the stop to B/E on a long trade then when I try and stop and reverse after a winning short trade I get instantly stopped out.

    I've tried commenting out each separate chunk of code to narrow it down but it seems to be anything that has to do with the move stop to B/E code

    Any ideas? Many thanks.

    protected override void OnBarUpdate()
    {


    // Resets the stop loss to the original value when all positions are closed
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    SetStopLoss(CalculationMode.Ticks, StopLoss);
    }
    // If a long position is open, allow for stop loss modification to breakeven

    else if (Position.MarketPosition == MarketPosition.Long)
    {
    // Once the price is greater than entry price+30 ticks, set stop loss to breakeven
    if (Close[0] > Position.AvgPrice + 30 * TickSize)
    {
    SetStopLoss(CalculationMode.Price, Position.AvgPrice);
    }
    }
    else if (Position.MarketPosition == MarketPosition.Short)
    {
    // Once the price is less than price+30 ticks, set stop loss to breakeven
    if (Close[0] < Position.AvgPrice - 30 * TickSize)
    {
    SetStopLoss(CalculationMode.Price, Position.AvgPrice);
    }
    }

    // Condition set 1

    if (CrossBelow(ParabolicSAR(0.02, 0.2, 0.02), Close, 1))
    {
    EnterLong(Contracts, "");
    }
    // Condition set 2

    if (CrossAbove(ParabolicSAR(0.02, 0.2, 0.02), Close, 1))
    {
    EnterShort(Contracts, "");
    }

    }

    #2
    Hi cloudcowboy,

    Welcome to the NT forums! Code looks pretty good and it's likely a race condition when reversing -- your stop loss will use the last value set and your "resetting block" when flat will not have a chance to set your stop loss in time.

    I would add a similar resetting statement to your entry blocks so that the stop loss is set to a static level before entering. As soon as it reads the position, your dynamic adjustments will take over.

    Code:
    if (CrossBelow(ParabolicSAR(0.02, 0.2, 0.02), Close, 1))
    {
    SetStopLoss(CalculationMode.Ticks, StopLoss);
    EnterLong(Contracts, ""); 
    }
    // Condition set 2
    
    if (CrossAbove(ParabolicSAR(0.02, 0.2, 0.02), Close, 1))
    {
    SetStopLoss(CalculationMode.Ticks, StopLoss);
    EnterShort(Contracts, ""); 
    }
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Dear All,
      I have written stoploss based profits (Breakeven trading). While running this script changing of stoploss will not executed. Please me to find exact code....

      #region Using declarations
      using System;
      using System.ComponentModel;
      using System.Diagnostics;
      using System.Drawing;
      using System.Drawing.Drawing2D;
      using System.Xml.Serialization;
      using NinjaTrader.Cbi;
      using NinjaTrader.Data;
      using NinjaTrader.Indicator;
      using NinjaTrader.Gui.Chart;
      using NinjaTrader.Strategy;
      #endregion

      // This namespace holds all strategies and is required. Do not change it.
      namespace NinjaTrader.Strategy
      {
      /// <summary>
      ///
      /// </summary>
      [Description("")]
      public class vwap2 : Strategy
      {
      #region Variables
      // Wizard generated variables
      private int myInput0 = 1; // Default setting for MyInput0
      // User defined variables (add any user defined variables below)
      #endregion
      private IOrder stopOrder = null;
      /// <summary>
      /// This method is used to configure the strategy and is called once before any strategy method is called.
      /// </summary>
      protected override void Initialize()
      {
      CalculateOnBarClose = false;
      SetStopLoss(CalculationMode.Ticks, 50);

      }

      /// <summary>
      /// Called on each bar update event (incoming tick)
      /// </summary>
      protected override void OnBarUpdate()
      {
      if (CurrentBar < 1)
      return;
      if (Position.MarketPosition == MarketPosition.Long)
      {
      // Once the price is greater than entry price+10 ticks, set stop loss to breakeven
      if (stopOrder != null && stopOrder.StopPrice > Position.AvgPrice && Close[0] > Position.AvgPrice + 10 * TickSize)
      {
      ChangeOrder(stopOrder, stopOrder.Quantity, stopOrder.LimitPrice, Position.AvgPrice);
      }
      }
      if (Position.MarketPosition == MarketPosition.Short)
      {
      // Once the price is greater than entry price+10 ticks, set stop loss to breakeven
      if (stopOrder != null && stopOrder.StopPrice < Position.AvgPrice && Close[0] < Position.AvgPrice - 10 * TickSize)
      {
      ChangeOrder(stopOrder, stopOrder.Quantity, stopOrder.LimitPrice, Position.AvgPrice);
      }
      }
      double vwapval=VWMA(5)[0];

      if (vwapval < Open[0] && Open[0]+5*TickSize>Open[0])
      {
      EnterLong(10,"TREND UP");
      }

      if (vwapval > Open[0] && Open[0]-5*TickSize<Open[0])
      {
      EnterShort(10,"TREND DOWN");
      }

      }
      }
      }


      Thanks & Regards,
      G.Mugavaiselvam

      Comment


        #4
        mugavai, welcome to our forums - I would not expect that to work, since the SetStopLoss would not expose any IOrder access and the call for ChangeOrder would be limited to working in our unmanaged order submission approach only.

        For a sample how to adjust your SetStopLoss value dynamically, please check into this link -

        BertrandNinjaTrader Customer Service

        Comment


          #5
          Helo Bertrand,

          Please give me working script for stoploss and trailing stoploss please..I need to modify stoploss if my trade is in profit...Please


          Thanks in Advance...
          G.Mugavaiselvam
          Last edited by mugavai; 12-19-2012, 07:22 AM.

          Comment


            #6
            Hello,

            If you refer to the link that Bertrand provided, you can see a script which shows you how to modify the price of a stop loss order. You can apply these same concepts to your script and you should see the results you are looking for.
            MatthewNinjaTrader Product Management

            Comment


              #7
              mugavai, please check into the working example I've referred you to and test it out to learn how the code structure would perform, then work the same logic into your custom script please.
              BertrandNinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by PaulMohn, Today, 12:36 PM
              0 responses
              2 views
              0 likes
              Last Post PaulMohn  
              Started by love2code2trade, 04-17-2024, 01:45 PM
              4 responses
              38 views
              0 likes
              Last Post love2code2trade  
              Started by alifarahani, Today, 09:40 AM
              2 responses
              14 views
              0 likes
              Last Post alifarahani  
              Started by junkone, Today, 11:37 AM
              3 responses
              20 views
              0 likes
              Last Post NinjaTrader_ChelseaB  
              Started by frankthearm, Yesterday, 09:08 AM
              12 responses
              44 views
              0 likes
              Last Post NinjaTrader_Clayton  
              Working...
              X