试图索引最后一个值,但它给我带来了整个列表。
我在 Python 中制定了一个股票市场策略。我用以下方式计算 Williams %R 的指标:
def williamsCalculator(highest, lowest, closing_prices):
williams_list = [0] * len(highest)
highest_high = highest.max()
highest_high = float(highest_high)
lowest_low = lowest.min()
lowest_low = float(lowest_low)
last_close = closing_prices[0]
#We calculate the Williams %R and we save it in one dataframe.
williams = ((last_close - highest_high) / (highest_high - lowest_low)) * 100
williams_list[1:] = williams_list[:-1]
williams_list[0] = williams
return williams_list
使用函数和库 CCXT,我从一只股票/加密货币中获取最近 20 分钟的数据,然后我计算了 Williams。
exchange = ccxt.binance({"Public Key": "XXXXX", "Secret Key": "XXXXX"})
prices = exchange.fetch_ohlcv(symbol=market, timeframe="1m", limit=20)
highest_prices = [candle[2] for candle in prices]
highest = pd.DataFrame(highest_prices)
lowest_prices = [candle[3] for candle in prices]
lowest = pd.DataFrame(lowest_prices)
closing_prices = [candle[4] for candle in prices]
closing = pd.DataFrame(closing_prices)
现在我将列表的值赋给一个变量并尝试索引列表的最后一个结果。
williams_list = williamsCalculator(highest, lowest, closing)
last_call = williams_list[0]
print(last_call)
现在这会返回 Williams %R 和他的 dtype 的整个列表。像这样的东西:
[0 -54.488189
1 -45.564304
2 -48.241470
3 -63.044619
4 -46.036745
5 -49.343832
6 -74.068241
7 -67.401575
8 -60.892388
9 -63.202100
10 -82.204724
11 -82.152231
12 -70.026247
13 -85.669291
14 -82.152231
15 -40.997375
16 -24.934383
17 -28.346457
18 -12.650919
19 -28.976378
20 -36.955381
Name: 0, dtype: float64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
我怎样才能得到 Williams %R 的最后一个值?在这种情况下,我只想要 0 的值,即 -54.488189,没有列名和数据类型。