美文网首页
量化交易 - 简单实战

量化交易 - 简单实战

作者: 左心Chris | 来源:发表于2019-11-12 10:05 被阅读0次

    核心策略

    N日突破择时策略(海龟交易法则)

    超参数

    观察候选集
    买入候选集
    超参数: N1, N2, Rate,买入价,买入数量

    核心代码

    wait = {'stock_name' : {'N1': 10, 'N2' : 5, 'Rate' : 0.2}}
    order = {'stock_name' : {'value' : 256, 'quantity' : 10}}
    # 读取数据
    df_stockload = web.DataReader("600410.SS", "yahoo", datetime.datetime(2018,10,1), datetime.datetime(2019,4,1))
    print(df_stockload.describe())
    # 最近N1交易日最高
    df_stockload['N1_High'] = df_stockload.High.rolling(window=N1).max()#计算最近N1个交易日最高价
    print(df_stockload.info())
    # 目前出现过的最大值填充前N1个nan
    expan_max = df_stockload.Close.expanding().max()
    print(expan_max)
    # 同理最小值
    df_stockload['N2_Low'] = df_stockload.Low.rolling(window=N2).min()#计算最近N2个交易日最低价
    print(df_stockload.head())
    
    expan_min = df_stockload.Close.expanding().min()
    df_stockload['N2_Low'].fillna(value=expan_min,inplace=True)#目前出现过的最小值填充前N2个nan
    print(df_stockload.head())
    # 寻找时间序列
    """ 收盘价超过N1最高价 买入股票持有"""
    buy_index = stock_df[stock_df.Close > stock_df.N1_High.shift(1)].index
    print(buy_index)
    """
    DatetimeIndex(['2018-11-13', '2018-11-15', '2018-11-16', '2019-02-25',
                   '2019-03-05', '2019-03-06', '2019-03-07', '2019-03-08',
                   '2019-03-25'],
                  dtype='datetime64[ns]', name='Date', freq=None)
    """
    """ 收盘价超过N2最低价 卖出股票持有"""
    sell_index = stock_df[stock_df.Close < stock_df.N2_Low.shift(1)].index
    print(sell_index)
    """
    DatetimeIndex(['2018-10-10', '2018-10-11', '2018-10-12', '2018-10-15',
                   '2018-10-16', '2018-12-18', '2018-12-25', '2019-01-02',
                   '2019-01-29', '2019-01-31'],
                  dtype='datetime64[ns]', name='Date', freq=None)
    """
    # 将signal序列使用shift(1)
    df_stockload['signal'] = df_stockload.signal.shift(1)
    print(df_stockload.signal)
    # 填充空值
    df_stockload['signal'].fillna(value=0, inplace=True)
    print(df_stockload.signal)
    

    部署

    通过crontab和http://sc.ftqq.com/3.version来部署任务

    相关文章

      网友评论

          本文标题:量化交易 - 简单实战

          本文链接:https://www.haomeiwen.com/subject/ecdfictx.html