Esempio n. 1
0
 def get_tradeable_assets(self):
     url = 'https://api.tradier.com/v1/markets/lookup'
     params = {'types': 'stock,etf,index', 'linebreak': 'true'}
     r = s.get(url, params=params)
     content = json.loads(r.text)
     stocks = Stock.objects.all()
     stocks = [item.symbol for item in stocks]
     for stock in content["securities"]["security"]:
         if stock["symbol"] not in stocks:
             new_stock = Stock()
             new_stock.symbol = stock["symbol"]
             new_stock.company = stock["description"]
             if StockExchange.objects.filter(code=stock["exchange"]):
                 new_stock.exchange = StockExchange.objects.get(
                     code=stock["exchange"])
                 new_stock.bid = 0
                 new_stock.ask = 0
                 new_stock.last = 0
                 new_stock.price = 0
                 new_stock.save()
     stocks = Stock.objects.all()
     stocks = [item.symbol for item in stocks]
     return stocks
Esempio n. 2
0
    def stream_data(self, session_id, symbols):
        url = 'https://stream.tradier.com/v1/markets/events'
        params = {
            'sessionid': session_id,
            'linebreak': 'true',
            'filter': 'trade,timesale,summary',
            'symbols': symbols
        }
        #'filter':'trade',
        r = s.post(url, data=params, stream=True)
        #print(r.status_code)
        print("Opening stream...")
        for line in r.iter_lines():
            if line:  # filter out keep-alive new lines
                if "is not a valid symbol" in str(line):
                    line = str(line)
                    print(str(line))
                    symbol = symbol.replace("b'", "")
                    symbol = symbol.replace("'", "")
                    symbol = line.replace(" is not a valid symbol", "")
                    print(symbol)
                    bad_stock = Stock.objects.get(symbol=symbol)
                    bad_stock.delete()
                    print("Just deleted %s from database" % bad_stock)
                data = json.loads(line)
                try:
                    if Stock.objects.get(symbol=data["symbol"]):
                        stock = Stock.objects.get(symbol=data["symbol"])
                    else:
                        stock = Stock()
                        stock.symbol = data["symbol"]
                    if data["type"] == "trade":
                        if data["price"] != "" and data["price"] != 'NaN':
                            stock.price = data["price"]
                        else:
                            stock.price = 0
                        if data["cvol"] != "" and data["cvol"] != 'NaN':
                            stock.volume = data["cvol"]
                        else:
                            stock.volume = 0
                        if data["last"] != "" and data["last"] != 'NaN':
                            stock.last = data["last"]
                        else:
                            stock.last = 0
                    elif data["type"] == "timesale":
                        if data["bid"] != "" and data["bid"] != 'NaN':
                            stock.bid = data["bid"]
                        else:
                            stock.bid = 0
                        if data["ask"] != "" and data["ask"] != 'NaN':
                            stock.ask = data["ask"]
                        else:
                            stock.ask = 0
                    else:
                        if data["prevClose"] != "" and data[
                                "prevClose"] != 'NaN':
                            stock.close_price = data["prevClose"]
                        else:
                            stock.close_price = 0
                        if data["open"] != "" and data["open"] != 'NaN':
                            stock.open_price = data["open"]
                        else:
                            stock.open_price = 0
                        if data["high"] != "" and data["high"] != 'NaN':
                            stock.high = data["high"]
                        else:
                            stock.high = 0
                        if data["low"] != "" and data["low"] != 'NaN':
                            stock.low = data["low"]
                        else:
                            stock.low = 0
                    stock.save()

                except:
                    print(sys.exc_info())
                    print(data)