I have a dataframe, filled_orders, like that:
price amount side fees timestamp
0 0 2 bids 0 2019-06-25 12:24:46.570000
1 3 2 asks 0 2019-06-25 12:22:46.570000
2 2 4 bids 0 2019-06-25 12:22:46.570000
3 5 1 asks 0 2019-06-25 12:24:46.570000
4 1 4 asks 0 2019-06-26 12:24:46.570000
Then a loop like that:
for index, row in filled_orders.iterrows():
if row.side == 'asks':
filled_orders = filled_orders.drop(index)
elif row.side == 'bids':
filled_orders = filled_orders.drop(index)
What I need to do is to do this loop until there is either one asks
or one bids
remaining. What I tried right above the loop is the following:
while (filled_orders.side == 'bids').empty == True or (filled_orders.side == 'asks').empty == True:
at the end what I need in the dataframe example above is the following:
price amount side fees timestamp
4 1 4 asks 0 2019-06-26 12:24:46.570000
Basically, no matter what input I put in that code, at the end what I need is just rows with only bids
or asks
But it does not work... any idea? Thanks!
the full code looks like this:
while (filled_orders.side == 'bids').any() == False or (filled_orders.side == 'asks').any() == False:
for index, row in filled_orders.iterrows():
if row.side == 'asks':
if filled_orders.index.contains(filled_orders.first_valid_index()):
if filled_orders.loc[index, 'amount'] == filled_orders.loc[filled_orders.first_valid_index(), 'amount'] and filled_orders.loc[filled_orders.first_valid_index(), 'side'] == 'bids':
filled_orders = filled_orders.drop(filled_orders.first_valid_index())
filled_orders = filled_orders.drop(index)
elif filled_orders.loc[index, 'amount'] < filled_orders.loc[filled_orders.first_valid_index(), 'amount'] and filled_orders.loc[filled_orders.first_valid_index(), 'side'] == 'bids':
filled_orders.at[filled_orders.first_valid_index(), 'amount'] = float(filled_orders.at[filled_orders.first_valid_index(), 'amount']) - float(filled_orders.at[index, 'amount'])
filled_orders = filled_orders.drop(index)
elif filled_orders.loc[index, 'amount'] > filled_orders.loc[filled_orders.first_valid_index(), 'amount'] and filled_orders.loc[filled_orders.first_valid_index(), 'side'] == 'bids':
filled_orders.at[index, 'amount'] = float(filled_orders.at[index, 'amount']) - float(filled_orders.at[filled_orders.first_valid_index(), 'amount'])
filled_orders = filled_orders.drop(filled_orders.first_valid_index())
if row.side == 'bids':
if filled_orders.index.contains(filled_orders.first_valid_index()):
if filled_orders.loc[index, 'amount'] == filled_orders.loc[filled_orders.first_valid_index(), 'amount'] and filled_orders.loc[filled_orders.first_valid_index(), 'side'] == 'asks':
filled_orders = filled_orders.drop(filled_orders.first_valid_index())
filled_orders = filled_orders.drop(index)
elif filled_orders.loc[index, 'amount'] < filled_orders.loc[filled_orders.first_valid_index(), 'amount'] and filled_orders.loc[filled_orders.first_valid_index(), 'side'] == 'asks':
filled_orders.at[filled_orders.first_valid_index(), 'amount'] = filled_orders.at[filled_orders.first_valid_index(), 'amount'] - filled_orders.at[index, 'amount']
filled_orders = filled_orders.drop(index)
elif filled_orders.loc[index, 'amount'] > filled_orders.loc[filled_orders.first_valid_index(), 'amount'] and filled_orders.loc[filled_orders.first_valid_index(), 'side'] == 'asks':
filled_orders.at[index, 'amount'] = float(filled_orders.at[index, 'amount']) - float(filled_orders.at[filled_orders.first_valid_index(), 'amount'])
filled_orders = filled_orders.drop(filled_orders.first_valid_index())
The error that I receive is the following:
AttributeError: 'NoneType' object has no attribute 'side'