-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
207 lines (172 loc) · 6.14 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import json
from datetime import timedelta
from json import JSONDecodeError
from flask import Flask, request, render_template
from flask import redirect, url_for, flash, session
from database.update import *
from database.model import *
from algorithem.shelley07 import *
from appConfig.tradingViewConfig import password
from appConfig.databaseConfig import *
# ---------Flask Basic Setting---------
app = Flask(__name__)
app.secret_key = 'SECRET_KEYYY'
app.permanent_session_lifetime = timedelta(minutes=5)
version = 'Beta v0.7.2 - FLIP'
# --------- Binance API---------
client = Client(binanceAPIConfig.API_KEY, binanceAPIConfig.API_SECRET)
# ---------Database config---------
ENV = ''
if ENV == 'dev':
app.debug = True
app.config['SQLALCHEMY_DATABASE_URI'] = LOCAL_DATABASE_URL
app.config['SQLALCHEMY_DATABASE_URI'] = HEROKU_DATABASE_URL
version = "TESTING"
else:
app.debug = False
app.config['SQLALCHEMY_DATABASE_URI'] = HEROKU_DATABASE_URL
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
def init_db():
db.init_app(app)
db.app = app
db.create_all()
# version = "DEBUG"
init_db()
discordWebhookConfig.\
debug_hook.\
send("```EnvisionPlus - %s Successfully Deploy!!```" % version)
# ---------頁面---------
@app.route('/', methods=['GET', 'POST'])
def logindef():
# if post method
if request.method == "POST":
userName = request.form['userName']
session["userName"] = userName
# Check whether user is in database
found_user = Users.query.filter_by(userName=userName).first()
if found_user:
session["email"] = found_user.email
else:
usr = Users(userName, "")
db.session.add(usr)
db.session.commit()
return redirect(url_for("user"))
else:
# if user is in database
if "userName" in session:
flash('already login')
return redirect(url_for("user"))
return render_template('login.html')
# def home():
# return render_template('index.html')
@app.route('/index', methods=['GET', 'POST'])
def home():
if request.method == "POST":
try:
email = request.form['email']
session["email"] = email
except KeyError:
return redirect(url_for("home"))
found_user = Users.query.filter_by(email=email).first()
if found_user:
session["email"] = found_user.email
else:
usr = Users("NONE", email)
db.session.add(usr)
db.session.commit()
return redirect(url_for("home"))
else:
return render_template('home.html', version=version)
# login
@app.route('/login', methods=['GET', 'POST'])
def login():
# post
if request.method == "POST":
userName = request.form['userName']
session["userName"] = userName
# check whether user is in database
found_user = Users.query.filter_by(userName=userName).first()
if found_user:
session["email"] = found_user.email
else:
usr = Users(userName, "")
db.session.add(usr)
db.session.commit()
return redirect(url_for("user"))
else:
# if user login
if "userName" in session:
flash('already login')
return redirect(url_for("user"))
return render_template('login.html')
# logout
@app.route('/logout')
def logout():
# if user login
if "userName" in session:
session.pop("userName", None)
session.pop("email", None)
flash("You have been logged out", "info")
return redirect(url_for("login"))
@app.route('/trading_account')
def getBinanceFutureBalance():
account = client.futures_account()
data = {
'Total Margin Balance' :account["totalMarginBalance"],
'Total Wallet Balance' :account["totalWalletBalance"],
'Unrealized Profit' :account["totalUnrealizedProfit"],
'Available Balance' :account["availableBalance"]
}
return render_template("binancestat.html", data=data)
@app.route('/pairUpdate')
# update each share % by trigger activate
def shareUpdate():
pairs = TradingGroupData.query.all()
total_activate_trigger = 0.000
for pair in pairs:
if pair.trade_trigger:
total_activate_trigger += 1
else:
pair.trade_percent = None
if total_activate_trigger != 0:
trade_percent = (1/total_activate_trigger) - 0.002
for pair in pairs:
if pair.trade_trigger:
pair.trade_percent = trade_percent
db.session.commit()
@app.route('/tradeMode', methods=['POST'])
def tradeMode():
dataList = json.loads(request.data)
found_pair_dataframe = TradingGroupData.query.filter_by(pair=dataList['pair']).first()
if not found_pair_dataframe:
return {'Error': 'ERROR_PAIR_NOT_SUPPORT'}
if found_pair_dataframe.trade_trigger != dataList['tradeMode']:
found_pair_dataframe.trade_trigger = dataList['tradeMode']
discordWebhookConfig.debug_hook.send("%s trigger set to %d "
% (found_pair_dataframe.pair,
found_pair_dataframe.trade_trigger))
db.session.commit()
shareUpdate()
return 'TODO'
# ----------------------------------------WebHook---------------------------------------------
@app.route('/tradingviewWH', methods=['POST'])
# Listening port
def process_data():
try:
received_datas = json.loads(request.data)
except JSONDecodeError:
debug_hook.send("Port Received None JSON data %s" % request.data)
return {'Error': 'ERROR_INVALID_FORMAT'}
# verifing user password
if received_datas['Password'] != password:
debug_hook.send("Server port received unknown request")
return {'Error': 'ERROR_PASSWORD'}
err = update_database(received_datas, TimeFrameData, TradingGroupData, db, version, Stats, Trades)['Error']
if err != 'ERROR_OK':
return err
err = updatePosition(TimeFrameData, TradingGroupData, received_datas, db, Stats, version, Trades)
db.session.commit()
return err
if __name__ == '__main__':
init_db()
app.run()