class Simulator: k_linebreak = '--------------------------------' def __init__(self, market): # stocksource self.market = market self.date = None # Use QTimer to implement animation function # to get rid of time.sleep self.timer = QtCore.QTimer() self.timer.timeout.connect(self.Tick) def MainLoop(self): # ms_interval = 10 ms_interval = 1 self.timer.start(ms_interval) # Old implementation # for i in range(100): # self.Tick() # import time # time.sleep(1) def Initialize(self): self.SetupUI() for user in self.market.users: user.strategy.SetContext(self.market, user) def Tick(self): print Simulator.k_linebreak weekday = self.date.isoweekday() if weekday == 6 or weekday == 7: print 'Simulator:Skip weekends' else: # self.market.GenerateNewPrice() self.market.SetDate(self.date) self.market.UserAction() self.UpdateUI() import datetime delta = datetime.timedelta(1) self.date = self.date + delta if self.date > self.end_date: self.timer.stop() def SetupUI(self): user = self.market.users[0] self.userinfo_panel = GraphicsUserInfoPanel() # if this is not a class member, will be gc at the end of this function self.userinfo_panel.SetBasicInfo(user.name, user.money) self.userinfo_panel.show() def UpdateUI(self): # self.userinfo_panel.ClearStock() self.userinfo_panel.SetDate(self.date) user = self.market.users[0] self.userinfo_panel.SetBasicInfo(user.name, user.money) for stock_ticker in user.stocks.keys(): stock_amount = user.stocks[stock_ticker] self.userinfo_panel.UpdateStock(stock_ticker, stock_amount) def Simulate(self): if not self.start_date == None: self.date = self.start_date self.market.start_date = self.start_date else: print 'Simulator:Set start date first.' return self.Initialize() self.MainLoop() for user in self.market.users: user.PrintInfo() print 'End of simulation'
def SetupUI(self): user = self.market.users[0] self.userinfo_panel = GraphicsUserInfoPanel() # if this is not a class member, will be gc at the end of this function self.userinfo_panel.SetBasicInfo(user.name, user.money) self.userinfo_panel.show()
def SetupUI(self): user = self.market.users[0] self.userinfo_panel = GraphicsUserInfoPanel() # if this is not a class member, will be gc at the end of this function self.userinfo_panel.SetBasicInfo(user.name, user.money) self.userinfo_panel.show()
class Simulator: k_linebreak = '--------------------------------' def __init__(self, market): # stocksource self.market = market self.date = None # Use QTimer to implement animation function # to get rid of time.sleep self.timer = QtCore.QTimer() self.timer.timeout.connect(self.Tick) def MainLoop(self): # ms_interval = 10 ms_interval = 1 self.timer.start(ms_interval) # Old implementation # for i in range(100): # self.Tick() # import time # time.sleep(1) def Initialize(self): self.SetupUI() for user in self.market.users: user.strategy.SetContext(self.market, user) def Tick(self): print Simulator.k_linebreak weekday = self.date.isoweekday() if weekday == 6 or weekday == 7: print 'Simulator:Skip weekends' else: # self.market.GenerateNewPrice() self.market.SetDate(self.date) self.market.UserAction() self.UpdateUI() import datetime delta = datetime.timedelta(1) self.date = self.date + delta if self.date > self.end_date: self.timer.stop() def SetupUI(self): user = self.market.users[0] self.userinfo_panel = GraphicsUserInfoPanel() # if this is not a class member, will be gc at the end of this function self.userinfo_panel.SetBasicInfo(user.name, user.money) self.userinfo_panel.show() def UpdateUI(self): # self.userinfo_panel.ClearStock() self.userinfo_panel.SetDate(self.date) user = self.market.users[0] self.userinfo_panel.SetBasicInfo(user.name, user.money) for stock_ticker in user.stocks.keys(): stock_amount = user.stocks[stock_ticker] self.userinfo_panel.UpdateStock(stock_ticker, stock_amount) def Simulate(self): if not self.start_date == None: self.date = self.start_date self.market.start_date = self.start_date else: print 'Simulator:Set start date first.' return self.Initialize() self.MainLoop() for user in self.market.users: user.PrintInfo() print 'End of simulation'