예제 #1
0
def do_minute_update():
    trackings = []
    #query observe list and do update:
    obs = models.StockItem()
    observings = obs.get_observe()
    obsList = []
    b = models.StockRealtime()
    if len(observings) > 0:
        for i in observings:
            c = i.get('code')
            obsList.append(c)
    #add new methods
        b.new_by_codes(obsList)
        b.save_all()
    #TODO: query tracking list and exe tradecommand;
    if len(b.stocks.values()) > 0:
        #get tradecommand and calculate trade result;
        trades = models.Trade()
        alltrade = trades.load_all()
        for b in alltrade:
            c = b.command.get('code')
            p = b.command.get('price')
            if p == b.stocks.get(c).get('trade'):
                act = models.Account()
                act.load(b.command.acc)
                cost = p * b.command.get('hands') * -100
                if act.balance > cost:
                    act.trans(cost)
                    act.update()
                    b.trading(b.command.get('hands'))
                    b.update()  #assume all completed
예제 #2
0
 def _get_trade(self):
     tra = gbce.Trade()
     tra.Price = 1003
     tra.Quantity = 1000
     setattr(tra, "Stock Symbol", "7UP")
     setattr(tra, "Buy/Sell", "Sell")
     return tra
예제 #3
0
    def test_add_unknown_trade_to_exchange(self):
        tra = gbce.Trade()
        tra.Quantity = 1000
        tra.Price = 203
        setattr(tra, "Stock Symbol", "ABC")
        setattr(tra, "Buy/Sell", "Buy")

        self.assertRaises(RuntimeError, self.me.add_trade, tra)
예제 #4
0
def ticker_handler(msg):
    ticker_data = models.TickerData(sequence=msg['sequence'],
                                    open_24h=msg['open_24h'],
                                    volume_24h=msg['volume_24h'],
                                    low_24h=msg['low_24h'],
                                    high_24h=msg['high_24h'],
                                    volume_30d=msg['volume_30d'],
                                    best_bid=msg['best_bid'],
                                    best_ask=msg['best_ask'])
    trade = models.Trade(trade_id=msg['trade_id'],
                         time=msg['time'],
                         product_id=msg['product_id'],
                         size=msg['last_size'],
                         price=msg['price'],
                         side=msg['side'],
                         ticker_data=ticker_data)
    trade.save()
예제 #5
0
    def test_requirement_2_a_iv(self):
        """For a given stock, calculate Volume Weighted Stock Price based on trades in past five minutes."""
        stk = gbce.Stock()

        satr = setattr  # shortcut
        satr(stk, "Stock Symbol", "GIN")
        satr(stk, "Type", "Preferred")
        satr(stk, "Fixed Dividend", 2)
        satr(stk, "Par Value", 100)
        self.Market.add_stock(stk)

        t = gbce.Trade()
        setattr(t, "Stock Symbol", "GIN")
        setattr(t, "Buy/Sell", 'Buy')
        setattr(t, "Quantity", 1000)
        setattr(t, "Price", 114)
        self.Market.add_trade(t)

        self.assertTrue(self.Market.get_stock("GIN").\
                        volume_weighted_stock_price(self.Market) > 0.0)
예제 #6
0
    def test_requirement_2_a_iii(self):
        """For a given stock, record a trade, with timestamp, quantity, buy or sell indicator and price."""
        stk = gbce.Stock()

        satr = setattr  # shortcut
        satr(stk, "Stock Symbol", "GIN")
        satr(stk, "Type", "Preferred")
        satr(stk, "Fixed Dividend", 2)
        satr(stk, "Par Value", 100)

        self.Market.add_stock(stk)

        t = gbce.Trade()
        setattr(t, "Stock Symbol", "GIN")
        setattr(t, "Buy/Sell", 'Buy')
        setattr(t, "Quantity", 1000)
        setattr(t, "Price", 114)
        self.Market.add_trade(t)  # adds Timestamp

        self.assertTrue(len(self.Market.trades) > 0)

        # verify timestamp added ok
        self.assertIsNotNone(self.Market.trades[0].Timestamp)
예제 #7
0
    def test_requirement_2_b(self):
        """Calculate the GBCE All Share Index using teh geometric mean of the
        Volume Weighted Stock Price for all stocks."""

        for stock in SAMPLE_DATA:
            stk = gbce.Stock()
            setattr(stk, "Stock Symbol", stock[0])
            setattr(stk, "Type", stock[1])
            setattr(stk, "Last Dividend", stock[2])
            setattr(stk, "Fixed Dividend", stock[3])
            setattr(stk, "Par Value", stock[4])
            self.Market.add_stock(stk)

        for trade_no in range(1000):
            t = gbce.Trade()
            setattr(t, "Stock Symbol",
                    choice(list(map(lambda x: x[0], SAMPLE_DATA))))
            setattr(t, "Buy/Sell", choice(['Buy', 'Sell']))
            setattr(t, "Quantity", choice(list(range(100, 200))))
            setattr(t, "Price", choice(list(range(400, 475))))
            self.Market.add_trade(t)

        self.assertTrue(self.Market.all_share_index() > 400 and \
                        self.Market.all_share_index() < 475)
예제 #8
0
 def test_add_blank_trade_to_exchange(self):
     t = gbce.Trade()
     self.assertRaises(RuntimeError, self.me.add_trade, t)