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

stoploss same bar

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

    stoploss same bar

    hi,

    what's the best practice for stop-loss execution that reflects the correct order in which the data formed the Close[0] bar? the problem I am seeing in one of my system is that, many time, especially when I run an optimiser in strategy analyser, the system is giving me entry and exit on the same bar (say 5 min bar).

    My setting is:

    Code:
    Calculate= Calculate.OnBarClose;
    I'm NOT using tick replay during the strategy analyser. My stop-loss is:

    Code:
    			else if (State == State.Configure)
    			{
    				AddDataSeries(Data.BarsPeriodType.Day, 1);
    				AddDataSeries(Data.BarsPeriodType.Week, 1);
    				SetProfitTarget("Target1", CalculationMode.Ticks, Target1*10);
    				SetProfitTarget("Target2", CalculationMode.Ticks, Target2*10);
    				SetProfitTarget("Target3", CalculationMode.Ticks, Target3*10);
    				SetProfitTarget("Target1S", CalculationMode.Ticks, Target1*10);
    				SetProfitTarget("Target2S", CalculationMode.Ticks, Target2*10);
    				SetProfitTarget("Target3S", CalculationMode.Ticks, Target3*10);
    				SetStopLoss(CalculationMode.Ticks, ST*10);
    			}
    I know ninja trader 7 had issues such as entry the one i'm discussing in this post but I thought n8 was suppose to get rid of those issues through their "intrabar formation" system? Is n8 still using high/low/open/close of the bar without knowing the order in which they formed the bar? I saw the sample file regarding intrabar execution, would that be the only solution (more coding)? My problem is not that my stop-loss is being hit on the same particular bar, I just don't trust the fact that my stop-loss is being executed correctly. In other words, suppose the following time stamp for a 5 min bar

    5 min bar open = 1.67
    5 min bar high = 2
    5 min bar low = 1
    5 min bar close = 1.67

    and my entry was 1.55 and my stop-loss was 1, but my entry happened AFTER the low had occurred. So in this example, based on the configurations I mentioned above, is my stop-loss in n8 being hit?

    If it is being hit and there is not other solution besides following your recommended intrabar sample file, tick replay, or market replay; would the below solution suffice if I want my stops to be executed in the correct order? Theoretically even the below is not sufficient because I'm waiting an entire bar to be completed before my stop-loss order is going to be submitted.

    Code:
    if( condition)
    
    EnterShort(...);
    
    if(Position.MarketPosition == MarketPosition.Short && BarsSinceEntry() >1)
    
    ExitShortStop(...);
    The problem that I can think of in the above suggestion I made is that, since I am setting a limit stop-loss, what happens if after 1 bar, the price has gone far away from my limit, will I have to add further code to check to see if my order was filled and then turn it into an market order? Again, more codding, can someone please make an optimal solution for my problem?

    Another solution that might work:

    Code:
    		private void StopManager()
    		
    		{		
    			if (Position.MarketPosition == MarketPosition.Flat)
    		
    			{
    				SetStopLoss(CalculationMode.Ticks, ST*10);
    			}
    			
    			else if (Position.MarketPosition == MarketPosition.Long)
    			
    			{
    				if (Position.MarketPosition == MarketPosition.Long
    					&& Close[0] < Position.AveragePrice)
    			{
    				SetStopLoss(CalculationMode.Ticks, ST*10);
    			}
    			
    			else if (Position.MarketPosition == MarketPosition.Long
    				&& Close[0] > Position.AveragePrice + Target1 * 0.0001)
    			{
    				SetStopLoss(CalculationMode.Price, Position.AveragePrice);
    			}
    			
    			else if (Position.MarketPosition == MarketPosition.Long
    				&& Close[0] > Position.AveragePrice + Target2 * 0.0001)
    				{
    					SetStopLoss(CalculationMode.Price, Position.AveragePrice + Target1 * 0.0001);
    				}
    			}
    Last edited by staycool3_a; 12-06-2017, 12:23 AM.

    #2
    Hello,

    Thank you for the post.

    You had noted:
    the problem I am seeing in one of my system is that, many time, especially when I run an optimizer in strategy analyzer, the system is giving me entry and exit on the same bar (say 5 min bar).
    My problem is not that my stop-loss is being hit on the same particular bar, I just don't trust the fact that my stop-loss is being executed correctly.
    Just to clarify, are you having this happen or is this hypothetical? Was this happening in NT7 but has not in NT8 and now you are just skeptical of the NT8 fill logic?

    If you are seeing this happen, have you at this point tried to use the explanation of the fill logic in reference to said orders to see if the backfill logic was followed as expected? you can find a walkthrough in the following documentation:


    If you are comparing some specific orders, I would suggest walking through the fill guidelines with the orders you may be referring to in order to compare what happened. There are multiple steps to a fill, it would really depend if you are speaking of a hypothetical situation in your example or you are comparing actual orders that happened historically in the test. If you are comparing actual orders, the fill logic page should be able to be used to follow the process that leads to the fill with the price data used.



    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      Hello,

      Thank you for the post.

      You had noted:


      Just to clarify, are you having this happen or is this hypothetical? Was this happening in NT7 but has not in NT8 and now you are just skeptical of the NT8 fill logic?

      If you are seeing this happen, have you at this point tried to use the explanation of the fill logic in reference to said orders to see if the backfill logic was followed as expected? you can find a walkthrough in the following documentation:


      If you are comparing some specific orders, I would suggest walking through the fill guidelines with the orders you may be referring to in order to compare what happened. There are multiple steps to a fill, it would really depend if you are speaking of a hypothetical situation in your example or you are comparing actual orders that happened historically in the test. If you are comparing actual orders, the fill logic page should be able to be used to follow the process that leads to the fill with the price data used.



      I look forward to being of further assistance.
      It's not a hypothetical scenario. I'm experiencing this in my strategy while doing backtesting. My strategy is entering the trade and then my stop-loss is being hit on the same bar.

      I read the entire historical backfill logic and to be honest it's causing me to be even more sceptical of the fills I am getting. The entire way the the new high and new low are being calculated during your 1st,2nd and third pass, as a logic it makes sense, but in testing strategies that use (for example) 124 minute bars, it can cause issues because a 124 bar is a 2 hour bar and it can potentially have a range of 50 ticks. And then the way you guys are deciding to use min/maxes between the "new high" or "new low" and have three different passes; seems extremely confusing and again causes me to think that my fills are just randomly being choosen (based on your logic) and your logic has no way of knowing if after my entry, my stop-loss was hit or not - but according to your logic, it may or may not hit, based on the "new high" or "new low."

      But this is your software and I'm sure there is a reason for it and I'm not going to sit here and argue. My only question is trying to get accurate fills. What are the best practices that you propose, or anyone else in the community proposes?

      Most of my strategies use minute bars that are greater than 10 minutes. So would it be best practice to make the "primary" bar 1 minute and generate my orders on the higher time frame (10 minute for example) and then receive fills based on 1 minute? That way, based on the n8 backfill logic, at least there is added accuracy?

      OR

      Is the best solution to simply get an extremely expensive computer witha mega large hard-drive and while using strategy analyser, run the test using 1 tick data as the primary and generate orders at a higher time frame based on your strategy?


      I just want an honest solution here guys. Clearly there is a problem and I'd appreciate a solution.

      I understand that backtesting is to get an "idea" to see if your strategy is going to work in real-time. And then in real-time it may/may not work. But to test an idea, it's essential to get fills that make sense.

      If there is anyone out there that can offer a proposal - that'd be greatly appreciate. Tired of testing strategies and going through each trade on the chart and trades log to see if the trades would have actually gotten the fills or not.
      Last edited by staycool3_a; 12-06-2017, 11:38 PM.

      Comment


        #4
        Not sure it this is what you're asking, but if you use tight stops and the possibility exists that you can get stopped out on the same bar you're entering on, then yes using 1minute data for the strategy analyser run through is the most accurate way to backtest without tick data.

        For example, I have some breakout strategies that use buystops where the possibility exists in realtime to get stopped out on the same bar as the entry. I run the backtest on 1min even though I may use entry logic based off a higher time frame chart. You can then look at a few trades on the chart and get a pretty good idea if your stop is a safe way away in most situations. Then of course test with realtime data on demo to double check.

        The other option is to design strategies that enter on bar close. This makes the backtests most accurate (barring slippage/spreads etc), as this mimics the way NT reads historical data i.e. OHLC.

        Comment


          #5
          Originally posted by pmn100 View Post
          Not sure it this is what you're asking, but if you use tight stops and the possibility exists that you can get stopped out on the same bar you're entering on, then yes using 1minute data for the strategy analyser run through is the most accurate way to backtest without tick data.

          For example, I have some breakout strategies that use buystops where the possibility exists in realtime to get stopped out on the same bar as the entry. I run the backtest on 1min even though I may use entry logic based off a higher time frame chart. You can then look at a few trades on the chart and get a pretty good idea if your stop is a safe way away in most situations. Then of course test with realtime data on demo to double check.

          The other option is to design strategies that enter on bar close. This makes the backtests most accurate (barring slippage/spreads etc), as this mimics the way NT reads historical data i.e. OHLC.
          THanks for the suggestion. That's exactly what I'm doing now, went back to all my strategies and added a secondary tick data series and am executing my orders on tick data and deriving my signals from higher time frames. I think that's the best solution.

          Comment


            #6
            Originally posted by calhawk01 View Post
            THanks for the suggestion. That's exactly what I'm doing now, went back to all my strategies and added a secondary tick data series and am executing my orders on tick data and deriving my signals from higher time frames. I think that's the best solution.
            Are you getting stopped out and re entered multiple times inside the stop loss range you have set? This is what is happening on one strategy I'm testing right now and it's puzzling the heck out of me. Seems to happen at market speed or sped up. at 1 minute time frames and multiple minute time frames the same way. I suspect it's something I've goofed up in the code but am puzzled what it might be.

            Comment


              #7
              I have some quick general / theoretiacal questions for my understanding on that regards:

              I have a managed strategy that calculates on Bar close and that runs with a tick size of lets say 1200Ticks. Sometimes I noticed, that for example I am in a Long position and in the same Bar the Strategy swaps into a short position as well as the Stop Loss (of my long position) happens in the same Bar. Then the following happens: The result is that the Strategy ovewiev window shows me "position" 1Short / Account position 2Short and a Sync "False"

              I guess this happens when the same (swap from long to short as well as the stop loss long) happens in the same bar with calculation on bar close.

              Now my 2 questions on that:
              1. Would this issue not occur if I'd change the calculation mode from on Bar close to on each tick? (Because then he'd realize either that the position swap has occured and set a new S/L based on the new entry)? Or other way round the S/L has occured already and threfor no position swap just a short buy will be required

              2. If the above solution would be to set "calculate on each tick" then why when backtesting my strategies with the settings "on bar close" and "on each tick" deliver me the same results?

              Thanks for a quick explanation on that :-)

              Comment


                #8
                Hello Coral,

                You may be attempting to call entry and exit orders on the same bar. The position does not immediately change as it takes time for orders to fill, and this will happen after the code on the next lines are evaluated.



                Changing to Calculate.OnEachTick would not change this if orders are still being submitted at the same time. So it would depend on what the logic is on whether this would have any change.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Thank you Chelsea this is exactly my issue.

                  I've read the posts, but I still cannot figure out how to solve that properly with the managed approach & strategy builder and (calculating on bar close).

                  Would it be possible to explain me an easy way (and how to set that up with the strategy builder) how to avoid that the stop loss is triggererd in the same bar where the position swap (from log to short or viceversa) occurs?

                  - (Something like "If there is a strategy posision swap in this bar then ignoere Stop Loss in the same current bar").

                  Thank you for any hint on that :-)
                  Last edited by Coral; 03-30-2021, 06:28 AM.

                  Comment


                    #10
                    Hello Coral,

                    The logic of the strategy would control this.

                    If a condition is triggering an exit, the conditions to trigger an entry would explicitly exclude the conditions from the first condition set so that both conditions are not true at the same time, and prevent both orders from being submitted.

                    A stop loss (added on the Stops and Targets) cannot be unset or cancelled. An exit order, like 'Exit long position with a stop order' will be automatically cancelled if not submitted on each new bar. The logic can decide if a stop order is submitted.

                    The position will not have changed if you are calling multiple orders on the same bar. So checking the position, would not correct this. However, the position can be checked under Strategy -> Current market position.
                    Chelsea B.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by sidlercom80, 10-28-2023, 08:49 AM
                    168 responses
                    2,261 views
                    0 likes
                    Last Post sidlercom80  
                    Started by Barry Milan, Yesterday, 10:35 PM
                    3 responses
                    10 views
                    0 likes
                    Last Post NinjaTrader_Manfred  
                    Started by WeyldFalcon, 12-10-2020, 06:48 PM
                    14 responses
                    1,428 views
                    0 likes
                    Last Post Handclap0241  
                    Started by DJ888, 04-16-2024, 06:09 PM
                    2 responses
                    9 views
                    0 likes
                    Last Post DJ888
                    by DJ888
                     
                    Started by jeronymite, 04-12-2024, 04:26 PM
                    3 responses
                    41 views
                    0 likes
                    Last Post jeronymite  
                    Working...
                    X