So, I've been working my way through the quantstrat code to make sure it
does what I expect it to do.  I discovered that an ordertype of "market"
means that buy orders get filled at the market bid price and sell orders get
filled at the market ask price.  In my mind, market order means pay to take
liquidity, not get paid for providing liquidity.  But, after looking at the
comments in the code, I see that this is intentional.  If you want your buys
to lift offers and sells to hit bids, in the quantstrat world, you have
"opside" orders, not "market" order.

In the file traderules.r, here is a snippet from the ruleSignal function:
                switch(pricemethod,
                opside = {
                    if (orderqty>0)
                        prefer='ask'  # we're buying, so pay what they're
asking
                    else
                        prefer='bid'  # we're selling, so give it to them
for what they're bidding
                    orderprice <- try(getPrice(x=data,
prefer=prefer))[timestamp]
 },
                market = {
                    if(is.BBO(mktdata)){
                        if (orderqty>0)
                            prefer='bid'  # we're buying, so work the bid
price
                        else
                            prefer='ask'  # we're selling, so work the ask
price
                  }

But, you CAN't use "opside" for orderside because several function (e.g.
updateOrders, addOrder) don't allow it.  They will throw this error:

ordertype: opside  must be one of
"market","limit","stoplimit","stoptrailing", or"iceberg"

Ok.  So, instead of using "opside" for ordertype, how about changing the
code for "market" ordertype.  That makes a little progress, but then you get
to the ruleOrderProc function from orders.R.  Here's the first part of a
switch statement in ruleOrderProc.

switch(orderType,
                    market = {
                        switch(freq$scale,
                                yearly = ,
                                quarterly = ,
                                monthly = ,
                                daily = {

txntime=as.character(index(ordersubset[ii,])) # transacts on this bar, e.g.
in the intraday cross, or leading into the end of month, quarter, etc.
                                    # txntime=as.character(timestamp) # use
this if you wanted to transact on the close of the next bar

txnprice=as.numeric(getPrice(last(mktdata[txntime]), ...=...))
                                }, #end daily and lower frequency processing
                                {
                                    txnprice =
as.numeric(getPrice(mktdataTimestamp))
                                    #TODO extend this to figure out which
side to prefer
                                    txntime = timestamp
                                }) # end switch on frequency
                    }

There is no code written yet to support market orders on intraday Bid/Ask
data (or any other timescale for that matter)!  But, to me, the term "market
order" doesn't mean anything unless you've got bids and asks.

Aside from that, notice that for daily data, it passes ...=... to getPrice,
but getPrice doesn't ask for that argument.

Garrett

	[[alternative HTML version deleted]]

