当前位置:网站首页>Back test of quantitative trading - example of futures CTA strategy (tqzfuturerenkoscalpingstrategy)
Back test of quantitative trading - example of futures CTA strategy (tqzfuturerenkoscalpingstrategy)
2022-06-25 12:59:00 【Post-Truth】
from math import ceil, floor
from tqz_strategy.template import CtaTemplate
from public_module.object import BarData, RenkoData
from public_module.constant import RenkoDirection
from public_module.utility import BarGenerator
class TQZFutureRenkoScalpingStrategy(CtaTemplate):
"""
future strategy(1h period). abandoned
"""
author = "tqz"
# --- param part ---
fast_window = 30
slow_window = 250
lots_size = 0
renko_size = 0
min_tick_price_flow = 0
parameters = ["fast_window", "slow_window", "lots_size", "renko_size", "min_tick_price_flow"]
# --- var part ---
fast_ma_value = 0.0
# fast_ma1 = 0.0
slow_ma_value = 0.0
# slow_ma1 = 0.0
variables = ["fast_ma_value", "slow_ma_value"]
def __init__(self, cta_engine, strategy_name, vt_symbol, setting):
""""""
super().__init__(cta_engine, strategy_name, vt_symbol, setting)
self.bg = BarGenerator(self.on_bar)
self.bar_close_prices = []
self.renko_list = []
self.first_bar_close_price = 0
def on_bar(self, bar: BarData):
"""
Callback of new bar data update.
"""
# 1. update self.bars_close_prices & update params.
if self.__update_params_ok(new_bar=bar) is False:
return
# 2. trend direction.
long_direction = self.fast_ma_value > self.slow_ma_value
short_direction = self.fast_ma_value < self.slow_ma_value
# 3. modify postion.
last_renko0 = self.renko_list[-1]
last_renko1 = self.renko_list[-2]
if long_direction:
if self.pos == 0:
if last_renko1.renko_direction == RenkoDirection.SHORT and last_renko0.renko_direction == RenkoDirection.LONG:
self.set_position(pos=self.lots_size)
elif self.pos < 0:
if last_renko1.renko_direction == RenkoDirection.SHORT and last_renko0.renko_direction == RenkoDirection.LONG:
self.set_position(pos=self.lots_size)
else:
self.set_position(pos=0)
elif self.pos > 0:
if last_renko1.renko_direction == RenkoDirection.LONG and last_renko0.renko_direction == RenkoDirection.SHORT:
self.set_position(pos=0)
elif short_direction:
if self.pos == 0:
if last_renko1.renko_direction == RenkoDirection.LONG and last_renko0.renko_direction == RenkoDirection.SHORT:
self.set_position(pos=-1 * self.lots_size)
elif self.pos > 0:
if last_renko1.renko_direction == RenkoDirection.LONG and last_renko0.renko_direction == RenkoDirection.SHORT:
self.set_position(pos=-1 * self.lots_size)
else:
self.set_position(pos=0)
elif self.pos < 0:
if last_renko1.renko_direction == RenkoDirection.SHORT and last_renko0.renko_direction == RenkoDirection.LONG:
self.set_position(pos=0)
def __update_params_ok(self, new_bar: BarData) -> bool:
if len(self.bar_close_prices) < self.slow_window:
self.bar_close_prices.append(new_bar.close_price)
self.__update_renko_list(new_bar=new_bar)
return False
# update self.bar_close_prices
self.bar_close_prices.remove(self.bar_close_prices[0])
self.bar_close_prices.append(new_bar.close_price)
# update fast_ma & slow_ma
self.fast_ma_value = sum(self.bar_close_prices[-self.fast_window:]) / self.fast_window
self.slow_ma_value = sum(self.bar_close_prices[-self.slow_window:]) / self.slow_window
# update renko_list
self.__update_renko_list(new_bar=new_bar)
return True
def __update_renko_list(self, new_bar: BarData):
if len(self.renko_list) is 0:
if self.first_bar_close_price is 0: # init strategy.
self.first_bar_close_price = new_bar.close_price
else:
""" Determine whether the generation of the first renko Conditions """
ticks_diff = (new_bar.close_price - self.first_bar_close_price) / self.min_tick_price_flow
if ticks_diff > self.renko_size:
""" Update the first renko In red """
renko_counts = floor(ticks_diff / self.renko_size)
renko_price = self.first_bar_close_price + renko_counts * self.renko_size * self.min_tick_price_flow
self.renko_list.append(RenkoData(renko_price=renko_price, renko_direction=RenkoDirection.LONG, renko_value=renko_counts))
elif ticks_diff < -1 * self.renko_size:
""" Update the first renko It's green """
renko_counts = ceil(ticks_diff / self.renko_size)
renko_price = self.first_bar_close_price + renko_counts * self.renko_size * self.min_tick_price_flow
self.renko_list.append(RenkoData(renko_price=renko_price, renko_direction=RenkoDirection.SHORT, renko_value=renko_counts))
else:
last_renko = self.renko_list[-1]
ticks_diff = (new_bar.close_price - last_renko.renko_price) / self.min_tick_price_flow
if last_renko.renko_direction == RenkoDirection.LONG: # Currently, it is red brick
if ticks_diff > self.renko_size:
""" Add red bricks """
renko_counts = floor(ticks_diff / self.renko_size)
renko_price = last_renko.renko_price + renko_counts * self.renko_size * self.min_tick_price_flow
self.renko_list.append(RenkoData(renko_price=renko_price, renko_direction=RenkoDirection.LONG, renko_value=last_renko.renko_value+renko_counts))
elif ticks_diff < -2 * self.renko_size:
""" New green brick """
renko_counts = ceil(ticks_diff / self.renko_size)
renko_price = last_renko.renko_price + renko_counts * self.renko_size * self.min_tick_price_flow
self.renko_list.append(RenkoData(renko_price=renko_price, renko_direction=RenkoDirection.SHORT, renko_value=renko_counts+1))
elif last_renko.renko_direction == RenkoDirection.SHORT: # Currently green bricks
if ticks_diff < -1 * self.renko_size:
""" New green brick """
renko_counts = ceil(ticks_diff / self.renko_size)
renko_price = last_renko.renko_price + renko_counts * self.renko_size * self.min_tick_price_flow
self.renko_list.append(RenkoData(renko_price=renko_price, renko_direction=RenkoDirection.SHORT, renko_value=last_renko.renko_value+renko_counts))
elif ticks_diff > 2 * self.renko_size:
""" Add red bricks """
renko_counts = floor(ticks_diff / self.renko_size)
renko_price = last_renko.renko_price + renko_counts * self.renko_size * self.min_tick_price_flow
self.renko_list.append(RenkoData(renko_price=renko_price, renko_direction=RenkoDirection.LONG, renko_value=renko_counts-1))
def set_position(self, pos: int):
self.pos = pos
def on_init(self):
"""
Callback when strategy is inited.
"""
self.write_log(msg=f'strategy_name: {self.strategy_name}, fast_window: {self.fast_window}, slow_window: {self.slow_window}, lots_size: {self.lots_size}, renko_size: {self.renko_size}, min_tick_price_flow: {self.min_tick_price_flow} 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.')
边栏推荐
- Differences between JS and JQ operation objects
- 量化交易之回测篇 - 期货CTA策略实例(TQZFutureRenkoScalpingStrategy)
- LeetCode链表题解技巧归纳总结
- My first experience of go+ language -- a collection of notes on learning go+ design architecture
- Optimal solution for cold start
- 地理空间搜索:kd树的实现原理
- 美创入选“2022 CCIA中国网络安全竞争力50强”榜单
- Circular exercises of JS
- @Scheduled implementation of scheduled tasks (concurrent execution of multiple scheduled tasks)
- 用include what you use拯救混乱的头文件
猜你喜欢

架构师必备的七种能力
![[visio] solving the fuzzy problem of parallelogram in word](/img/04/8a1de2983d648e67f823b5d973c003.png)
[visio] solving the fuzzy problem of parallelogram in word

更新pip&下载jupyter lab

Serevlt初识

Connect with the flight book and obtain the user information according to the userid

My first experience of go+ language -- a collection of notes on learning go+ design architecture

Render values to corresponding text

2021-09-28

二叉树之_哈夫曼树_哈弗曼编码

Geospatial search - > R tree index
随机推荐
词法陷阱(C)
2021-10-21
剑指 Offer 第 1 天栈与队列(简单)
CUDA error: unspecified launch failure
Optimal solution for cold start
Methods of strings in JS charat(), charcodeat(), fromcharcode(), concat(), indexof(), split(), slice(), substring()
[转]以终为始,详细分析高考志愿该怎么填
阿里稳定性之故障应急处理流程
@Scheduled implementation of scheduled tasks (concurrent execution of multiple scheduled tasks)
MySQL writes user-defined functions and stored procedure syntax (a detailed case is attached, and the problem has been solved: errors are reported when running user-defined functions, and errors are r
Command line garbled
list.replace, str.append
出手即不凡,这很 Oracle!
Resolved: could not find artifact XXX
剑指Offer 第 2 天链表(简单)
Lexical trap
字节跳动Dev Better技术沙龙来啦!参与活动赢好礼,限时免费报名中!
Meichuang was selected into the list of "2022 CCIA top 50 Chinese network security competitiveness"
JSTL tag: fmt:formatdate tag format Chinese standard time or timestamp
Alibaba stability fault emergency handling process