Skip to content

Commit

Permalink
add example
Browse files Browse the repository at this point in the history
  • Loading branch information
51bitquant committed Jul 13, 2022
1 parent 99f2dd0 commit cde5eee
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 5 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,4 @@ howtrader/log/*
main_window.spec
*.json
*.db
examples/howtrader/*
strategies
examples/howtrader/*
6 changes: 5 additions & 1 deletion docs/howtrader文档说明.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@

2. [如何在linux上运行howtrader](./如何在linux上运行howtrader.md)

3. [如何添加自定义策略](./如何添加自定义策略.md)
3. [如何添加自定义策略](./如何添加自定义策略.md)

4. [策略重启时如何获取策略保存中的数据](./策略重启时如何获取策略保存中的数据.md)

5. [如何对接tradingview等三方交易信号](./如何对接tradingview等三方交易信号.md)
Empty file.
5 changes: 3 additions & 2 deletions examples/backtest_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from howtrader.trader.object import Interval
from datetime import datetime
from strategies.atr_rsi_strategy import AtrRsiStrategy # 要导入你回测的策略,你自己开发的。
from strategies.atr_rsi_15min_strategy import AtrRsi15MinStrategy

engine = BacktestingEngine()
engine.set_parameters(
Expand All @@ -16,8 +17,8 @@
capital=1000000,
)

engine.add_strategy(AtrRsiStrategy, {})

# engine.add_strategy(AtrRsiStrategy, {})
engine.add_strategy(AtrRsi15MinStrategy, {})

engine.load_data()
engine.run_backtesting()
Expand Down
146 changes: 146 additions & 0 deletions examples/strategies/atr_rsi_15min_strategy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
from howtrader.app.cta_strategy import (
CtaTemplate,
StopOrder
)

from howtrader.trader.object import TickData, BarData, TradeData, OrderData
from howtrader.trader.utility import BarGenerator, ArrayManager
from decimal import Decimal


class AtrRsi15MinStrategy(CtaTemplate):
""""""

author = "51bitquant"

atr_length = 22
atr_ma_length = 10
rsi_length = 5
rsi_entry = 16
trailing_percent = 0.8
fixed_size = 1

atr_value = 0
atr_ma = 0
rsi_value = 0
rsi_buy = 0
rsi_sell = 0
intra_trade_high = 0
intra_trade_low = 0

parameters = [
"atr_length",
"atr_ma_length",
"rsi_length",
"rsi_entry",
"trailing_percent",
"fixed_size"
]
variables = [
"atr_value",
"atr_ma",
"rsi_value",
"rsi_buy",
"rsi_sell",
"intra_trade_high",
"intra_trade_low"
]

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, 15, self.on_15min_bar)
self.am = ArrayManager()

def on_init(self):
"""
Callback when strategy is inited.
"""
self.write_log("策略初始化")

self.rsi_buy = 50 + self.rsi_entry
self.rsi_sell = 50 - self.rsi_entry

self.load_bar(10)

def on_start(self):
"""
Callback when strategy is started.
"""
self.write_log("策略启动")

def on_stop(self):
"""
Callback when strategy is stopped.
"""
self.write_log("策略停止")

def on_tick(self, tick: TickData):
"""
Callback of new tick data update.
"""
self.bg.update_tick(tick)

def on_bar(self, bar: BarData):
"""
Callback of new bar data update.
"""
self.bg.update_bar(bar)

def on_15min_bar(self, bar: BarData):
self.cancel_all()

am = self.am
am.update_bar(bar)
if not am.inited:
return

atr_array = am.atr(self.atr_length, array=True)
self.atr_value = atr_array[-1]
self.atr_ma = atr_array[-self.atr_ma_length:].mean()
self.rsi_value = am.rsi(self.rsi_length)

if self.pos == 0:
self.intra_trade_high = bar.high_price
self.intra_trade_low = bar.low_price

if self.atr_value > self.atr_ma:
if self.rsi_value > self.rsi_buy:
price = bar.close_price * 1.01
self.buy(Decimal(price), Decimal(self.fixed_size))
elif self.rsi_value < self.rsi_sell:
price = bar.close_price * 0.99
self.short(Decimal(price), Decimal(self.fixed_size))

elif self.pos > 0:
self.intra_trade_high = max(self.intra_trade_high, bar.high_price)
self.intra_trade_low = bar.low_price
long_stop = self.intra_trade_high * (1 - self.trailing_percent / 100)
self.sell(Decimal(long_stop), Decimal(abs(self.pos)), stop=True)

elif self.pos < 0:
self.intra_trade_low = min(self.intra_trade_low, bar.low_price)
self.intra_trade_high = bar.high_price

short_stop = self.intra_trade_low * (1 + self.trailing_percent / 100)
self.cover(Decimal(short_stop), Decimal(abs(self.pos)), stop=True)

self.put_event()

def on_order(self, order: OrderData):
"""
Callback of new order data update.
"""
pass

def on_trade(self, trade: TradeData):
"""
Callback of new trade data update.
"""
self.put_event()

def on_stop_order(self, stop_order: StopOrder):
"""
Callback of stop order update.
"""
pass

0 comments on commit cde5eee

Please sign in to comment.