4.83K 浏览
0

try:
    turtle.state = json.load(open("turtle_statema.json", "r"))  # 读取数据: 本策略目标净持仓数,上一次开仓价
except FileNotFoundError:
    pass
print("当前持仓数: %d, 上次调仓价: %f" % (turtle.state["position"], turtle.state["last_price"]))
print("    ")
print("移动止损价%f" %turtle.state["baobenprice"])
print("    ")
try:
    turtle.strategy()
finally:
    turtle.api.close()
    json.dump(turtle.state, open("turtle_statema.json", "w"))  # 保存数据

文件运行后不能创建json文件

Shoe X 已回答的问题 2020年10月22日

这是海龟交易策略的示例代码吧?https://doc.shinnytech.com/tqsdk/latest/demo/strategy.html#id10
我刚才跑了一下海龟,没问题的,请问具体报什么错?

成交后,手工关闭运行程序,json文件没有价格的记录,还是nan,只有程序非法推出后才有记录,我是用windows的计划任务每天收盘关闭,开盘启动,这样关闭后成交价没有写入json文件,怎么才能将成交价实时写入json文件,收盘关闭后json文件有记录

0

根据你的描述,一个简单的办法是在每次调仓动作后都写入文件,以免遗漏。

于是,找到仅有的一处target_pos.set_target_volume(),它出现在def set_position()中,

例如这样:

def set_position(self, pos):
    self.state["position"] = pos
    self.state["last_price"] = self.quote["last_price"]
    self.target_pos.set_target_volume(self.state["position"])
    # 保存数据
    with open("turtle_state.json", "w") as n:
        json.dump(self.state, n)

Shoe X 已回答的问题 2020年10月22日
您正在查看1个答案中的1个,单击此处查看所有答案。