Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Position.Breakeven() doesn't work! Beta 15.

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

    Position.Breakeven() doesn't work! Beta 15.

    Position.Breakeven() works neither Backtest nor MarketReplay.
    I use something like:
    Code:
    protected override void Initialize()
    {
        SetStopLoss(Stop, false);
    }
    protected override void OnBarUpdate()
    {
    [COLOR=black][FONT=Verdana]   if(BarsSinceEntry() >= 1)[/FONT][/COLOR]
    [FONT=Verdana][COLOR=black]   {[/COLOR][/FONT]
    [FONT=Verdana][COLOR=black]        PrintWithTimeStamp("Breakeven");    [/COLOR][/FONT]
    [FONT=Verdana][COLOR=black]        Position.Breakeven(); //In this point nothing happens[/COLOR][/FONT]
    [FONT=Verdana][COLOR=black]        DrawDot("Breakeven" + CurrentBar, true, 0, Position.AvgPrice, Color.Orange);[/COLOR][/FONT]
    [FONT=Verdana][COLOR=black]   }[/COLOR][/FONT]

    #2
    EloyOrion,

    Position.Breakeven is not supported. If you want to check for prices you will need to check your entry price via Position.AvgPrice and check against the current traded price via Close[0].
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Oops, Josh, Position.Breakeven() is a NinjaScript method that modifiers all stop loss orders to the position’s average entry price. If it not supported, then it should not appear in the NinjaScript IntelliSense, IMHO. I need to move the stop in Breakeven. What can I do in this case?

      Comment


        #4
        EloyOrion,

        There are a lot of underlying internals that appear in NinjaScript which are not supported. They will show up, but use of them is not supported by us and people who do use them use it with that understanding and do so as their own endeavours.

        If you want to move a stop to breakeven, please see this reference sample: http://www.ninjatrader-support2.com/...ead.php?t=3222
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Thank you Josh. This is what I need!

          Comment


            #6
            Does it work really on each incoming tick?

            Hello Guys

            I appreciate the sample code, how to move stop order to break-even, but I'm not sure whether it works correctly on each incoming tick.

            For instance if user wants to move stop order to break-even if 50 ticks of profit is reached on 15 min chart, it can happen following:

            Within the 15min, 50 tick profit is reached, but bar finally closes at 48tick profit. Then the condition test will fail, am I right?

            if (Close[0] > Position.AvgPrice + 50 * TickSize) // this will fail because profit is only 48 ticks

            If I'm right, is there any way how to achieve a move o stop loss order on historical data on minute chart with each tick, but the strategy itself has set

            CalculateOnBarClose = true;


            ??

            Thank you for the answer.
            Last edited by mmltrading; 07-04-2010, 02:48 PM.

            Comment


              #7
              mmltrading,

              If you are using CalculateOnBarClose = true you will not be able to update your orders on each tick. You will need to use CalculateOnBarClose = false in real-time to achieve it to move on each incoming tick.
              Josh P.NinjaTrader Customer Service

              Comment


                #8
                How to find close of the Bar with CalculateOnBarClose = false

                Hello Josh

                Thank you for the answer, so my question now is how to find out which tick is the close of the bar if CalculateOnBarClose = false? Is there any way how to find out this for instance for minute and range charts?

                Martin

                Comment


                  #9
                  Martin,

                  The closing event of a bar is the same event as the opening of a new bar. You can use FirstTickOfBar to find it. When you use this, change your [0] indexing to [1] to get the close of the bar that just closed.

                  Code:
                  if (FirstTickOfBar)
                  {
                       // Do something with Close[1];
                  }
                  This would be the same regardless of minute/range bars.
                  Josh P.NinjaTrader Customer Service

                  Comment


                    #10
                    Josh

                    Thank you, that is what I need. My last questions are whether Ninja supports multiple time frames on historical data (backtest / optimizer) or it just works in real-time / simulated trading?

                    If the answer is yes (it's supported) and I run bactest on 3min time-frame and add another time-frame to the strategy as Add(PeriodType.Tick, 1); the 3min timeframe is the primary array of bars (BarsInProgress = 0)?

                    My problem is I need for instance to enter into trade (long) in following case:

                    Close[0] > Close[1] // on 3 min chart
                    Then when I'm in trade and have exactly 20tick (that's why I'm adding tick ) of profit I need to move stoploss to breakEven + 10 tick.

                    I need my move of the stop loss work in the same way as SetStopLoss(...) - with each incoming tick. Is this possible?

                    Comment


                      #11
                      mmltrading, it is definitely possible to backtest multiple time frames. You are correct with your BarsArray observation.

                      If you want to keep the calculations on close separate from the tick calculations, please see this reference sample: Separating logic to either calculate once on bar close or on every tick.

                      For actually moving the stop, this link, posted earlier in the thread, goes over how to move the stop loss: http://www.ninjatrader-support2.com/...ead.php?t=3222.
                      AustinNinjaTrader Customer Service

                      Comment


                        #12
                        Hello Josh

                        Your replies are very helpful. My strategy is ready and working except one thing.
                        As you know I have CalculateOnBarClose = false; // calculating on each tick

                        I need in OnBarUpdate() on each incoming tick to get actual (current) market price to be able to calculate current profit/loss (from Position.AvgPrice). The problem I'm dealing with is, that Position.GetProfitLoss() needs to supply some value to calculate it. I can calculate myself, it's easy, but the problem is I cannot find a property how to get actual Market price. Could you help me with this?

                        Thanks Martin

                        Comment


                          #13
                          Originally posted by mmltrading View Post
                          Hello Josh

                          Your replies are very helpful. My strategy is ready and working except one thing.
                          As you know I have CalculateOnBarClose = false; // calculating on each tick

                          I need in OnBarUpdate() on each incoming tick to get actual (current) market price to be able to calculate current profit/loss (from Position.AvgPrice). The problem I'm dealing with is, that Position.GetProfitLoss() needs to supply some value to calculate it. I can calculate myself, it's easy, but the problem is I cannot find a property how to get actual Market price. Could you help me with this?

                          Thanks Martin
                          Close[0] would be the actual market price
                          ArtSenior Software Developer

                          Comment


                            #14
                            That is strange. Following code will always shows me only Close price of each 3 minute bar (when backtesting on historical 3 min data). Not prices with difference of one tick, for instance on TF instrument it's 0.1 point. It shows me values with difference sometimes even more than 1 point and when I checked the chart, it corresponds with the close of 3 minute bar.

                            protected override void Initialize()
                            {
                            CalculateOnBarClose = false;
                            }

                            protected override void OnBarUpdate()
                            {
                            System.Windows.Forms. MessageBox.Show(Close[0].ToString());
                            }

                            I've checked my data and I have tick data of this instrument in the time I'm backtesting. What I'm doing wrong? I have Ninja 7.0.0.18

                            Comment


                              #15
                              Originally posted by mmltrading View Post
                              That is strange. Following code will always shows me only Close price of each 3 minute bar (when backtesting on historical 3 min data). Not prices with difference of one tick, for instance on TF instrument it's 0.1 point. It shows me values with difference sometimes even more than 1 point and when I checked the chart, it corresponds with the close of 3 minute bar.

                              protected override void Initialize()
                              {
                              CalculateOnBarClose = false;
                              }

                              protected override void OnBarUpdate()
                              {
                              System.Windows.Forms. MessageBox.Show(Close[0].ToString());
                              }

                              I've checked my data and I have tick data of this instrument in the time I'm backtesting. What I'm doing wrong? I have Ninja 7.0.0.18
                              CalculateOnBarClose = false will provide "tick by tick" event on real time ticks only. On historical data you will still be limited to one event per bar.
                              ArtSenior Software Developer

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              606 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              353 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              105 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              560 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              561 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X