-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.py
59 lines (45 loc) · 1.76 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
from flask_socketio import SocketIO, emit
from flask import Flask, render_template, url_for, copy_current_request_context
from random import random
from time import sleep
from threading import Thread, Event
from resources.TD.td import TD
from main import TraderPy
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
app.config['DEBUG'] = True
#turn the flask app into a socketio app
socketio = SocketIO(app, async_mode=None, logger=True, engineio_logger=True)
async_thread = Thread()
td_thread = Thread()
thread_stop_event = Event()
td = TD()
print('TD Object Created')
def getAccountData():
while not thread_stop_event.isSet():
number = td.getAccountValue()
number = "${:,.2f}". format(number)
socketio.emit('newnumber', {'number': number}, namespace='/test')
orders = td.getOrders()
orders=orders[0]
print(orders)
socketio.emit('neworder', {'order': orders["orderLegCollection"][0]["instrument"]["symbol"], 'orderType': "{}".format(orders["orderLegCollection"][0]["instruction"])}, namespace='/test')
socketio.sleep(5)
@app.route('/')
def index():
#only by sending this page first will the client be connected to the socketio instance
return render_template('index.html')
@socketio.on('connect', namespace='/test')
def test_connect():
# need visibility of the global thread object
global async_thread
print('Client connected')
if not async_thread.is_alive():
print("Starting Thread")
async_thread = socketio.start_background_task(getAccountData)
@socketio.on('disconnect', namespace='/test')
def test_disconnect():
print('Client disconnected')
if __name__ == '__main__':
td_thread = socketio.start_background_task(TraderPy)
socketio.run(app, use_reloader=False)