Rss & SiteMap

金字塔客服中心 - 专业程序化交易软件提供商 http://www.weistock.com/bbs/

专业程序化软件提供商
共2 条记录, 每页显示 10 条, 页签: [1]
[浏览完整版]

标题:版主我自己模仿写的python策略怎么回测无交易记入

1楼
股票小白 发表于:2019/12/2 15:23:15
import time
import os
import csv
import numpy
import talib as ta

def init(context):
    context.s1 = "SH600152"
    context.long_period = 20
    context.short_period = 5
    
    

# before_trading此函数会在每天基准合约的策略交易开始前被调用,当天只会被调用一次。--(选择实现)
def before_trading(context):
    pass


# 你选择的品种的数据更新将会触发此段逻辑,例如日或分钟历史数据切片或者是实时数据切片更新。--(必须实现)
def handle_bar(context):
    close = history_bars(context.s1,context.long_period,'self','close',True)
    if len(close) < context.long_period+1:
        return 
    ma5 = ta.SMA(close,context.short_period)  
    ma20 = ta.SMA(close,context.long_period)
        
    if ma5[-1]>ma20[-1] and ma5[-2]<ma20[-2]:
        buy_open(context.s1, "market", volume=100)
    if ma5[-1]<ma20[-1] and ma5[-2]>ma20[-2]:
        portfolio = get_portfolio(context.s1,0)
        sell_close(context.s1,"market", volume=portfolio.buy_quantity)
    
    
    
    
        #buy_open(context.s1, "Market", volume = 100)    #  市价开多
    #buy_open(context.s1, "Limit", 25.45, 100)       #  限价开多
    
    
# after_trading函数会在每天交易结束后被调用,当天只会被调用一次。 --(选择实现)
def after_trading(context):
    pass
2楼
yukizzc 发表于:2019/12/2 16:16:13
回测周期选的对不对。,历史是否有数据
建议自己勤用print输出下信息
共2 条记录, 每页显示 10 条, 页签: [1]


Powered By Dvbbs Version 8.3.0
Processed in 0.02930 s, 2 queries.