0
I am trying to analyze the data from the following table. This is a Trades record just to make it clear.
DATAFRAME:
def lista_preco():
#ainda falta resolver os trades depois de 19h
lista_dias = df['DATE'].unique().tolist()
lista = df['pair'].unique().tolist()
lista_buy = []
lista_sell = []
for dias in lista_dias:
for tickers in lista:
if (df['pair']== tickers and df['type'] == 'sell'):
mask1 = (df['pair']== tickers and df['type'] == 'sell')
df2 = df[mask1]
df2['price_vol'] = df2['price'] * df2['vol']
preco = df2['price_vol'].sum(axis = 0) / df2['vol'].sum(axis = 0)
lista_sell.append([dias,tickers,preco,"LONG"])
if (df['pair']== tickers and df['type'] == 'buy'):
mask2 = df['pair']== tickers and df['type'] == 'buy'
df2 = df[mask2]
df2['price_vol'] = df2['price'] * df2['vol']
preco = df2['price_vol'].sum(axis = 0) / df2['vol'].sum(axis = 0)
lista_buy.append([dias, tickers, preco, "SHORT"])
return lista_buy, lista_sell
ERROR:
ValueError Traceback (most recent call last)
<ipython-input-16-d301c71c9e88> in <module>
----> 1 lista_preco()
<ipython-input-15-8ac3ff545e1d> in lista_preco()
12 for dias in lista_dias:
13 for tickers in lista:
---> 14 if (df['pair']== tickers and df['type'] == 'sell'):
15
16 mask1 = (df['pair']== tickers and df['type'] == 'sell')
~\anaconda3\lib\site-packages\pandas\core\generic.py in __nonzero__(self)
1477 def __nonzero__(self):
1478 raise ValueError(
-> 1479 f"The truth value of a {type(self).__name__} is ambiguous. "
1480 "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
1481 )
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
It would help if you could provide CSV or a sample of it to test, and say what you want as a final result
– Miguel
What I want to do with this data is to calculate the average price (I already know how it does). But the point is that I need to calculate the average price for purchase transactions ("buy" = "LONG") and sale("sell" = "SHORT") separately for the same asset. In addition, I also need to record the date on which the operation took place and the volume("vol") in a list. type : [[DATA,PAIR,PRICE,"LONG"], ...]
– Pedro Ivo
You can provide csv pf, only a few lines serve
– Miguel
csv is in the following format: DATE;time;pair;type;ordertype;price;cost;fee;vol;margin;Misc 30/12/2020;15:11:05;ABEV3;buy;market;10;1000;12;300;0;0 30/12/2020;15:10:08;limit MRV3;;buy;20;15500;9;200;0;0 30/12/2020;15:09:11;PETR4;;limit sell;30;4516;8;100;0;0 30/12/2020;13:46:53;ABEV3;buy;market;40;45415;10;500;0;0 30/12/2020;13:20:07;TAEE11;buy;limit;10;5465;13;600;0;0
– Pedro Ivo
Is the average price per day for each of the types? Or the total average price for each of the types?
– Miguel
Average price per day for each asset with distinction between buy and sell. Here is how to calculate the average price: https://ajuda.easynvest.com.br/hc/pt-br/articles/360049317813-Como-realizar-o-c%C3%A1lculo-do-Pre%C3%A7o-M%C3%A9dio-
– Pedro Ivo
Checks pf if the answer solves the problem
– Miguel