书接上一回
柒拾- 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][

最低0.47元/天 解锁文章
1830

被折叠的 条评论
为什么被折叠?



