【发布时间】:2018-06-13 15:00:29
【问题描述】:
我正在尝试使用本教程中描述的算法交易:
https://www.oreilly.com/learning/algorithmic-trading-in-less-than-100-lines-of-python-code
class MomentumTrader(opy.Streamer):
def __init__(self, momentum, *args, **kwargs):
opy.Streamer.__init__(self, *args, **kwargs)
self.ticks = 0
self.position = 0
self.df = pd.DataFrame()
self.momentum = momentum
self.units = 100000
def create_order(self, side, units):
order = oanda.create_order(config["oanda"]["account_id"],
instrument="EUR_USD", units=units, side=side,
type="market")
print("\n", order)
def on_success(self, data):
self.ticks += 1
# print(self.ticks, end=", ")
# appends the new tick data to the DataFrame object
self.df = self.df.append(pd.DataFrame(data["tick"],
index=[data["tick"]["time"]]))
# transforms the time information to a DatetimeIndex object
self.df.index = pd.DatetimeIndex(self.df["time"])
# resamples the data set to a new, homogeneous interval
dfr = self.df.resample("5s").last()
# calculates the log returns
dfr["returns"] = np.log(dfr["ask"] / dfr["ask"].shift(1))
# derives the positioning according to the momentum strategy
dfr["position"] = np.sign(dfr["returns"].rolling(
self.momentum).mean())
if dfr["position"].ix[-1] == 1:
# go long
if self.position == 0:
self.create_order("buy", self.units)
elif self.position == -1:
self.create_order("buy", self.units * 2)
self.position = 1
elif dfr["position"].ix[-1] == -1:
# go short
if self.position == 0:
self.create_order("sell", self.units)
elif self.position == 1:
self.create_order("sell", self.units * 2)
self.position = -1
if self.ticks == 250:
# close out the position
if self.position == 1:
self.create_order("sell", self.units)
elif self.position == -1:
self.create_order("buy", self.units)
self.disconnect()
mt = MomentumTrader(momentum=12, environment="practice",
access_token=config["oanda"]["access_token"])
mt.rates(account_id=config["oanda"]["account_id"],
instruments=["DE30_EUR"],
ignore_heartbeat=True)
我收到了这个错误:
JSONDecodeError:期望用双引号括起来的属性名称:第 1 行第 2 列(字符 1)
我想我需要使用 oandapyv20,但没有任何备忘单可以指导我如何将这些简单的代码 sn-ps 迁移到新的 api。
那么应该用什么来代替“mt.rates”呢?
【问题讨论】:
标签: python trading algorithmic-trading