Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to set breakeven + 1 tick

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

    How to set breakeven + 1 tick

    Hello everyone and Happy New Year.
    I have a strategy - stop&reverse system, when price crossing a SMA
    After i get 10 ticks in profit, i want to move my stop loss to breakeven + 1 tick
    I tried to do my self after i found few examples here, but is not working.
    I will be very happy if someone can help me.
    Here is the code.
    I do not know where to modify and what.
    Thank you.

    [Description("Price cross SMA")]
    public class PriceCrossSMA : Strategy
    {
    #region Variables
    // Wizard generated variables
    private int sma = 50; // Default setting for Sma
    private int target = 100; // Default setting for Target
    private int stopLoss = 10; // Default setting for StopLoss
    // User defined variables (add any user defined variables below)
    #endregion

    /// <summary>
    /// This method is used to configure the strategy and is called once before any strategy method is called.
    /// </summary>
    protected override void Initialize()
    {
    Add(SMA(Sma));
    Add(SMA(Sma));
    SetProfitTarget("", CalculationMode.Ticks, Target);
    SetStopLoss("", CalculationMode.Ticks, StopLoss, false);

    CalculateOnBarClose = true;
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // Condition set 1
    if (CrossAbove(Close, SMA(Sma), 1))
    {
    EnterLong(DefaultQuantity, "");
    }

    // Condition set 2
    if (CrossBelow(Close, SMA(Sma), 1))
    {
    EnterShort(DefaultQuantity, "");
    }
    }

    #2
    Hello,

    The following refernece sample demonstraes how to move your stop loss to break even:



    If you'd like to do BreakEven + 1, you'd just modify the following:

    Code:
    				if (Close[0] > Position.AvgPrice + 10 * TickSize)
    				{
    				[B]	SetStopLoss(CalculationMode.Price, Position.AvgPrice + 1 * TickSize);[/B]
    				}
    MatthewNinjaTrader Product Management

    Comment


      #3
      Thank you for your answer.
      I do know where to modify.
      I put it just before the Condition set1 but the strategy not working.

      Comment


        #4
        Hi Mario,

        Could you please clarify what you mean by not working?

        Is it compiling after adding the code or is not adjusting the SetStopLoss accordingly?
        Cal H.NinjaTrader Customer Service

        Comment


          #5
          Yes, it is compiling, but when i enable the strategy, nothing happend.
          I put the code here to see where i made the change.
          May i do not change correctly.
          Someone can help me, please?

          [Description("Price cross SMA")]
          public class PriceCrossSMA : Strategy
          {
          #region Variables
          // Wizard generated variables
          private int sma = 50; // Default setting for Sma
          private int target = 100; // Default setting for Target
          private int stopLoss = 10; // Default setting for StopLoss
          // User defined variables (add any user defined variables below)
          #endregion

          /// <summary>
          /// This method is used to configure the strategy and is called once before any strategy method is called.
          /// </summary>
          protected override void Initialize()
          {
          Add(SMA(Sma));
          Add(SMA(Sma));
          SetProfitTarget("", CalculationMode.Ticks, Target);
          SetStopLoss("", CalculationMode.Ticks, StopLoss, false);

          CalculateOnBarClose = true;
          }

          /// <summary>
          /// Called on each bar update event (incoming tick)
          /// </summary>
          protected override void OnBarUpdate()
          {
          if (Close[0] > Position.AvgPrice + 10 * TickSize)
          {
          SetStopLoss(CalculationMode.Price, Position.AvgPrice + 1 * TickSize);
          }
          // Condition set 1
          if (CrossAbove(Close, SMA(Sma), 1))
          {
          EnterLong(DefaultQuantity, "");
          }

          // Condition set 2
          if (CrossBelow(Close, SMA(Sma), 1))
          {
          EnterShort(DefaultQuantity, "");
          }

          }
          Last edited by mario2306; 01-02-2014, 05:05 AM.

          Comment


            #6
            Hello mario2306,

            Thanks for your reply.

            Please send yesterday's log and trace files to support[at]ninjatrader[dot]com so I may look into this further.

            You will find the log file on your PC in the (My) Documents > NinjaTrader 7 > Log folder.

            The log file will be named "log.20140101.txt"
            You will find the trace file on your PC in the (My) Documents > NinjaTrader 7 > Trace folder.

            The trace file will be named "trace.20140101.txt"

            Once located please send both files as attachments to your email.
            In the message put ATTN Brandon and provide a link to this forum topic.
            BrandonNinjaTrader Customer Service

            Comment


              #7
              I did what you say.
              I put a zip file, may help, but without any modification.
              Maybe someone can do the modification and put it here in zip format.
              I want to have a breakeven + 1 tick when i have 10 ticks of profit, but rest to be the same.
              Thank you.
              Attached Files
              Last edited by mario2306; 01-03-2014, 05:56 AM.

              Comment


                #8
                Hello mario2306,

                Thanks for your reply.

                I have not received the requested files. Did another support representative reply to you? If so what ticket number is it under?
                BrandonNinjaTrader Customer Service

                Comment


                  #9
                  Nobody reply.
                  I wanted to send mail to you but i can not.
                  I do not know way.
                  I trade Gold, but the time i do not remember, because i tried to use the strategy with modification and when i saw it does not work i wiped the modification, and after that i did another modification in another place, i tried again, and so.
                  It is better if you can to unlock the code, do the modification and send it to me back in zip format to test the strategy.
                  Thank you very much.

                  Comment


                    #10
                    Hello mario2306,

                    Thanks for your reply.

                    It seems a bit more needs to be added to get the strategy to work.
                    Attached I have provided a strategy to do this that you may view the code.
                    Attached Files
                    BrandonNinjaTrader Customer Service

                    Comment


                      #11
                      Brandon i tried your startegy ATMstop2.
                      When i press enable, worked, when he reached 10 ticks in profit the stop loss not move to breakeven + 1 tick and when i had to go from short position to long position i got two error messages and strategy closed.
                      I put this two error messages bellow:
                      1- Strategy 'ATMstop2/ab8bfbf12b56400d9950abad568127b8' submitted an order that generated the following error 'OrderRejected'. Strategy has sent cancel requests attempted to close the position and terminated itself.
                      2- Sell stop or sell stop limit orders can't be placed above the market. affected Order: Sell 1 Stop @ 1239

                      Comment


                        #12
                        Hello mario2306,

                        Thanks for you reply.

                        Please send me your log and trace files so I may look into this further.
                        To do this go to Help> Mail to Support
                        In the message put ATTN Brandon and include a link to this forum topic.
                        Also let me know what SMA, StopLoss, and Target values were being used.
                        BrandonNinjaTrader Customer Service

                        Comment


                          #13
                          How to set Be+1 trading 2 contracts

                          Good day I wrote a code to trade 2 contracts with the same StopLosses (SL) and different ProfitTargets (PT). When first PT is hit I want to move SL of the second contract to BE+1. Please find below the code. I have problem to define reset for SL when position is flat and moving second SL to BE+1. Thanks in advance for your advice. Zdenek

                          #region Variables
                          // Wizard generated variables
                          private int profitTarget1 = 10; // Default setting for ProfitTarget1
                          private int profitTarget2 = 40; // Default setting for ProfitTarget2
                          private int stopLoss1 = 20; // Default setting for StopLoss1
                          private int stopLoss2 = 20; // Default setting for StopLoss2
                          private int cciPeriod = 14; // Default setting for CciPeriod
                          private int cciValue = -250; // Default setting for CciValue
                          // private int zahajeni = 1; // Default setting for Zahajeni
                          // private int zakonceni = 1; // Default setting for Zakonceni
                          // User defined variables (add any user defined variables below)
                          #endregion

                          /// <summary>
                          /// This method is used to configure the strategy and is called once before any strategy method is called.
                          /// </summary>
                          protected override void Initialize()
                          {
                          Add(CCI(CciPeriod));
                          SetProfitTarget("Long1", CalculationMode.Ticks, ProfitTarget1);
                          SetProfitTarget("Long2", CalculationMode.Ticks, ProfitTarget2);
                          SetStopLoss("Long1", CalculationMode.Ticks, StopLoss1, false);
                          SetStopLoss("Long2", CalculationMode.Ticks, StopLoss2, false);

                          CalculateOnBarClose = true;
                          }

                          /// <summary>
                          /// Called on each bar update event (incoming tick)
                          /// </summary>
                          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, StopLoss1);
                          SetStopLoss(CalculationMode.Ticks, StopLoss2);
                          }
                          // If a long position is open, allow for stop loss modification to breakeven
                          else if (Close[0] > Position.AvgPrice + ProfitTarget1)
                          SetStopLoss("Long2", CalculationMode.Price, Position.AvgPrice+(1*TickSize), false);

                          // Condition set 1
                          if (CrossAbove(CCI(CciPeriod), CciValue, 1))
                          {
                          EnterLong(DefaultQuantity, "Long1");
                          EnterLong(DefaultQuantity, "Long2");

                          }
                          }

                          Comment


                            #14
                            Hi squashfan,

                            Thanks for your post.

                            To detect when the price of the profit target has been touched and move the stop loss to breakeven +1 use:
                            Code:
                            else if (Close[0] > Position.AvgPrice + ProfitTarget1 [B]* TickSize[/B])
                            SetStopLoss("Long2", CalculationMode.Price, Position.AvgPrice+TickSize, false);
                            To reset using signal names when flat:
                            Code:
                            if (Position.MarketPosition == MarketPosition.Flat)
                            {
                            SetStopLoss("Long1", CalculationMode.Ticks, StopLoss1, false);
                            SetStopLoss("Long2", CalculationMode.Ticks, StopLoss2, false);
                            }
                            Let me know if this is not what you are looking for.
                            Chelsea B.NinjaTrader Customer Service

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by cre8able, Today, 03:20 PM
                            0 responses
                            5 views
                            0 likes
                            Last Post cre8able  
                            Started by Fran888, 02-16-2024, 10:48 AM
                            3 responses
                            47 views
                            0 likes
                            Last Post Sam2515
                            by Sam2515
                             
                            Started by martin70, 03-24-2023, 04:58 AM
                            15 responses
                            114 views
                            0 likes
                            Last Post NinjaTrader_Jesse  
                            Started by The_Sec, Today, 02:29 PM
                            1 response
                            7 views
                            0 likes
                            Last Post NinjaTrader_Jesse  
                            Started by jeronymite, 04-12-2024, 04:26 PM
                            2 responses
                            31 views
                            0 likes
                            Last Post NinjaTrader_BrandonH  
                            Working...
                            X