Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug when several executions in an order #12

Open
YAmikep opened this issue Nov 10, 2017 · 4 comments
Open

Bug when several executions in an order #12

YAmikep opened this issue Nov 10, 2017 · 4 comments

Comments

@YAmikep
Copy link

YAmikep commented Nov 10, 2017

When there are several executions within an order, the quantity extracted is wrong because the current code uses the quantity of the first execution.

For example, the CSV will say that the quantity of the order is 100 while it is actually 231.

{
    ...
    'average_price': '1.30000000',
    'cumulative_quantity': '231.00000',
    'price': '1.31000000',
    'quantity': '231.00000', 
    'executions': [
        {'id': 'xxx',
        'price': '1.30000000',
        'quantity': '100.00000',
        'settlement_date': '2015-12-22',
        'timestamp': '2015-12-17T20:19:52.656000Z'},
        {'id': 'yyy',
        'price': '1.30000000',
        'quantity': '100.00000',
        'settlement_date': '2015-12-22',
        'timestamp': '2015-12-17T20:19:52.656000Z'},
        {'id': 'zzz',
        'price': '1.30000000',
        'quantity': '31.00000',
        'settlement_date': '2015-12-22',
        'timestamp': '2015-12-17T20:19:52.657000Z'}],
    ...
}

By removing the 2 following lines, the code will use the quantity of the order which I believe is correct.
https://github.com/joshfraser/robinhood-to-csv/blob/master/csv-export.py#L95-L96

@joshfraser
Copy link
Owner

Agreed that this is a bug. Not sure the right fix is deleting those lines. Don't have time to investigate right now though.

@wezzybytes
Copy link
Contributor

I don't think the CSV output should replace order quantity and price with first execution quantity and price. Order price and quantity are entered by a user when creating an order. It may differ from the executed price and quantity.

Here's an example of a market order from my portfolio (note both prices):

Order:{  
   'updated_at':'2018-04-04T14:43:25.745567Z',
   'ref_id':None,
   'time_in_force':'gfd',
   'fees':'0.00',
   'cancel':None,
   'response_category':None,
   'id':'[ORDER_ID]',
   'cumulative_quantity':'100.00000',
   'stop_price':None,
   'reject_reason':None,
   'instrument':'https://api.robinhood.com/instruments/[INSTRUMENT_ID]/',
   'state':'filled',
   'trigger':'immediate',
   'override_dtbp_checks':False,
   'type':'market',
   'last_transaction_at':'2018-04-04T14:43:25.648000Z',
   'price':'3.05000000',
   'executions':[
      {  
         'timestamp':'2018-04-04T14:43:25.648000Z',
         'price':'2.91990000',
         'settlement_date':'2018-04-09',
         'id':'[EXECUTION_ID]',
         'quantity':'100.00000'
      }
   ],
   'extended_hours':False,
   'account':'https://api.robinhood.com/accounts/[ACCOUNT_ID]/',
   'url':'https://api.robinhood.com/orders/[ORDER_ID]/',
   'created_at':'2018-04-04T14:43:25.427702Z',
   'side':'buy',
   'override_day_trade_checks':False,
   'position':'https://api.robinhood.com/accounts/[ACCOUNT_ID]/positions/[POSITION_ID]/',
   'average_price':'2.91990000',
   'quantity':'100.00000'
}

I propose to leave the order price and quantity alone. Cumulative quantity is the sum of execution[quantity], and average_price is the average of execution[price]. Most users refer to these as filled quantity and filled price, respectively.

@joshfraser
Copy link
Owner

@YAmikep are you happy with @cdesai-qi's solution?

@YAmikep
Copy link
Author

YAmikep commented Dec 27, 2018

Yes, sounds good, the order information should not be changed by the executions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants