柒拾壹- tushare 模拟策略交易 (二)

书接上一回
柒拾- tushare 模拟策略交易 (一)


一、模拟交易

根据官网上的文章,并且以他们的规则
主要是要模拟买入卖出的情况,以下是代码(如果不是官网的代码不能直接拿来,我就可以懒惰一点):

'''
Descripttion: 回测模块
Version: 1.0
Author: BerryBC
Date: 2024-02-18 17:50:01
LastEditors: BerryBC
LastEditTime: 2024-02-20 18:00:00
'''

import datetime
import time
import mysql.connector
from sqlalchemy import create_engine
import tushare

from Lib.DBConn import claDBConn


class claBackTestingDailyDeal(object):
    def __init__(self, strTitle='临时测试', strDesc='', fltInitCash=500000):
        '''
        description: 初始化
        '''
        # 存一个固定资产
        self.fltInitCash = fltInitCash

        # 获取链接
        self.dbMySQL = claDBConn()
        strSQLMaxInd = "SELECT MAX(bt_ind) FROM tb_bt_daily_cfg"
        listMaxInd = self.dbMySQL.ReturnSQLData(strSQLMaxInd)

        # 如果有回测序号则取最大,如没有则取1
        self.intInd = 1
        if listMaxInd is not None and listMaxInd[0][0] is not None:
            self.intInd = listMaxInd[0][0] + 1

        # 初始化该次回测数据
        strSQLBTCfg = "INSERT INTO tb_bt_daily_cfg (bt_ind, bt_title,bt_desc,bt_init_cash,bt_time) VALUES (" + \
            str(self.intInd)+", '"+strTitle+"', '"+strDesc+"', "+str(fltInitCash)+" ,now())"
        self.dbMySQL.RunSQLOnce(strSQLBTCfg)

        strSQLBTCfg = "DELETE FROM  tb_bt_daily_my_capital WHERE bt_ind = "+str(self.intInd)+""
        self.dbMySQL.RunSQLOnce(strSQLBTCfg)

        strSQLBTCfg = "DELETE FROM  tb_bt_daily_tmp_my_stock_pool WHERE bt_ind = "+str(self.intInd)+""
        self.dbMySQL.RunSQLOnce(strSQLBTCfg)

        # 初始化数据库数据
        self.dbMySQL.RefreshDailyQuote()
        self.dbMySQL.FixCalDay()

    def CalCapital(self, strDate=None):
        '''
        description: 计算当前资产
        return: {dict}  intLock : 股票资产
                        intRest : 现金资产
        '''
        dictCapital = {
   
   'intLock': 0, 'intRest': 0}
        if strDate is not None:
            # 先取最近的一个可用资产
            strSQLRestMoney = "SELECT * FROM  tb_bt_daily_my_capital WHERE bt_ind = " + \
                str(self.intInd)+"  AND state_dt <= '"+strDate+"' ORDER BY seq DESC LIMIT 1"
            listRestMoney = self.dbMySQL.ReturnSQLData(strSQLRestMoney)

            # 数据库中能查出的剩余资产
            if len(listRestMoney)>0 and listRestMoney[0][
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值