def quant_stock(stock_number, stock_name, **kwargs): if not pre_sdt_check(stock_number, **kwargs): return real_time = kwargs.get('real_time', False) sdt = SDT.objects(Q(stock_number=stock_number) & Q(today_closing_price__ne=0.0) & Q(date__lte=kwargs['qr_date'])).order_by('-date')[:ema_volume] if float(sdt[0].increase_rate.replace('%', '')) > 9: return '' if real_time: sdt = setup_realtime_sdt(stock_number, sdt, kwargs) if not sdt: return trading_data = format_trading_data(sdt) df = calculate_macd(DataFrame(trading_data), kwargs['short_ema'], kwargs['long_ema'], kwargs['dif_ema']) today = df.iloc[-1] yestoday = df.iloc[-2] strategy_direction = 'long' if yestoday['dif'] < 0 < today['dif']: strategy_name = 'dif_day_%s_%s_%s_%s' % (strategy_direction, kwargs['short_ema'], kwargs['long_ema'], kwargs['dif_ema']) increase_rate = round((today['close_price'] - yestoday['close_price']) / yestoday['close_price'], 4) * 100 qr = QR( stock_number=stock_number, stock_name=stock_name, date=today.name, strategy_direction=strategy_direction, strategy_name=strategy_name, init_price=today['close_price'], industry_involved=kwargs.get('industry_involved'), increase_rate=increase_rate ) if real_time and strategy_direction == 'long': return qr if not real_time and not check_duplicate_strategy(qr): qr.save() return qr return ''
def quant_stock(stock_number, stock_name, **kwargs): week_long = kwargs.get('week_long', False) if not pre_sdt_check(stock_number, **kwargs): return real_time = kwargs.get('real_time', False) sdt = SDT.objects(Q(stock_number=stock_number) & Q(today_closing_price__ne=0.0) & Q(date__lte=kwargs['qr_date'])).order_by('-date')[:ema_volume] if float(sdt[0].increase_rate.replace('%', '')) > 9: return if real_time: sdt = setup_realtime_sdt(stock_number, sdt, kwargs) if not sdt: return trading_data = format_trading_data(sdt) df = calculate_macd(DataFrame(trading_data), kwargs['short_ema'], kwargs['long_ema'], kwargs['dif_ema']) df = calculate_ma(df, kwargs['short_ma'], kwargs['long_ma']) today = df.iloc[-1] yestoday = df.iloc[-2] if short_ma <= long_ma: strategy_direction = 'long' else: strategy_direction = 'short' strategy_name = 'ma_macd_%s_%s_%s' % (strategy_direction, short_ma, long_ma) if week_long: strategy_name = 'weeklong_' + strategy_name if today['diff_ma'] > 0 > yestoday['diff_ma']: increase_rate = round((today['close_price'] - yestoday['close_price']) / yestoday['close_price'], 4) * 100 qr = '' if strategy_direction == 'long': if today['macd'] > 0 > today['dif'] and today['dea'] < 0: qr = QR( stock_number=stock_number, stock_name=stock_name, date=today.name, strategy_direction=strategy_direction, strategy_name=strategy_name, init_price=today['close_price'], industry_involved=kwargs.get('industry_involved'), increase_rate=increase_rate ) elif strategy_direction == 'short': if today['macd'] < 0 < today['dif'] and today['dea'] > 0: qr = QR( stock_number=stock_number, stock_name=stock_name, date=today.name, strategy_direction=strategy_direction, strategy_name=strategy_name, init_price=today['close_price'], industry_involved=kwargs.get('industry_involved'), increase_rate=increase_rate ) if isinstance(qr, QR): if real_time: return qr if not check_duplicate_strategy(qr): qr.save() return qr return ''
def quant_stock(stock_number, stock_name, **kwargs): short_ma = kwargs['short_ma'] long_ma = kwargs['long_ma'] qr_date = kwargs['qr_date'] week_long = kwargs.get('week_long', False) if not pre_sdt_check(stock_number, **kwargs): return real_time = kwargs.get('real_time', False) if short_ma <= long_ma: strategy_direction = 'long' quant_count = long_ma + 5 else: strategy_direction = 'short' quant_count = short_ma + 5 strategy_name = 'ma_%s_%s_%s' % (strategy_direction, short_ma, long_ma) if week_long: strategy_name = 'weeklong_' + strategy_name sdt = SDT.objects( Q(stock_number=stock_number) & Q(today_closing_price__ne=0.0) & Q(date__lte=qr_date)).order_by('-date')[:quant_count] if len(sdt) < quant_count: # trading data not enough return if real_time: sdt = setup_realtime_sdt(stock_number, sdt, kwargs) if not sdt: return trading_data = format_trading_data(sdt) if not trading_data: return df = calculate_ma(DataFrame(trading_data), short_ma, long_ma) today = df.iloc[-1] yestoday = df.iloc[-2] if today['diff_ma'] > 0 > yestoday['diff_ma']: increase_rate = round( (today['close_price'] - yestoday['close_price']) / yestoday['close_price'], 4) * 100 qr = QR(stock_number=stock_number, stock_name=stock_name, date=today.name, strategy_direction=strategy_direction, strategy_name=strategy_name, init_price=today['close_price'], industry_involved=kwargs.get('industry_involved'), increase_rate=increase_rate) if real_time: return qr if not check_duplicate_strategy(qr): qr.save() return qr return ''
def quant_stock(stock_number, stock_name, **kwargs): if not pre_sdt_check(stock_number, **kwargs): return real_time = kwargs.get('real_time', False) week_long = kwargs.get('week_long', False) strategy_direction = 'long' strategy_name = 'break_through_%s_%s_%s' % (strategy_direction, kwargs['short_ma'], kwargs['long_ma']) if week_long: strategy_name = 'weeklong_' + strategy_name sdt = SDT.objects(Q(stock_number=stock_number) & Q(today_closing_price__ne=0.0) & Q(date__lte=kwargs['qr_date'])).order_by('-date')[:kwargs['long_ma']+10] if not sdt: return if not real_time: if sdt[0].today_closing_price <= sdt[0].today_average_price: return if real_time: sdt = setup_realtime_sdt(stock_number, sdt, kwargs) if not sdt: return if sdt[0].today_closing_price < sdt[0].today_opening_price: return trading_data = format_trading_data(sdt) df = calculate_ma(DataFrame(trading_data), short_ma, long_ma) today = df.iloc[-1] yestoday = df.iloc[-2] if yestoday['close_price'] < yestoday['long_ma'] and today['close_price'] > today['short_ma']\ and today['close_price'] > today['long_ma']: increase_rate = round((today['close_price'] - yestoday['close_price']) / yestoday['close_price'], 4) * 100 qr = QR( stock_number=stock_number, stock_name=stock_name, date=today.name, strategy_direction=strategy_direction, strategy_name=strategy_name, init_price=today['close_price'], industry_involved=kwargs.get('industry_involved'), increase_rate=increase_rate ) if real_time: return qr if not check_duplicate_strategy(qr): qr.save() return qr return ''
def quant_stock(stock_number, stock_name, **kwargs): length = kwargs['length'] qr_date = kwargs['qr_date'] real_time = kwargs.get('real_time', False) if not pre_sdt_check(stock_number, **kwargs): return strategy_name = 'new_peak_%s' % length strategy_direction = 'long' sdt = SDT.objects( Q(stock_number=stock_number) & Q(today_closing_price__ne=0.0) & Q(date__lte=qr_date)).order_by('-date')[:length] if len(sdt) < length: return if real_time: sdt = setup_realtime_sdt(stock_number, sdt, kwargs) trading_data = format_trading_data(sdt) if not trading_data: return df = DataFrame(trading_data) today_data = df.iloc[-1] if df['close_price'].max() <= today_data['close_price']: qr = QR(stock_number=stock_number, stock_name=stock_name, date=today_data.date, strategy_direction=strategy_direction, strategy_name=strategy_name, init_price=today_data['close_price'], industry_involved=kwargs.get('industry_involved'), increase_rate=float(sdt[0].increase_rate.replace('%', ''))) if real_time: return qr if not check_duplicate_strategy(qr): qr.save() return qr return