diff --git a/__pycache__/MyTT.cpython-310.pyc b/__pycache__/MyTT.cpython-310.pyc deleted file mode 100644 index 0a8d58f5..00000000 Binary files a/__pycache__/MyTT.cpython-310.pyc and /dev/null differ diff --git a/__pycache__/formula.cpython-310.pyc b/__pycache__/formula.cpython-310.pyc index 77495187..564012a2 100644 Binary files a/__pycache__/formula.cpython-310.pyc and b/__pycache__/formula.cpython-310.pyc differ diff --git a/formula.py b/formula.py index 4d301aec..63d5d66b 100644 --- a/formula.py +++ b/formula.py @@ -1,15 +1,6 @@ import backtrader as bt -import datetime -import pandas as pd -import numpy as np -import os, sys -import copy -import math -import warnings - -from MyTT import * -from printAnalyzer import printTradeAnalysis +from strategy.MyTT import * class Result: diff --git "a/formula_\344\270\211\346\256\265\345\272\225\350\203\214\351\251\260.py" "b/formula_\344\270\211\346\256\265\345\272\225\350\203\214\351\251\260.py" index c1f09eb0..c2de2531 100644 --- "a/formula_\344\270\211\346\256\265\345\272\225\350\203\214\351\251\260.py" +++ "b/formula_\344\270\211\346\256\265\345\272\225\350\203\214\351\251\260.py" @@ -1,18 +1,9 @@ import logging -import backtrader as bt import datetime -import pandas as pd -import numpy as np -import os, sys -import copy -import math -import warnings - -from MyTT import * +from strategy.MyTT import * from formula import Formula, Result -from printAnalyzer import printTradeAnalysis # 沪深300 1d 20220426 diff --git "a/formula_\350\267\214\347\240\264\344\270\255\346\236\242\345\272\225\350\203\214\351\251\260.py" "b/formula_\350\267\214\347\240\264\344\270\255\346\236\242\345\272\225\350\203\214\351\251\260.py" index 39c96231..e37beb00 100644 --- "a/formula_\350\267\214\347\240\264\344\270\255\346\236\242\345\272\225\350\203\214\351\251\260.py" +++ "b/formula_\350\267\214\347\240\264\344\270\255\346\236\242\345\272\225\350\203\214\351\251\260.py" @@ -1,16 +1,4 @@ -import backtrader as bt -import datetime -import pandas as pd -import numpy as np -import os, sys -import copy - -import math -import warnings - -from MyTT import * from formula import Formula -from printAnalyzer import printTradeAnalysis # warnings.filterwarnings("ignore") diff --git a/momentum.py b/momentum.py index df33e764..48b72084 100644 --- a/momentum.py +++ b/momentum.py @@ -1,14 +1,7 @@ import backtrader as bt import datetime -import pandas as pd -import numpy as np -import os, sys -import copy -import math -import warnings - -from MyTT import * +from strategy.MyTT import * from data import MySQLData from printAnalyzer import printTradeAnalysis diff --git a/server.py b/server.py index 6423ed88..5cb48e83 100644 --- a/server.py +++ b/server.py @@ -1,20 +1,12 @@ from sanic import Sanic, json, SanicException, text from sanic.log import logger -from strategy import * - from dateutil.parser import parse import akshare import Ashare -from data import KlineData -from strategy import strategy_弱转强 -from strategy.base import run_formula - -# from strategy.strategy_弱转强 import Strategy - -strategyArr = { - "底部连阳_回踩_突破": strategy_弱转强.Strategy, -} +from strategy.base import init_cerebro +from strategy.comparative_strength_rebound import Strategy +from strategy.data import ArrowData app = Sanic(name='quant-api') @@ -30,26 +22,24 @@ async def hello_world(request): async def handler(request): d = request.json # strategy_底部连阳_回踩_突破.Strategy - f = eval("strategy_" + d['strategy'] + ".Strategy") + # f = eval( d['strategy'] + ".Strategy") df = None + s1 = Strategy + cerebro = init_cerebro() + cerebro.addstrategy(s1) start = parse(d['start']) end = parse(d['end']) - datas = [] for section in d["symbol_group"]: exchange, symbol, interval = section.split(".") - data = KlineData( + data = ArrowData( exchange=exchange, symbol=symbol, start_date=start, end_date=end, freq=interval, ) - datas.append(data) - try: - result = run_formula(f, datas) - except Exception as e: - logger.error('run_formula error:%s %s', e, d) - raise e + cerebro.adddata(data) + result = cerebro.run() if df is None: df = result[0].orders else: diff --git a/MyTT.py b/strategy/MyTT.py similarity index 99% rename from MyTT.py rename to strategy/MyTT.py index 601c2a0e..7e2c2571 100644 --- a/MyTT.py +++ b/strategy/MyTT.py @@ -18,9 +18,6 @@ import numpy as np; import pandas as pd -from decrator import to_array - - #------------------ 0级:核心工具函数 -------------------------------------------- def RD(N,D=3): return np.round(N,D) #四舍五入取3位小数 def RET(S,N=1): return np.array(S)[-N] #返回序列倒数第N个值,默认返回最后一个 diff --git a/strategy/__pycache__/MyTT.cpython-310.pyc b/strategy/__pycache__/MyTT.cpython-310.pyc new file mode 100644 index 00000000..9ca18a20 Binary files /dev/null and b/strategy/__pycache__/MyTT.cpython-310.pyc differ diff --git a/strategy/__pycache__/base.cpython-310.pyc b/strategy/__pycache__/base.cpython-310.pyc index 61fe9212..eed72ea4 100644 Binary files a/strategy/__pycache__/base.cpython-310.pyc and b/strategy/__pycache__/base.cpython-310.pyc differ diff --git a/strategy/__pycache__/comparative_strength_rebound.cpython-310.pyc b/strategy/__pycache__/comparative_strength_rebound.cpython-310.pyc new file mode 100644 index 00000000..244cec0b Binary files /dev/null and b/strategy/__pycache__/comparative_strength_rebound.cpython-310.pyc differ diff --git a/strategy/__pycache__/comparative_strength_rebound_test.cpython-310-pytest-7.4.3.pyc b/strategy/__pycache__/comparative_strength_rebound_test.cpython-310-pytest-7.4.3.pyc new file mode 100644 index 00000000..8a33b47a Binary files /dev/null and b/strategy/__pycache__/comparative_strength_rebound_test.cpython-310-pytest-7.4.3.pyc differ diff --git a/strategy/__pycache__/custom_mytt.cpython-310.pyc b/strategy/__pycache__/custom_mytt.cpython-310.pyc index 7732a859..057f1e7c 100644 Binary files a/strategy/__pycache__/custom_mytt.cpython-310.pyc and b/strategy/__pycache__/custom_mytt.cpython-310.pyc differ diff --git a/strategy/__pycache__/data.cpython-310.pyc b/strategy/__pycache__/data.cpython-310.pyc index aee8f8fc..881f0497 100644 Binary files a/strategy/__pycache__/data.cpython-310.pyc and b/strategy/__pycache__/data.cpython-310.pyc differ diff --git "a/strategy/__pycache__/strategy_\347\233\270\345\257\271\345\274\272\345\212\277.cpython-310.pyc" "b/strategy/__pycache__/strategy_\347\233\270\345\257\271\345\274\272\345\212\277.cpython-310.pyc" deleted file mode 100644 index 1bae063c..00000000 Binary files "a/strategy/__pycache__/strategy_\347\233\270\345\257\271\345\274\272\345\212\277.cpython-310.pyc" and /dev/null differ diff --git "a/strategy/__pycache__/test_strategy_\347\233\270\345\257\271\345\274\272\345\212\277.cpython-310-pytest-7.4.3.pyc" "b/strategy/__pycache__/test_strategy_\347\233\270\345\257\271\345\274\272\345\212\277.cpython-310-pytest-7.4.3.pyc" index 381f10e7..20b801f4 100644 Binary files "a/strategy/__pycache__/test_strategy_\347\233\270\345\257\271\345\274\272\345\212\277.cpython-310-pytest-7.4.3.pyc" and "b/strategy/__pycache__/test_strategy_\347\233\270\345\257\271\345\274\272\345\212\277.cpython-310-pytest-7.4.3.pyc" differ diff --git a/strategy/base.py b/strategy/base.py index 05cb9ffc..e20cfd1e 100644 --- a/strategy/base.py +++ b/strategy/base.py @@ -1,6 +1,138 @@ +import datetime + import pytz from formula import * +from strategy.data import ArrowData + +pd.set_option('display.max_rows', None) # 显示所有行 +pd.set_option('display.max_columns', None) # 显示所有列 +pd.set_option('display.width', None) # 显示所有列宽 + + +def get_data(exchange, bk, freq, + start_date=datetime.datetime(2024, 1, 1), + end_date=datetime.datetime(2024, 12, 12), + tz=pytz.timezone('Asia/Shanghai')): + return ArrowData( + exchange=exchange, + symbol=bk, + freq=freq, + start_date=start_date, + end_date=end_date, + tz=tz + ) + + +def init_cerebro(): + cerebro = bt.Cerebro() + # 添加手续费,按照万分之二收取 + cerebro.broker.setcommission(commission=0.0002, stocklike=True) + # 设置初始资金为100万 + cerebro.broker.setcash(1_0000_0000) + # 添加自定义的分析指标 + cerebro.addanalyzer(bt.analyzers.PyFolio, timeframe=bt.TimeFrame.Minutes, compression=None) + cerebro.addanalyzer(TradeList, _name='tradelist') + + return cerebro + + +class TradeList(bt.Analyzer): + def __init__(self): + + self.trades = [] + self.cumprofit = 0.0 + + def notify_trade(self, trade): + if not trade.isclosed: + return + if len(trade.history) <= 0: + return + brokervalue = self.strategy.broker.getvalue() + + dir = 'short' + if trade.history[0].event.size > 0: dir = 'long' + + pricein = trade.history[len(trade.history) - 1].status.price + priceout = trade.history[len(trade.history) - 1].event.price + datein = bt.num2date(trade.history[0].status.dt, tz=trade.data.p.tz, naive=False) + dateout = bt.num2date(trade.history[len(trade.history) - 1].status.dt, tz=trade.data.p.tz, naive=False) + # if trade.data._timeframe >= bt.TimeFrame.Days: + # datein = datein.date() + # dateout = dateout.date() + + pcntchange = 100 * priceout / pricein - 100 + pnl = trade.history[len(trade.history) - 1].status.pnlcomm + pnlpcnt = 100 * pnl / brokervalue + barlen = trade.history[len(trade.history) - 1].status.barlen + pbar = pnl / barlen + self.cumprofit += pnl + + size = value = 0.0 + for record in trade.history: + if abs(size) < abs(record.status.size): + size = record.status.size + value = record.status.value + + highest_in_trade = max(trade.data.high.get(ago=0, size=barlen + 1)) + lowest_in_trade = min(trade.data.low.get(ago=0, size=barlen + 1)) + hp = 100 * (highest_in_trade - pricein) / pricein + lp = 100 * (lowest_in_trade - pricein) / pricein + if dir == 'long': + mfe = hp + mae = lp + if dir == 'short': + mfe = -lp + mae = -hp + + self.trades.append({'ref': trade.ref, + 'ticker': trade.data._name, + 'dir': dir, + 'datein': datein, + 'pricein': pricein, + 'dateout': dateout, + 'priceout': priceout, + 'chng%': round(pcntchange, 2), + 'pnl': pnl, 'pnl%': round(pnlpcnt, 2), + 'size': size, + 'value': value, + 'cumpnl': self.cumprofit, + 'nbars': barlen, 'pnl/bar': round(pbar, 2), + 'mfe%': round(mfe, 2), 'mae%': round(mae, 2)}) + + def get_analysis(self): + return self.trades + + +def print_result(cerebro): + # 获取交易记录 + transactions = cerebro.broker.get_transactions() + # 初始化总收益率 + total_return = 0.0 + # 打印所有交易记录及每笔交易的收益率 + for i, transaction in enumerate(transactions): + # 买入或卖出的价格 + price = transaction.price + # 买入或卖出的数量 + size = transaction.size + # 交易成本(如果有) + commission = transaction.comm + # 交易类型('buy' 或 'sell') + order = 'buy' if size > 0 else 'sell' + # 计算单笔交易的价值 + value = price * size + # 打印交易信息 + print(f'Transaction {i}: {order} {abs(size)} units at {price}') + # 计算并打印单笔交易的收益率(此处简化处理,未考虑交易成本和时间价值) + if order == 'buy': + print(f' Buy Value: {value}') + else: + # 卖出时计算收益率 + profit = (price - transactions[i - 1].price) * size - 2 * commission # 假设买入和卖出都有佣金 + print(f' Sell Profit/Loss: {profit}') + total_return += profit + # 打印总收益率 + print(f'Total Return: {total_return}') # 我们使用的时候,直接用我们新的类读取数据就可以了。 @@ -32,16 +164,11 @@ def __init__(self): def log(self, txt, dt=None): ''' Logging function fot this strategy''' - dt = dt or self.datas[0].datetime.datetime(0) - dt = pytz.utc.localize(dt) - # 定义东八区时区 - shanghai_timezone = pytz.timezone('Asia/Shanghai') - # 将UTC datetime转换为东八区datetime - shanghai_datetime = dt.astimezone(shanghai_timezone) - print('{}, {}'.format(shanghai_datetime, txt)) + dt = dt or self.datas[0].datetime.datetime(0, naive=False) + print('%s, %s' % (dt.isoformat(), txt)) def notify_order(self, order): - symbol = order.p.data.p.symbol + symbol = order.p.data.p.name if order.isbuy(): if order.executed.dt: executed_dt = bt.num2date(order.executed.dt) @@ -79,11 +206,6 @@ def notify_order(self, order): self.order = None - def notify_trade(self, trade): - if not trade.isclosed: - return - self.log(f'策略收益:\n毛收益 {trade.pnl:.2f}, 净收益 {trade.pnlcomm:.2f}') - def formula(self, data): return Formula(data, self.ind[data]) @@ -97,37 +219,15 @@ def get_idx(self): return self.data.close.idx -def get_cerebro(data): - cerebro = bt.Cerebro() - tf = bt.TimeFrame.Minutes - - # 添加数据到cerebro - if isinstance(data, list): - if data[0].p.freq.endswith('d') or data[0].p.freq.endswith('w'): - tf = bt.TimeFrame.Days - for d in data: - cerebro.adddata(d, name=d.p.symbol) - else: - cerebro.adddata(data, name=data.p.table) - if data.p.freq.endswith('d') or data.p.freq.endswith('w'): - tf = bt.TimeFrame.Days - - # 添加手续费,按照万分之二收取 - cerebro.broker.setcommission(commission=0.0002, stocklike=True) - # 设置初始资金为100万 - cerebro.broker.setcash(1_0000_0000) - - cerebro.addanalyzer(bt.analyzers.PyFolio, timeframe=tf, compression=None) - return cerebro - - def buys_time(result): for i in result._trades: print(i) + + def is_valid_number(value): # 检查变量是否是数字类型 if isinstance(value, (int, float, complex)): # 检查是否不是NaN、正无穷或负无穷 if not (isinstance(value, float) and (value != value or value == float('inf') or value == float('-inf'))): return True - return False \ No newline at end of file + return False diff --git "a/strategy/strategy_\347\233\270\345\257\271\345\274\272\345\212\277.py" b/strategy/comparative_strength_rebound.py similarity index 66% rename from "strategy/strategy_\347\233\270\345\257\271\345\274\272\345\212\277.py" rename to strategy/comparative_strength_rebound.py index c6cb88b5..458b92a1 100644 --- "a/strategy/strategy_\347\233\270\345\257\271\345\274\272\345\212\277.py" +++ b/strategy/comparative_strength_rebound.py @@ -1,17 +1,18 @@ import backtrader as bt -from custom_mytt import HHV from formula import Formula from strategy.base import BaseStrategy, is_valid_number import numpy as np +from strategy.custom_mytt import * + # ComparativeStrengthReboundStrategy 比较强度反弹策略 # -class ComparativeStrengthReboundStrategy(BaseStrategy): +class Strategy(BaseStrategy): params = ( ('lookback_period', 10), # Lookback period for comparison - ('threshold', 8), # Threshold for 90% confidence + ('threshold', 6), # Threshold for 90% confidence ) def __init__(self): @@ -25,21 +26,38 @@ def prenext(self): def next(self): current_date = self.datas[0].datetime.datetime(0) - if current_date.day == 22: + if current_date.day == 22 and current_date.hour == 13 and current_date.minute == 30: pass - if self.ref_data.close > self.ind[self.ref_data].ma20: - return + # Calculate the comparison result for each data # Loop through the other datas for data in self.other_datas: - self.next_one(data) + try: + self.next_buy(data) + self.next_sell(data) + except: + continue - def next_one(self, data): + def next_buy(self, data): current_date = self.datas[0].datetime.datetime(0) data_today = data.close[0] if data_today < data.ma5[0]: return - # f1 = self.formula(data) + if data.close < self.ind[data].ma5: + return + # 当前close在最近10天价位的中上位置 + high = self.V(HHV(data.close, 10)) + low = self.V(LLV(data.close, 10)) + rate = (data.close[0] - low) / (high - low) + if rate < 0.5: + return + high = self.V(HHV(self.ref_data.close, 10)) + low = self.V(LLV(self.ref_data.close, 10)) + ref_rate = (self.ref_data.close[0] - low) / (high - low) + if rate < ref_rate: + return + + # f1 = self.formula(data) pct_change_max = self.V(HHV(data.pct_change, 20)) if not is_valid_number(pct_change_max): return @@ -67,3 +85,9 @@ def next_one(self, data): if ref_today < ref_yesterday and data_today > data_yesterday: self.log(f'BUY {data._name}, Ref Down and {data._name} Up') self.order = self.buy(data) + + def next_sell(self, data): + if self.getposition(data=data).size == 0: + return + if data.close[0] < self.ind[data].ma5[0]: + self.sell(data) diff --git a/strategy/comparative_strength_rebound_test.py b/strategy/comparative_strength_rebound_test.py new file mode 100644 index 00000000..c00e0367 --- /dev/null +++ b/strategy/comparative_strength_rebound_test.py @@ -0,0 +1,146 @@ +import datetime +from pprint import pprint +from unittest import TestCase + +import akshare +import pytz + +from data import ArrowData +from strategy import * +from strategy import comparative_strength_rebound +from strategy.base import init_cerebro, print_result, TradeList, get_data +import backtrader as bt +import pandas as pd + +from strategy.comparative_strength_rebound import Strategy + + +class TestStrategy(TestCase): + def setUp(self): + self.strategy = comparative_strength_rebound.Strategy + + def test_backtest_沪深300(self): + cerebro = init_cerebro() + freq = "1h" + data_ref = get_data("asindex", "sh000300", freq, + start_date=datetime.datetime(2024, 1, 2), + end_date=datetime.datetime(2024, 12, 12), ) + + cerebro.adddata(data_ref) + df = akshare.stock_zh_a_spot_em() + for symbol in df['代码'][:100]: + try: + data = get_data("as", symbol, freq, + start_date=datetime.datetime(2024, 1, 2), + end_date=datetime.datetime(2024, 12, 12), ) + cerebro.adddata(data) + except: + pass + cerebro.addstrategy(self.strategy) # 添加策略 + + # 启动回测 + result = cerebro.run(tradehistory=True) + # 返回结果 + ret = pd.DataFrame(result[0].analyzers.tradelist.get_analysis()) + pprint(ret) + + def test_backtest_算力(self): + cerebro = init_cerebro() + bk = "BK1134" + data_ref = self.get_data(bk) + cerebro.adddata(data_ref) + df = akshare.stock_board_concept_cons_em(symbol="算力概念") + for symbol in df['代码']: + try: + data = ArrowData( + exchange="as", + symbol=symbol, + freq="1h", + start_date=datetime.datetime(2024, 1, 2), + end_date=datetime.datetime(2024, 12, 12), + tz=pytz.timezone('Asia/Shanghai') + ) + cerebro.adddata(data) + except: + pass + cerebro.addstrategy(self.strategy) # 添加策略 + + # 启动回测 + result = cerebro.run(tradehistory=True) + # 返回结果 + ret = pd.DataFrame(result[0].analyzers.tradelist.get_analysis()) + pprint(ret) + + def test_克来机电_1h(self): + # 引力传媒 + # 添加数据到cerebro + data_a = self.get_data("BK1090") + data_b = ArrowData( + exchange="as", + symbol="603960", + freq="1h", + start_date=datetime.datetime(2024, 1, 2), + end_date=datetime.datetime(2024, 12, 12), + tz=pytz.timezone('Asia/Shanghai') + ) + + cerebro = get_cerebro([data_a, data_b]) + cerebro = bt.Cerebro() + cerebro.adddata(data_a) + cerebro.adddata(data_b) + + cerebro.addstrategy(self.strategy) # 添加策略 + + result = cerebro.run() + hit_list = list(result[0].orders.hit_dt.astype(str).values.tolist()) + self.assertIn("2024-02-01 10:30:00", hit_list, "hit") + + def test_中视传媒_1h(self): + # 引力传媒 + # 添加数据到cerebro + data_a = ArrowData( + exchange="emindustry", + symbol="BK0486", + freq="1h", + start_date=datetime.datetime(2024, 1, 1), + end_date=datetime.datetime(2024, 12, 12), + ) + data_b = ArrowData( + exchange="as", + symbol="600088", + freq="1h", + start_date=datetime.datetime(2024, 1, 1), + end_date=datetime.datetime(2024, 12, 12), + ) + + cerebro = get_cerebro([data_a, data_b]) + cerebro = bt.Cerebro() + cerebro.adddata(data_a) + cerebro.adddata(data_b) + + cerebro.addstrategy(ComparativeStrengthReboundStrategy) # 添加策略 + + result = cerebro.run() + hit_list = list(result[0].orders.hit_dt.astype(str).values.tolist()) + self.assertIn("2024-01-23 09:30:00", hit_list, "hit") + + def test_华映科技_5m(self): + # 华映科技 + # 添加数据到cerebro + freq = "5m" + data_a = get_data("emindustry", "BK1038", freq, ) + data_b = get_data("as", "000536", freq, ) + + cerebro = init_cerebro() + cerebro.adddata(data_a) + cerebro.adddata(data_b) + + cerebro.addstrategy(self.strategy) # 添加策略 + + result = cerebro.run(tradehistory=True) + hit_list = list(result[0].orders.hit_dt.astype(str).values.tolist()) + self.assertIn("2024-02-22 13:30:00", hit_list, "hit") + self.assertNotIn("2024-03-05 09:50:00", hit_list, "hit") + # 返回结果 + ret = pd.DataFrame(result[0].analyzers.tradelist.get_analysis()) + pprint(ret) diff --git a/strategy/custom_mytt.py b/strategy/custom_mytt.py index c6e4115a..50821cd8 100644 --- a/strategy/custom_mytt.py +++ b/strategy/custom_mytt.py @@ -1,6 +1,7 @@ -import MyTT import numpy as np +from strategy import MyTT + def to_array(func): def wrapper(*args, **kwargs): @@ -29,3 +30,6 @@ def wrapper(*args, **kwargs): @to_array def HHV(S, N): # HHV(C, 5) 最近5天收盘最高价 return MyTT.HHV(S, N) +@to_array +def LLV(S, N): # HHV(C, 5) 最近5天收盘最高价 + return MyTT.LLV(S, N) \ No newline at end of file diff --git a/strategy/data.py b/strategy/data.py index 48d7e416..4f6b83c2 100644 --- a/strategy/data.py +++ b/strategy/data.py @@ -37,7 +37,7 @@ class ArrowData(bt.feeds.PandasData): # 可以根据需要添加其他参数 ) - def __init__(self, exchange, symbol, freq, start_date, end_date): + def __init__(self, exchange, symbol, freq, start_date, end_date,tz=pytz.timezone('Asia/Shanghai')): req = { "name": f"kline_{exchange}_{symbol}_{freq}", "timestampStart": start_date.timestamp() * 1000, @@ -52,7 +52,7 @@ def __init__(self, exchange, symbol, freq, start_date, end_date): # 确保时间列是datetime类型 self.p.dataname = df self.p.freq = freq - self.p.symbol = symbol + self.p.name = symbol self.p.timeframe = bt.TimeFrame.Days # 调用父类的__init__方法,传递处理后的DataFrame super(ArrowData, self).__init__() diff --git "a/strategy/test_strategy_\347\233\270\345\257\271\345\274\272\345\212\277.py" "b/strategy/test_strategy_\347\233\270\345\257\271\345\274\272\345\212\277.py" deleted file mode 100644 index 41295967..00000000 --- "a/strategy/test_strategy_\347\233\270\345\257\271\345\274\272\345\212\277.py" +++ /dev/null @@ -1,69 +0,0 @@ -import datetime -from unittest import TestCase - -import pytz - -from data import ArrowData -from strategy import * -from strategy.base import get_cerebro -from strategy.strategy_相对强势 import ComparativeStrengthReboundStrategy -import backtrader as bt - - -class TestStrategy(TestCase): - def test_克来机电_1h(self): - # 引力传媒 - # 添加数据到cerebro - data_a = ArrowData( - exchange="emconcept", - symbol="BK1090", - freq="1h", - start_date=datetime.datetime(2024, 1, 2), - end_date=datetime.datetime(2024, 12, 12), - ) - data_b = ArrowData( - exchange="as", - symbol="603960", - freq="1h", - start_date=datetime.datetime(2024, 1, 2), - end_date=datetime.datetime(2024, 12, 12), - ) - - cerebro = get_cerebro([data_a, data_b]) - cerebro = bt.Cerebro() - cerebro.adddata(data_a) - cerebro.adddata(data_b) - - cerebro.addstrategy(ComparativeStrengthReboundStrategy) # 添加策略 - - result = cerebro.run() - hit_list = list(result[0].orders.hit_dt.astype(str).values.tolist()) - self.assertIn("2024-02-01 10:30:00", hit_list, "hit") - def test_中视传媒_1h(self): - # 引力传媒 - # 添加数据到cerebro - data_a = ArrowData( - exchange="emindustry", - symbol="BK0486", - freq="1h", - start_date=datetime.datetime(2024, 1, 1), - end_date=datetime.datetime(2024, 12, 12), - ) - data_b = ArrowData( - exchange="as", - symbol="600088", - freq="1h", - start_date=datetime.datetime(2024, 1, 1), - end_date=datetime.datetime(2024, 12, 12), - ) - - cerebro = get_cerebro([data_a, data_b]) - cerebro = bt.Cerebro() - cerebro.adddata(data_a) - cerebro.adddata(data_b) - - cerebro.addstrategy(ComparativeStrengthReboundStrategy) # 添加策略 - - result = cerebro.run() - hit_list = list(result[0].orders.hit_dt.astype(str).values.tolist()) - self.assertIn("2024-01-23 09:30:00", hit_list, "hit") diff --git "a/strategy_\346\236\201\345\200\274\345\257\271\346\257\224_test.py" "b/strategy_\346\236\201\345\200\274\345\257\271\346\257\224_test.py" index b86a51d1..c5517733 100644 --- "a/strategy_\346\236\201\345\200\274\345\257\271\346\257\224_test.py" +++ "b/strategy_\346\236\201\345\200\274\345\257\271\346\257\224_test.py" @@ -1,20 +1,9 @@ from unittest import TestCase -import backtrader as bt import datetime -import pandas as pd -import numpy as np -import os, sys -import copy -import math -import warnings - -from MyTT import * -from formula import * -from data import MySQLData, new_data -from printAnalyzer import printTradeAnalysis -from strategy import run_formula, buys_time +from data import new_data +from strategy import run_formula from strategy_极值对比 import Strategy diff --git a/test_formula.py b/test_formula.py deleted file mode 100644 index 85f0bfd5..00000000 --- a/test_formula.py +++ /dev/null @@ -1,62 +0,0 @@ -from unittest import TestCase -import backtrader as bt - -import datetime -import pandas as pd -import numpy as np -import os, sys -import copy - -import math -import warnings - -from MyTT import * -from formula import * -from data import MySQLData -from printAnalyzer import printTradeAnalysis - - -class TestFormula(TestCase): - def test_f_底部连阳上穿均线_1(self): - # 添加数据到cerebro - data = MySQLData( - "kline_ashare", - symbol="002995", - contract_type="spot", - start_date=datetime.datetime(2023, 1, 1), - end_date=datetime.datetime(2023, 6, 12), - interval="30m", - ) - result=self.run_formula("反弹至均线后下跌不创新低", data) - self.assertIn("2020-01-01 19:00:00",result,"hit") - def test_f_底部连阳上穿均线_300081(self): - # 添加数据到cerebro - data = MySQLData( - "kline_ashare", - symbol="300081", - contract_type="spot", - start_date=datetime.datetime(2023, 1, 1), - end_date=datetime.datetime(2023, 6, 12), - interval="1h", - ) - result=self.run_formula("反弹至均线后下跌不创新低", data) - self.assertIn("2020-01-01 19:00:00",result,"hit") - - def run_formula(self, func_name, data): - cerebro = bt.Cerebro() - - # 添加数据到cerebro - cerebro.adddata(data) - - print("加载数据完毕") - # 添加手续费,按照万分之二收取 - cerebro.broker.setcommission(commission=0.0002, stocklike=True) - # 设置初始资金为100万 - cerebro.broker.setcash(1_0000_0000) - # 添加策略 - cerebro.addstrategy(FormulaStrategy) - - cerebro.addanalyzer(bt.analyzers.PyFolio, timeframe=bt.TimeFrame.Minutes, compression=None) - # 运行回测 - backtest_result = cerebro.run() - return backtest_result diff --git "a/test_strategy_ma60\345\272\225\350\203\214\351\251\260.py" "b/test_strategy_ma60\345\272\225\350\203\214\351\251\260.py" index 19e47f65..14491880 100644 --- "a/test_strategy_ma60\345\272\225\350\203\214\351\251\260.py" +++ "b/test_strategy_ma60\345\272\225\350\203\214\351\251\260.py" @@ -1,20 +1,8 @@ from unittest import TestCase -import backtrader as bt -import datetime -import pandas as pd -import numpy as np -import os, sys -import copy - -import math -import warnings - -from MyTT import * from formula import * from data import KlineData -from printAnalyzer import printTradeAnalysis -from strategy import run_formula, buys_time +from strategy import run_formula from strategy_ma60底背驰 import Strategy diff --git "a/test_strategy_\344\270\211\346\256\265\345\272\225\350\203\214\351\251\260.py" "b/test_strategy_\344\270\211\346\256\265\345\272\225\350\203\214\351\251\260.py" index ea09b3d7..eb7a08cb 100644 --- "a/test_strategy_\344\270\211\346\256\265\345\272\225\350\203\214\351\251\260.py" +++ "b/test_strategy_\344\270\211\346\256\265\345\272\225\350\203\214\351\251\260.py" @@ -1,20 +1,9 @@ from unittest import TestCase -import backtrader as bt import datetime -import pandas as pd -import numpy as np -import os, sys -import copy -import math -import warnings - -from MyTT import * -from formula import * from data import KlineData -from printAnalyzer import printTradeAnalysis -from strategy import run_formula, buys_time +from strategy import run_formula from strategy_三段底背驰 import Strategy diff --git "a/test_strategy_\344\270\213\350\267\214\346\217\220\345\211\215\344\274\201\347\250\263.py" "b/test_strategy_\344\270\213\350\267\214\346\217\220\345\211\215\344\274\201\347\250\263.py" index 6790b94e..60c57718 100644 --- "a/test_strategy_\344\270\213\350\267\214\346\217\220\345\211\215\344\274\201\347\250\263.py" +++ "b/test_strategy_\344\270\213\350\267\214\346\217\220\345\211\215\344\274\201\347\250\263.py" @@ -1,20 +1,9 @@ from unittest import TestCase -import backtrader as bt import datetime -import pandas as pd -import numpy as np -import os, sys -import copy -import math -import warnings - -from MyTT import * -from formula import * from data import KlineData -from printAnalyzer import printTradeAnalysis -from strategy import run_formula, buys_time +from strategy import run_formula from strategy_下跌提前企稳 import Strategy diff --git "a/test_strategy_\345\272\225\351\203\250\350\277\236\351\230\263_\345\233\236\350\270\251_\347\252\201\347\240\264.py" "b/test_strategy_\345\272\225\351\203\250\350\277\236\351\230\263_\345\233\236\350\270\251_\347\252\201\347\240\264.py" index 6ebc9043..a5ecf3a3 100644 --- "a/test_strategy_\345\272\225\351\203\250\350\277\236\351\230\263_\345\233\236\350\270\251_\347\252\201\347\240\264.py" +++ "b/test_strategy_\345\272\225\351\203\250\350\277\236\351\230\263_\345\233\236\350\270\251_\347\252\201\347\240\264.py" @@ -1,20 +1,9 @@ from unittest import TestCase -import backtrader as bt import datetime -import pandas as pd -import numpy as np -import os, sys -import copy -import math -import warnings - -from MyTT import * -from formula import * from data import KlineData -from printAnalyzer import printTradeAnalysis -from strategy import run_formula, buys_time +from strategy import run_formula from strategy_底部连阳_回踩_突破 import Strategy diff --git "a/test_strategy_\345\272\225\351\203\250\350\277\236\351\230\263_\345\233\236\350\270\251_\347\252\201\347\240\264_ban.py" "b/test_strategy_\345\272\225\351\203\250\350\277\236\351\230\263_\345\233\236\350\270\251_\347\252\201\347\240\264_ban.py" index 277e623d..6ee66749 100644 --- "a/test_strategy_\345\272\225\351\203\250\350\277\236\351\230\263_\345\233\236\350\270\251_\347\252\201\347\240\264_ban.py" +++ "b/test_strategy_\345\272\225\351\203\250\350\277\236\351\230\263_\345\233\236\350\270\251_\347\252\201\347\240\264_ban.py" @@ -1,20 +1,9 @@ from unittest import TestCase -import backtrader as bt import datetime -import pandas as pd -import numpy as np -import os, sys -import copy -import math -import warnings - -from MyTT import * -from formula import * from data import MySQLData -from printAnalyzer import printTradeAnalysis -from strategy import run_formula, buys_time +from strategy import run_formula from strategy_底部连阳_回踩_突破 import Strategy