robinhood-to-csv icon indicating copy to clipboard operation
robinhood-to-csv copied to clipboard

Bug when several executions in an order

Open YAmikep opened this issue 7 years ago • 4 comments

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

YAmikep avatar Nov 10 '17 00:11 YAmikep

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

joshfraser avatar Nov 19 '17 08:11 joshfraser

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.

wezzybytes avatar Sep 09 '18 11:09 wezzybytes

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

joshfraser avatar Dec 27 '18 07:12 joshfraser

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

YAmikep avatar Dec 27 '18 18:12 YAmikep