Ejemplo n.º 1
0
def week_period_strategy(code, time_freq, start_date, end_date):
    """
    周期选股(周四买,周一卖)
    :param code:
    :param time_freq:
    :param start_date:
    :param end_date:
    :return:
    """
    data = st.get_single_price(code, time_freq, start_date, end_date)
    # 新建周期字段
    data['weekday'] = data.index.weekday
    # 周四买入
    data['buy_signal'] = np.where((data['weekday'] == 3), 1, 0)
    # 周一卖出
    data['sell_signal'] = np.where((data['weekday'] == 0), -1, 0)

    data = compose_signal(data)  # 整合信号
    data = calculate_prof_pct(data)  # 计算收益
    data = calculate_cum_prof(data)  # 计算累计收益率
    # data = caculate_max_drawdown(data)  # 最大回撤
    return data
Ejemplo n.º 2
0
#!/usr/bin/env python
# encoding: utf-8
'''
@author: DeltaF
@software: pycharm
@file: stock.py
@time: 2021/3/5 02:08
@desc: 获取价格,并且计算涨跌幅
'''

import data.stock as st

# 获取平安银行的行情数据(日K)
data = st.get_single_price('000001.XSHE', 'daily', '2020-01-01', '2020-02-01')
# print(data)

# 计算涨跌幅,验证准确性
# data = st.calculate_change_pct(data)
# print(data)  # 多了一列close_pct

# 获取周K
data = st.transfer_price_freq(data, 'w')
print(data)

# 计算涨跌幅,验证准确性
data = st.calculate_change_pct(data)
print(data)  # 多了一列close_pct
Ejemplo n.º 3
0
    # print(data[['close', 'short_ma', 'long_ma', 'buy_signal', 'sell_signal','signal']])

    # 删除多余的columns
    data.drop(labels=['buy_signal', 'sell_signal'], axis=1)

    return data


if __name__ == '__main__':
    # 股票列表
    stocks = ['000001.XSHE', '000858.XSHE', '002594.XSHE']
    # 存放累计收益率
    cum_profits = pd.DataFrame()
    # 循环获取数据
    for code in stocks:
        df = st.get_single_price(code, 'daily', '2016-01-01', '2021-01-01')
        df = ma_strategy(df)  # 调用双均线策略
        cum_profits[code] = df['cum_profit'].reset_index(drop=True)  # 存储累计收益率
        # 折线图
        df['cum_profit'].plot(label=code)
        # 筛选有信号点
        # df = df[df['signal'] != 0]
        # 预览数据
        print("开仓次数:", int(len(df)))
        # print(df[['close', 'signal', 'pro   、fit_pct', 'cum_profit']])

    # 预览
    print(cum_profits)
    # 可视化
    # cum_profits.plot()
    plt.legend()
Ejemplo n.º 4
0
#!/usr/bin/env python
# encoding: utf-8
'''
@author: DeltaF
@software: pycharm
@file: stock.py
@time: 2021/3/3 00:29
@desc: 用于调用股票行情数据的脚本
'''

import data.stock as st
import pandas as pd

# 初始化变量
code = '000001.XSHG'

# 调用一只股票的行情数据
data = st.get_single_price(code=code,
                           time_freq='daily',
                           start_date='2021-02-01',
                           end_date='2021-03-01')
# 存入csv
st.export_data(data=data, filename=code, type='price')

# 从csv中获取数据
data = st.get_csv_data(code=code, type='price')
print(data)

# 实时更新数据:假设每天更新日K数据 > 存到csv文件里面 > data.to_csv(append)
Ejemplo n.º 5
0
    data['buy_signal'] = np.where((data['weekday'] == 3), 1, 0)
    # 周一卖出
    data['sell_signal'] = np.where((data['weekday'] == 0), -1, 0)

    data = compose_signal(data)  # 整合信号
    data = calculate_prof_pct(data)  # 计算收益
    data = calculate_cum_prof(data)  # 计算累计收益率
    # data = caculate_max_drawdown(data)  # 最大回撤
    return data


if __name__ == '__main__':
    # df = week_period_strategy('000001.XSHE', 'daily', None, datetime.date.today())
    # print(df[['close', 'signal', 'profit_pct', 'cum_profit']])
    # print(df.describe())
    # df['profit_pct'].plot()
    # plt.show()

    # 查看平安银行最大回撤
    df = st.get_single_price('000001.XSHE', 'daily', '2006-01-01',
                             '2021-01-01')
    df = caculate_max_drawdown(df)
    # print(df[['close', 'roll_max', 'daily_dd', 'max_dd']])
    df[['daily_dd', 'max_dd']].plot()
    plt.show()

    # 计算夏普比率
    # df = st.get_single_price('000001.XSHE', 'daily', '2006-01-01', '2021-01-01')
    # sharpe = calculate_sharpe(df)
    # print(sharpe)
Ejemplo n.º 6
0
@file: comp_sharpe_ratio.py
@time: 2021/3/16 22:20
@desc:
'''
import data.stock as st
import strategy.base as stb
import pandas as pd
import matplotlib.pyplot as plt

# 获取3只股票的数据:比亚迪、宁德时代、隆基
codes = ['002594.XSHE', '300750.XSHE', '601012.XSHG']

# 容器:存放夏普
sharpes = []
for code in codes:
    data = st.get_single_price(code, 'daily', '2018-10-01', '2021-01-01')
    print(data.head())

    # 计算每只股票的夏普比率
    daily_sharpe, annual_sharpe = stb.calculate_sharpe(data)
    sharpes.append([code, annual_sharpe])  # 存放 [[c1,s1],[c2,s2]..]
    print(sharpes)

# 可视化3只股票并比较
sharpes = pd.DataFrame(sharpes, columns=['code', 'sharpe']).set_index('code')
print(sharpes)

# 绘制bar图
sharpes.plot.bar(title='Compare Annual Sharpe Ratio')
plt.xticks(rotation=30)
plt.show()