Beginner python. Take it slow with me please:)
I have a consolidation script that ends with 'printing' a list of files whether they are consolidating or breakout candidates. While it prints nicely I want to take those prints and create 2 separate csv files.
What the script does is it reads hundreds of CSV files in a dataset/daily folder and creates the dataframe to then figure out what stocks are consolidators or breakout candidates.
What I am trying to do is have the script EXPORT two separate lists one listing consolidators and another listing breakout stock symbols from the data but am having a ton of trouble doing so. You can see those lines at the bottom.
Any help would be greatly appreciated.
import os, pandas
def is_consolidating(df, percentage=2.5):
recent_candlesticks = df[-15:]
max_close = recent_candlesticks['Close'].max()
min_close = recent_candlesticks['Close'].min()
threshold = 1 - (percentage / 100)
if min_close > (max_close * threshold):
return True
return False
def is_breaking_out(df, percentage=4.0):
if df.empty:
pass
else:
last_close = df[-1:]['Close'].values[0]
#print (last_close)
if is_consolidating(df[:-1], percentage=percentage):
recent_closes = df[-16:-1]
if last_close > recent_closes['Close'].max():
return True
return False
for filename in os.listdir('datasets/daily'):
df = pandas.read_csv('datasets/daily/{}'.format(filename))
if is_consolidating(df):
df.to_csv('export_consolidators.csv')
print("{} is consolidating".format(filename))
if is_breaking_out(df):
df.to_csv('export_breakouts.csv')
print("{} is breaking out".format(filename))
Write mode of pandas.DataFrame.to_csv() is w
by default. Possible values can referred to open() .
I think you want to store each kind of dataframe to separate files by appending. You may do
for filename in os.listdir('datasets/daily'):
df = pandas.read_csv('datasets/daily/{}'.format(filename))
if is_consolidating(df):
df.to_csv('export_consolidators.csv', mode='a', header=False)
print("{} is consolidating".format(filename))
if is_breaking_out(df):
df.to_csv('export_breakouts.csv', mode='a', header=False)
print("{} is breaking out".format(filename))
a
here means append mode.
You could also store them in a list first, then pandas.concat() each list into one dataframe.
consolidating_list = []
breaking_out_list = []
for filename in os.listdir('datasets/daily'):
df = pandas.read_csv('datasets/daily/{}'.format(filename))
if is_consolidating(df):
consolidating_list.append(df)
print("{} is consolidating".format(filename))
if is_breaking_out(df):
breaking_out_list.append(df)
print("{} is breaking out".format(filename))
combined_consolidating = pd.concat(consolidating_list).reset_index()
combined_breaking_out = pd.concat(breaking_out_list).reset_index()
combined_consolidating.to_csv('export_consolidators.csv')
combined_breaking_out.to_csv('export_breakouts.csv')
If there are many kinds of dataframe. You could use a two-dimensional list to decrease the redundant statement.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.