NinjaTrader Support Forum  

Go Back   NinjaTrader Support Forum > NinjaScript Development Support > Strategy Development

Strategy Development Support for the development of custom automated trading strategies using NinjaScript.

Reply
 
Thread Tools Display Modes
Old 04-24-2012, 01:13 PM   #1
Synax.one
Junior Member
 
Join Date: Apr 2012
Posts: 20
Thanks: 0
Thanked 0 times in 0 posts
Default 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
Synax.one is offline  
Reply With Quote
Old 04-24-2012, 01:30 PM   #2
NinjaTrader_Matthew
NinjaTrader Customer Service
 
NinjaTrader_Matthew's Avatar
 
Join Date: Apr 2010
Location: Denver, CO, USA
Posts: 4,858
Thanks: 162
Thanked 579 times in 570 posts
Default

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);
            
        }
NinjaTrader_Matthew is offline  
Reply With Quote
Old 04-25-2012, 07:54 AM   #3
Synax.one
Junior Member
 
Join Date: Apr 2012
Posts: 20
Thanks: 0
Thanked 0 times in 0 posts
Default 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 ?
Synax.one is offline  
Reply With Quote
Old 04-25-2012, 08:08 AM   #4
NinjaTrader_Matthew
NinjaTrader Customer Service
 
NinjaTrader_Matthew's Avatar
 
Join Date: Apr 2010
Location: Denver, CO, USA
Posts: 4,858
Thanks: 162
Thanked 579 times in 570 posts
Default

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:

http://www.ninjatrader.com/support/h...inprogress.htm

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?
NinjaTrader_Matthew is offline  
Reply With Quote
Old 04-25-2012, 08:44 AM   #5
Synax.one
Junior Member
 
Join Date: Apr 2012
Posts: 20
Thanks: 0
Thanked 0 times in 0 posts
Default 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;

}
Synax.one is offline  
Reply With Quote
Old 04-25-2012, 09:07 AM   #6
NinjaTrader_Matthew
NinjaTrader Customer Service
 
NinjaTrader_Matthew's Avatar
 
Join Date: Apr 2010
Location: Denver, CO, USA
Posts: 4,858
Thanks: 162
Thanked 579 times in 570 posts
Default

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.
NinjaTrader_Matthew is offline  
Reply With Quote
Old 04-25-2012, 09:17 AM   #7
Synax.one
Junior Member
 
Join Date: Apr 2012
Posts: 20
Thanks: 0
Thanked 0 times in 0 posts
Default exit from position

exitlong means exit from last position. when i removed it only one trade was executed.
Synax.one is offline  
Reply With Quote
Old 04-25-2012, 09:24 AM   #8
NinjaTrader_Matthew
NinjaTrader Customer Service
 
NinjaTrader_Matthew's Avatar
 
Join Date: Apr 2010
Location: Denver, CO, USA
Posts: 4,858
Thanks: 162
Thanked 579 times in 570 posts
Default

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?
NinjaTrader_Matthew is offline  
Reply With Quote
Old 04-25-2012, 09:46 AM   #9
Synax.one
Junior Member
 
Join Date: Apr 2012
Posts: 20
Thanks: 0
Thanked 0 times in 0 posts
Default 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.
Synax.one is offline  
Reply With Quote
Old 04-25-2012, 10:17 AM   #10
Synax.one
Junior Member
 
Join Date: Apr 2012
Posts: 20
Thanks: 0
Thanked 0 times in 0 posts
Default

so how can i do that ???
Synax.one is offline  
Reply With Quote
Old 04-25-2012, 10:39 AM   #11
NinjaTrader_Matthew
NinjaTrader Customer Service
 
NinjaTrader_Matthew's Avatar
 
Join Date: Apr 2010
Location: Denver, CO, USA
Posts: 4,858
Thanks: 162
Thanked 579 times in 570 posts
Default

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?
NinjaTrader_Matthew is offline  
Reply With Quote
Old 04-25-2012, 10:43 AM   #12
Synax.one
Junior Member
 
Join Date: Apr 2012
Posts: 20
Thanks: 0
Thanked 0 times in 0 posts
Default

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
Synax.one is offline  
Reply With Quote
Old 04-25-2012, 11:52 AM   #13
NinjaTrader_Matthew
NinjaTrader Customer Service
 
NinjaTrader_Matthew's Avatar
 
Join Date: Apr 2010
Location: Denver, CO, USA
Posts: 4,858
Thanks: 162
Thanked 579 times in 570 posts
Default

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 at 11:57 AM.
NinjaTrader_Matthew is offline  
Reply With Quote
Old 04-26-2012, 06:42 AM   #14
Synax.one
Junior Member
 
Join Date: Apr 2012
Posts: 20
Thanks: 0
Thanked 0 times in 0 posts
Default 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
Synax.one is offline  
Reply With Quote
Old 04-26-2012, 08:00 AM   #15
NinjaTrader_Matthew
NinjaTrader Customer Service
 
NinjaTrader_Matthew's Avatar
 
Join Date: Apr 2010
Location: Denver, CO, USA
Posts: 4,858
Thanks: 162
Thanked 579 times in 570 posts
Default

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:

http://www.ninjatrader.com/support/h...nstruments.htm
NinjaTrader_Matthew is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Can a strategy or indicator have more than a single source file? alex.nt General Programming 7 11-30-2011 04:11 AM
Primary data source pehon Connecting 2 07-14-2011 10:38 AM
External data source nicknamed Connecting 6 08-10-2010 04:05 PM
Any way to specify data source? heech Connecting 2 03-16-2009 04:29 PM
strategy using picks from a different source j0dan Strategy Development 3 04-23-2008 01:38 PM


All times are GMT -6. The time now is 12:36 AM.