Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # trading model for use with the DAS Program artificial financial market environment
- # this code should be used with the Stock Exchange API service
- # written by Gregory Cave on the 25th of January 2017
- # last updated on the 15th of March 2017
- # developed in a Python 3 programming environment
- # ====================================================================================
- # import the main modules
- import json
- import math
- import os
- import pickle
- import random
- import sys
- import time
- import urllib
- # ====================================================================================
- # import the tkinter modules
- from tkinter import *
- from tkinter import ttk
- from tkinter.font import Font
- from tkinter.messagebox import *
- # ====================================================================================
- # append the filepath to import models
- sys.path.append('models')
- # import stock models
- import news
- import notification
- import order
- import stock
- import stock_history
- import transaction
- import user
- import user_stocks
- # ====================================================================================
- # class for main application
- class App(ttk.Frame):
- # class variable to track direction of column
- # header sort
- SortDir = True # descending
- # ================================================================================
- # init for app class
- # ================================================================================
- def __init__(self, isapp=True, name='model'):
- ttk.Frame.__init__(self, name=name) # app frame for init
- self.pack(expand=Y, fill=BOTH) # app screen properties (pack) for init
- self.master.title('DAS Program Trading Model') # app title for init
- self.master.wm_state('zoomed')
- #self.master.resizable(width=False, height=False) # uncomment for disable resizing
- self.width=self.master.winfo_screenwidth()
- self.height=self.master.winfo_screenheight()
- self.isapp = isapp # isapp for init
- self.__vars = {} # menu vars for init
- self.logged_in=False
- self._create_panel()
- # ================================================================================
- # load stock data function (with API)
- # ================================================================================
- def _load_stock_data(self):
- self.data = [
- ('DAAS', '74.74', '73.52', '75.92', '76.91', '68.22', '75.51', '187', '-1'),
- ('KAYT', '69.22', '67.33', '78.13', '77.29', '65.65', '73.25', '643', '-1'),
- ('EQNX', '63.57', '62.29', '65.44', '67.55', '58.92', '64.00', '266', '-1')]
- # configure column headings
- for c in self.dataCols:
- self.tree.heading(c, text=c.title(),
- command=lambda c=c: self._column_sort(c, App.SortDir))
- self.tree.column(c, width=Font().measure(c.title()))
- # add data to the tree
- for item in self.data:
- self.tree.insert('', 'end', values=item)
- # and adjust column widths if necessary
- for idx, val in enumerate(item):
- iwidth = Font().measure(val)
- if self.tree.column(self.dataCols[idx], 'width') < iwidth:
- self.tree.column(self.dataCols[idx], width = iwidth)
- # ================================================================================
- # load news data function (with API)
- # ================================================================================
- def _load_news_data(self):
- id=1
- user_id=1
- if user_id == 1:
- author='Gregory Cave'
- stock_id=()
- title='Debt Advice Specialist (DAS) Program for Investors'
- intro='DAS Program Remains Popular with the Shareholders of AMP'
- created_at='FEB 20 AT 21:36'
- body = ["The Debt Advice Specialist (DAAS) stock took a steep dive today ",
- "after certain individuals decided they would sell their $371.32 shares ",
- "for a mere $5! The result is shown in the graph below, ",
- "where the stock peaked at $371 ",
- "and dipped down to $5 where it stuck for a while. "]
- self.news = [id, author, stock_id, title, intro, body, created_at]
- # ================================================================================
- # load users data function (with API)
- # ================================================================================
- def _load_user_data(self):
- users=open(user_file, 'rb')
- self.data = []
- end_of_file=False
- while not end_of_file:
- try:
- # Unpickle the next object.
- user = pickle.load(users)
- user_id = user.get_id()
- first_name = user.get_first_name()
- last_name = user.get_last_name()
- cash=user.get_cash()
- account_value=user.get_account_value(user_id, cash, stocks_file, user_stocks_file)
- name= first_name + " " + last_name
- # Add the user to self.data
- self.data.append(['1st', name, account_value, cash])
- except EOFError:
- # Set the flag to indicate the end
- # of the file has been reached
- end_of_file = True
- # Close the file
- users.close()
- # configure column headings
- for c in self.dataCols:
- self.tree.heading(c, text=c.title(),
- command=lambda c=c: self._column_sort(c, App.SortDir))
- self.tree.column(c, width=Font().measure(c.title()))
- # add data to the tree
- for item in self.data:
- self.tree.insert('', 'end', values=item)
- # and adjust column widths if necessary
- for idx, val in enumerate(item):
- iwidth = Font().measure(val)
- if self.tree.column(self.dataCols[idx], 'width') < iwidth:
- self.tree.column(self.dataCols[idx], width = iwidth)
- # ================================================================================
- # column sort function for market data feed
- # ================================================================================
- def _column_sort(self, col, descending=False):
- # grab values to sort as a list of tuples (column value, column id)
- data = [(self.tree.set(child, col), child) for child in self.tree.get_children('')]
- # reorder data
- # tkinter looks after moving other items in
- # the same row
- data.sort(reverse=descending)
- for indx, item in enumerate(data):
- self.tree.move(item[1], '', indx) # item[1] = item Identifier
- # reverse sort direction for next sort operation
- App.SortDir = not descending
- # ================================================================================
- # configure text styles function (for user interface)
- # ================================================================================
- def _create_styles(self):
- # font styles
- family = 'Courier' # font family
- self.text.tag_configure('bold', font=(family, 12, 'bold', 'italic'))
- self.text.tag_configure('big', font=(family, 14, 'bold'))
- self.text.tag_configure('verybig', font=('Helvetica', 24, 'bold'))
- self.text.tag_configure('tiny', font=('Times', 8, 'bold'))
- self.text.tag_configure('underline', underline='on')
- self.text.tag_configure('overstrike', overstrike='on')
- self.text.tag_configure('super', offset='4p', font=(family, 10))
- self.text.tag_configure('sub', offset='-2p', font=(family, 10))
- # foreground/background styles
- self.text.tag_configure('bgstipple', background='black',
- borderwidth=0, bgstipple='gray12')
- self.text.tag_configure('fgstipple', foreground='blue', fgstipple='gray50')
- self.text.tag_configure('color1', background='#a0b7ce')
- self.text.tag_configure('color2', foreground='red')
- self.text.tag_configure('raised', relief=RAISED, borderwidth=1)
- self.text.tag_configure('sunken', relief=SUNKEN, borderwidth=1)
- # margins, spacing, and alignment
- self.text.tag_configure('right', justify='right')
- self.text.tag_configure('center', justify='center')
- self.text.tag_configure('margins', lmargin1='12m', lmargin2='6m',
- rmargin='10m')
- self.text.tag_configure('spacing', spacing1='10p', spacing2='2p',
- lmargin1='12m', lmargin2='6m', rmargin='10m')
- # ================================================================================
- # create panel function (for user interface)
- # ================================================================================
- def _create_panel(self):
- # create Panel
- self.panel = Frame(self, name='model')
- self.panel.pack(side=TOP, fill=BOTH, expand=Y)
- # create statusbar
- statusBar = ttk.Frame()
- self.__status = ttk.Label(self.master, text=' ', relief=SUNKEN, borderwidth=1,
- font=('Helv 10'), anchor=W, name='status')
- # pack statusbar
- self.__status.pack(side=LEFT, padx=2, expand=Y, fill=BOTH)
- statusBar.pack(side=BOTTOM, fill=X, pady=2)
- self._create_submenus()
- # create structure and windows
- self._create_window()
- self._stock_pane()
- self._home()
- # ================================================================================
- # create window structure function (for user interface)
- # ================================================================================
- def _create_window(self):
- self.outer = ttk.PanedWindow(self.panel, orient=HORIZONTAL, name='outer')
- self.outer.pack(expand=Y, fill=BOTH, padx=10, pady=(6,10))
- self.left = ttk.PanedWindow(self.outer, orient=VERTICAL, name='left')
- self.right = ttk.PanedWindow(self.outer, orient=VERTICAL, name='right')
- self.outer.add(self.left)
- self.outer.add(self.right)
- ltop = ttk.LabelFrame(self.left, text='Stocks', padding=3, name='ltop')
- self.left.add(ltop)
- rtop = ttk.LabelFrame(self.right, padding=3, name='rtop')
- self.right.add(rtop)
- # ================================================================================
- # create submenus function (for user interface)
- # ================================================================================
- def _create_submenus(self):
- # create the submenus
- # the routines are essentially the same:
- # 1. create the submenu, passing the main menu as parent
- # 2. add the submenu to the main menu as a 'cascade'
- # 3. add the submenu's individual items
- # try and except to destroy the menu if it already exists e.g. for user login
- try:
- self._menu.destroy()
- except:
- pass
- # create the main menu (only displays if child of the 'root' window)
- self.master.option_add('*tearOff', False) # disable all tearoff's
- self._menu = Menu(self.master, name='menu')
- self._create_home_menu()
- self._create_stocks_menu()
- self._create_news_menu()
- self._create_users_menu()
- self._create_support_menu()
- self.master.config(menu=self._menu)
- # set up standard bindings for the Menu class
- # (essentially to capture mouse enter/leave events)
- self._menu.bind_class('Menu', '<<MenuSelect>>', self._update_command)
- # ================================================================================
- # create home menu function (for user interface)
- # ================================================================================
- def _create_home_menu(self):
- self.home_menu = Menu(self._menu, name='home_menu')
- self._menu.add_cascade(label='Home', menu=self.home_menu, underline=0)
- # if the user is logged in, show the user's cash and name
- if self.logged_in == True:
- self.home_menu.add_command(label=self.cash, state='disabled')
- self.home_menu.add_command(label=self.name, state='disabled')
- self.home_menu.add_command(label='Home', underline=6,
- command=lambda: self._home())
- if self.logged_in == True:
- # if the user is an admin, show the admin panel
- if self.admin == True:
- self.home_menu.add_command(label='Admin', underline=6,
- command=lambda: self._admin())
- else:
- self.home_menu.add_command(label='Admin', underline=6, state='disabled',
- command=lambda: self._admin())
- self.home_menu.add_command(label='Profile', underline=6,
- command=lambda: self._profile())
- self.home_menu.add_command(label='Logout', underline=6,
- command=lambda: self._logout())
- else:
- self.home_menu.add_command(label='Register', underline=6,
- command=lambda: self._registration())
- self.home_menu.add_command(label='Login', underline=6,
- command=lambda: self._login())
- self.home_menu.add_separator()
- self.home_menu.add_command(label='Exit',
- command=lambda: self.master.destroy()) # kill toplevel window
- # ================================================================================
- # create stocks menu function (for user interface)
- # ================================================================================
- def _create_stocks_menu(self):
- self.stocks_menu = Menu(self._menu, name='stocks_menu')
- self._menu.add_cascade(menu=self.stocks_menu, label='Stocks', underline=0)
- #_____________________________________________________________________________
- def create_order_menu(self, cascades):
- # build Cascades->Radio Buttuns subment
- submenu = Menu(cascades)
- cascades.add_cascade(label='Create Order', underline=0,
- menu=submenu)
- self.__vars['size'] = StringVar()
- self.__vars['font'] = StringVar()
- for item in ('Buy Order', 'Sell Order'):
- submenu.add_radiobutton(label='{}'.format(item),
- variable=self.__vars['size'])
- create_order_menu(self, self.stocks_menu)
- self.stocks_menu.add_command(label='Stock Data', underline=6,
- command=lambda: self._stock_data())
- # ================================================================================
- # create news menu function (for user interface)
- # ================================================================================
- def _create_news_menu(self):
- self._menu.add_command(label='News', underline=0, command=lambda: self._news())
- # ================================================================================
- # create users menu function (for user interface)
- # ================================================================================
- def _create_users_menu(self):
- self._menu.add_command(label='Users', underline=0, command=lambda: self._users())
- # ================================================================================
- # create help & support menu function (for user interface)
- # ================================================================================
- def _create_support_menu(self):
- self._menu.add_command(label='Help & Support', underline=0, command=lambda: self._support())
- # ================================================================================
- # Bound and Command methods
- # ================================================================================
- def _print_command(self, e, txt):
- # triggered by multiple menu items that print letters or greetings
- # or by an accelerator keypress (CTRL+A, CTRL+B, etc).
- print(txt)
- #_________________________________________________________________________________
- def _update_command(self, evt):
- # triggered on mouse entry if a menu item has focus
- # (focus occurs when user clicks on a top level menu item)
- try:
- item = self.tk.eval('%s entrycget active -label' % evt.widget )
- self.__status.configure(background='gray90', foreground='black',
- text=item)
- except TclError:
- # no label available, ignore
- pass
- # ================================================================================
- # frame construction method(s)
- # ================================================================================
- def _create_rtop(self, current_tab):
- # create the top right position for use in the frame
- rtop = ttk.LabelFrame(self.right, text=current_tab, padding=3, name='rtop')
- return rtop
- #_________________________________________________________________________________
- def _create_frame(self, rtop):
- # try and except to destroy the frame if it exists
- # so that a new frame can be created
- try:
- self.frame.destroy()
- except:
- pass
- # create the new frame using the top right position variable (rtop)
- self.frame = ttk.Frame(rtop)
- # set frame resize priorities
- self.frame.rowconfigure(1, weight=1)
- self.frame.columnconfigure((0,1), weight=1, uniform=1)
- # pack the frame
- self.frame.pack(expand=Y, fill=BOTH)
- # ================================================================================
- # canvas construction method
- # ================================================================================
- def _create_canvas(self, current_tab):
- # create and add the submenus
- self._create_submenus()
- # create and add frame widget
- rtop = self._create_rtop(current_tab)
- self.right.add(rtop)
- self._create_frame(rtop)
- # ================================================================================
- # notebook construction method
- # ================================================================================
- def _create_notebook(self, name):
- self.nb = ttk.Notebook(self.frame, name=name)
- # extend bindings to top level window allowing
- # CTRL+TAB - cycles through tabs
- # SHIFT+CTRL+TAB - previous tab
- # ALT+K - select tab using mnemonic (K = underlined letter)
- self.nb.enable_traversal()
- self.nb.pack(fill=BOTH, expand=Y, padx=2, pady=3)
- # ================================================================================
- # label construction method
- # ================================================================================
- def _create_label(self, frame):
- # create and add label
- self.label = ttk.Label(frame, justify=LEFT, anchor=N)
- # position and set resize behaviour
- self.label.grid(row=0, column=0, columnspan=2, sticky='new', pady=5)
- # ================================================================================
- # text construction method
- # ================================================================================
- def _create_text(self):
- # create text styles
- self.text = Text(self.label, height=self.height, width=self.width, setgrid=True, wrap=WORD,
- undo=True, pady=2, padx=3)
- self._create_styles()
- # ================================================================================
- # seperator construction method
- # ================================================================================
- def _create_seperator(self):
- self.text.insert(END, '\n\n')
- for each in range (0, 110):
- self.text.insert(END, '_')
- self.text.insert(END, '\n\n')
- # ================================================================================
- # home section function
- # ================================================================================
- def _home(self):
- self._create_canvas('Home')
- # ================================================================================
- # stocks function (using data from self._load_data())
- # ================================================================================
- def _stock_data(self):
- self._create_canvas('Market Data')
- #_____________________________________________________________________________
- def create_header(self):
- self.text = Text(self.frame, height=6, width=self.width, setgrid=True, wrap=WORD,
- undo=True, pady=2, padx=3)
- self._create_styles()
- self.text.insert(END, "\nStocks", 'big')
- self.text.insert(END, "\n\nBrowse the stocks available that are currently trading. You must be logged in to trade stocks.", 'bold')
- # position in frame and set resize constraints
- self.text.grid(row=0, columnspan=2)
- # set text state to disabled
- self.text.config(state=DISABLED)
- #_____________________________________________________________________________
- def create_table(self):
- # create the tree
- self.dataCols = ('Stock', 'Price ($)', 'Bid ($)', 'Offer ($)', 'High ($)', 'Low ($)', 'Last Close ($)', 'Volume', 'Change (%)')
- self.tree = ttk.Treeview(columns=self.dataCols,
- show = 'headings')
- # add tree to frame
- self.tree.grid(in_=self.frame, row=1, column=0, columnspan=4, sticky="nsew")
- self._load_stock_data()
- #_____________________________________________________________________________
- create_header(self)
- create_table(self)
- # ================================================================================
- # account registration function
- # ================================================================================
- def _registration(self):
- self._create_canvas('Register')
- # ================================================================================
- # account login function
- # ================================================================================
- def _login(self):
- self._create_canvas('Login')
- #_____________________________________________________________________________
- def create_login():
- demoPanel = Frame(self.frame)
- demoPanel.pack(side=TOP, fill=X)
- self.flds = [] # entry fields
- lbls = ['Email Address:','Password:']
- for i in range(2):
- f = ttk.Frame()
- f.pack(in_=demoPanel, side=TOP, fill=X, padx=10)
- lbl = ttk.Label(text=lbls[i], width=15)
- e = ttk.Entry(width=20)
- self.flds.append(e) # save reference to field
- # on resize, expand entry but not label
- lbl.pack(in_=f, side=LEFT)
- e.pack(in_=f, side=LEFT, expand=Y, fill=X)
- self.flds[0].focus_set()
- #_____________________________________________________________________________
- def do_login():
- users=open(user_file, 'rb')
- self.data = []
- end_of_file=False
- while not end_of_file:
- try:
- # Unpickle the next object.
- user = pickle.load(users)
- first_name = user.get_first_name()
- last_name = user.get_last_name()
- if user.get_id() == 1:
- self.logged_in=True
- self.name= first_name + " " + last_name
- self.admin = user.get_admin()
- self.cash=user.get_cash()
- except EOFError:
- # Set the flag to indicate the end
- # of the file has been reached
- end_of_file = True
- # Close the file
- srchStr = StringVar()
- e = ttk.Entry(width=40, textvariable=srchStr)
- e.pack(in_=self.frame, side=LEFT)
- btn = ttk.Button(text='Search',
- command=lambda ss=srchStr: search_text(self, ss.get()))
- btn.pack(in_=self.frame, side=LEFT, padx=10, pady=5)
- # 'Return' triggers button routine
- e.bind('<Return>', lambda evt, btn=btn: btn.invoke())
- self.frame.grid(row=0, column=0, sticky="nsew")
- self._home()
- #_____________________________________________________________________________
- create_login()
- #do_login()
- # ================================================================================
- # profile section function
- # ================================================================================
- def _profile(self):
- self._create_canvas('Profile')
- # ================================================================================
- # orders function
- # ================================================================================
- def _orders(self):
- self._create_canvas('Create Order')
- # ================================================================================
- # news function
- # ================================================================================
- def _news(self):
- self._create_canvas('News')
- #_____________________________________________________________________________
- def search_text(self, srchString):
- # remove previous search results
- self.text.tag_remove('search', 0.0, END)
- # empty search string?
- if not srchString:
- return 'break' # don't propagate event
- cur = 1.0 # current position
- length = IntVar() # num of matched chars
- while True:
- cur = self.text.search(srchString,
- cur, END,
- count=length)
- if not cur:
- return 'break'
- # 'end' position of matched characters
- matchEnd = '{0}+{1}c'.format(cur, length.get())
- self.text.tag_add('search', cur, matchEnd)
- cur = self.text.index(matchEnd)
- #_____________________________________________________________________________
- def create_search_panel(self):
- self.label=ttk.Label(text='Search News: ',
- width=13, anchor=W).pack(in_=self.frame, side=LEFT)
- srchStr = StringVar()
- e = ttk.Entry(width=40, textvariable=srchStr)
- e.pack(in_=self.frame, side=LEFT)
- btn = ttk.Button(text='Search',
- command=lambda ss=srchStr: search_text(self, ss.get()))
- btn.pack(in_=self.frame, side=LEFT, padx=10, pady=5)
- # 'Return' triggers button routine
- e.bind('<Return>', lambda evt, btn=btn: btn.invoke())
- self.frame.grid(row=0, column=0, sticky="nsew")
- #_____________________________________________________________________________
- def create_text_panel(self):
- # create scrolled text widget
- rtop = self.nametowidget('model.outer.right.rtop')
- txtFrame = ttk.Frame(rtop)
- txtFrame.grid(row=1, column=0, sticky="nsew")
- self.text = Text(txtFrame, height=20, setgrid=True, wrap=WORD,
- undo=True, pady=2, padx=3)
- self._create_styles()
- xscroll = ttk.Scrollbar(txtFrame, command=self.text.xview,
- orient=HORIZONTAL)
- yscroll = ttk.Scrollbar(txtFrame, command=self.text.yview,
- orient=VERTICAL)
- self.text.configure(xscrollcommand=xscroll.set, yscrollcommand=yscroll.set)
- # configure 'search' style tag
- self.text.tag_configure('search', background='yellow')
- # position in frame and set resize constraints
- self.text.grid(row=0, columnspan=2)
- yscroll.grid(row=0, column=1, sticky=NSEW)
- txtFrame.rowconfigure(0, weight=1)
- txtFrame.columnconfigure(0, weight=1)
- # add text to scrolled text widget
- txt = ["The Debt Advice Specialist (DAAS) stock took a steep dive today ",
- "after certain individuals decided they would sell their $371.32 shares ",
- "for a mere $5! The result is shown in the graph below, ",
- "where the stock peaked at $371 ",
- "and dipped down to $5 where it stuck for a while. "]
- self.text.insert(END, ''.join(txt))
- #_____________________________________________________________________________
- create_search_panel(self)
- create_text_panel(self)
- # ================================================================================
- # users function
- # ================================================================================
- def _users(self):
- self._create_canvas('Users')
- #_____________________________________________________________________________
- def create_header(self):
- self.text = Text(self.frame, height=6, width=self.width, setgrid=True, wrap=WORD,
- undo=True, pady=2, padx=3)
- self._create_styles()
- self.text.insert(END, "\nUsers", 'big')
- self.text.insert(END, "\n\nValues shown here do NOT include stocks managed by the user.", 'bold')
- # position in frame and set resize constraints
- self.text.grid(row=0, columnspan=2)
- # set text state to disabled
- self.text.config(state=DISABLED)
- #_____________________________________________________________________________
- def create_table(self):
- # create the tree
- self.dataCols = ('Rank', 'User', 'Account Value ($)', 'Cash ($)')
- self.tree = ttk.Treeview(columns=self.dataCols,
- show = 'headings')
- # add tree to frame
- self.tree.grid(in_=self.frame, row=1, column=0, columnspan=4, sticky="nsew")
- self._load_user_data()
- #_____________________________________________________________________________
- create_header(self)
- create_table(self)
- # ================================================================================
- # help & support section function
- # ================================================================================
- def _support(self):
- self._create_canvas('Help & Support')
- # create and add notebook
- name='support'
- self._create_notebook(name)
- #_____________________________________________________________________________
- def create_support_getting_started(self):
- frame = ttk.Frame(self.nb, name='getting_started')
- frame.pack(side=TOP, fill=BOTH, expand=Y)
- # create and add label
- self._create_label(frame)
- # create and add text
- self._create_text()
- # Header
- self.text.insert(END, "\nGetting Started", 'big')
- self.text.insert(END, "\n\nWelcome to the help and support section.")
- self.text.insert(END, "\n\nThis is your resource for learning about how the stock exchange works, ")
- self.text.insert(END, "as well as stock trading in general.")
- # Seperator
- self._create_seperator()
- #Body
- self.text.insert(END, "How to Start Trading", 'big')
- self.text.insert(END, "\n\nYou can start by buying, and selling stocks.")
- self.text.insert(END, " You were given ")
- self.text.insert(END, "$20000", 'bold')
- self.text.insert(END, " when you first joined to help you get started. ")
- self.text.insert(END, "\n\nFollow the few steps below to make your first trade.")
- self.text.insert(END, "\n\n1.", 'bold')
- self.text.insert(END, " Take a look at the ")
- self.text.insert(END, "Debt Advice Specialist Program (DAAS)", 'bold')
- self.text.insert(END, " stock and click on the ")
- self.text.insert(END, "Buy" , 'bold')
- self.text.insert(END, " tab.")
- self.text.insert(END, "\n2.", 'bold')
- self.text.insert(END, " Choose the ")
- self.text.insert(END, "Market", 'bold')
- self.text.insert(END, " type, leave the Quantity as 1, and set the ")
- self.text.insert(END, "Expiry", 'bold')
- self.text.insert(END, " to ")
- self.text.insert(END, "End of Day.", 'bold')
- self.text.insert(END, "\n3.", 'bold')
- self.text.insert(END, " Press the ")
- self.text.insert(END, "Place Bid", 'bold')
- self.text.insert(END, " button.")
- self.text.insert(END, "\n\nThat's all!", 'bold')
- self.text.insert(END, " Within the next 5 minutes you'll ")
- self.text.insert(END, "own your very own stock.", 'bold')
- self.text.insert(END, "\n\nYou can then either sell the stock straight away (look out for those fees!),")
- self.text.insert(END, " or keep them until the value increases and make your fortune!")
- # Seperator
- self._create_seperator()
- self.text.insert(END, "What next?", 'big')
- self.text.insert(END, "\n\nHead on over to the ")
- self.text.insert(END, "Stocks", 'bold')
- self.text.insert(END, " page and browse the other stocks available to trade.")
- self.text.insert(END, "\n\nYou can also take a look at your ")
- self.text.insert(END, "Portfolio", 'bold')
- self.text.insert(END, " to see how your stocks are doing and look at previous orders you've placed.")
- # set text state to disabled
- self.text.config(state=DISABLED)
- xscroll = ttk.Scrollbar(frame, command=self.text.xview,
- orient=HORIZONTAL)
- yscroll = ttk.Scrollbar(frame, command=self.text.yview,
- orient=VERTICAL)
- self.text.configure(xscrollcommand=xscroll.set, yscrollcommand=yscroll.set)
- # position in frame and set resize constraints
- self.text.grid(row=0, column=0, sticky=NSEW)
- yscroll.grid(row=0, column=1, sticky=NSEW)
- frame.rowconfigure(0, weight=1)
- frame.columnconfigure(0, weight=1)
- # pack text into notebook
- self.text.pack()
- # add to notebook (underline = index for short-cut character)
- self.nb.add(frame, text='Getting Started', underline=0, padding=2)
- #_____________________________________________________________________________
- def create_support_stocks(self):
- frame = ttk.Frame(self.nb, name='stocks')
- frame.pack(side=TOP, fill=BOTH, expand=Y)
- # create and add label
- self._create_label(frame)
- # create and add text
- self._create_text()
- # Header
- self.text.insert(END, "\nStocks", 'big')
- self.text.insert(END, "\n\nStocks, are a virtual unit of investment in a company.")
- self.text.insert(END, "\n\nThis is your resource for learning about how the stock exchange works, as well as stock trading in general.")
- # Seperator
- self._create_seperator()
- # Body
- self.text.insert(END, "Buying and Selling Stocks", 'big')
- self.text.insert(END, "\n\nBuying and selling stocks is an investment.")
- self.text.insert(END, "\n\nOver time, stocks can gain or lose value depending on multiple factors.")
- self.text.insert(END, "\n\nThe value of a stock will also depend on whether a stocks price moves up or down and when you buy and sell.")
- self.text.insert(END, "\n\nYou should expect to make both gains and losses.")
- # Seperator
- self._create_seperator()
- #Footer
- self.text.insert(END, "Stock Types", 'big')
- self.text.insert(END, "\n\nThere are currently 3 different types of stock trading on the exchange:")
- self.text.insert(END, "\n\n • Company Stocks")
- self.text.insert(END, "\n\n • Community Stocks")
- self.text.insert(END, "\n\n • Server Stocks")
- # set text state to disabled
- self.text.config(state=DISABLED)
- # create scrolled text widget
- xscroll = ttk.Scrollbar(frame, command=self.text.xview,
- orient=HORIZONTAL)
- yscroll = ttk.Scrollbar(frame, command=self.text.yview,
- orient=VERTICAL)
- self.text.configure(xscrollcommand=xscroll.set, yscrollcommand=yscroll.set)
- # position in frame and set resize constraints
- self.text.grid(row=0, column=0, sticky=NSEW)
- yscroll.grid(row=0, column=1, sticky=NSEW)
- frame.rowconfigure(0, weight=1)
- frame.columnconfigure(0, weight=1)
- # pack text into notebook
- self.text.pack()
- # add to notebook (underline = index for short-cut character)
- self.nb.add(frame, text='Stocks', underline=0, padding=2)
- #_____________________________________________________________________________
- def create_support_buying_stocks(self):
- frame = ttk.Frame(self.nb, name='buying_stocks')
- frame.pack(side=TOP, fill=BOTH, expand=Y)
- # create and add label
- self._create_label(frame)
- # create and add text
- self._create_text()
- # Header
- self.text.insert(END, "\nBuying Stocks", 'big')
- self.text.insert(END, "\n\nBuying stocks allows you to own an investment in a company, community or server.")
- self.text.insert(END, "\n\nWhen buying stocks, you are not buying them directly from the exchange, but from another user.")
- self.text.insert(END, "\n\nTo buy stocks, another user must have those stocks for sale.")
- # Seperator
- self._create_seperator()
- # Body
- self.text.insert(END, "Buy Orders", 'big')
- self.text.insert(END, "\n\nTo buy a stock, you must place a buy order which you can do from the stock's page.")
- self.text.insert(END, "\n\nThere are two types of buy orders you can choose from.")
- self.text.insert(END, "\n\nOrder Types", 'big')
- self.text.insert(END, "\n\nA ")
- self.text.insert(END, "Market Buy Order", 'bold')
- self.text.insert(END, " is one that will be fulfilled at the 'current' market price.")
- self.text.insert(END, "\nThe 'current' price is the price of the stock at the time the order is processed, before any transaction fees.")
- self.text.insert(END, "\n\nA ")
- self.text.insert(END, "Limit Buy Order", 'bold')
- self.text.insert(END, " is one that will be fulfilled at a maximum of the limit you set when placing the order, before any fees.")
- #Footer
- self.text.insert(END, "\n\nOrder Expiry", 'big')
- self.text.insert(END, "\n\nYou can set your orders to expire either at End Of Day ")
- self.text.insert(END, "(EOD)", 'bold')
- self.text.insert(END, " or Good 'Til Cancelled ")
- self.text.insert(END, "(GTC).", 'bold')
- self.text.insert(END, "\n\nEOD orders will expire at the end of the trading day (23:59:59 UTC) if they are not fulfilled, whereas GTC orders will not expire.")
- # set text state to disabled
- self.text.config(state=DISABLED)
- # create scrolled text widget
- xscroll = ttk.Scrollbar(frame, command=self.text.xview,
- orient=HORIZONTAL)
- yscroll = ttk.Scrollbar(frame, command=self.text.yview,
- orient=VERTICAL)
- self.text.configure(xscrollcommand=xscroll.set, yscrollcommand=yscroll.set)
- # position in frame and set resize constraints
- self.text.grid(row=0, column=0, sticky=NSEW)
- yscroll.grid(row=0, column=1, sticky=NSEW)
- frame.rowconfigure(0, weight=1)
- frame.columnconfigure(0, weight=1)
- # pack text into notebook
- self.text.pack()
- # add to notebook (underline = index for short-cut character)
- self.nb.add(frame, text='Buying Stocks', underline=0, padding=2)
- #_____________________________________________________________________________
- def create_support_selling_stocks(self):
- frame = ttk.Frame(self.nb, name='selling_stocks')
- frame.pack(side=TOP, fill=BOTH, expand=Y)
- # create and add label
- self._create_label(frame)
- # create and add text
- self._create_text()
- # Header
- self.text.insert(END, "\nSelling Stocks", 'big')
- self.text.insert(END, "\n\nSelling stocks allows you to sell your investment in a company, community or server.")
- self.text.insert(END, "\n\nWhen selling stocks, you are not selling them directly to the exchange, but to another user.")
- self.text.insert(END, "\n\nTo sell stocks, another user must want to buy those stocks.")
- # Seperator
- self._create_seperator()
- # Body
- self.text.insert(END, "Sell Orders", 'big')
- self.text.insert(END, "\n\nTo sell a stock, you must place a sell order which you can do from the stock's page.")
- self.text.insert(END, "\n\nThere are two types of sell orders you can choose from.")
- self.text.insert(END, "\n\nOrder Types", 'big')
- self.text.insert(END, "\n\nA ")
- self.text.insert(END, "Market Sell Order", 'bold')
- self.text.insert(END, " is one that will be fulfilled at the 'current' market price.")
- self.text.insert(END, "\nThe 'current' price is the price of the stock at the time the order is processed, before any transaction fees.")
- self.text.insert(END, "\n\nA ")
- self.text.insert(END, "Limit Sell Order", 'bold')
- self.text.insert(END, " is one that will be fulfilled at a minimum of the limit you set when placing the order, before any fees.")
- #Footer
- self.text.insert(END, "\n\nOrder Expiry", 'big')
- self.text.insert(END, "\n\nYou can set your orders to expire either at End Of Day ")
- self.text.insert(END, "(EOD)", 'bold')
- self.text.insert(END, " or Good 'Til Cancelled ")
- self.text.insert(END, "(GTC).", 'bold')
- self.text.insert(END, "\n\nEOD orders will expire at the end of the trading day (23:59:59 UTC) if they are not fulfilled, whereas GTC orders will not expire.")
- # set text state to disabled
- self.text.config(state=DISABLED)
- # create scrolled text widget
- xscroll = ttk.Scrollbar(frame, command=self.text.xview,
- orient=HORIZONTAL)
- yscroll = ttk.Scrollbar(frame, command=self.text.yview,
- orient=VERTICAL)
- self.text.configure(xscrollcommand=xscroll.set, yscrollcommand=yscroll.set)
- # position in frame and set resize constraints
- self.text.grid(row=0, column=0, sticky=NSEW)
- yscroll.grid(row=0, column=1, sticky=NSEW)
- frame.rowconfigure(0, weight=1)
- frame.columnconfigure(0, weight=1)
- # pack text into notebook
- self.text.pack()
- # add to notebook (underline = index for short-cut character)
- self.nb.add(frame, text='Selling Stocks', underline=0, padding=2)
- #_____________________________________________________________________________
- def create_support_order_fees(self):
- frame = ttk.Frame(self.nb, name='order_fees')
- frame.pack(side=TOP, fill=BOTH, expand=Y)
- # create and add label
- self._create_label(frame)
- # create and add text
- self._create_text()
- # Header
- self.text.insert(END, "\nOrder Fees", 'big')
- self.text.insert(END, "\n\nJust like most stock exchanges, there are commission fees placed on orders we fulfill for you.")
- # Seperator
- self._create_seperator()
- # Body
- self.text.insert(END, "For ")
- self.text.insert(END, "Market Orders", 'bold')
- self.text.insert(END, ", there is a fee of $20.00.")
- self.text.insert(END, "\n\nFor ")
- self.text.insert(END, "Limit Orders", 'bold')
- self.text.insert(END, ", there is a fee of $30.00.")
- # set text state to disabled
- self.text.config(state=DISABLED)
- # create scrolled text widget
- xscroll = ttk.Scrollbar(frame, command=self.text.xview,
- orient=HORIZONTAL)
- yscroll = ttk.Scrollbar(frame, command=self.text.yview,
- orient=VERTICAL)
- self.text.configure(xscrollcommand=xscroll.set, yscrollcommand=yscroll.set)
- # position in frame and set resize constraints
- self.text.grid(row=0, column=0, sticky=NSEW)
- yscroll.grid(row=0, column=1, sticky=NSEW)
- frame.rowconfigure(0, weight=1)
- frame.columnconfigure(0, weight=1)
- # pack text into notebook
- self.text.pack()
- # add to notebook (underline = index for short-cut character)
- self.nb.add(frame, text='Order Fees', underline=0, padding=2)
- #_____________________________________________________________________________
- def create_support_order_matching(self):
- frame = ttk.Frame(self.nb, name='order_matching')
- frame.pack(side=TOP, fill=BOTH, expand=Y)
- # create and add label
- self._create_label(frame)
- # create and add text
- self._create_text()
- # Header
- self.text.insert(END, "\nOrder Matching", 'big')
- self.text.insert(END, "\n\nOrders are matched in a certain order, according to a set of rules.")
- self.text.insert(END, "\n\nThis exchange is using a Price-time Priority matching algorithm..")
- # Seperator
- self._create_seperator()
- # Body
- self.text.insert(END, "Orders are executed in an order determined by following the rules below. ")
- self.text.insert(END, "\n\n • Buy Orders", 'bold')
- self.text.insert(END, ": Market orders are executed first (from the oldest to the newest), and then limit orders (from the highest to the lowest).")
- self.text.insert(END, "\n\n • Sell Orders", 'bold')
- self.text.insert(END, ": Market orders are executed first (from the oldest to the newest), and then limit orders (from the lowest to the highest).")
- self.text.insert(END, "\n\nOrders placed at the same price are executed in time-priority order (from oldest to newest).")
- # set text state to disabled
- self.text.config(state=DISABLED)
- # create scrolled text widget
- xscroll = ttk.Scrollbar(frame, command=self.text.xview,
- orient=HORIZONTAL)
- yscroll = ttk.Scrollbar(frame, command=self.text.yview,
- orient=VERTICAL)
- self.text.configure(xscrollcommand=xscroll.set, yscrollcommand=yscroll.set)
- # position in frame and set resize constraints
- self.text.grid(row=0, column=0, sticky=NSEW)
- yscroll.grid(row=0, column=1, sticky=NSEW)
- frame.rowconfigure(0, weight=1)
- frame.columnconfigure(0, weight=1)
- # pack text into notebook
- self.text.pack()
- # add to notebook (underline = index for short-cut character)
- self.nb.add(frame, text='Order Matching', underline=0, padding=2)
- #_____________________________________________________________________________
- def create_support_dividends(self):
- frame = ttk.Frame(self.nb, name='dividends')
- frame.pack(side=TOP, fill=BOTH, expand=Y)
- # create and add label
- self._create_label(frame)
- # create and add text
- self._create_text()
- # Header
- self.text.insert(END, "\nDividends", 'big')
- self.text.insert(END, "\n\nDividends are paid out daily for certain stocks.")
- # Seperator
- self._create_seperator()
- # Body
- self.text.insert(END, "Each day, any stocks with a price of ")
- self.text.insert(END, "$30.00", 'bold')
- self.text.insert(END, " or more will pay out a ")
- self.text.insert(END, "1%", 'bold')
- self.text.insert(END, " dividend for each stock to shareholders in that stock.")
- self.text.insert(END, "\n\nFor example, if a certain stock is worth ")
- self.text.insert(END, "$50", 'bold')
- self.text.insert(END, " and you own ")
- self.text.insert(END, "200", 'bold')
- self.text.insert(END, " stocks, you will be paid out ")
- self.text.insert(END, "$100", 'bold')
- self.text.insert(END, " each day for owning that stock!")
- # set text state to disabled
- self.text.config(state=DISABLED)
- # create scrolled text widget
- xscroll = ttk.Scrollbar(frame, command=self.text.xview,
- orient=HORIZONTAL)
- yscroll = ttk.Scrollbar(frame, command=self.text.yview,
- orient=VERTICAL)
- self.text.configure(xscrollcommand=xscroll.set, yscrollcommand=yscroll.set)
- # position in frame and set resize constraints
- self.text.grid(row=0, column=0, sticky=NSEW)
- yscroll.grid(row=0, column=1, sticky=NSEW)
- frame.rowconfigure(0, weight=1)
- frame.columnconfigure(0, weight=1)
- # pack text into notebook
- self.text.pack()
- # add to notebook (underline = index for short-cut character)
- self.nb.add(frame, text='Dividends', underline=0, padding=2)
- #_____________________________________________________________________________
- def create_support_definitions(self):
- frame = ttk.Frame(self.nb, name='definitions')
- frame.pack(side=TOP, fill=BOTH, expand=Y)
- # create and add label
- self._create_label(frame)
- # create and add text
- self._create_text()
- # Header
- self.text.insert(END, "\nDefinitions", 'big')
- self.text.insert(END, "\n\nA quick rundown of the definitions used on the site and stock trading in general.")
- # Seperator
- self._create_seperator()
- # Body
- self.text.insert(END, "GTC", 'big')
- self.text.insert(END, "\nGood 'Til Cancelled - ", 'bold')
- self.text.insert(END, "This is where a buy or sell order will not expire at the end of the trading day.")
- self.text.insert(END, "\n\nEOD", 'big')
- self.text.insert(END, "\nEnd of Day - ", 'bold')
- self.text.insert(END, "This is where a buy or sell order will expire at the end of the trading day.")
- # set text state to disabled
- self.text.config(state=DISABLED)
- # create scrolled text widget
- xscroll = ttk.Scrollbar(frame, command=self.text.xview,
- orient=HORIZONTAL)
- yscroll = ttk.Scrollbar(frame, command=self.text.yview,
- orient=VERTICAL)
- self.text.configure(xscrollcommand=xscroll.set, yscrollcommand=yscroll.set)
- # position in frame and set resize constraints
- self.text.grid(row=0, column=0, sticky=NSEW)
- yscroll.grid(row=0, column=1, sticky=NSEW)
- frame.rowconfigure(0, weight=1)
- frame.columnconfigure(0, weight=1)
- # pack text into notebook
- self.text.pack()
- # add to notebook (underline = index for short-cut character)
- self.nb.add(frame, text='Definitions', underline=0, padding=2)
- #_____________________________________________________________________________
- def create_support_rules(self):
- frame = ttk.Frame(self.nb, name='rules')
- frame.pack(side=TOP, fill=BOTH, expand=Y)
- # create and add label
- self._create_label(frame)
- # create and add text
- self._create_text()
- # Header
- self.text.insert(END, "\nRules", 'big')
- self.text.insert(END, "\n\nThese rules apply to the exchange and are enforced automatically.")
- # Seperator
- self._create_seperator()
- # Body
- self.text.insert(END, "1. You cannot own more than ")
- self.text.insert(END, "100%", 'bold')
- self.text.insert(END, " of a stock at any one time.")
- self.text.insert(END, "\n\n2. You cannot place a limit buy or sell order more than ")
- self.text.insert(END, "10%", 'bold')
- self.text.insert(END, " away from the current market price of a stock.")
- self.text.insert(END, "\nFor example, if a stock price is ")
- self.text.insert(END, "$100", 'bold')
- self.text.insert(END, ", you cannot place a limit sell order for less than ")
- self.text.insert(END, "$90", 'bold')
- self.text.insert(END, ", or more than ")
- self.text.insert(END, "$110.", 'bold')
- self.text.insert(END, "\n\n3. Stocks with a value over ")
- self.text.insert(END, "$50", 'bold')
- self.text.insert(END, " that changes by ")
- self.text.insert(END, "10%", 'bold')
- self.text.insert(END, " or more on a single day will halt trading until the end of the day.")
- # set text state to disabled
- self.text.config(state=DISABLED)
- # create scrolled text widget
- xscroll = ttk.Scrollbar(frame, command=self.text.xview,
- orient=HORIZONTAL)
- yscroll = ttk.Scrollbar(frame, command=self.text.yview,
- orient=VERTICAL)
- self.text.configure(xscrollcommand=xscroll.set, yscrollcommand=yscroll.set)
- # position in frame and set resize constraints
- self.text.grid(row=0, column=0, sticky=NSEW)
- yscroll.grid(row=0, column=1, sticky=NSEW)
- frame.rowconfigure(0, weight=1)
- frame.columnconfigure(0, weight=1)
- # pack text into notebook
- self.text.pack()
- # add to notebook (underline = index for short-cut character)
- self.nb.add(frame, text='Rules', underline=0, padding=2)
- #_____________________________________________________________________________
- def create_support_faq(self):
- frame = ttk.Frame(self.nb, name='faq')
- frame.pack(side=TOP, fill=BOTH, expand=Y)
- # create and add label
- self._create_label(frame)
- # create and add text
- self._create_text()
- # Header
- self.text.insert(END, "\nFrequently Asked Questions", 'big')
- self.text.insert(END, "\n\nHere, you can find the answers to some of the most frequently asked questions.")
- # Seperator
- self._create_seperator()
- # Body
- self.text.insert(END, "Q: What is stock trading all about?", 'big')
- self.text.insert(END, "\n\nA: ", 'bold')
- self.text.insert(END, "Stock trading is all about return on investment.")
- self.text.insert(END, "\nWe start you off with some virtual cash to buy your initial stocks, but from there on out you'll need to manage your own portfolio.")
- self.text.insert(END, "\n\nQ. Can I use real money?", 'big')
- self.text.insert(END, "\n\nA: ", 'bold')
- self.text.insert(END, "No, only virtual money is used here. More money cannot be given or bought.")
- self.text.insert(END, "\n\nQ. Can I make my own stock?", 'big')
- self.text.insert(END, "\n\nA: ", 'bold')
- self.text.insert(END, "You can apply to have stock added. You must have at least $5,000.00 cash to apply.")
- self.text.insert(END, "\n\nQ. I've made my own stock, how do other people buy it?", 'big')
- self.text.insert(END, "\n\nA: ", 'bold')
- self.text.insert(END, "You must create a sell order to sell the stock that was given to you, so others can then buy it.")
- # set text state to disabled
- self.text.config(state=DISABLED)
- # create scrolled text widget
- xscroll = ttk.Scrollbar(frame, command=self.text.xview,
- orient=HORIZONTAL)
- yscroll = ttk.Scrollbar(frame, command=self.text.yview,
- orient=VERTICAL)
- self.text.configure(xscrollcommand=xscroll.set, yscrollcommand=yscroll.set)
- # position in frame and set resize constraints
- self.text.grid(row=0, column=0, sticky=NSEW)
- yscroll.grid(row=0, column=1, sticky=NSEW)
- frame.rowconfigure(0, weight=1)
- frame.columnconfigure(0, weight=1)
- # pack text into notebook
- self.text.pack()
- # add to notebook (underline = index for short-cut character)
- self.nb.add(frame, text='FAQ', underline=0, padding=2)
- #_____________________________________________________________________________
- create_support_getting_started(self)
- create_support_stocks(self)
- create_support_buying_stocks(self)
- create_support_selling_stocks(self)
- create_support_order_fees(self)
- create_support_order_matching(self)
- create_support_dividends(self)
- create_support_definitions(self)
- create_support_rules(self)
- create_support_faq(self)
- # ================================================================================
- # admin section function
- # ================================================================================
- def _admin(self):
- self._create_canvas('Admin')
- # create the notebook
- name='admin'
- self._create_notebook(name)
- def create_admin_dashboard(self):
- frame = ttk.Frame(self.nb, name='dashboard')
- frame.pack(side=TOP, fill=BOTH, expand=Y)
- msg = ["Ttk is the new Tk themed widget set. One of the widgets ",
- "it includes is the notebook widget, which provides a set ",
- "of tabs that allow the selection of a group of panels, ",
- "each with distinct content. They are a feature of many ",
- "modern user interfaces. Not only can the tabs be selected ",
- "with the mouse, but they can also be switched between ",
- "using Ctrl+Tab when the notebook page heading itself is ",
- "selected. Note that the second tab is disabled, and cannot "
- "be selected."]
- lbl = ttk.Label(frame, wraplength='7i', justify=LEFT, anchor=N,
- text=''.join(msg))
- # position and set resize behaviour
- lbl.grid(row=0, column=0, columnspan=2, sticky='new', pady=5)
- # add to notebook (underline = index for short-cut character)
- self.nb.add(frame, text='Dashboard', underline=0, padding=2)
- def create_admin_stocks(self):
- frame = ttk.Frame(self.nb, name='stocks')
- frame.pack(side=TOP, fill=BOTH, expand=Y)
- # add to notebook (underline = index for short-cut character)
- self.nb.add(frame, text='Stocks', underline=0, padding=2)
- def create_admin_buy_orders(self):
- frame = ttk.Frame(self.nb, name='buy_orders')
- frame.pack(side=TOP, fill=BOTH, expand=Y)
- # add to notebook (underline = index for short-cut character)
- self.nb.add(frame, text='Buy Orders', underline=0, padding=2)
- def create_admin_sell_orders(self):
- frame = ttk.Frame(self.nb, name='sell_orders')
- frame.pack(side=TOP, fill=BOTH, expand=Y)
- # add to notebook (underline = index for short-cut character)
- self.nb.add(frame, text='Sell Orders', underline=0, padding=2)
- def create_admin_news(self):
- frame = ttk.Frame(self.nb, name='news')
- frame.pack(side=TOP, fill=BOTH, expand=Y)
- # add to notebook (underline = index for short-cut character)
- self.nb.add(frame, text='News', underline=0, padding=2)
- def create_admin_users(self):
- frame = ttk.Frame(self.nb, name='users')
- frame.pack(side=TOP, fill=BOTH, expand=Y)
- # add to notebook (underline = index for short-cut character)
- self.nb.add(frame, text='Users', underline=0, padding=2)
- create_admin_dashboard(self)
- create_admin_stocks(self)
- create_admin_buy_orders(self)
- create_admin_sell_orders(self)
- create_admin_news(self)
- create_admin_users(self)
- # ================================================================================
- # fill stock pane function (with API)
- # ================================================================================
- def _stock_pane(self):
- # create and add stock labels
- stocks = ('AVLN',
- 'ASTR',
- 'BETA',
- 'DAAS',
- 'EQNX',
- 'KAYT',
- 'RFRD')
- ltop = self.nametowidget('model.outer.left.ltop')
- for each in stocks:
- # extract and format stock identifier
- identifier = each.split('/')[-1]
- identifier = [c if c != '_' else ' ' for c in identifier]
- # create two labels for each stock; one with the identifier
- # the other to hold the stock price
- lbl = ttk.Label(ltop, text=''.join(identifier), anchor=W)
- price = ttk.Label(ltop, textvariable=StringVar(), anchor=W)
- lbl.pack(fill=X)
- price.pack(fill=X)
- ttk.Separator(ltop).pack(fill=X)
- # set the current price for the indicated stock
- self._update_stock_price(price, each)
- # ================================================================================
- # update stock pane function (with API)
- # ================================================================================
- def _update_stock_price(self, price, identifier):
- # stock label helper function
- # sets the price immediately and updates it every 1000ms
- #
- # price - one of the 'stock' labels
- # stock - the associated stock identifier
- stocks=open(stocks_file, 'rb')
- stock_data = []
- end_of_file=False
- while not end_of_file:
- try:
- # Unpickle the next object.
- stock = pickle.load(stocks)
- # Store the next object
- stock_data.append([stock.get_identifier(), stock.get_price()])
- except EOFError:
- # Set the flag to indicate the end
- # of the file has been reached
- end_of_file = True
- # Close the file
- stocks.close()
- for each in stock_data:
- if each[0] == identifier:
- c=each[1]
- varname = price.cget('textvariable')
- price.setvar(varname, c)
- self.after(1000, self._update_stock_price, *(price, identifier))
- # ================================================================================
- # logout function
- # ================================================================================
- def _logout(self):
- self.logged_in=False
- self._home()
- # ===================================================================================
- # function for the main program
- if __name__ == '__main__':
- #_________________________________________________________________________________
- # global variable to store current directory
- cwd = os.getcwd()
- new_cwd = ""
- for each in cwd:
- if each == '\\':
- each = '/'
- new_cwd+=each
- cwd=new_cwd
- #_________________________________________________________________________________
- # global variables to store filepaths for static data
- stocks_file = (cwd + '/data/stocks.dat') # Stock Data
- transaction_file = (cwd + '/data/transactions.dat') # Transaction Data
- user_file = (cwd + '/data/users.dat') # User Data
- user_stocks_file = (cwd + '/data/user_stocks.dat') # User Stocks Data
- #_________________________________________________________________________________
- # global variables to store filepaths for data feeds
- news_file = (cwd + '/feeds/news.dat') # News Feeds
- notification_file = (cwd + '/feeds/notifications.dat') # Notification Feeds
- stock_history_file = (cwd + '/feeds/stock_history.dat') # Market Feeds
- #_________________________________________________________________________________
- # global variables to store filepaths for orders
- buy_orders_file = (cwd + '/orders/buy_orders.dat') # Buy Orders
- sell_orders_file = (cwd + '/orders/sell_orders.dat') # Sell Orders
- #_________________________________________________________________________________
- # call the main program
- App().mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement