IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 人工智能 -> 国信证券学习系列(4) -> 正文阅读

[人工智能]国信证券学习系列(4)

机器学习篇章,本章不过时脚本小子,机器学习最核心的是机器,是模型。

学习,无非就是找些有的没的因子扔进去,但说实话,机器学习,太过容易过拟合,容易无效化。回测好看的一笔,实盘垃圾的不行。

获取训练数据:

        #获取训练数据
        #用20160101到20170101一年间的数据生成训练集
        df = ContextInfo.get_market_data(['open','high','low','close','volume'],stock_code=[ContextInfo.stock],start_time='20160101',end_time='20200101',dividend_type='front')
        df = df.sort_index()
        days = df.index.values
        days_close = df['close'].values

?获取训练因子:

 #计算训练因子
        for i in range(14, len(days) - 5):
            start_day = days[i - 14]
            end_day = days[i]
            data = ContextInfo.get_market_data(['open','high','low','close','volume'],stock_code=[ContextInfo.stock],end_time=end_day,count=15,skip_paused=False,dividend_type='front')
            data = data.sort_index()
            open = data['open'].values
            close = data['close'].values
            max = data['high'].values
            min = data['low'].values
            volume = data['volume'].values
            close_mean = close[-1] / np.mean(close)
            volume_mean = volume[-1] / np.mean(volume)
            max_mean = max[-1] / np.mean(max)
            min_mean = min[-1] / np.mean(min)
            vol = volume[-1]
            return_now = close[-1] / close[0]
            std = np.std(np.array(close), axis = 0)
            #features用于存放因子
            features = [close_mean, volume_mean, max_mean, min_mean, vol, return_now, std]           #计算出的6个因子作为特征
            x_all.append(features)

        for i in range(len(days_close) - 19):
            if days_close[i+19] > days_close[i+14]:
                label = 1
            else:
                label = 0
            y_all.append(label)

?生成训练模型:

        x_train = x_all[:-1]
        y_train = y_all[:-1]
        
        #生成训练好的模型
        ContextInfo.clf = svm.SVC(C=1.0, kernel='rbf', degree=3, gamma='auto', coef0=0.0, shrinking=True, probability=False,tol=0.001, cache_size=200, verbose=False, max_iter=-1, decision_function_shape='ovr', random_state=None)
        try:
            ContextInfo.clf.fit(x_train, y_train)
        except:
            e = traceback.format_exc()

运用模型,判断涨跌信号:

 #运用模型,判断涨跌信号
        try:
            prediction = ContextInfo.clf.predict(features)[0] # 机器学习判断涨跌
            if prediction == 1:
                ContextInfo.holding = int(ContextInfo.money*0.95/(open_today))/100
                order_shares(ContextInfo.stock,ContextInfo.holding*100,'fix',open_today,ContextInfo,ContextInfo.accountid)
                ContextInfo.buyprice = open_today
                buy_condition = True
                print(today)
                print('open long position to 0.95')
        except :
            print(('predict error occur,bar:', d))

?策略代码:

#coding:gbk
#!/usr/bin/python
"""
回测模型示例(非实盘交易策略)

#单股机器学习模型,在主图下直接运行即可
#模型中以过去15个交易日数据生成特征变量以预测5个交易日后的涨跌,
#特征变量我们选取了平均收盘价,平均成交量,平均最高价,平均最低价,总收益,收盘价的标准差
#训练结束后,回测过程中在每个星期一预测本周五的涨跌,以此为据开仓
"""
import pandas as pd
import numpy as np
import time
from datetime import *
from sklearn import svm
import traceback
def init(ContextInfo):
    ContextInfo.stock = ContextInfo.stockcode + '.' + ContextInfo.market
    ContextInfo.set_universe([ContextInfo.stock])
    ContextInfo.holding = 0
    ContextInfo.days = 0
    ContextInfo.money = ContextInfo.capital
    ContextInfo.accountid = "testS"

def handlebar(ContextInfo):
    buy_condition = False
    sell_condition = False
    d = ContextInfo.barpos
    if ContextInfo.days == 0:
        #用20160101到20170101一年间的数据生成训练集
        df = ContextInfo.get_market_data(['open','high','low','close','volume'],stock_code=[ContextInfo.stock],start_time='20160101',end_time='20170101',dividend_type='front')
        df = df.sort_index()
        days = df.index.values
        days_close = df['close'].values
        print('start training SVM')
        x_all = []
        y_all = []
        for i in range(14, len(days) - 5):
            start_day = days[i - 14]
            end_day = days[i]
            data = ContextInfo.get_market_data(['open','high','low','close','volume'],stock_code=[ContextInfo.stock],end_time=end_day,count=15,skip_paused=False,dividend_type='front')
            data = data.sort_index()
            open = data['open'].values
            close = data['close'].values
            max = data['high'].values
            min = data['low'].values
            volume = data['volume'].values
            close_mean = close[-1] / np.mean(close)
            volume_mean = volume[-1] / np.mean(volume)
            max_mean = max[-1] / np.mean(max)
            min_mean = min[-1] / np.mean(min)
            vol = volume[-1]
            return_now = close[-1] / close[0]
            std = np.std(np.array(close), axis = 0)
            #features用于存放因子
            features = [close_mean, volume_mean, max_mean, min_mean, vol, return_now, std]           #计算出的6个因子作为特征
            x_all.append(features)

        for i in range(len(days_close) - 19):
            if days_close[i+19] > days_close[i+14]:
                label = 1
            else:
                label = 0
            y_all.append(label)
        x_train = x_all[:-1]
        y_train = y_all[:-1]

        ContextInfo.clf = svm.SVC(C=1.0, kernel='rbf', degree=3, gamma='auto', coef0=0.0, shrinking=True, probability=False,tol=0.001, cache_size=200, verbose=False, max_iter=-1, decision_function_shape='ovr', random_state=None)
        try:
            ContextInfo.clf.fit(x_train, y_train)
        except:
            e = traceback.format_exc()
            print(('value error, bar:', e))
        print('training finish!')

    timetag = ContextInfo.get_bar_timetag(d)
    timetag_start = ContextInfo.get_bar_timetag(d-15)
    timetag_end = ContextInfo.get_bar_timetag(d-1)           #过去15个交易日的起止时间

    today = timetag_to_datetime(timetag, '%Y%m%d')
    start_date = timetag_to_datetime(timetag_start, '%Y%m%d')
    end_date = timetag_to_datetime(timetag_end, '%Y%m%d')
    weekday = datetime.strptime(today, '%Y%m%d').isoweekday()
    open_today = ContextInfo.get_market_data(['open'],stock_code=[ContextInfo.stock],skip_paused=False,dividend_type='front')
    close_today = ContextInfo.get_market_data(['close'],stock_code=[ContextInfo.stock],skip_paused=False,dividend_type='front')
    #print ContextInfo.holding
    #print weekday
    if ContextInfo.holding == 0 and weekday == 1:            #每个星期一判断是否开仓
        data = ContextInfo.get_market_data(['open','high','low','close','volume'],stock_code=[ContextInfo.stock],end_time=end_date,count=15,skip_paused=False, dividend_type='front')
        data = data.sort_index()
        close = data['close'].values
        max = data['high'].values
        min = data['low'].values
        volume = data['volume'].values
        close_mean = close[-1] / np.mean(close)
        volume_mean = volume[-1] / np.mean(volume)
        max_mean = max[-1] / np.mean(max)
        min_mean = min[-1] / np.mean(min)
        vol = volume[-1]
        return_now = close[-1] / close[0]
        std = np.std(np.array(close), axis = 0)

        features = [close_mean, volume_mean, max_mean, min_mean, vol, return_now, std]
        features = np.array(features).reshape(1, -1)
        try:
            prediction = ContextInfo.clf.predict(features)[0]
            if prediction == 1:
                ContextInfo.holding = int(ContextInfo.money*0.95/(open_today))/100
                order_shares(ContextInfo.stock,ContextInfo.holding*100,'fix',open_today,ContextInfo,ContextInfo.accountid)
                ContextInfo.buyprice = open_today
                buy_condition = True
                print(today)
                print('open long position to 0.95')
        except :
            print(('predict error occur,bar:', d))
    elif ContextInfo.holding > 0 and close_today/ContextInfo.buyprice >= 1.1:        #每个交易日判断止盈止损
        order_shares(ContextInfo.stock,-ContextInfo.holding*100,'fix',close_today,ContextInfo,ContextInfo.accountid)
        ContextInfo.holding = 0
        sell_condition = True
        print(today)
        print('reach profit stop limit, close position')
    elif ContextInfo.holding > 0 and close_today/ContextInfo.buyprice < 0.98 and weekday == 5:
        order_shares(ContextInfo.stock,-ContextInfo.holding*100,'fix',close_today,ContextInfo,ContextInfo.accountid)
        ContextInfo.holding = 0
        sell_condition = True
        print(today)
        print('reach lose stop limit, close position')
    ContextInfo.days += 1

    ContextInfo.paint('do_buy', int(buy_condition), -1, 0)
    ContextInfo.paint('do_sell', int(sell_condition), -1, 0)

  人工智能 最新文章
2022吴恩达机器学习课程——第二课(神经网
第十五章 规则学习
FixMatch: Simplifying Semi-Supervised Le
数据挖掘Java——Kmeans算法的实现
大脑皮层的分割方法
【翻译】GPT-3是如何工作的
论文笔记:TEACHTEXT: CrossModal Generaliz
python从零学(六)
详解Python 3.x 导入(import)
【答读者问27】backtrader不支持最新版本的
上一篇文章      下一篇文章      查看所有文章
加:2022-05-05 11:19:11  更:2022-05-05 11:20:49 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/26 7:47:01-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码