wtpy使用手册

wtpy开源项目的使用


cta策略编写例子

<p>自己编写策略</p> <ol> <li>按照模板编写策略文件 比如建立 空中花园策略 编写 空中花园策略.py,放在Strategies下面 策略如下:</li> </ol> <pre><code class="language-python"> # -*- coding: utf-8 -*- import pandas as pd from wtpy import BaseCtaStrategy from wtpy import CtaContext import numpy as np import math class strategy_fun(BaseCtaStrategy): def __init__(self, name:str, code:str, barCnt:int, period:str): BaseCtaStrategy.__init__(self, name) self.__period__ = period self.__bar_cnt__ = barCnt self.__code__ = code self.__margin_rate__ = 0.15 # 保证金比率 self.__money_pct__ = 0.1 # 每次使用的资金比率 self.__capital__ = 10000000 self.today_entry = 0 # 限制每天开仓次数的参数 self.__cleartimes__=[[1450,1530]] def on_init(self, context:CtaContext): code = self.__code__ #品种代码 context.stra_get_bars(code, 'd1', self.__bar_cnt__, isMain=False) context.stra_get_bars(code, self.__period__, self.__bar_cnt__, isMain = True) context.stra_log_text("SkyGardenStra inited") pInfo = context.stra_get_comminfo(code) self.__volscale__ = pInfo.volscale def on_session_begin(self, context:CtaContext, curTDate:int): self.new_day = 1 self.today_entry = 0 def on_calculate(self, context:CtaContext): code = self.__code__ #品种代码 trdUnit = 1 theCode = code df_bars = context.stra_get_bars(code,'d1', self.__bar_cnt__, isMain=False) closes = df_bars.closes time1=df_bars.bartimes[-1] # 获取昨日收盘价 last_day_close = closes[-1] df_bars = context.stra_get_bars(theCode, self.__period__, self.__bar_cnt__, isMain = True) time2=df_bars.bartimes[-1] context.stra_log_text("time2:"+str(time2)+"time1:"+str(time1)) # 尾盘清仓 curPos = context.stra_get_position(code) curPrice = context.stra_get_price(code) curTime = context.stra_get_time() curDate = context.stra_get_date() bCleared = False for tmPair in self.__cleartimes__: if curTime &gt;= tmPair[0] and curTime &lt;= tmPair[1]: if curPos != 0: # 如果持仓不为0,则要检查尾盘清仓 context.stra_set_position(code, 0, "clear") # 清仓直接设置仓位为0 context.stra_log_text("尾盘清仓") bCleared = True break if bCleared: return if self.new_day == 1: self.cur_open = df_bars.opens[-1] self.cur_high = df_bars.highs[-1] self.cur_low = df_bars.lows[-1] self.new_day = 0 #把策略参数读进来,作为临时变量,方便引用 margin_rate = self.__margin_rate__ money_pct = self.__money_pct__ volscale = self.__volscale__ capital = self.__capital__ trdUnit_price = volscale * margin_rate * curPrice cur_money = capital + context.stra_get_fund_data(0) curPos = context.stra_get_position(code)/trdUnit strG=code+'; '+str(curDate)+' '+str(curTime)+',curPrice:'+str(curPrice) context.stra_log_text(strG) if curPos == 0 and self.today_entry == 0: if curPrice &gt; self.cur_high and self.cur_open &gt; last_day_close*1.01: context.stra_enter_long(code, math.floor(cur_money*money_pct/trdUnit_price),'enterlong') context.stra_log_text('当前价格:%.2f &gt; 昨日收盘价:%.2f*1.01,做多%s手' % (curPrice, last_day_close, \ math.floor(cur_money*money_pct/trdUnit_price))) self.today_entry = 1 return if curPrice &lt; self.cur_low and curPrice &lt; last_day_close * 0.99: context.stra_enter_short(code, math.floor(cur_money*money_pct/trdUnit_price),'entershort') context.stra_log_text('当前价格:%.2f &lt; 昨日收盘价:%.2f * 0.99,做空%s手' % (curPrice, self.cur_low, \ math.floor(cur_money*money_pct/trdUnit_price))) self.today_entry = 1 return </code></pre> <ol> <li>建立run.py文件 如下:</li> </ol> <pre><code class="language-python"> # -*- coding: utf-8 -*- from wtpy import WtBtEngine,EngineType from wtpy.apps import WtBtAnalyst import importlib if __name__ == "__main__": #创建一个运行环境,并加入策略 engine = WtBtEngine(EngineType.ET_CTA) engine.init('../common/', "configbt.yaml") engine.configBacktest(201109260930,202210121500) engine.configBTStorage(mode="csv", path="../storage_tb/") engine.commitBTConfig() strategy_name='空中花园策略' straInfo=importlib.import_module('Strategies.'+ strategy_name).strategy_fun(name='pydt_IF2', code="CFFEX.IF.HOT", barCnt=50, period="m30") engine.set_cta_strategy(straInfo) engine.run_backtest(bAsync=False) analyst = WtBtAnalyst() analyst.add_strategy(strategy_name, folder="./outputs_bt/pydt_IF2/", init_capital=500000, rf=0.02, annual_trading_days=240) analyst.run() #kw = input('press any key to exit\n') engine.release_backtest() </code></pre> <ol> <li>运行结果: <img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=e8e61989c2058aba0fdd7675bc37ee1b" alt="" /></li> </ol>

页面列表

ITEM_HTML