1

I'm having trouble querying a simple .csv file of trade data with conditions. The .csv file has 5 columns - [open, high, low, close, volume] with a time-series index, of minute by minute trades.

What I want to do is build a script that

(1) tells me how many times the close price minus the open price of the previous minute is positive.

(2) the volume of the previous minute is greater than that of the minute before it.

So far I have the first half (1) down with:

ts2 = ts[(ts["close"]-ts["open"].shift(1))>0]

However, I am having trouble combining it with the volume conditions (2) I require. I have attempted the following, both resulting in syntax and other errors.

ts2 = ts[(ts["close"]-ts["open"].shift(1))>0]
ts3 = ts[(ts["volume"].shift(1)-ts["volume"].shift(2))>0]
ts4 = ts[ts2 & ts3]

ts4 = ts[(ts["close"]-ts["open"].shift(1)>0) & (ts["volume"].shift(1)-ts["volume"].shift(2)>0)

Ultimately I would use the:

print(len(ts4)) 

To find how many occurrences there are in my csv file of the conditional query.

Please let me know how I can combine both conditions, and if there is a way to improve my current methodology.

Thank you, all help is greatly appreciated!

user2906838
  • 1,178
  • 9
  • 20
A. Blackmagic
  • 233
  • 1
  • 3
  • 9

1 Answers1

1

You are almost there. Just make ts2 and ts3 masks instead of actual queried dataframe.

ts2 = (ts["close"] - ts["open"].shift(1)) > 0  #this is a mask
ts3 = (ts["volume"].shift(1) - ts["volume"].shift(2)) > 0 #this is a mask
ts4 = ts.loc[ts2 & ts3] #query using 2 masks

Hope this helps.

gyoza
  • 2,112
  • 2
  • 12
  • 18