【发布时间】:2017-10-13 01:41:13
【问题描述】:
我目前正在编写一个小型 Python 程序来使用他们的 API 在 1Broker.com 上查看我当前的交易,它应该列出所有未平仓头寸,使用计数器对其进行编号,然后更新选择的统计数据(盈亏百分比和 position_id ) 在每次运行时,在睡眠定时器之后。
我在一个肮脏的早期草稿中工作,但是决定“优化”并清理我的代码......并且破坏了它。 以下是错误部分的摘录:
if self.total_open_positions != []:
counter = int("0")
for position in self.total_open_positions:
counter += 1
display = {}
display["Open Order"] = str(counter)
display["ID"] = str(position["position_id"])
display["P/L Percent"] = str(position["profit_loss_percent"])
print display
time.sleep(timer - ((time.time() - starttime) % timer))
if self.total_positions == []:
print "All trades closed"
P/L percent 和 ID 中的数据在每次循环时都拒绝更新。
提前感谢任何提供帮助的人:)
“拒绝更新”是指当程序运行时,它会根据需要检索 P/L 百分比。然而,每个连续循环打印相同的盈亏百分比。这意味着 str(position["profit_loss_percent"]) 的值尚未更新为从网站检索到的最新数据。 (例如,显示 3%,此时交易量现在高达 6%)
左图是第一稿。但是,您看,P/L% 在每次迭代中都会发生变化。而在右图中它保持不变。
至于self.total_open_positions,等于我的api请求:
self.total_open_positions = requests.get(API_URL)
这里有一个至少可以正常工作的“肮脏”版本的 sn-p,也许它会帮助显示我的意图和我的菜鸟级技能(这就是我需要帮助的原因哈哈):
total_open_orders = open_orders["response"]
while total_open_orders == []:
print "Checking again......"
time.sleep(timer1 - ((time.time() - starttime) % timer1))
else:
#When orders are found, loop through and display
while True:
#Wait desired time between refreshing stats
time.sleep(timer1 - ((time.time() - starttime) % timer1))
#Position number (oldest first)
counter = 0
##Print a small seperator between refreshes
print "#" * 20
#Loop through list of positions and print stats for each
for order in total_open_orders:
#Add to counter for each position
counter += 1
#Display stats to user (anything in '[]' is JSON format
try:
print "#" * 40
print "Open Order #: " + str(counter)
print "ID #: " + str(order["position_id"])
print "Market: " + str(order["symbol"])
print "Entry: " + str(order["entry_price"])
print "Stop Loss: " + str(order["stop_loss"])
print "Take Profit: " + str(order["take_profit"])
print "P/L: " + str(order["profit_loss_percent"])
print "#" * 40
print ""
#Catch any connection errors and print for debugging
except Exception as e:
print e
【问题讨论】:
-
拒绝更新是什么意思?您能否展示一个示例输出 - 以及您的期望。在 for 循环之前你真的不需要
if self.total_open_positions != []:。以及任何不做counter = 0vsint("0")的理由 -
它在哪里抛出
None-type错误? -
因为请求的数据是一个json对象,如果没有未平仓交易,response = [None]。这会导致任何获取 ID 和 P/L 的尝试出错,因为当时它们不存在
-
但如果它是一个空列表,
for循环将永远不会进入。这就是为什么我认为空列表的测试是多余的。 -
int("0")太搞笑了!
标签: python loops dynamic nested