-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
143 lines (123 loc) · 3.9 KB
/
app.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
from flask import Flask, render_template
import trader
from datetime import datetime, timedelta
import threading
import math
from time import sleep
app = Flask(__name__)
trader.setup("BTCUSD")
Time = datetime.now()
curr_price = trader.quote()
THRESHOLD = 0.6/100
bought = True
buys = [[math.floor(datetime.now().timestamp() * 1000), curr_price]]
data = [[math.floor(datetime.now().timestamp() * 1000), curr_price]]
Buy_price = curr_price
sell_price = curr_price
sells = []
balance = 0
print(f"current price: {curr_price}")
investment = curr_price
def buy():
global bought, curr_price, Buy_price, balance, sell_price
if not bought:
Buy_price = curr_price
balance = (balance * 100 - Buy_price * 100) / 100
bought = True
print("buy", curr_price, balance)
buys.append([math.floor(datetime.now().timestamp() * 1000), curr_price])
def sell():
global bought, curr_price, Buy_price, balance, sell_price
if bought:
sell_price = curr_price
balance = balance + sell_price
bought = False
print("sell", curr_price, balance)
sells.append([math.floor(datetime.now().timestamp() * 1000), curr_price])
def stock_log():
global bought, curr_price, Buy_price, balance, sell_price
print("value: "+str(curr_price), "Stock held: " + str(bought),
"balance: "+str(balance))
def getNAV():
global bought, curr_price, Buy_price, balance, sell_price
if bought:
return balance + curr_price
else:
return balance
def main():
global bought, curr_price, Buy_price, balance, sell_price
while True:
if trader.quote() < curr_price:
curr_price = trader.quote()
data.append([math.floor(datetime.now().timestamp() * 1000), curr_price])
if bought and curr_price > Buy_price and (curr_price * 100 - Buy_price * 100) / 100 >= THRESHOLD * curr_price:
sell()
else:
stock_log()
elif trader.quote() > curr_price:
curr_price = trader.quote()
data.append([math.floor(datetime.now().timestamp() * 1000), curr_price])
if not bought and curr_price < sell_price and (sell_price * 100 - curr_price * 100) / 100 >= (THRESHOLD/2) * curr_price:
buy()
else:
stock_log()
@app.route("/")
def home():
return render_template("zing.jinja")
@app.route("/investment")
def initial():
global investment
return { "initial": investment }
@app.route("/info", methods=["POST"])
def info():
return {
"data": [
{
"type": "line",
"values": data
},
{
"type": "scatter",
"values": buys,
"marker": {
"size": 8,
"background-color": "#00ff00",
}
},
{
"type": "scatter",
"values": sells,
"marker": {
"size": 8,
"background-color": "#ff0000",
}
}
],
"bought": bought,
"balance": balance,
"NAV": getNAV()
}
@app.route("/buy", methods = ["POST"])
def buy_req():
buy()
return { "bought": bought }
@app.route("/sell", methods = ["POST"])
def sell_req():
sell()
return { "bought": bought }
# def decimate():
# global data
# start = data[0][0] % 5
# while True:
# sleep(60)
# print(len(data))
# for i in data:
# if (datetime.utcfromtimestamp(i[0]/1000).minute % 5 != start) and not i in buys:
# print(datetime.utcfromtimestamp(i[0]/1000).minute)
# data.remove(i)
# print(len(data))
threading.Thread(target=main).start()
# simple = threading.Thread(target=decimate)
# simple.start()
if __name__ == "__main__":
app.run()