Exemple #1
0
 def post(self):
     data = request.data
     stock_data = json.loads(data)
     if isinstance(stock_data, (unicode, str,)):
         stock_data = eval(stock_data)
     new_stock = Stock(**stock_data)
     return new_stock, 201
Exemple #2
0
def stocks_update(arr: list):
    arr = filter(lambda x: x['type'] in stock_types.enums, arr)
    g = [(r['symbol'], r['description'], r['type'], r['exchange'],
          r['lotsize'], r['rating'], r['volatility'], r.get('last_price'))
         for r in arr]
    for i in g:
        session.merge(Stock(*i))
    session.commit()
Exemple #3
0
async def add_stocks(request: StockTickerRequest,
                     backgroud_tasks: BackgroundTasks,
                     db: Session = Depends(ensure_db_connection)):
    """
    Add new stock(s) to saved watchlist
    """
    for ticker in request.tickers:
        new_stock = Stock()
        new_stock.symbol = ticker
        db.add(new_stock)
        db.commit()
        backgroud_tasks.add_task(fetch_stock_data, new_stock.id)

    return {
        "data": request.tickers,
        "message": "New stocks added to watchlist",
        "status": 200,
    }
Exemple #4
0
 def chart_history_lines(self, id):
     if Stock.get(id=id):
         return_dict = {'GroupedData':{}}
         stock = Stock[id]
         query = select((hl.date.year, hl.date.month, avg(hl.close)) for s in Stock for hl in s.history_lines if
                s.id == stock.id and hl.date.year == datetime.date.today().year)
         data = query.order_by(1, 2)[:]
         changed_data = []
         for d in data:
             changed_data.append({'label': month_replacer.get(d[1]), 'value': round(d[2], 2)})
         return_dict['GroupedData'] = changed_data
         return_dict['maxYaxis'] = max(changed_data, key=lambda x: x['value'])
         return return_dict
     abort(404)
Exemple #5
0
 def populate_lines(self, id):
     date_from = request.args.get('date_from', '1970-01-01')[:10]
     date_to = request.args.get('date_to', str(datetime.date.today()))[:10]
     if Stock.get(id=id):
         stock = Stock[id]
         history_dict = ystockquote.get_historical_prices(stock.code, date_from, date_to)
         for date, values in history_dict.items():
             new_vals = {
                 'date': date, 'volume': values['Volume'], 'high': values['High'],
                 'low': values['Low'], 'open': values['Open'], 'close': values['Close']
             }
             if not StockHistory.get(stock=stock, date=date):
                 stock.history_lines.create(**new_vals)
             else:
                 StockHistory.get(stock=stock, date=date).set(**new_vals)
         return convert_stocks(to_dict(Stock[id]))
     abort(404)
Exemple #6
0
def buildStockModels(code, data, stockManager):
    stock = Stock(code)
    stock.set_kdata(data['df_3m'])
    stock.set_ktoday(data['df_realTime'])
    stock.set_macd(data['macd'])
    stock.set_kdj(data['kdj'])
    stock.set_ma(data['ma'])
    stock.set_turnover(data['turnover'])
    stock.set_volume(data['volume'])
    stock.set_bigMoney(data['bigMoney'])
    stock.set_concept(data['concept'])
    stockManager.addStock(stock)
Exemple #7
0
 def get(self):
     stock_select = Stock.select()
     stocks = to_dict(stock_select)
     # Cast date in string
     return convert_stocks(stocks)
Exemple #8
0
 def delete(self, id):
     stock = Stock.get(id=id)
     if stock:
         stock.delete()
         return '', 204
     abort(404)
Exemple #9
0
 def put(self, id):
     # data = parser.parse_args()
     data = json.loads(request.data)
     stock = Stock.get(id=id)
     stock.set(**data)
     return Stock[id], 201