-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLaunch.py
148 lines (111 loc) · 4.86 KB
/
Launch.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
# Raw Packages
from datetime import datetime, timedelta
import numpy as np
import pandas as pd
from pandas import DataFrame
# Gui Packages
import tkinter
from tkinter import *
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
# Data Source
import fxcmpy
# Token
TOKEN = "8ee215b13f266c6fc67cd1523a0d9d775cf0b062" # Change this
# Establishes connection to FXCM server
con = fxcmpy.fxcmpy(access_token= TOKEN, server='demo') # change to real
print ("Connection established")
# perimeters
DATA_POINTS = 10000 # Number of data points pulled from the server (0-10,000)
ping_speed = 600000 # ping every minute(miliseconds)
period_length = "m5" # Frquency of data points
history_length = int(period_length[1: len(period_length)])# Length of history pulled from the server (minutes)
gap = 90000 # Placeholder for rolling average (# of data points)
# Instantiates our objects
market_table = DataFrame()
data = con.get_candles('USD/CAD', period = period_length, number = DATA_POINTS)
GUI = tkinter.Tk()
GUI.title("USD/CAD")
GUI.geometry('1000x600')
frame = Frame(GUI, width = 1000, height = 600)
frame.place(x= 0, y = 0)
# Instantiates our data
length = 0 # Placeholder for the length of the data
mvavg = []
pr = []
upper_band = []
lower_band = []
ind = []
time = []
def fxcm_data_collect():
data = con.get_candles('USD/CAD', period = period_length, number = DATA_POINTS) # Collects data 1 * 150,0000 minutes back
length = len(data)
for i in range (0, length):
mean = data['askclose'][-gap: -2].mean()
mvavg.append(mean)
upper_band.append(mean + 2 * data['askclose'][-gap: -2].std())
lower_band.append(mean - 2 * data['askclose'][-gap: -2].std())
ind.append(i)
time_of_trade = datetime.now() - timedelta(hours=0, minutes= int(period_length[1: len(period_length)]) * (length - i))
time.append(time_of_trade)
def compile_plot():
table = {'Time' : time[-9998: -2],
'Closing Price' : data['askclose'][-9998: -2],
'Moving Average': mvavg[-9998: -2],
'Upper Band' : upper_band[-9998: -2],
'Lower Band' : lower_band[-9998: -2]}
return DataFrame(table, columns = ['Time', 'Moving Average', 'Closing Price', 'Upper Band', 'Lower Band'])
# Creates window with Upper and Lower Bounds
def load_gui(market_table):
for widget in frame.winfo_children(): # clears_data_on_window
widget.destroy()
# Latest closing price
closing_price = tkinter.Label(frame, text = "Closing price is " + "{:.5f}".format(data['askclose'][-2]), font = ("Times New Roman", 18)).pack()
# Latest moving average
moving_average = tkinter.Label(frame, fg = "blue", text = "Moving average is " + "{:.5f}".format(mvavg[-2]), font = ("Times New Roman", 18)).pack()
# Latest upper bound
upper_bound = tkinter.Label(frame, fg = "green", text = "Upper Band is " + "{:.5f}".format(upper_band[-2]), font = ("Times New Roman", 18)).pack()
# Latest lower bound
lower_bound = tkinter.Label(frame, fg = "green", text = "Lower Band is " + "{:.5f}".format(lower_band[-2]), font = ("Times New Roman", 18)).pack()
# Market graph
figure = plt.Figure(figsize=(5,4), dpi=100) # Size of plot
axis = figure.add_subplot(111) # position of graph
line2 = FigureCanvasTkAgg(figure, frame) # embedding graph in GUI
line2.get_tk_widget().pack(side=tkinter.LEFT, fill=tkinter.BOTH)
#market_graph = market_table[['Time', 'Moving Average', 'Closing Price', 'Upper Band', 'Lower Band']].groupby('Time').sum()
market_graph = market_table['Closing Price'][-10:-2].groupby(market_table['Time'][-10:-2]).sum()
market_graph.plot(kind='line', legend=True, ax=axis, color = 'k', fontsize=8, linewidth= 2.5)
market_graph = market_table['Moving Average'][-10:-2].groupby(market_table['Time'][-10:-2]).sum()
market_graph.plot(kind='line', legend=True, ax=axis, color='b', fontsize=8, linewidth= 2.5)
market_graph = market_table['Upper Band'][-10:-2].groupby(market_table['Time'][-10:-2]).sum()
market_graph.plot(kind='line', legend=True, ax=axis, color='g', fontsize=8, linewidth= 2.5)
market_graph = market_table['Lower Band'][-10:-2].groupby(market_table['Time'][-10:-2]).sum()
market_graph.plot(kind='line', legend=True, ax=axis, color='g', fontsize=8, linewidth= 2.5)
axis.set_title('USD/CAD')
frame.pack()
def closes_fxcm_connection(con):
con.close()
print ("Connection terminated")
def run_gui():
GUI.mainloop()
def clear_data():
data = None
length = 0 # Placeholder for the length of the data
mvavg = []
pr = []
upper_band = []
lower_band = []
ind = []
def ping():
clear_data()
print("fetching data...")
fxcm_data_collect()
market_table = compile_plot()
load_gui(market_table)
print("fetched data")
GUI.after(60000, ping)
def main():
ping()
run_gui()
closes_fxcm_connection(con)
main()