【量化】4天學會python機器學習與量化交易-筆記1(p6~p10)

視頻:4天學會python機器學習與量化交易
平臺:米筐
4天學會python機器學習與量化交易,肯定是不可能的,最多入個門。
學習原因:
1,講在線策略,免去本地搭建環境,下載數據等瑣事。
2,主要講A股交易,和有的量化資料不一樣(講期權),有實用價值。
3,講的比較簡單,適合入門。

2020.2.12 開始學習,之前沒學過,看看要多久看完並學會,立個flag。

p6 獲取板塊、交易行情數據

RQData API文檔:這裏
獲取板塊、交易行情數據
1,獲取股票

def init(context):
    context.s1 = '000001.XSHE'
    
    #獲取計算機通信行業股票
    context.stock_list = industry('C39')

    #獲取能源板塊股票
    context.sector_list = sector('Energy')

    #獲取指數成分股股票,如滬深300指數股票  (相當於獲取股票池)
    context.index_list = index_components('000300.XSHG')

2,獲取價格
2.1 收盤價格 history_bars
這裏指獲取獲取該股票某一天前面5天的收盤價格

def handle_bar(context,bar_dict):
    close = history_bars(context.s1, 5, '1d', 'close')
    logger.info(close)

結果:

2016-09-28  INFO  [9.16 9.15 9.04 9.06 9.05]
2016-09-29  INFO  [9.15 9.04 9.06 9.05 9.06]
2016-09-30  INFO  [9.04 9.06 9.05 9.06 9.07]
2016-09-30  WARN  訂單被拒單: [600397.XSHG] 已漲停。
2016-09-30  WARN  訂單創建失敗: 下單量爲0
2016-09-30  WARN  訂單創建失敗: 下單量爲0

其中,[9.16 9.15 9.04 9.06 9.05],9.05(最右邊的)爲9.28號的價格。close的類型爲<class 'numpy.ndarray'>

2.2 獲取多個指標
變成2維數據

def handle_bar(context,bar_dict):
    #獲取前面5天收盤價
    close = history_bars(context.s1, 5, '1d', 'close')
    # 獲取多個指標
    history_1 = history_bars(context.s1, 5, '1d', ['close', 'open'])
    print(history_1)

結果:

2016-09-28  INFO  [(9.16, 9.1 ) (9.15, 9.16) (9.04, 9.13) (9.06, 9.04) (9.05, 9.06)]
2016-09-29  INFO  [(9.15, 9.16) (9.04, 9.13) (9.06, 9.04) (9.05, 9.06) (9.06, 9.05)]
2016-09-30  INFO  [(9.04, 9.13) (9.06, 9.04) (9.05, 9.06) (9.06, 9.05) (9.07, 9.06)]
2016-09-30  WARN  訂單創建失敗: 下單量爲0
2016-09-30  WARN  訂單創建失敗: 下單量爲0

2.3 獲取分鐘數據

    # 獲取1分鐘
    close_1min = history_bars(context.s1, 5, '1m', 'close')

要將右邊時間選項修改爲“分鐘”。

p7 獲取財務數據與定時器

獲取財務數據與定時器

bar_dict也可以獲取行情數據,但只能獲取當前日期的。

get_fundamentals()獲取財務數據,基本面,用來選股。在before_trading或handle_bar中調用,不能在init調用。回測時不用。

1,獲取市盈率PE - 簡單查詢

def before_trading(context):
    # 獲取財務數據,默認獲取所有A股的股票財務數據
    # 創建查詢語句
    q = query(fundamentals.eod_derivative_indicator.pe_ratio)
    # 回測不需要傳日期,默認當天的數據
    fund = get_fundamentals(q)

    logger.info(fund.T)
    pass

2,過濾查詢

def handle_bar(context,bar_dict):
    # 獲取財務數據,默認獲取所有A股的股票財務數據
    # 創建查詢語句
    # 增加filter
    q = query(
        fundamentals.eod_derivative_indicator.pe_ratio,
        fundamentals.eod_derivative_indicator.pcf_ratio
    ).filter(
        fundamentals.eod_derivative_indicator.pe_ratio > 20,
        fundamentals.eod_derivative_indicator.pcf_ratio < 50
    ).order_by(
        fundamentals.eod_derivative_indicator.pe_ratio
    ).filter(
        fundamentals.stockcode.in_(context.index_list)
    ).limit(10)
    # 回測不需要傳日期,默認當天的數據
    fund = get_fundamentals(q)

    logger.info(fund.T)
    pass

3,定時器scheduler
通過財務數據選股,不會每天獲取,常以每週/每月選取。
定時器必須在init中使用。
例子:按月查詢。

def init(context):
    # 定義按月運行的一個定時運行器
    # 每月只運行一次,指定第一個交易日
    scheduler.run_monthly(get_data, tradingday=1)

def get_data(context, bar_dict):
    #函數都不用return
    # 在這裏按月去查詢財務數據
    q = query(
        fundamentals.eod_derivative_indicator.pb_ratio
    ).filter(fundamentals.stockcode.in_(context.index_list))

    fund = get_fundamentals(q)

    logger.info(fund.T)

運行順序:

  • 按月
  • scheduler.run_monthly(get_data, tradingday=1)
  • 2月,2月1號假設爲第一個交易日,before_trading -> get_data -> handle_bar
  • 2月其他日期,before_trading -> handle_bar

p8 投資組合與交易

投資組合與交易

1,
在handle_bar進行交易。默認按收盤價買入(撮合方式:當前bar收盤價)。

def handle_bar(context,bar_dict):
    # 進行交易
    # 每天的收盤價,假如第一天11.33*1000
    # order_shares(context.s1, 1000)

    # 按持倉比例,始終佔0.1,有買有賣
    order_target_percent(context.s1, 0.10)

2,限價單,市價單

  • 限價單:掛單,市價20.1,指定20.5賣出,19.2買入(先等它降價,再買)
  • 市價單:目的爲了快速成交,當前市價多少,買入、賣出就是多少(默認)
  • 滑點的設置影響:爲了更好的模擬實際交易中訂單對市場的衝擊,引入滑點。例如,設置滑點爲0.1,原本買入價格爲10元,設置後成交價格爲11元,因爲買的人太多了。
    – 在回測中使用。成交價更高。一般默認取0.1即可。
  • 交易的賣空設置:股票T+1,不要設置。

3,投資組合 portfoilo

  • 資金:可用價值,市場價值,總價值
  • 倉位:當前持有的股票代碼數量 等相關信息
  • stock_account也可以查看相關信息
def handle_bar(context,bar_dict):
    # 按持倉比例,始終佔0.1,有買有賣
    order_target_percent(context.s1, 0.10)
    order_target_percent("000004.XSHE", 0.10)

    # 一旦買入交易之後,投資組合會發生變化
    # 資金、倉位
    print(context.stock_account) #這種也可以
    print("-----")
    print(context.portfolio.positions.keys()) #一般使用這種
    print(context.portfolio.positions[context.s1].quantity) #持股數量

    print("-----")
    print("投資組合的可用資金爲", context.portfolio.cash)
    print("投資組合的市場價值爲", context.portfolio.market_value)
    print("投資組合的總價值爲", context.portfolio.total_value)

結果:

2016-01-04  INFO  StockAccount({'daily_pnl': -200295.42416000017, 'margin': 1985482.0, 'positions': ['000004.XSHE', '000001.XSHE'], 'transaction_cost': 1747.2241600000002, 'cash': 7814222.57584, 'type': 'STOCK', 'dividend_receivable': 0, 'total_value': 9799704.57584, 'position_pnl': 0, 'trading_pnl': -198548.2000000002, 'market_value': 1985482.0, 'frozen_cash': 0.0, 'total_cash': 7814222.57584})
2016-01-04  INFO  -----
2016-01-04  INFO  ['000001.XSHE', '000004.XSHE']
2016-01-04  INFO  88100
2016-01-04  INFO  -----
2016-01-04  INFO  投資組合的可用資金爲 7814222.57584
2016-01-04  INFO  投資組合的市場價值爲 1985482.0
2016-01-04  INFO  投資組合的總價值爲 9799704.57584
......

p9 策略的收益指標

視頻位置:策略的收益指標
1,策略的評價指標

  • 收益指標
    – 回測收益率:(最終總價值-初始總價值)/初始總價值
    – 基準收益:參考的標準,市場表現情況作爲標準來看我們的策略。默認HS300.
    – 年化收益率:平均每年的收益情況,重點看這個。年化一般達到15%~30%較好,當然越高越好。

p10 策略風險指標

視頻鏈接:策略風險指標

1,風險指標

  • 風險指標指的是,在獲取收益的時候,承擔一些風險。
  • 最大回撤比率:最大回撤最好保持在10%~30%之間,越小越好。
  • 夏普比例:夏普比例越大越好,越大說明單位風險所獲得的風險彙報越高。
    – 如國債爲4%收益,你的收益爲16%,波動爲5%,那麼夏普比例爲:(16%-4%)/5%
    – 一般要使你的策略達到1.5以上才較好。通常爲0.7~1.5

未完待續。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章