joshfraser / robinhood-to-csv

Python script to export Robinhood trades to a CSV file
MIT License
259 stars 109 forks source link

Bug when several executions in an order #12

Open YAmikep opened 7 years ago

YAmikep commented 7 years ago

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 commented 7 years ago

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 commented 6 years ago

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 commented 5 years ago

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

YAmikep commented 5 years ago

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