def test_add_ais(self):
     market = Market()
     self.assertEqual(len(market.ais), 0)
     ais = [AI(market.basic), AI(market.basic)]
     market.add_ais(ais)
     self.assertEqual(len(market.ais), 2)
     ai = AI(market.basic)
     market.add_ais(ai)
     self.assertEqual(len(market.ais), 3)
     self.assertIn(ai, market.ais)
Exemplo n.º 2
0
class StockSimApp(App):
    def __init__(self):
        super(StockSimApp, self).__init__()
        self.market = Market()
        self.ais = generate_ai()
        self.market.add_ais(self.ais)
        for each in self.market.ais:
            each.market = copy(self.market.basic)
        self.user = User(self.market)
        self.current_company = "AAPL"
        self.current_price = str(float(self.market.basic["AAPL"][-1]))
        self.generate_graph(self.current_company)

    def start(self):
        Clock.schedule_interval(lambda dt: self.main_loop(self.market), 2)

    def main_loop(self, market):
        for each in market.ais:
            choice = each.choice(market.basic)
            if len(choice.keys()) > 0:
                for key in market.active_offers.keys():
                    market.active_offers[key].append(choice[key])
        market.calculate()
        self.generate_graph(self.current_company)
        print(self.market.active_offers)

    def generate_graph(self, company):
        plt.style.use('dark_background')
        plt.plot(self.market.basic[company],
                 color='#A967D5',
                 marker='o',
                 linewidth=2,
                 markersize=5)
        plt.savefig('graph.jpg')
        plt.clf()

    def build(self):
        Config.set('graphics', 'fullscreen', 'auto')
        Config.write()
        screenmanager = ScreenManager()
        screenmanager.add_widget(MainScreen())
        screenmanager.add_widget(ProfileScreen())
        self.start()
        return screenmanager
 def test_calculate(self):
     market = Market()
     market.add_ais([AI(market.basic), AI(market.basic), AI(market.basic)])
     for each in market.ais:
         choice = each.choice(market.basic)
         if len(choice.keys()) > 0:
             for key in market.active_offers.keys():
                 market.active_offers[key].append(choice[key])
     market.calculate()
     for each in market.ais:
         self.assertNotEqual(each.money, 8000)
         self.assertNotEqual(
             each.stocks, {
                 'AAPL': 0,
                 'AMD': 0,
                 'AMZN': 0,
                 "INTC": 0,
                 "MSFT": 0,
                 "CSCO": 0,
                 "GPRO": 0,
                 "NVDA": 0
             })