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 01-15-2011, 12:13 AM   #1
Baruch
Senior Member
 
Join Date: Mar 2009
Posts: 442
Thanks: 0
Thanked 25 times in 16 posts
Default How to implement Bid/Ask fills in Strategy

Hi,
To implement more realistic fills in Forex, we need to use Bid/Ask data for the fills.
The syntax for adding a Bid/Ask data series is
Code:
Add(string instrumentName, PeriodType periodType, int period, MarketDataType marketDataType)
My question is how do I know what is the name of the instrumentName I'm testing against? Can I retrieve it prior to adding this data series?

Baruch
Baruch is offline  
Reply With Quote
Old 01-15-2011, 12:17 PM   #2
NinjaTrader_Austin
NinjaTrader Customer Service
 
NinjaTrader_Austin's Avatar
 
Join Date: Jun 2009
Location: Denver, CO
Posts: 3,149
Thanks: 10
Thanked 90 times in 82 posts
Default

Baruch, yes, you can use a certain property to get the instrument's name:

Code:
string instName = Instrument.FullName;
NinjaTrader_Austin is offline  
Reply With Quote
Old 01-15-2011, 02:09 PM   #3
Baruch
Senior Member
 
Join Date: Mar 2009
Posts: 442
Thanks: 0
Thanked 25 times in 16 posts
Default

Austin thanks,
Yes I know that, but the Add() function is used in Initialize. Will at that point the instrument be known to the strategy?

Baruch
p.s.
Why don't you have an overload for bid/ask without the instrumentName?
Baruch is offline  
Reply With Quote
Old 01-15-2011, 08:53 PM   #4
NinjaTrader_Austin
NinjaTrader Customer Service
 
NinjaTrader_Austin's Avatar
 
Join Date: Jun 2009
Location: Denver, CO
Posts: 3,149
Thanks: 10
Thanked 90 times in 82 posts
Default

Baruch, please try checking if instrument is null first.
Code:
if (Instrument != null)
{
  string instName = Instrument.FullName;
  Add(instName, ...);
}
If that does not work, then you'll have to manually add the instrument.

I am not sure why there isn't a add bid/ask overload. I'll forward that as a suggestion.
NinjaTrader_Austin is offline  
Reply With Quote
Old 01-16-2011, 01:24 AM   #5
Baruch
Senior Member
 
Join Date: Mar 2009
Posts: 442
Thanks: 0
Thanked 25 times in 16 posts
Default

Austin hi,
I know how to check and prevent the strategy from crashing. Do you really think that
Quote:
If that does not work, then you'll have to manually add the instrument.
is an appropriate answer?

Baruch
Baruch is offline  
Reply With Quote
Old 01-16-2011, 05:30 AM   #6
bukkan
Senior Member
 
Join Date: Feb 2009
Posts: 285
Thanks: 2
Thanked 54 times in 41 posts
Default

you can get the current bid/ask from the following code. no need to add any additional dataseries.

Code:
this.GetCurrentAsk();
this.GetCurrentBid();
the same has an overload too, useful for multiseries strategies/indicators.
bukkan is offline  
Reply With Quote
Old 01-16-2011, 07:12 AM   #7
Baruch
Senior Member
 
Join Date: Mar 2009
Posts: 442
Thanks: 0
Thanked 25 times in 16 posts
Default

bukkun thanks,
But I think that I do need the Add(). This is because I need to enter and exit my trades on different BarsInProgress. So I enter LongLimit on Ask series and exit Shorts on Ask series.

Baruch
Baruch is offline  
Reply With Quote
Old 01-16-2011, 08:23 AM   #8
bukkan
Senior Member
 
Join Date: Feb 2009
Posts: 285
Thanks: 2
Thanked 54 times in 41 posts
Default

Quote:
Originally Posted by Baruch View Post
bukkun thanks,
But I think that I do need the Add(). This is because I need to enter and exit my trades on different BarsInProgress. So I enter LongLimit on Ask series and exit Shorts on Ask series.

Baruch
you dont need to add the bid ask series. if you wan to enter long limit at ask you can simply use this code.

Code:
EnterLongLimit(GetCurrentAsk(),"signal name");
anyway to get the instrument name in Initialize you can get it from the below code.

Code:
ChartControl.GetChartCaption
you have to clean up it a bit.
bukkan is offline  
Reply With Quote
Old 01-16-2011, 08:43 AM   #9
Baruch
Senior Member
 
Join Date: Mar 2009
Posts: 442
Thanks: 0
Thanked 25 times in 16 posts
Default

Quote:
you don't need to add the bid ask series. if you wan to enter long limit at ask you can simply use this code.


Code:
EnterLongLimit(GetCurrentAsk(),"signal name");
The syntax for limit orders is usually EnterLimit Long/Short(BarsInProcess, true,...,MyPrice,...)
I don't want to enter immediately, but at specific price! The question is if on current bar the high >= MyPrice so it will enter a trade.

Baruch

p.s.
I'm running back tests on granular resolution of 1 minute.
Baruch is offline  
Reply With Quote
Old 01-16-2011, 08:21 PM   #10
NinjaTrader_Austin
NinjaTrader Customer Service
 
NinjaTrader_Austin's Avatar
 
Join Date: Jun 2009
Location: Denver, CO
Posts: 3,149
Thanks: 10
Thanked 90 times in 82 posts
Question

GetCurrentAsk() and Bid() return the close of the bar when you're backtesting, not actual bids and asks. When running real-time, these methods behave as expected.

Baruch, the reason I suggested adding in the null check is because sometimes it works.

From the help guide, "NOTE: The properties in this class [Instrument] should NOT be accessed within the Initialize() method."

That is why I recommended you add in the manual name, so you have a working solution.

Basically, anything that could give you the instrument name (Instrument, BarsPeriod, etc) you can't access from Initialize().
Attached Images
File Type: png bars init.PNG (12.1 KB, 14 views)
File Type: png inst init.PNG (10.7 KB, 10 views)
NinjaTrader_Austin is offline  
Reply With Quote
Old 01-17-2011, 01:16 AM   #11
Baruch
Senior Member
 
Join Date: Mar 2009
Posts: 442
Thanks: 0
Thanked 25 times in 16 posts
Default

Hi Austin,
I understand perfectly.
Quote:
From the help guide, "NOTE: The properties in this class [Instrument] should NOT be accessed within the Initialize() method."
Thats exactly my point if it is not a working solution then I can't use it. On the other hand if I want to backtest Forex strategy, I need bid/ask series.
So your second solution is to hard code the curency I run the back test against in to the Add(). So do you prepose to create 20 strategies for 20 curency pairs only to work with bid/ask?

Baruch
Baruch is offline  
Reply With Quote
Old 01-17-2011, 06:32 AM   #12
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,569
Thanks: 262
Thanked 1,017 times in 998 posts
Default

Baruch, this is unfortunately correct - or you add a string input so you can set the symbol needed at runtime.
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 01-17-2011, 09:33 AM   #13
Baruch
Senior Member
 
Join Date: Mar 2009
Posts: 442
Thanks: 0
Thanked 25 times in 16 posts
Default

Bertrand,
I have a question:
If in my strategy I add:
Add(PeriodType.Tick,10);

And I backtest my strategy on instrument XXX and "Price based on = Last" and "Type=Minute"
What will be the instrument and the MarketDataType of my second data series?
Now if I backtest my strategy on instrument XXX and "Price based on = Ask" and "Type=Minute"
What will be the instrument and the MarketDataType of my second data series?
Hopefully from your answers you will understand that it needs to be an overload with MarketDataType but without an Instrument.

Baruch
Baruch is offline  
Reply With Quote
Old 01-17-2011, 11:24 AM   #14
NinjaTrader_RyanM
NinjaTrader Customer Service
 
NinjaTrader_RyanM's Avatar
 
Join Date: Sep 2009
Location: Denver, CO
Posts: 8,117
Thanks: 249
Thanked 418 times in 415 posts
Default

Hello Baruch,

If you don't specify a MarketDataType within the add statement, then added series will be last. It will work this way even if you select ask as primary series when running the strategy.

Hard coding instrument name is only solution for now. I will forward a request for new overload that allows specifying marketdata type without requiring instrument.
Last edited by NinjaTrader_RyanM; 01-17-2011 at 11:29 AM.
NinjaTrader_RyanM is offline  
Reply With Quote
Old 01-17-2011, 11:43 AM   #15
Baruch
Senior Member
 
Join Date: Mar 2009
Posts: 442
Thanks: 0
Thanked 25 times in 16 posts
Default

Thanks,
I will wait. In the mean time I want to say that it works by extracting the name from Instrument.FullName

Baruch
Baruch 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
Backtest order fills at Ask\Bid? Ironleg Strategy Analyzer 5 03-07-2012 07:19 AM
How do I implement this idea falcon00 Automated Trading 1 05-28-2010 05:35 AM
IDataSeries - Can we implement and pass to NT methods/functions? Brillo Indicator Development 9 04-27-2010 08:48 AM
Market Replay Fills vs Live Real Money Fills Lusitano Strategy Development 1 03-02-2009 09:38 AM
implement a function on NT kuroro13 Indicator Development 8 08-06-2008 05:31 PM


All times are GMT -6. The time now is 12:10 PM.