from tqz_strategy.template import CtaTemplate
from public_module.object import BarData
class TQZStockDoubleMaStrategy(CtaTemplate):
"""
re write.
"""
author = "tqz"
fast_window = 30
slow_window = 250
strategy_money = 0
parameters = ["fast_window", "slow_window", "strategy_money"]
fast_ma0 = 0
fast_ma1 = 0
slow_ma0 = 0
slow_ma1 = 0
variables = ["fast_ma0", "fast_ma1", "slow_ma0", "slow_ma1"]
def __init__(self, cta_engine, strategy_name, vt_symbol, setting):
""""""
super().__init__(cta_engine, strategy_name, vt_symbol, setting)
self.bar_close_prices = []
def on_init(self):
"""
Callback when strategy is inited.
"""
self.write_log(msg=f'strategy_name: {self.strategy_name} on_init.')
pass
def on_start(self):
"""
Callback when strategy is started.
"""
self.write_log(msg=f'strategy_name: {self.strategy_name} on_start.')
pass
def on_stop(self):
"""
Callback when strategy is stopped.
"""
self.write_log(msg=f'strategy_name: {self.strategy_name} on_stop.')
pass
def update_bars_close_prices(self, new_bar: BarData) -> bool:
self.bar_close_prices.append(new_bar.close_price)
if len(self.bar_close_prices) > self.slow_window:
# update params
self.slow_ma0 = sum(self.bar_close_prices[-self.slow_window:]) / self.slow_window
self.slow_ma1 = sum(self.bar_close_prices[-self.slow_window-1:-1]) / self.slow_window
self.fast_ma0 = sum(self.bar_close_prices[-self.fast_window:]) / self.fast_window
self.fast_ma1 = sum(self.bar_close_prices[-self.fast_window-1:-1]) / self.fast_window
bars_is_enough = True
else:
bars_is_enough = False
return bars_is_enough
def on_bar(self, bar: BarData):
"""
Callback of new bar data update.
"""
# 1. update self.bars_close_prices & update params.
if self.update_bars_close_prices(new_bar=bar) is False:
return
# 2. trend direction.
cross_over = self.fast_ma0 > self.slow_ma0 and self.fast_ma1 < self.slow_ma1
cross_below = self.fast_ma0 < self.slow_ma0 and self.fast_ma1 > self.slow_ma1
# 3. modify postion.
if cross_over:
if self.pos == 0:
self.buy(bar.close_price, self.get_current_pos(bar_close_price=bar.close_price))
elif self.pos < 0:
self.cover(bar.close_price, abs(self.pos))
self.buy(bar.close_price, self.get_current_pos(bar_close_price=bar.close_price))
elif cross_below:
self.sell(bar.close_price, abs(self.pos))
print(f'DoubleMaStrategy, bar.datetime: {bar.datetime}, bar.close_price: {bar.close_price}, self.fast_ma0: {round(self.fast_ma0, 3)}, self.fast_ma1: {round(self.fast_ma1, 3)}, self.slow_ma0: {round(self.slow_ma0, 3)}, self.slow_ma1: {round(self.slow_ma1, 3)}, pos: {self.pos}')
self.put_event()
def get_current_pos(self, bar_close_price: float):
return int((self.strategy_money / bar_close_price) / self.get_min_stock_lots()) * self.get_min_stock_lots()
量化交易之回测篇 - 测试用的策略demo(双均线策略股票版)
原创ErwinSmith 博主文章分类:策略回测 ©著作权
©著作权归作者所有:来自51CTO博客作者ErwinSmith的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
量化交易开发之基本语法(三)
本教程则是以量化的情景从零讲解python编程,所以将更适合想学做量化策略的人。
数据 变量名 python -
量化交易开发之函数API(四)
我们讲解一下python中的函数知识
API 数据 python -
量化交易开发之初识量化(一)
本系列课程将开启手把手保姆级实战课程,开发属于你自己的量化策略!!!
量化交易 策略因子 实战教学 -
Python量化交易实战-38使用开源项目回测双均线策略
使用PyAlgoTrade回测双均线策略双均线策略:长短周期均线
analyzer 数据 git -
量化交易之回测篇 - 套利策略模型(初版)
fro
回测 List -
量化交易实战【1】自己搭建一个的股票交易回测框架,并通过均线择时策略进行回测
本文介绍了一个股票交易的基础回测框架,并且通过均线择时策略,详细的演示了策略搭建的过程,和大家共同学习交流。
python 量化交易 量化交易框架 择时策略 均线策略