Esempio n. 1
0
import datetime

import talib as ta
import tushare as ts

import freedom.db.stock_trade_daily_dao as stock_trade_daily_dao
import freedom.db.stock_trade_calendar_dao as stock_trade_calendar_dao
import freedom.log.logger as my_logger

__logger = my_logger.getlogger('common')


def is_open_day(date):
    """
    查看指定日期是否是交易日
    :param date: 字符串日期,格式%Y-%m-%d
    :return: True | False
    """
    result = stock_trade_calendar_dao.select_one(date)
    if result and result['is_open'] == 1:
        return True

    return False


def get_the_day_before_yesterday(date):
    """
    获取指定日期前一天的日期
    :param date
    :return: date
    """
Esempio n. 2
0
import datetime

import pandas as pd
import talib as ta

import freedom.common as my_common
import freedom.db.stock_base_dao as stock_base_dao
import freedom.db.stock_trade_daily_dao as stock_trade_daily_dao
import freedom.db.stock_trade_calendar_dao as stock_trade_calendar_dao
import freedom.log.logger as my_logger
import freedom.model.common_indicator as my_common_indicator

__logger = my_logger.getlogger("ma_scatter")


def sma_upward(code, date, timeperiod):
    """
    计算指定日期指定周期均线是否开始拐头向上
    :param code:
    :param date:
    :param timeperiod:
    :return: True | False
    """
    count = timeperiod + 2
    stock_df = stock_trade_daily_dao.query_stock_trade_daily_limit_size(
        code, str(date), count)
    if len(stock_df) >= count:
        last_trade_date = stock_df[len(stock_df) - 1:]['trade_date'].values[0]
        if date.strftime('%Y-%m-%d') == str(last_trade_date):
            sma_list = ta.SMA(stock_df['close'], timeperiod)
            sma_1 = sma_list[len(sma_list) - 3]
Esempio n. 3
0
import talib as ta

import freedom.common as my_common
import freedom.db.stock_trade_daily_dao as stock_trade_daily_dao
import freedom.log.logger as my_logger

__logger = my_logger.getlogger('common_indicator')


def avg_volume_ratio(code, date, avg_day=5):
    """
    计算当日成交量是指定周期成交量均值的倍率
    :param code:
    :param date:
    :param avg_day:
    :return:
    """
    last_date = my_common.get_the_day_before_yesterday(date)
    df1 = stock_trade_daily_dao.query_stock_trade_daily_limit_size(
        code, str(last_date), avg_day)
    if len(df1) >= avg_day:
        sma = ta.SMA(df1['volume'][len(df1) - avg_day:], avg_day)
        v_avg = sma.values[len(sma) - 1]
        # 当日成交量
        df2 = stock_trade_daily_dao.query_stock_trade_daily(
            code, str(date), str(date))
        if not df2.empty:
            v_current = df2[0:1]['volume'].values[0]
            return v_current / v_avg
        else:
            __logger.warn("获取当前成交量信息为空, code = {}, date = {}".format(
Esempio n. 4
0
import datetime

import pandas as pd
import talib as ta

import freedom.db.stock_base_dao as stock_base_dao
import freedom.db.stock_trade_daily_dao as stock_trade_daily_dao
import freedom.log.logger as my_logger
import freedom.common as my_common
import freedom.model.common_indicator as my_common_indicator

__logger = my_logger.getlogger('candle_stick')


def white_line_over_sma5_10_20(code, date):
    """
    筛选上一个交易日的收盘价和当天收盘价由下到上穿越5、10、20日均线
    :param df:
    :return:
    """
    df = stock_trade_daily_dao.query_stock_trade_daily_limit_size(code, str(date), 20)
    if len(df) < 20:
        # __logger.warn("Valid DataFrame data size less than 20, code = {}, date = {}".format(code, str(date)))
        return False

    # 当前收盘价
    current_close = df[len(df) - 1:]['close'].values[0]
    # 上一个交易日收盘价
    yesterday_close = df[len(df) - 2:len(df) - 1]['close'].values[0]
    if current_close > yesterday_close:
        # 5日均线
Esempio n. 5
0
import pandas as pd

import freedom.db.dbutil as db_util
import freedom.log.logger as my_logger

logger = my_logger.getlogger('stock_trade_daily_dao')


def query_stock_trade_daily(code, start_date, end_date):
    """
    根据code、起止日期(包含)查询数据,并按照交易日期升序排序
    :param code:
    :param start_date:
    :param end_date:
    :return: DafaFrame
        `code`, `open`, `high`, `low`, `close`, `volume`, `amount`, `turn_over_ratio`, `increase`, `amplitude`, `trade_date`
    """
    sql = 'select `code`, `open`, `high`, `low`, `close`, `volume`, `amount`, `turn_over_ratio`, `increase`, `amplitude`, `trade_date` from stock_trade_daily where `code` = "{code}" and `trade_date` BETWEEN "{start_date}" and "{end_date}" order by `trade_date`'.format(
        code=code, start_date=start_date, end_date=end_date)

    try:
        return pd.read_sql(sql, con=db_util.get_connection())
    except:
        logger.exception("query_stock_trade_daily error")

    return None


def query_stock_trade_daily_limit_size(code, end_date, size):
    """
    查询指定交易日期(包含)前指定数量的数据
Esempio n. 6
0
"""
回测连续涨停的股票
"""

import pandas as pd

import freedom.db.stock_base_dao as stock_base_dao
import freedom.db.stock_trade_daily_dao as stock_trade_daily_dao
import freedom.log.logger as my_logger

__logger = my_logger.getlogger('continuous_increase')


def _t1():
    columns = ["code", "status", "trade_date"]
    three_day_list = []
    four_day_list = []
    # 涨停
    increase_upper = 9.8
    start_date = '2017-01-01'
    end_date = '2018-10-01'
    stock_base_list = stock_base_dao.query_stock_base()
    # stock_base_list = stock_base_list[0:100]
    for stock_base in stock_base_list:
        df = stock_trade_daily_dao.query_stock_trade_daily(
            stock_base['code'], start_date, end_date)
        start_index = 0
        end_index = start_index + 1
        length = len(df)
        while end_index <= length:
            increase = df[start_index:end_index]['increase'].values[0]
Esempio n. 7
0
import datetime

import pandas as pd

import freedom.common as my_common
import freedom.db.stock_base_dao as stock_base_dao
import freedom.db.stock_trade_daily_dao as stock_trade_daily_dao
import freedom.log.logger as my_logger
import freedom.model.common_indicator as my_common_indicator

__logger = my_logger.getlogger('high_open')


def is_jump_high(code, date):
    """
    是否是跳空高开的数据
    :param code: 代码
    :return: True | False
    """
    some_gap_date = date - datetime.timedelta(days=1)
    stock_trade_df = stock_trade_daily_dao.query_stock_trade_daily(
        code, str(some_gap_date), str(date))
    if len(stock_trade_df) >= 2:
        last = stock_trade_df[len(stock_trade_df) - 1:]
        last2 = stock_trade_df[len(stock_trade_df) - 2:len(stock_trade_df) - 1]

        last_low = last['low'].values[0]
        last2_high = last2['high'].values[0]
        if last_low - last2_high > 0:
            return True
Esempio n. 8
0
import freedom.db.dbutil as db_util
import freedom.log.logger as my_logger

__logger = my_logger.getlogger('stock_trade_calendar_dao')


def select_one(date):
    """
    根据日期查询是否是交易日信息
    :param date:
    :return:
    """
    result = {}
    sql = 'select `date`, is_open from stock_trade_calendar where `date` = "{}"'.format(
        date)
    conn = db_util.get_connection()
    try:
        cur = conn.cursor()
        cur.execute(sql)
        res = cur.fetchone()
        cur.close()

        if len(res) > 0:
            result['date'] = str(res[0])
            result['is_open'] = res[1]
    except:
        __logger.exception("Query stock_trade_calendar error")

    return result
Esempio n. 9
0
import pandas as pd

import freedom.db.dbutil as db_util
import freedom.log.logger as my_logger

__logger = my_logger.getlogger('stock_base_dao')


def update_stock_status(code, status):
    """
    修改stock交易状态
    :param code:
    :param status: 1: 正常; 2: 停牌
    :return:
    """
    sql = 'update stock_base set status = {} where code = "{}" and type = 1'.format(
        status, code)

    conn = db_util.get_connection()
    try:
        cur = conn.cursor()
        cur.execute(sql)
        conn.commit()
        cur.close()
    except:
        conn.rollback()
        __logger.exception("Update stock_status error")


def query_stock_base():
    """
Esempio n. 10
0
import datetime

import tushare as ts
import pandas as pd

import freedom.common as my_common
import freedom.db.stock_base_dao as stock_base_dao
import freedom.db.stock_trade_daily_dao as stock_trade_daily_dao
import freedom.log.logger as my_logger

__logger = my_logger.getlogger('gather_data')


def check_stock_status(trade_date):
    """
    利用当日成交数据校验系统中的数据停牌、交易状态是否正确
    :return:
    """
    if my_common.is_open_day(str(trade_date)):
        day_all_df = ts.get_day_all(str(trade_date))
        if not day_all_df.empty:
            stock_series = stock_base_dao.query_stock_base_to_map()
            for index, row in day_all_df.iterrows():
                code = my_common.convert_standard_code(row['code'])
                stock_local = stock_series.get(code)
                # 本系统中无此stock数据
                if not stock_local:
                    __logger.warning(
                        "Stock is not exists in local system, code = {}".
                        format(code))
                    continue