VeighNa量化社区
你的开源社区量化交易平台
fighter's Avatar
Member
离线
33 帖子
声望: 0

谢谢,明白了
郭易燔 wrote:

这里python使用datetime来处理时间数据的,结束日期2021/2/20会在python中补全成2021.2.20 00:00:00,下载数据截止到结束日期,所以回测使用的数据只到前一天。

两次回测参数都一样,一个结束日期是2-19,最后交易日是2-18,一个结束日期是2-20,最后交易日2-19
xiaohe wrote:

那可以贴一下截图看看

description

description

谢谢,这下明白了!
最后一个交易日也没有交易出现,也要把回测期间往后推一天才行,这又是什么原因呢?

郭易燔 wrote:

回测的过程是这样的:先从你的数据库中读取你起止日期之间的数据,然后根据load_bar第一个参数,把第一个参数天数的数据用来初始化。然后会用剩下的数据进行计算。注意这时并不意味着初始化完成,初始化需要100根数据,load_bar数据不足100根,会用之后的数据继续初始化。用来初始化的数据不回进行下单操作。只有初始化完成后才能下单。

实盘中:会从数据源或者数据库中,根据load_bar第一个参数,从当前日期往前下载第一个参数天数的数据,并用来初始化。usedatabase参数是在这里用的,用来选择是否先从数据库中下载数据。同样如果load_bar数据不足100根,那就会等待,直到实盘中产生的数据和load_bar数据的总会达到100了,才会开始下单操作。

你如果非要从某一天开始的话,把开始回测的日期往前挪N个交易日,然后self.load_bar(N)就可以了。

是指回测时没有第一天的交易(符合交易产生条件),如果调整回测范围把日期往前挪一天就有交易了。我没有RQdata,是用的Udata把分钟线数据都下载下来了,self.load_bar(1, Interval.MINUTE, None, True)这里把用database设为了True,理论上回测的第一天就应该有交易了。请问可能是什么问题呢?
xiaohe wrote:

还是没有第一天数据指的是?
load_bar读取的数据只用来计算指标,不会用来交易的。剩下的数据才会用来交易。
https://www.vnpy.com/docs/cn/cta_strategy.html

请问现在最新版的代码中,也是CTP没有数据,就去找RQ吗?现在用的是Udata。回测的时候下载了历史数据,self.load_bar(1, Interval.MINUTE, None, True),把用database改了成True,还是要等一天才有交易,请问可能是什么问题呢?

都搜索不到了

Q:有微信群,QQ群吗?
A:vn.py框架学习群:666359421 ; vn.py框架交流群:262656087

我用self.load_bar(1, Interval.MINUTE, None, True),这样就可以从历史数据中去取数了吧,分钟线的历史数据是下载好了的。但是也不行,还是没有第一天数据。

如果有历史数据的话,就把use_database那里改成 true 可以么?
郭易燔 wrote:

没办法,除非改框架。
load_bar用的就是交易日,无论你是什么周期的数据,都是使用多少个交易日的数据。
load_bar的数据是用来初始化运算的,不进行交易。
load_bar不要使用0天。load_bar数据不足的话,会继续使用后面的数据进行初始化,直到初始化完成。

load_bar()里面的数字是天数
def load_bar(
self,
days: int,
interval: Interval = Interval.MINUTE,
callback: Callable = None,
use_database: bool = False
):
"""
zuck-zhang wrote:

self.load_bar(10) 这里要问一下周期的问题。 如果self.bg是1分钟,那么这里load_bar(10)就是10分钟; 如果是30min合成bar,那么这里load_bar(10)就是10个30分钟把 。 还是说load_bar 代表的是 过去10个交易日的所有分钟数据

https://www.vnpy.com/forum/topic/29608-vnpyru-he-shi-yong-udata
看看是不是这个原因
程群 wrote:

按着 官网 https://udata.hs.net/datas/458/ 获取期货代码列表
填好 token
from hs_udata import set_token,fut_list
set_token(token = 'xxxxxxxxxxxxxxxxxx')
data = fut_list()
print(data.head())
可以获得数据

但是 按照官网 https://udata.hs.net/datas/457/ 获取期货盘后分钟切片(也就是回测用的历史数据)
填好 token
from hs_udata import set_token,fut_quote_minute
set_token(token = 'xxxxxxxxxxxxxxxxxx')
data = fut_quote_minute(en_prod_code = "SC2109.INE"
,begin_date="2021-05-01",end_date="2021-05-20")
print(data.head())

没报错,返回空

description
vnpy数据管理 获取0条

description

在我刚买1年服务时 偶尔 能有数据返回,
是不是我打开的方式不对,还是说总有那么几天服务器要抽风。

郭易燔 wrote:

你数据往前多下几天就可以了
再前几天的数据是有的。但是怎么load_bar把当天第一根分钟数据作为初始化呢

谢谢!我开始以为load_bar是load一根bar。对于分钟回测,应该怎么写呢? 我用了 self.load_bar(0)试了试也没有变化
def load_bar(
self,
days: int,
interval: Interval = Interval.MINUTE,
callback: Callable = None,
use_database: bool = False
):

代码如下,基本上是参照模板改的。
from vnpy_ctastrategy import (
CtaTemplate,
StopOrder,
TickData,
BarData,
TradeData,
OrderData,
BarGenerator,
ArrayManager,
)
import math

class GridStrategy(CtaTemplate):
""""""

author = "Fighter"

#定义参数

initial_price = 1.0
step_price = 1.0 
step_volume = 1.0
max_pos = 4

vt_orderid = ""
pos = 0
trade_price = 0
parameters = ["initial_price", "step_price", "step_volume", "max_pos"]
variables = ["pos", "vt_orderid", "trade_price"]

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.am = ArrayManager()

def on_init(self):
    """
    Callback when strategy is inited.
    """
    self.write_log("策略初始化")
    self.load_bar(1)
    self.pos = 0

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

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

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.
    """

    if abs(bar.close_price - self.trade_price)/self.step_price * 0.8 < 1:
        return

    if self.initial_price > bar.close_price:
        target_trade_distance = (self.initial_price - bar.close_price) / self.step_price

        target_trade_position = min(math.floor(target_trade_distance) * self.step_volume, self.max_pos)

        target_trade_volume = target_trade_position - self.pos

        if target_trade_volume > 0:
            self.buy(bar.close_price, target_trade_volume)
            self.trade_price = bar.close_price

        elif target_trade_volume < 0:
            self.sell(bar.close_price, abs(target_trade_volume))
            self.trade_price = bar.close_price

        if abs(target_trade_position) == self.max_pos:

            self.initial_price = self.initial_price - self.step_price * target_trade_position / 2

    elif self.initial_price < bar.close_price:

        target_trade_distance = (bar.close_price - self.initial_price) / self.step_price
        target_trade_position = - min(math.floor(target_trade_distance) * self.step_volume, self.max_pos)
        target_trade_volume = target_trade_position - self.pos


        if target_trade_volume > 0:
            self.buy(bar.close_price, target_trade_volume)
            self.trade_price = bar.close_price


        elif target_trade_volume < 0:
            self.sell(bar.close_price, abs(target_trade_volume))
            self.trade_price = bar.close_price

        if abs(target_trade_position) == self.max_pos:
            self.initial_price = self.initial_price - self.step_price * target_trade_position / 2




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

自己写了一个策略,回测时总是没有回测起始日数据,请问可能是什么原因呢?按照策略逻辑,肯定是应该有交易的。比如下图,策略从19日开始,首个交易日就是22日,19日无交易,但是我把回测日期调到18日开始,19日就有交易了。
description

description

CSV分钟线也是从VNPY导出来的(另外一台电脑的),新导入了(2.9.0版本)就出错。
不用导入,直接用udata下载的话,也是报这个错误。VNPY是卸载了原来的2.5版本,新安装的2.9版本。请问是怎么回事呢?
Traceback (most recent call last):
File "c:\vnstudio\lib\site-packages\peewee.py", line 3160, in execute_sql
cursor.execute(sql, params or ())
sqlite3.OperationalError: table dbbardata has no column named turnover

xiaohe wrote:

可以自己在策略里打印排查看看
搞定了,谢谢

xiaohe wrote:

可以自己在策略里打印排查看看
请问在回测引擎里面怎么打印呢

写了一个网格交易的策略,回测的时候,最大回撤都无穷大了,请问是怎么回事呢?
description

from vnpy.app.cta_strategy import (
CtaTemplate,
StopOrder,
TickData,
BarData,
TradeData,
OrderData,
BarGenerator,
ArrayManager,
)
import math

class GridStrategy(CtaTemplate):
""""""

author = "Fighter"

#定义参数

initial_price = 1.0
step_price = 1.0 
step_volume = 1.0
max_pos = 4

vt_orderid = ""
pos = 0
parameters = ["initial_price", "step_price", "step_volume", "max_pos"]
variables = ["pos", "vt_orderid"]

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.am = ArrayManager()

def on_init(self):
    """
    Callback when strategy is inited.
    """
    self.write_log("策略初始化")
    self.load_bar(1)
    self.pos = 0

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

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

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)
    #self.cancel_all()

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

    if self.initial_price > bar.close_price:
    # 价格在基准价之上时
    # 计算当前K线收盘价与初始价的距离
        target_buy_distance = (self.initial_price - bar.close_price) / self.step_price
    # 计算当前价位应该持有的仓位,取整。再和最大仓位置比较
        target_buy_position = min(math.floor(target_buy_distance) * self.step_volume, self.max_pos)
    # 当前应该持有的仓位减去原有持仓就是该再买入的仓位
        target_buy_volume = target_buy_position - self.pos
        # Buy when price dropping
        if target_buy_volume > 0:
            self.buy(bar.close_price, target_buy_volume)
    # Sell when price rising
        elif target_buy_volume < 0:
            self.sell(bar.close_price, target_buy_volume)

    elif self.initial_price < bar.close_price:

    # 价格在基准价之下时
        target_buy_distance = (bar.close_price - self.initial_price) / self.step_price
        target_buy_position = - min(math.floor(target_buy_distance) * self.step_volume, self.max_pos)
        target_buy_volume = target_buy_position - self.pos

        # Buy when price dropping
        if target_buy_volume > 0:
            self.buy(bar.close_price, target_buy_volume)
        # Sell when price rising
        elif target_buy_volume < 0:
            self.sell(bar.close_price, target_buy_volume)


    # Update UI
    #self.put_variables_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

就是,该打击。我也上过当

有原 wrote:

fighter wrote:

85行改回来,试着下载股票数据,下载分钟线也是无响应。下载日线就是下载数据零条。
description
https://github.com/youyuanrsq/vnpy_udata/blob/main/vnpy_udata/udata_datafeed.py
我上午排查了一下,这个链接是最终版本了。目前UData只提供分钟数据

用了这个版本,就可以了。谢谢!

© 2015-2022 微信 18391752892
备案服务号:沪ICP备18006526号

沪公网安备 31011502017034号

【用户协议】
【隐私政策】
【免责条款】