NinjaTrader Support Forum  
X

Attention!

This website will be down for maintenance from Friday May 24th at 6PM MDT until Sunday May 26th at 12PM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com


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 09-07-2010, 12:08 PM   #1
kaydgee
Member
 
Join Date: Jun 2010
Posts: 34
Thanks: 0
Thanked 0 times in 0 posts
Default Logging each tick as trading at the bid/ask

Hello all,

I'd like to develop an execution algorithm that takes into account recent trades and whether they occurred at the bid or at the ask.

I'm wondering if I can record the most recent Bid and Ask during the OnMarketData() method (using GetCurrentBid() and GetCurrentAsk()) and whenever the OnBarUpdate() method runs (with CalculateOnBarClose==false), compare the most recent Close[0] with the most recent Bid & Ask and look for a match.

My question is if OnBarUpdate() is called at the start of every bar, even if there was no trade, and, if so, which method should I use to find the price at which the last trade occurred?

All the best,

Ken
kaydgee is offline  
Reply With Quote
Old 09-07-2010, 01:07 PM   #2
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 Kaydgee,

There won't be a bar without having a trade. OnBarUpdate() is called for every trade.

You may want to work with last price value of OnMarketData() though, to align the values you're seeking to compare. You're then capturing the values all within the same event.

http://www.ninjatrader-support.com/H...arketData.html
NinjaTrader_RyanM is offline  
Reply With Quote
Old 09-07-2010, 01:30 PM   #3
kaydgee
Member
 
Join Date: Jun 2010
Posts: 34
Thanks: 0
Thanked 0 times in 0 posts
Default

Quote:
Originally Posted by NinjaTrader_RyanM View Post
There won't be a bar without having a trade. OnBarUpdate() is called for every trade.
Good point.

As for capturing all data in OnMarketData(), would something like the below section of code work?

Code:
protected override void OnMarketData(MarketDataEventArgs e) 
{ 
    if (e.MarketDataType == MarketDataType.Last)
    {
        double lastPrice = e.Price;
        if (lastPrice == currentBid) Print("Last trade at bid");
        else if (lastPrice == currentAsk) Print("Last trade at ask");
        else Print("Last trade at neither bid nor ask");
    }
    else if (e.MarketDataType == MarketDataType.Ask) double currentAsk = e.Price;
    else if (e.MarketDataType == MarketDataType.Bid) double currentBid = e.Price;
}
kaydgee is offline  
Reply With Quote
Old 09-07-2010, 03:01 PM   #4
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

Yes, that looks like a good approach, although would probably need to declare your variables so they're available outside of their else if blocks.
NinjaTrader_RyanM 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
Tick Filter + Bid/Ask cgeorgan Automated Trading 1 07-28-2010 08:55 AM
Adding Bid / Ask tick data to an indicator mrlogik Version 7 Beta General Questions & Bug Reports 4 07-27-2010 01:40 PM
MB Trading Connection - No Last But Getting Bid and Ask jirkoo Miscellaneous Support 13 03-24-2009 06:47 AM
Logging in/logging out spacebalts67 Miscellaneous Support 3 03-09-2009 03:17 PM
LMT orders ... BUY / SELL @ LAST / ASK / BID in automated trading. mkundi Automated Trading 4 04-04-2006 10:12 PM


All times are GMT -6. The time now is 06:07 PM.