Back to Community
how to find purchase time of positions ?

Hi,

I want to sell old positions in my portfolio. Is there anyway to find purchase time for each position through api ?

thanks,

tony

8 responses

it's a pain in the butt.

I just added this feature to my private framework, here's how I do it

every bar,
for each open order,
check if order.filled is different from order.filled from last bar
if yes, the difference is the amount filled last bar. (use price/volume/time of last bar to compute fill price / etc)

Tony and Jason,

Below and attached is code I wrote to understand this issue better. Indeed, I don't see a built-in way of tracking partial order fills. But if you are only interested in tracking completely filled orders (or orders that were partially filled and then cancelled), you could use get_order(context.order_id).dt to obtain the datetime stamp of the completed order transaction.

Grant

def initialize(context):  
    context.spy = sid(8554)  
    context.order_id = None  
    context.first_bar = False  
    context.bar_count = 0  
    set_slippage(slippage.VolumeShareSlippage(volume_limit=1e-5, price_impact=0))

def handle_data(context, data):  
    context.bar_count += 1

    if not context.first_bar:  
        context.order_id = order(context.spy,100)  
        print context.order_id  
        context.first_bar = True  
    print get_order(context.order_id)  
    print get_order(context.order_id).dt # datetime stamp, order completely filled or cancelled  
    print get_order(context.order_id).filled # cumulative number of shares filled  
    print context.portfolio.positions[context.spy]  
    if context.bar_count == 5:  
        cancel_order(context.order_id)  
Clone Algorithm
10
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 541bf2c5b7392408acb938af
There was a runtime error.

i will release my code on the next update to QuantShim, though it's a huge framework so may be difficult to copy/paste into your own solution.

BTW, I make use of the undocumented order.status field. You can see it here: https://github.com/quantopian/zipline/blob/master/zipline/finance/blotter.py

values are:

ORDER_STATUS = Enum(  
    'OPEN',  
    'FILLED',  
    'CANCELLED',  
    'REJECTED',  
    'HELD',  
)

so CANCELLED is order.status==2

It would be nice if this was included as part of the Q framework, perhaps as variable of the "position" object, such as:

context.portfolio.positions[stock].opened_datetime  

Actually, I was very surprised to find that this was not already available...

@Makoto, the problem I have with this (and indeed Quantopian's order/position/portfolio architecture) is that it's assuming you do not need to track multiple positions per security.

That's actually the primary motivation behind why I started https://github.com/Novaleaf/QuantShim I have the concept of "StrategyPosition" which allows this "many to many" relationship between a strategy and a security.

That said, the amount of code needed to construct (what I hope is) a professional framework in Quantopian seems to be quite a turnoff for developers here. So I can certainly see the appeal of keeping the quantopian's "built in" objects as light as possible. (don't overwhelm users with complexity)

It seems the basic problem here is that one might want to close out positions established with a given order before the order is confirmed closed. Is this a correct assessment?

As an example, say I put an order in for 1000 shares of XYZ. Only 100 shares of XYZ are available in the first hour of trading, which are purchased per the order. Toward the end of the day, my order is still open, but I'd like to make a decision on selling the 100 shares based on when they were purchased. But, there is no time-stamped record in the order object of the partial fill.

So, I'm wondering if the order object should track fills (e.g. add a Pandas dataframe/timeseries)?

Classic... Yea it would be nice to have time stamp on the get_order(order_id)

what I would do here is create a list that documents the time of when you buy or sell a stock. In this mannor you just manage a list of whatever time class you want.

Something like this:
for stock in whatever ya wana buy:
order_id = Order(sid,10,10) - some order is made
context.buy_orders.append(order_id) - This is the order you made and can query its status later by adding the order_ID - 2 - canceled -3 rejected (SEC ticker BS likely)
context.buy_order_times[sid] = get_datetime() - then you can tell when you bought it...

remember to manage your list