-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStockBot.py
More file actions
137 lines (106 loc) · 4.28 KB
/
StockBot.py
File metadata and controls
137 lines (106 loc) · 4.28 KB
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
from alpaca_trade_api.rest import REST, TimeFrame
import pandas as pd
import json
import time
BASE_URL = "https://paper-api.alpaca.markets"
KEY_ID = "PKKR6QBBLW6PJSXNA3NU"
SECRET_KEY = "ytMJYuJnNlzPi2vTNWgENEVqsr74SjGCErvXSmgN"
api = REST(key_id=KEY_ID,secret_key=SECRET_KEY,base_url="https://paper-api.alpaca.markets")
active_assets = api.list_assets(status='active')
stocks = [a for a in active_assets if a.easy_to_borrow == True if a.shortable == True if a.exchange == 'NASDAQ']
clock = api.get_clock()
if clock.is_open == False:
exit()
def jsonParser(Ticker):
# Converts the data into a json compatable format
bars = api.get_bars(Ticker, TimeFrame.Hour).df
result = bars.to_json(orient="index")
parsed = json.loads(result)
return parsed
def readFile(path, ticker):
# Opens the choosen file and returns the section requested
file = open(path)
read = json.loads(file.read())
return read[ticker]
def toTheBottom(dic):
# Method returns a nested dictionary
for key, value in dic.items():
if type(value) is dict:
toTheBottom(value)
else:
return dic
def writeFile(data, fileName):
# Writes the given data to a json file
with open("data.json", "w") as fileName:
json.dump(data, fileName, indent=4)
data = {
}
count = 0
for asset in stocks:
data[f"{asset.symbol}"] = jsonParser(f"{asset.symbol}")
def AdjCreater(data, file):
for stock in reversed(data):
indStockData = (readFile(file,stock))
count = 0
for dates in indStockData:
DataPoint = toTheBottom(indStockData[dates])
if(count != 0):
DataPoint['adjopen'] = (OldOpen+OldClose)/2
DataPoint['adjlow'] = (DataPoint['low'])
DataPoint['adjhigh'] = (DataPoint['high'])
DataPoint['adjclose'] = (DataPoint['low'] + DataPoint['high'] + DataPoint['open'] + DataPoint['close'])/4
OldOpen = DataPoint['open']
OldClose = DataPoint['close']
count += 1
data[stock] = indStockData
return data
def MakeMoney(indStockData, stock):
count = 0
for dates in reversed(indStockData):
DataPoint = toTheBottom(indStockData[dates])
if(count <5 and len(indStockData) > 4):
print(stock)
if(count == 0):
OldestDataOpen = DataPoint['adjopen']
if(count == 1):
OldDataOpen = DataPoint['adjopen']
if(count == 2):
NewDataOpen = DataPoint['adjopen']
if(count == 3):
NewestDataOpen = DataPoint['adjopen']
NewestDataClose = DataPoint['adjclose']
if(count == 4):
if(OldestDataOpen>OldDataOpen and OldDataOpen> NewDataOpen and NewDataOpen < NewestDataOpen):
q = (20000/NewestDataClose)
q = int(q)
print('Buying ' + str(q) + " " + stock)
try:
api.submit_order(symbol=stock, qty=q, side='buy', type='market', time_in_force='gtc' )
except:
print("Couldn't buy " + stock)
if(OldestDataOpen < OldDataOpen and OldDataOpen < NewDataOpen and NewDataOpen > NewestDataOpen):
q = 0
try:
position = api.get_position(stock)
q = position.qty
q = int(q)
except:
print("No position")
if q <= 0:
q = (20000/NewestDataClose)
q = int(q)
print('Selling ' + str(q) + " " + stock)
try:
api.submit_order(symbol=stock, qty=q, side='sell', type='market', time_in_force='gtc' )
except:
print("Couldn't sell " + stock)
count += 1
writeFile(data, "data.json")
writeFile(AdjCreater(data, "data.json"), 'data.json')
for stock in data:
name = str(stock)
MakeMoney(readFile('data.json', stock), name)
#In case we need to convert to a normal time
# for times in parsed:
# times = int(times)/1000
# times = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(times)