Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Same source of data, different values in strategy

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

    Same source of data, different values in strategy

    Hello,

    I have Bid data with one price in each line imported in ninjatrader. In strategy I use GetCurrentBid() and it outputs different values like those in historical data manager.
    And also entry price and exit price in backtesting trades window arent identical to prices in historical data manager.
    Help me with getting right prices in strategy, please

    Thanks.

    Synax

    #2
    Hello,

    GetCurrentBid() is a real time method. When backtesting, it will substitute the closing price of the bar.

    If you would like to backtest using the historical bid data in your historical data manager, you will need to Add this data series to your strategy in the Initialize() method:

    Code:
            protected override void Initialize()
            {
                
                Add("ES 06-12", PeriodType.Minute, 1, MarketDataType.Bid);
                Add("ES 06-12", PeriodType.Minute, 1, MarketDataType.Ask);
                
            }
    Then you can access this by calling the approriate BarsArray index on OnBarUpdate() :

    Code:
            protected override void OnBarUpdate()
            {
                
                double bidData = BarsArray[1][0];
                double askData = BarsArray[2][0];
                
                Print("Bid Price" + bidData);
                Print("Ask Price" + askData);
                
            }
    MatthewNinjaTrader Product Management

    Comment


      #3
      OK

      now its OK, but everything prints and executes twice in row. method OnBarUpdate() is called twice in row i think. can you help me to avoid this double execution ?

      Comment


        #4
        Hello,

        Which BarsInProgress are you running the order methods in? If you're not famialir with BIP, please take a look at the following link:



        Also see the following link for additional information on running multi-bars scripts.

        If this is not clear, can you please provide me with a sample of your OBU() code that is resulting in the double executions?
        MatthewNinjaTrader Product Management

        Comment


          #5
          code

          Please, check the code. I really hope you help me. Thank you very much.

          protected override void Initialize()
          {
          CalculateOnBarClose = true;
          Add("$EURUSD", PeriodType.Tick, 1, MarketDataType.Bid);
          Add("$EURUSD", PeriodType.Tick, 1, MarketDataType.Ask);
          }

          /// <summary>
          /// Called on each bar update event (incoming tick)
          /// </summary>
          protected override void OnBarUpdate()
          {

          double lastProfit = 0;

          int tradesCount = myInput0==1?Performance.LongTrades.Count:Performan ce.ShortTrades.Count;

          if (tradesCount > 0)
          {
          // Get the last completed real-time trade (at index 0)
          Trade lastTrade = myInput0==1?Performance.LongTrades[tradesCount - 1]:Performance.ShortTrades[tradesCount - 1];

          // Calculate the PnL for the last completed real-time trade
          lastProfit = lastTrade.ProfitCurrency * lastTrade.Quantity;

          cash += lastProfit;
          account += lastProfit;

          Print(lastTrade.ProfitCurrency +" "+ lastTrade.Quantity);

          } else {
          lastProfit = 0;
          }

          Print(myInput0+": "+account+": "+cash + " "+lastBet+" ; "+lastProfit);




          bet = 1;


          if(myInput0==1) ExitLong();
          if(myInput0==2) ExitShort();


          if(myInput0==1) EnterLong((int)(bet*10000));
          if(myInput0==2) EnterShort((int)(bet*10000));


          lastBet = bet;

          }

          Comment


            #6
            Hello,

            When myInput0 == 1 or 2, you are placing an exit order and an entry order at the same time. This is going to place two orders. If you simply want to reverse the position, you can just use EnterLong and EnterShort. You do not need to use the ExitLong/ExitShort to get out of the position, as this will be handled by the EnterLong/EnterShort methods.
            MatthewNinjaTrader Product Management

            Comment


              #7
              exit from position

              exitlong means exit from last position. when i removed it only one trade was executed.

              Comment


                #8
                Yes, that is true but at the same time you are placing an EnterLong. If you are in a short position and envoke an EnterLong, it should exit the last short position and enter a new long position.


                Can you clarify what you are attempting to do with this method. How many orders are you looking to place?
                MatthewNinjaTrader Product Management

                Comment


                  #9
                  long short

                  on every barupdate i want to exit from last and place new position.
                  if input is 1 i trade only long with bid prices.
                  if input is 2 i trade only short with ask prices.

                  Comment


                    #10
                    so how can i do that ???

                    Comment


                      #11
                      Hello,

                      From your code, I'm not seeing where you set myInput0 to 2

                      Are you doing this from the strategy property?

                      Under what condition is myInput0 at 2?
                      MatthewNinjaTrader Product Management

                      Comment


                        #12
                        when i run backtest in strategy analyzer i run it with myinput0 with value 1 with bid prices and when i run backtest with myinput value 2 i run it with ask prices

                        Comment


                          #13
                          Ok, I understand. At any rate, you are still asking the strategy to do twice as much as you need.

                          Currently it's only looking for MyInput1 == 1, which would submit 2 orders in every instance.

                          You can try using MarketPosition to ensure that your strategy is flat, long, or short before placing an exit order or a entry order.

                          Try something such as:

                          Code:
                          
                          if(myInput0==1 && Position.MarketPosition == MarketPosition.Long)    ExitLong();
                          if(myInput0==2 && Position.MarketPosition ==MarketPosition.Short) ExitShort();
                                      
                                    
                          else if(myInput0==1 && Position.MarketPosition == MarketPosition.Flat) EnterLong((int)(bet*10000));
                          else if(myInput0==2 &&  Position.MarketPosition == MarketPosition.Flat) EnterShort((int)(bet*10000));
                          Last edited by NinjaTrader_Matthew; 04-25-2012, 11:57 AM.
                          MatthewNinjaTrader Product Management

                          Comment


                            #14
                            not working

                            i did that with exitlong and exitshort with that conditions and its not working.
                            i think whole method is called twice because also printing is outputed twice in row. same values. help me please

                            Comment


                              #15
                              Did you end up adding a BarsInProgress condition to your script?

                              If you only want this to fire on the primary data series, please make sure your script is only working in the context of BIP 0

                              Code:
                              protected override void OnBarUpdate()
                               {
                                              
                                    if(BarsInProgress == 0)
                                    {
                                      //strategy logic goes here
                              
                                    }
                              }
                              Please make sure you have read and understand the information in our help guide on working with multi-series scripts:

                              MatthewNinjaTrader Product Management

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              663 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              376 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              110 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              575 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              580 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X