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

Bid/Ask data in backtesting

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

    Bid/Ask data in backtesting

    Is it possible to force NinjaTrader (7) to use Bid data for long exits and Ask data for Short exits? In other words to have a backtest behave like a real world scenario.

    I have added bid and ask data using the Add() method:

    Code:
                Add(Instrument.FullName, PeriodType.Minute, BarsPeriod.Value, MarketDataType.Bid);        
                Add(Instrument.FullName, PeriodType.Minute, BarsPeriod.Value, MarketDataType.Ask);
    I can ensure that entry orders are placed on the right series i.e. Ask for Long entry, Bid for Short entry by using BarsInProgress(), but I don't know how to ensure that NT evaluates my stop and target orders on the right series.

    I note that none of the SetStopLoss() nor SetProfitTarget() methods include an overload that incorporates the barsInProgressIndex but all of the following 'Exit' methods do:

    ExitLong()
    ExitLongLimit()
    ExitLongStop()
    ExitLongStopLimit()
    ExitShort()
    ExitShortLimit()
    ExitShortStop()
    ExitShortStopLimit()

    I presume the answer lies in using one or more of these methods to place my stop and target orders(?) The NT7 manual has an article on using historical bid/ask so I gather this should be possible, but I'm just not sure how to get it to work.

    For additional context I am using stops and targets that are calculated dynamically at the time the trade is placed, but the stop/target values do not change again from that point.

    I find with large data sets (that you easily get in a decent sized backtest) that the backtest is not accurate because trade outcomes would have been different had the correct price series been used.

    #2
    Hello newuser, thanks for your post.

    In this case, you will need to submit your stop loss and profit target orders manually. This reference sample shows how to do this:



    Because you are using the standard "Enter" orders, you can specify a barsInProgressIndex.

    Alternatively, you can record the last bid or ask price when BarsInProgress == 1(ask) or 2(bid) in a class level variable assuming the only added series are the ask and bid series. Then you could call SetStopLoss/ProfitTarget with those recorded prices.

    Best regards.


    Chris L.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the prompt reply. I have a couple questions about your last paragraph:

      Alternatively, you can record the last bid or ask price when BarsInProgress == 1(ask) or 2(bid) in a class level variable assuming the only added series are the ask and bid series. Then you could call SetStopLoss/ProfitTarget with those recorded prices.
      I am only adding the bid and ask series but I'm not sure what you mean by "...record...in a class level variable... Then...call SetStopLoss/ProfitTarget with those recorded prices." Can you elaborate a little further on how to do that?

      Comment


        #4
        Hi newuser, thanks for your reply.

        You can do something like this:

        Code:
            public class GetBidAskForPrimary : Indicator
            {
                private double LastBid;
                private double LastAsk;
        
                ...
        
                protected override void OnBarUpdate()
                {
                    if(BarsInProgress == 0)
                    {
                        Print("Last Bid: " + LastBid);
                        Print("Last Ask: " + LastAsk);
                    }
        
                    if(BarsInProgress == 1)
                    {
                        if(Close[0] != LastBid)
                        {
                            LastBid = Close[0];
                        }
                    }
        
                    if(BarsInProgress == 2)
                    {
                        if(Close[0] != LastAsk)
                        {
                            LastAsk= Close[0];
                        }
                    }
                }
        Chris L.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by MarianApalaghiei, Today, 10:49 PM
        1 response
        8 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by love2code2trade, Yesterday, 01:45 PM
        4 responses
        28 views
        0 likes
        Last Post love2code2trade  
        Started by funk10101, Today, 09:43 PM
        0 responses
        8 views
        0 likes
        Last Post funk10101  
        Started by pkefal, 04-11-2024, 07:39 AM
        11 responses
        37 views
        0 likes
        Last Post jeronymite  
        Started by bill2023, Yesterday, 08:51 AM
        8 responses
        46 views
        0 likes
        Last Post bill2023  
        Working...
        X