Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Is this a real entry

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

    Is this a real entry

    I am writing a strategy that places an entry at the top of the last bar when conditions are met. When I test it, if the next bar repeats those conditions,
    a new entry is made. I added a counter so hopefully only the entry variables are set once. I still get the second entry. Is this a real
    outcome of the code or is this a problem with granularity of back test? The correct entry is at the top of the Gold candle which is
    the first candle to satisfy conditions.

    here is pertinent code:

    // Set 1
    if ((Close[1] < Open[1])
    && (Close[0] > Open[0])
    && (Low[0] < Low[1])
    && (High[0] < High[1])
    && (Close[0] > Close[1])
    && (Close[0] > EMA1[0])
    && (BarCounter1[0] > 60)
    && (BarCounter1[0] < 78)
    && (CounterSet==0))
    {
    EntryLong = (High[0] + (2 * TickSize)) ;
    BarBrush = Brushes.Gold;
    CounterSet=1;
    // StopLong = (High[0] - (Target * TickSize)) ;
    PlaySound(@"C:\Program Files\NinjaTrader 8\sounds\AutoBreakEven.wav");
    }

    // Set 2
    if (EntryLong > 0)

    {
    EnterLongLimit(Convert.ToInt32(Contracts), EntryLong, @"Long");
    }

    // Set 3
    if (Position.MarketPosition == MarketPosition.Long)
    {
    EntryLong = 0;
    CounterSet = 0;​

    out come picture:
    Click image for larger version

Name:	outcome.jpg
Views:	111
Size:	20.4 KB
ID:	1282728

    #2
    Hello BrJessey,

    Thanks for your post.

    In Set 1 of the code you shared, you are assigning a value to EntryLong which means this variable will be greater than 0.

    In Set 2 you check if EntryLong is greater than 0 and call EnterLongLimit() which places an order.

    Then in Set 3, you set EntryLong to 0 if you are in a long position. However, on the next bar update the code will be evaluated again and you will be assigning a new value to EntryLong if the conditions are true meaning this variable would be greater than 0 which means another trade may be placed.

    If you are only wanting to place a single trade, you could consider checking it Position.MarketPosition == MarketPosition.Flat in the condition to call your entry order so an order is only placed when you are in a flat position.

    Ultimately, to understand why the script is behaving as it is, such as placing orders or not placing orders or drawing objects when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

    In the strategy add prints (outside of any conditions) that print the values of every variable used in every condition that places an order along with the time of that bar.

    Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

    Below is a link to a forum post that demonstrates how to use prints to understand behavior.
    https://ninjatrader.com/support/foru...121#post791121​​
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      OK so I printed all variables for the candles in question.

      Long Set
      69
      12/11/2023 11:30:00 AM
      Order Entered
      69
      16375.25

      What that means is that on candle 69 ( the gold candle because of Ninja time keeping)
      The conditions for long were met and a long limit order was set for 16375.25
      There was never a variable set to 16370.25
      That trade should never have happened.
      There are no other variables in any order calls
      The outcome should have been no order on candle 70 and the long taken at 16375.25 on candle 71
      This happens on barclose as well as eachTick
      Is this a real outcome of the code or an artifact of backtest granularity?
      Last edited by BrJessey; 12-20-2023, 09:59 PM.

      Comment


        #4
        Hello BrJessey,

        Thanks for your notes.

        When backtesting a strategy on historical data there will be discrepancies compared to running the strategy real-time.

        Please review the help guide document on the differences on real-time vs backtest (historical).
        https://ninjatrader.com/support/help...ime_vs_bac.htm

        It is expected that a strategy running real-time (live brokerage account, live market simulation, Playback connection etc...) will produce different results than the performance results generated during a backtest.

        When in historical data, only the Open, High, Low, and Close will be available 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' and the script will not have the intra-bar information to accurately fill an order at the exact price and time.

        Below is a link to the help guide on Calculate.
        https://ninjatrader.com/support/help.../calculate.htm

        To improve the accuracy of a backtest, you may use Tick Replay along with an added 1-tick series to have logic processed intra-bar and have orders filled intrabar.

        Tick Replay would be used to have the logic process OnEachTick or OnPriceChange with historical data, but this does not allow for intra-bar order fills. You would need to add a single tick data series and submit orders to that single tick data series for a strategy that uses Tick Replay.

        High Order Fill Resolution allows for intra-bar order fills with historical processing, but is not compatible with Tick Replay.

        Please reference the SampleIntrabarBacktest example and the following Help Guide links for more information.

        SampleIntrabarBacktest 'Backtesting NinjaScript Strategies with an intrabar granularity' - https://ninjatrader.com/support/helpGuides/nt8/backtesting_ninjascript_strate.htm

        TickReplay — https://ninjatrader.com/support/help...ick_replay.htm

        Developing for Tick Replay -
        https://ninjatrader.com/support/helpGuides/nt8/developing_for__tick_replay.htm?zoom_highlightsub= developing+for+tick+replay

        Additional information may be found in this NinjaTrader Forum post —
        https://ninjatrader.com/support/forum/forum/ninjatrader-8/strategy-development/100192-comparing-real-time-historical-and-replay-performance?t=102504

        Further, note that when the mode of Calculate.OnBarClose is used this means that the strategy code executes once at the end of each bar and will perform calculations on completed bars. If your strategy determines to enter an order, the order would be placed on the next bar.

        Using the calculate mode of Calculate.OnPriceChange, or Calculate.OnEachTick, will allow the strategy to be executed intrabar with greater frequency which means your code will be executed on each tick or each change in price.
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Today, 05:17 AM
        0 responses
        39 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        124 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        64 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        41 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        46 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X