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

ExitLongStopMarket

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

    ExitLongStopMarket

    Hello,

    I tried to create an ExitLongStopMarket order to exit a Trade if its falling below the stop order but is still profitable.
    For example:
    I entered the trade at 100
    if its 120 I want to create a ExitLongStopMarket order at 110

    EnterLong(); =>Long_Entry_Value (= 100)
    ExitLongStopMarket(Long_Entry_Value + 10);

    The problem now is, that it doesn't use the stop order, instead, it goes below and closes with the stoploss.

    #2
    Hello Stefan1999,

    Thank you for your post.

    Would you be able to provide a snippet of the code you have or screenshots of your conditions and actions in the Strategy Builder if you're using that?

    Thanks in advance; I look forward to assisting you further.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Hello Kate,

      my code looks like this, with also a stop loss and a profit target.
      So I want to create like a positive stop loss.

      if (CrossAbove(SMA(1), SMA(10),1))
      {
      EnterLong();
      Long_Entry = Close[0]; (private double)
      }

      if ((Long_Entry + 2) < Low[0])
      ExitLongStopMarket(Long_Entry + 0.25);


      Thanks for helping

      Comment


        #4
        Hello Stefan1999,

        Thank you for your reply.

        It sounds like what you'd really want to do here is to move your existing stop loss to 1 tick above the average entry price once two points in profit is reached.

        I'm attaching a modified version of a example from our help guide that will move a stop to breakeven + 1 tick after 2 points (8 ticks) in profit. (This example assumes you're using an instrument where 1 point = 4 ticks, such as the ES). I'd test this out and then take a look at how the logic works.

        Please let us know if we may be of further assistance to you.
        Attached Files
        Kate W.NinjaTrader Customer Service

        Comment


          #5
          Hello Kate,

          it worked perfectly!

          Thanks a lot

          Comment


            #6
            Hello Kate,

            I have some Problems using past data, like High[2] - High[1], or SMA(1)[1].

            Example: BarHight1 = Open[1] - Close[1];

            If I use this in the backtest, the strategy doesn't function at all.

            Thanks in advance

            Comment


              #7
              Hello Stefan1999,

              Thank you for your note.

              Most likely, you're running into an error on bar 0, is that correct?

              It's important to understand that when a strategy starts processing, it starts at the oldest bar loaded on the chart. If you ask it what the high of two bars ago is on that very oldest bar, there are no bars two bars before it for that to process on, and an error would be thrown.

              You would need to tell it not to process until at least the number of bars needed for your calculations have been processed.

              Here's a link to our help guide that goes over how to make sure enough bars have been processed on the chart for your script to begin its calculations:



              Please let us know if we may be of further assistance to you.
              Kate W.NinjaTrader Customer Service

              Comment


                #8
                Hello Kate,

                thanks for the advice.

                I have a small issue with the entrance of a trade.

                If I test my strategy with a demo-account the strategy always enters the trade with up to 6 ticks slippage. Is there a function, or command where I can set a maximum, where my strategy must stop buying.

                Example: EnterLongLimit(Close[0]); The strategy enters the trade with Close[0] + 4Ticks (most times it enters correctly with Close[0])

                Best regards
                Stefan

                Comment


                  #9
                  Hello Stefan,

                  Thank you for your note.

                  Would you be able to provide a screenshot of one of these trades? A code snippet of your script where it places the limit order would be helpful as well.

                  Thanks in advance; I look forward to assisting you further.
                  Kate W.NinjaTrader Customer Service

                  Comment


                    #10
                    Hello Kate,

                    to measure the slippage from my strategy (Close[1] - Position.AveragePrice, the limit-order should by wen Close[0] is hit) I had printed out every slippage and most times it was around 2 ticks, which is ok, but there are some times, where it is over 6 ticks.

                    EnterLongLimit(Close[0] - 0.25); //He should by at Close[0] - 025
                    double Entry = Close[0] - 0.25;

                    double slippage = Position.AveragePrice - Entry;

                    Normally the slippage is around 2 ticks

                    Is there something I can do to prevent slippage over 4 ticks from happening?



                    During testing with a demo account I discovered, that the strategy behaves very differently between live trading and the backtest.

                    In the appendix are two images which sho the same time segment from the same strategy test live with a demo account and with the backtest.
                    Cloud this be because I use HeikinAshi as type in both instead of Minute?

                    Thanks in advance
                    Attached Files

                    Comment


                      #11
                      Hello Stefan1999,

                      Thank you for your reply.

                      I've created a very simple test strategy that places an EnterLongLimit order just as you're seeing above, and running it On Bar Close produces orders that are placed on the bar after they are triggered, one tick below the close of the previous bar as I would expect. I've attached the test Strategy below so you can test on your end.

                      It's important to understand that if you run this on each tick or on price change that the strategy will submit the order at 1 tick below the current price as that is currently the closing price for the bar. Since this can vary throughout when the bars are built, the price the order is submitted at may not be the same as 1 tick below the final close price of the bar.

                      You should expect that a strategy running real-time (live brokerage account, live market simulation, Market Replay etc...) will produce different results than the performance results generated during a backtest. This difference may be more easily seen on certain Bars types (e.g. Point and Figure, Heiken Ashi) than others due to their inherent nature in bar formation.

                      During a backtest you can select conservative or liberal fill algorithms which will produce different results. Fills are determined based on 4 data points, OHLC of a bar since that is the only information that is known during a backtest and there will be no intra-bar data. This means actions cannot happen intra-bar, fills cannot happen intra-bar. All prices and actions come from and occur when the bar closes as this is all the information that is known.

                      Because of this, OnBarUpdate will only update 'On bar close' as it does not have the intra-bar information necessary for 'On price change' or 'On each tick'.

                      Also, here is a link to the differences on real-time vs backtest (historical).


                      Adding intra-bar granularity can help with this.

                      Intra-bar granularity adds a second data series such as a 1 tick series so that the strategy has finer granularity in the historical data in between the OHLC of the primary series. This allows for more accurate trades by supplying the correct price at the correct time for the order to fill with.

                      In NinjaTrader 8, there have been two new enhancements so that programmers do not have to manually add this secondary series and code the script to for high accuracy fills (Order Fill Resolution) and for intra-bar actions (TickReplay).

                      Here is a link to our forums that goes into depth on using Order Fill Resolution and Tick Replay to ensure your backtests are as close to real time results as possible:

                      Citizens of the NinjaTrader Community, A common question we hear from clients is 'why are results from backtest different from real-time or from market replay?'. Live orders are filled on an exchange with a trading partner on an agreed upon price based on market dynamics. Backtest orders are not using these market dynamics.


                      High Fill Order Resolution and TickReplay cannot be used together. If it is necessary to have both, it is still possible to add intra-bar granularity to a script in the code itself for order fill accuracy and use TickReplay to update indicators with Calculate set to OnPriceChange or OnEachTick historically.

                      Please let us know if we may be of further assistance to you.
                      Attached Files
                      Kate W.NinjaTrader Customer Service

                      Comment


                        #12
                        Hello Kate,

                        I have written a test strategy, to test the slippage and the entries of the strategy. In order to do that, the strategy prints all necessary data in a txt file. After one day of testing, I discovered a difference between the bar hight between the printed data and the chart.

                        I think this lies on the data series (I use HeikinAshi), because in the Backtest the strategy does what it should.

                        It should buy if two Smas crosses and the previous is bigger than 4 Ticks, but also buys if the bar is under 4 Ticks in size and some times even if it didn't cross.

                        I will give you my program in the appendix to test.

                        Best regards Stefan
                        Attached Files

                        Comment


                          #13
                          Hello Stefan1999,

                          Thank you for your patience.

                          I've been testing this today on the ES 09-20 and have yet to see it take a trade if the signal bar does not have a crossover within 1 bar or if the signal bar is smaller than 4 ticks. So I can best try to reproduce, on what day and time was the last screenshot you posted, what is the base period type and value for the Heiken Ashi bars you are using?

                          Thanks in advance; I look forward to assisting you further.
                          Kate W.NinjaTrader Customer Service

                          Comment


                            #14
                            Hello Kate,

                            this is strange. I tested it today again and it didn't perform as it should. I use HeikinAshi bars (1 Minute) in the strategy window and also in the backtest and chart.
                            I have some images for you with the time stamp. They might be off due to the time difference ( timezone Berlin).
                            In the file, I attached to the strategy it prints out every entry trade with a completely different bar hight.

                            It must be a problem with the data series, because in the backtest it works, how it should, with no problems (also the bar height ist correct, which is printed to the file).

                            Thanks for helping
                            Stefan
                            Attached Files

                            Comment


                              #15
                              Hello Stefan1999,

                              Thank you for your note.

                              I was able to locate the issue with the bar height, although I'm uncertain why it was working for you in a backtest.

                              In your code when you are calculating the bar height it's in ticks. When you're using it in a condition for entry though, you're multiplying the number of ticks you want it to be more than by the tick size, which makes it think you want it to just be greater than 4 * TickSize or 1 tick.

                              Try modifying the entry conditions as follows:

                              Code:
                                          if (CrossAbove(SMA(Sma1), SMA(Sma2), 1) && Barhight > 4)        //Enters trade if Sma1 crosses above Sma2 & Barhight is bigger than 4 Ticks
                                          {
                                              EnterLong();
                                              count = 1;        //Sets count to 1 for append file
                                          }
                                          if (CrossBelow(SMA(Sma1), SMA(Sma2), 1) && Barhight > 4)     //Enters trade if Sma1 crosses below Sma2 & Barhight is bigger than 4 Ticks
                                          {
                                              EnterShort();
                                              count = 1;      //Sets count to 1 for append file
                                          }
                              I also note that you have 3 SMAs set up but you're only using the first two within the script. I'd advise you to remove the third, at least temporarily, so it's easier to see where crossovers occur.

                              Thanks in advance; I look forward to assisting you further.
                              Kate W.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by gentlebenthebear, Today, 01:30 AM
                              2 responses
                              13 views
                              0 likes
                              Last Post gentlebenthebear  
                              Started by Kaledus, Today, 01:29 PM
                              2 responses
                              7 views
                              0 likes
                              Last Post Kaledus
                              by Kaledus
                               
                              Started by frankthearm, Yesterday, 09:08 AM
                              13 responses
                              45 views
                              0 likes
                              Last Post frankthearm  
                              Started by PaulMohn, Today, 12:36 PM
                              2 responses
                              16 views
                              0 likes
                              Last Post PaulMohn  
                              Started by Conceptzx, 10-11-2022, 06:38 AM
                              2 responses
                              56 views
                              0 likes
                              Last Post PhillT
                              by PhillT
                               
                              Working...
                              X