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

IsFirstTickOfBar & Reverse Position

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

    IsFirstTickOfBar & Reverse Position

    Hi,

    1. Look at first attachment; According to NT #1 dot is the first tick of bar. But this is not make sense for me and #2 dot must be first tick of bar. Which is the right?

    2. I wrote sample strategy and want to reverse position when;

    A. Close[0] > Open[0] && Close[1] < Open[1] then EnterLong
    B. Close[0] < Open[0] && Close[1] > Open[1] then EnterShort

    Look at my second attachment, there is a short position, a few red bars later when i see green candle i want to reverse from short to long my position (order price must be close of green candle which is in a circle on attachment). I wrote related code but NT reversing position candle's open data. First question because of this issue. How can i solve this issue?

    PS: If i use 1 minute data, logic working correctly but when i try use not time based data for ex. renko bar type i cant reverse position from level that i want to reverse (close of reverse signal bar).

    Regards,
    Aytac
    Attached Files
    Last edited by aytacasan; 02-18-2017, 01:01 PM.

    #2
    Ok, i think i understand reason of my issue... I set order fill resolution from standard to high. After that, orders almost fill first tick of bar. But again not exactly first tick of bar. Maybe because of NT trying to simulate realistic back-test (actually not because most of the time orders executing both side (enter&exit) 1-2 thick my favor). But i want to see scenario that if everything goes well. How can i open/close positions exactly first tick of bar?

    Update 1: Also i want to ask that when i back-test my strategy i'm using Close[0] price for open/close orders price. Instead use Close[0], should i use Bid/Ask prices? I mean override OnMarketData method and take Bid&Ask on a global variable then in OnBarUpdate use Ask price for being long and use Bid price for being short?

    Update 2: After a little bit research (I knew it before, but I forgot), i remember when doing historical test Calculate property not important, even we set OnEachTick or OnPriceChange strategy analyzer only working OnBarClose (so OnBarUpdate method calling only when bar close). Plus OnMarketData also not important when we backtest the strategy, this method only calling when strategy working real time data not with historical data. As a result maybe i have to AddDataSeries to my strategy for Bid and/or Ask so in that way maybe strategy will open/close positions exactly first tick of bar. But this is cause plus 3 questions;

    1. If i set order fill resolution from standard to high and set parameters type=tick value=1 AddDataSeries is required or not?

    2. If i test strategy on data series price based on Bid even so i have to add Bid Data Series for use Bid prices?

    3. If i use EnterLong/EnterShort which is we can not specify a specific order price (because it's mean Market Order not Limit or Stop Order), strategy analyzer open positions from which price? I think using DataSeries based on price am i right? I mean if strategy working based on Bid so Bid price history will be use plus Close[0] mean Bid price also. Is it true?

    Now I'm really so confused
    Last edited by aytacasan; 02-18-2017, 01:05 PM.

    Comment


      #3
      Hello aytacasan,

      The first tick of a new bar will be the open of the bar.
      In the screenshot you have posted this would be the #1 marking.

      When NinjaTrader is running with Calculate set to OnBarClose, orders are placed after the bar closes using logic calculated from that bar.

      Calculate is always OnBarClose in historical data or backtest when TickReplay is disabled.

      Below is a link to the help guide on the Calculate property.


      Setting Order fill resolution to High and supplying a 1 tick series will not cause OnBarUpdate() to trigger intra-bar.

      This means that any orders that are triggered from that bar are submitted after the bar closes as the new bar opens. As the order is placed as the new bar opens and the timestamp of the order is within the time of the next bar, the order will show on the next bar.

      If the strategy were running with Calculate as OnPriceChange or OnEachTick, or if the script has intra-bar granularity and submitted orders on a smaller timeframe, this would cause the timestamp of the order to be within the bar it is triggered on as the order may be submitted intra-bar. This order would show on the same bar as the bar that triggered the order submission.

      This is outlined in the help guide Discrepancies: Real-Time vs Backtest.


      Also, below is a link to a forum thread about intra-bar granularity.


      What you need is a 1 tick series added directly to the script with AddDataSeries(). Orders should be submitted using that 1 tick series BarsInProgress. You can then trigger orders intra-bar if you would like and also have high resolution order fills.
      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        You said that;

        The first tick of a new bar will be the open of the bar.
        In the screenshot you have posted this would be the #1 marking.

        When NinjaTrader is running with Calculate set to OnBarClose, orders are placed after the bar closes using logic calculated from that bar.

        Calculate is always OnBarClose in historical data or backtest when TickReplay is disabled.

        Below is a link to the help guide on the Calculate property.


        So far i agree with you until if chart bar type is not renko. In Renko chart type first tick never ever can not be open price, this is impossible. Please talk about it with your development team. Because i'm not sure my English is enough for explain this to you.

        Setting Order fill resolution to High and supplying a 1 tick series will not cause OnBarUpdate() to trigger intra-bar.

        This means that any orders that are triggered from that bar are submitted after the bar closes as the new bar opens. As the order is placed as the new bar opens and the timestamp of the order is within the time of the next bar, the order will show on the next bar.

        I agree with you but after set order fill resolution and back-test strategy again (every settings & parameters & strategy code is exactly same). Test result changed and entry/exit order levels close the levels what i want. Don't know maybe reason is back-test working on renko bar type.

        If the strategy were running with Calculate as OnPriceChange or OnEachTick, or if the script has intra-bar granularity and submitted orders on a smaller timeframe, this would cause the timestamp of the order to be within the bar it is triggered on as the order may be submitted intra-bar. This order would show on the same bar as the bar that triggered the order submission.

        I think after change order fill resolution execution, entry/exit prices made more sense. Because of "orders on a smaller timeframe, this would cause the timestamp of the order to be within the bar it is triggered" like your said. With "standard option", strategy make 230million profit #3905 trades. With "high option", same strategy make 19million profit again/of course #3905 trades. Every entry/exit order fills same bar in both cases, difference comes from entry/exit orders more reliable in second case. Because first case every order entry & exit 1-2 tick filling best price for me.

        This is outlined in the help guide Discrepancies: Real-Time vs Backtest.


        Also, below is a link to a forum thread about intra-bar granularity.


        I will check your link and try to clarify my confusion about back-test.

        What you need is a 1 tick series added directly to the script with AddDataSeries(). Orders should be submitted using that 1 tick series BarsInProgress. You can then trigger orders intra-bar if you would like and also have high resolution order fills.

        Yes i change my strategy code yesterday like your said;

        else if (State == State.Configure)
        {
        AddDataSeries(Instrument.FullName, BarsPeriodType.Tick, 1, MarketDataType.Bid);
        AddDataSeries(Instrument.FullName, BarsPeriodType.Tick, 1, MarketDataType.Ask);
        }

        protected override void OnBarUpdate()
        {
        //Add your custom strategy logic here.
        if (BarsInProgress == 1 || BarsInProgress == 2)
        return;

        if (CurrentBar < BarsRequiredToTrade)
        return;

        if (IsTrendBullish)
        {
        var entryPrice = Closes[2][0];
        var stopLossPrice = ...; // Need only calculate order quantity vs risk management
        var quantity = Quantity(entryPrice, stopLossPrice);
        if (quantity > 0)
        {
        EnterLong(2, quantity, "Entry");
        }
        }
        else if (IsTtrendBearish)
        {
        var entryPrice = Closes[1][0];
        var stopLossPrice = ...; // Need only calculate order quantity vs risk management
        var quantity = Quantity(entryPrice, stopLossPrice);
        if (quantity > 0)
        {
        EnterShort(1, quantity, "Entry");
        }
        }
        }

        Is this way am i guarantee the back-test will be as possible as high resolution order fills? Or are you have any recommandation about any other way? Because althought change code like above, long entry filling close -1 tick and short entry filling +1 tick. Because of that strategy making money like crazy.
        Last edited by aytacasan; 02-19-2017, 07:29 PM.

        Comment


          #5
          Hello Chelsea,

          Please, first read my previous post. I want to clarify something because everything get mixed;

          I'm aware of difference of historical vs real time working strategy. I'm aware of intra-bar granularity and how can i simulate this in strategy code when i back-test on historical data. I'm aware of NT8 has some new features high order fill resolution & tick replay, so no longer we don't have to do with code if we want.

          I know almost all other users asking how can i do realistic back-test? I'm asking opposite!!!. I don't want to my orders fills more realistic or intra-bar thick prices etc. I'm asking how can i open/close my positions when really when first tick of bar.

          So when IsFirstTickOfBar is true if we have only OHLC historical prices (don't have intra-bar ticks etc) acccourding to NT? You will say open price is first tick of bar, yes it's true for think about NT infrastructure/logic. But in real life this is only true when your data series is time based data series for example 1 min data series. What about not time based data series it's completely wrong!!! for example for renko. Almost all platforms misunderstand not time based data series even they can not draw on a chart correctly (except NT and max 2-3 other platforms drawing correctly renko charts).

          Shortly if data series is Renko you can not say open price is first tick of bar. I mean in NT perspective maybe you can say that but i can not test my strategy in that way because it's not true. If i test like that almost all strategies make crazy money on renko data series. Because it's something like that code;

          If (next candle is up candle) Then GoLongNowAndMakeMoney();
          Else If (next candle is up candle) Then GoShortNowAndMakeMoney();

          Because of that although i don't want to intra-bar fill orders i'm trying to do so. Because of these reasons your logic about "when IsFirstTickOfBar true this mean right now a bar is closed" is come down! on historical data.On real time data? it's ok no problem about this logic.

          I hope my issue is more clear. If appropriate for you i want to send an email more detail.

          Thanks,
          Aytac

          Comment


            #6
            Hello aytacasan,

            IsFirstTickOfBar would not work historically when adding a secondary data series. This only works historically if TickReplay is enabled and the script is running with Calculate.OnEachTick.


            The advice I am providing you is to trigger an action on the first tick of a new bar. Have I misunderstood what you are trying to accomplish?

            Can you demonstrate that the first tick of the secondary series that occurs after the primary series closes does not match the open of the new building bar?
            A short script with prints could demonstrate this. Once the primary series closes, save the next close price of BarsInProgress to a variable. Then after the new primary bar closes print the open of that bar along with the saved first tick of new bar close price.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hello Chelsea,

              IsFirstTickOfBar would not work historically when adding a secondary data series. This only works historically if TickReplay is enabled and the script is running with Calculate.OnEachTick.


              In that link saying that "This property is only of value in scripts that run tick by tick which is when the Calculate property is set to Calculate.OnEachTick or Calculate.OnPriceChange."

              When we do historical back-test there is no any mean Calculate property and it'll work always like OnBarClose. So is that mean IsFirstTickOfBar property has no value when we do historical back-test?

              Of course unless Show Tick Replay option selected and/or order fill process high option selected or add secondary tick data. Am i right?


              The advice I am providing you is to trigger an action on the first tick of a new bar. Have I misunderstood what you are trying to accomplish?

              No you understand me correctly. But more important thing is to understand that when i back-test strategy on 1 minute bars, the strategy execute orders on right price levels. But when i back-test on renko chart bars same strategy execute orders on wrong price levels. Because of renko's internal dynamics and/or NT's back-test logic. Again when calculate is OnBarClose, no tick replay and no high fill order process and no secondary tick data.

              When i change standard to high order fill process or when i add tick series in code, order execution level is improved but still strategy making plus per trade 2-3 ticks.

              Also i have to say that strategy using EnterLong/EnterShort


              Can you demonstrate that the first tick of the secondary series that occurs after the primary series closes does not match the open of the new building bar?
              A short script with prints could demonstrate this. Once the primary series closes, save the next close price of BarsInProgress to a variable. Then after the new primary bar closes print the open of that bar along with the saved first tick of new bar close price.

              I don't understand what are you mean? Please tell me a little more detail (if possible with an example). But i have to say that you know renko already populated from tick and i'm tried to use bid/ask tick etc.

              Thanks,
              Aytac
              Last edited by aytacasan; 02-21-2017, 01:20 PM.

              Comment


                #8
                Hello aytacasan,

                IsFirstTickOfBar only works with Calculate.OnEachTick. In historical data, Calculate is always .OnBarClose unless TickReplay is enabled.
                So if TickReplay is disabled or Calculate is not set to .OnEachTick then IsFirstTickOfBar will always be true and will never be false and cannot be used.

                From the help guide:
                "On a historical data set, only the OHLCVT of the bar is known and not each tick that made up the bar. As a result, State.Historical data processes OnBarUpdate() only on the close of each historical bar even if this property is set to OnEachTick or OnPriceChange. You can use TickReplay or a Multi-time frame script to obtain intrabar data."



                If you disagree that the first tick of a bar is the open of that bar, please send a demonstration script showing this. Print the price first tick of a bar along with the open and show that they are different.
                Last edited by NinjaTrader_ChelseaB; 03-12-2017, 03:32 PM.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Yes i'm disagree but how can i write a script with NinjaScript that printing on real first ticks? If i can write this script with NT, this means also EnterLong/Short can execute with that price level.

                  For understand my issue you have to know renko's internal process. if we have 4 tick renko for ES, suppose our prior bar's (bars ago = 1) open is 2001 and close is 2000. Our current candle's (still it's candlestick form at that point) open/first tick will be 2000, after that suppose ES falls 2 ticks more and do new low at 1999,50 after that rise again to 2002 level and hit that level, at that point still we have candlestick that (0pen is 2000 High is 2002 Low is 1999,50 and close 2002). Because of hit next renko target candlestick will be change by NT with renko bar (0pen is 2001 High is 2002 Low is 1999,50 and close 2002). Soooooooo renko bar open is 2001 on chart but actually it's first tick is 2000. I want to open/close position from 2000 price level. I hope it's now clear.

                  If you repeat again that on historical data NT only knows OHLCV. So why when i set order fill resolution high or add tick series to strategy and use it why still can not open position from 2000? instead NT open positions from 2000.25 or 2000.50 or 1999,75 or 1999.50 etc.

                  PS: To tell you totruth i never use/try show tick replay option because of worry about performance reasons.
                  Last edited by aytacasan; 02-21-2017, 02:08 PM.

                  Comment


                    #10
                    Hello aytacasan,

                    I'm not able to discuss the logic of how the Renko bars work.
                    However, if you add a 1 tick series, trigger a bool after the primary bar closes to identify the first tick of a new bar on the secondary series, then wait for that new bar to close and compare the saved price of the first tick and show this does not match the open price I can forward this to our development and ask for clarity.

                    My understanding is the first tick of the new bar will be the open price of that bar, the first tick being the opening tick of that bar.

                    You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like our business development follow up with you with a list of affiliate consultants who would be happy to create this script or any others at your request.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      I'm not able to discuss the logic of how the Renko bars work.
                      However, if you add a 1 tick series, trigger a bool after the primary bar closes to identify the first tick of a new bar on the secondary series, then wait for that new bar to close and compare the saved price of the first tick and show this does not match the open price I can forward this to our development and ask for clarity.

                      If you want to test first tick price is same the renko bar's open price? We don't need to write to test for it. I can say the result YES. First tick of renko bar = renko bar's open. Which is the reason of write this posts to customer service.

                      My understanding is the first tick of the new bar will be the open price of that bar, the first tick being the opening tick of that bar.

                      I agree with you but only for time based bar data types. When the bar not time based like renko this assumption is wrong. I'll demonstrate this with a simple sample strategy. How can i send you an email?

                      You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like our business development follow up with you with a list of affiliate consultants who would be happy to create this script or any others at your request.
                      Last edited by aytacasan; 02-21-2017, 02:27 PM.

                      Comment


                        #12
                        Not many people try to use Renko bars in strategies because they are fake bars and repaint. You would have a hard time getting the open on historical bars by using the OHLC information.
                        Some of RJay's Renko bars do allow access to the actual OHLC values and are therefore usable in strategies with a little extra coding.
                        Last edited by eDanny; 02-22-2017, 02:51 PM. Reason: Added reference to RJay's bars.
                        eDanny
                        NinjaTrader Ecosystem Vendor - Integrity Traders

                        Comment


                          #13
                          Hello Danny,

                          Thank you very much for sharing your ideas. I'm not agree with you because when the subject is technical analysis. There is two facts;

                          1. All indicators are lagging. Although Renko is a bar type actually it's an indicator too. So Renko is lagging like other indicators.

                          2. All indicators can repaint if you don't use/write indicators correctly. For example if you write a strategy that open position according to ma crossover and run tick by tick without use IsFirstTickOfBar property. Crossover can be happens for a few seconds and when the bar fully formed you can see ma's no longer crossing each other.

                          I'm not saying that Renko strategies are money machine, like other indicators it's only a tool for traders. In real market how my strategy's orders can be filled don't know but in back-test i want to see my orders filled at first tick of bar at least when i use order fill resolution is high or when i add tick data series to the strategy. I tried all options and NT always execute orders in my favor. Which is so dangerous and so critical error. Maybe because of that NT's Renko don't have wicks.
                          Last edited by aytacasan; 02-23-2017, 08:57 AM.

                          Comment


                            #14
                            Usually if I have the need to backtest a strategy on UniRenko ( or any similar bar type where the open is fake ) I replace the EnterLong() entry order ( which would get a fill at open of next bar, which is wrong ) by a Buy Stop at High[0]+1*TickSize ( or replace that 1 by your preferred slippage )
                            Anyway, I try to avoid optimizing parameters on a bar type with fake values.

                            Hope that helps.
                            pmaglio
                            NinjaTrader Ecosystem Vendor - The Indicator Store

                            Comment


                              #15
                              Hello pmaglio,

                              Thanks for your response and appreciate that. I think your post is the most useful post about my problem. I'll try to use this solution. But even so still i'm thinking that when we use order fill resolution with high option, NT have to open/close positions from real first tick of bar not from fake open.

                              Regards,
                              Aytac

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by rocketman7, Today, 01:00 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post rocketman7  
                              Started by wzgy0920, 04-20-2024, 06:09 PM
                              2 responses
                              27 views
                              0 likes
                              Last Post wzgy0920  
                              Started by wzgy0920, 02-22-2024, 01:11 AM
                              5 responses
                              32 views
                              0 likes
                              Last Post wzgy0920  
                              Started by wzgy0920, 04-23-2024, 09:53 PM
                              2 responses
                              74 views
                              0 likes
                              Last Post wzgy0920  
                              Started by Kensonprib, 04-28-2021, 10:11 AM
                              5 responses
                              193 views
                              0 likes
                              Last Post Hasadafa  
                              Working...
                              X