-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstockTradingBot.py
167 lines (146 loc) · 5.84 KB
/
stockTradingBot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import math
from pprint import pprint
import csv
from datetime import date
import os
today = date.today()
today = str(today)
os.environ['TZ'] = ('US/EASTERN')
path = f"./recentTrades"
exist = os.path.exists(path)
if not exist:
os.makedirs(path)
def runner_algo(
min_momentum, # Min days trending to purchase
input_dollars): # Money per asset
candle_stick = get_data()
# Global Variable Init.
#support = float(candle_stick.get('0')[2])
end_of_file = list(candle_stick.items())[-1][1][0]
days_trending = 0
invested = False
prev_symbol = ""
portfolio = {}
daily_high_arr = [1]
date_arr = []
shares = 1
funds_remain = input_dollars
with open(f'./recentTrades/{today[5:]} stock_buy.csv', 'w+') as fst2:
# Iterate though every entry ("Candle Stick")
writer = csv.writer(fst2)
for (key, value) in list(candle_stick.items()):
# Get TICKR Symbol & CURRNT day HIGH
symbol = value[3]
date_arr.append(value[4])
daily_high_arr.append(float(value[1]))
curr_high = daily_high_arr[-1]
if symbol != prev_symbol: # SYMBOL (asset) CHANGE:
if invested == True:
# SELL! TIME line is ended (next stock is starting)..
sell_function = sell_asset(invested, writer, funds_remain,
daily_high_arr[-2], shares,
date_arr[-1], prev_symbol)
invested = sell_function[0]
funds_remain = sell_function[1]
portfolio.update({prev_symbol: funds_remain})
prev_symbol = value[3]
funds_remain = input_dollars
invested = False
support = 0
# START:
if curr_high >= support and value[0] != end_of_file:
if days_trending >= min_momentum:
support = daily_high_arr[
-2] # Support becomes the High 2 days prior to current
# BUY soon as min. momentum is met & there is enough money
if days_trending == min_momentum:
if invested == False:
asset = buy_asset(curr_high, funds_remain)
invested = True
funds_remain = asset[0]
shares = asset[1]
temp = ("BUY", value[3], curr_high, value[4])
writer.writerow(temp)
portfolio.update({symbol: funds_remain})
# HOLD
# elif funds_remain != input_dollars:
days_trending += 1
# Fell throuh support
else:
support = curr_high
days_trending = 0
mean = [1]
change = percent_change(daily_high_arr[-2], curr_high, mean)
change = change[0]
if change > 0.25 or value[0] == end_of_file:
# SELL! assets have fallen ___% far...
sell_function = sell_asset(invested, writer, funds_remain,
curr_high, shares, value[4],
prev_symbol)
invested = sell_function[0]
funds_remain = sell_function[1]
portfolio.update({symbol: funds_remain})
# END: Cumulative data -> txt file
with open(f'./recentTrades/{today[5:]} candle_read.csv', 'w+') as fst2:
# Print The statistics
writer = csv.writer(fst2)
cash_out = 0
cash_in = 0
for (symbol, dollar_value) in portfolio.items():
#fst2.write(f'day_day_perc_change{symbol}: $ {dollar_value:0.2f}\n')
cash_out += dollar_value
cash_in += 1
invested = str(input_dollars * cash_in)
returned = f'{cash_out:0.2f}'
profit = f"{cash_out-input_dollars*cash_in:0.2f}"
change = percent_change(cash_in * input_dollars, cash_out, [1])
change = f"{change[0] : 0.2f}"
temp = (invested, returned, profit, change)
writer.writerow(temp)
print("\n")
pprint(portfolio)
return (float(portfolio[symbol]))
# Helper function
def percent_change(a, b, mean):
c = ((a - b) / abs(b)) * 100
return (c, mean)
# BUY Function
def buy_asset(curr_high, funds_remain):
# Calculate how many shares can be purchased
multiplier = math.floor(funds_remain / float(curr_high))
# If at lease 1 share can be bought...
if multiplier > 1:
funds_remain = funds_remain - curr_high * multiplier
else:
funds_remain = funds_remain - curr_high
return (funds_remain, multiplier)
# SELL Function
def sell_asset(invested, writer, funds_remain, curr_high, shares, date,
prev_symbol):
with open(f'./recentTrades/{today[5:]} stock_sell.csv', 'a+') as fst2:
# Iterate though every entry ("Candle Stick")
writer = csv.writer(fst2)
if invested == True:
invested = False
funds_remain = funds_remain + curr_high * shares
temp = ("SELL", prev_symbol, curr_high, date)
writer.writerow(temp)
return (invested, funds_remain)
# Data Source
def get_data():
candle_stick = {}
k = 0
with open(f'./{today}/{today[5:]}\'s candle_stick.csv', mode='r') as fs:
reader = csv.reader(fs)
for row in reader:
if k >= 1:
days = row[0]
high = row[3]
low = row[4]
sym = row[6]
date = row[7]
candle_stick.update({days: (days, high, low, sym, date)})
k += 1
return candle_stick
if __name__ == "__main__":
runner_algo(1, 10000)