[英]How can I take a value from a list and then use it in a basic math equation?
I am trying to take a float value from an api provided list. 我正在尝试从api提供的列表中获取浮点值。 I was able to make it look like a float value when I print it but I don't think it is a true number because I cant use it in my math equation. 我可以在打印时使它看起来像浮点值,但我认为它不是真实的数字,因为我无法在数学方程式中使用它。 There are other functions to get trailTrigger and stopLoss but I am happy that they work. 还有其他函数可以获取TrailTrigger和stopLoss,但我很高兴它们能够正常工作。 I just can't get lastTradePrice to be a float I can use. 我只是无法让lastTradePrice成为我可以使用的浮点数。
# Call My last trade from Coinbase
def coinBaseMyLastTrade():
global lastTradePrice
global lastTradeSize
global lastTradeFee
global lastTradeSide
global lastTradeSettled
myLastTrade = requests.get(api_url + 'fills?product_id=BTC-USDC&limit=1', auth=auth)
lastTradeList = myLastTrade.json()
lastTradePrice = [float(lastTradeList_item['price']) for lastTradeList_item in lastTradeList]
lastTradeSize = [float(lastTradeList_item['size']) for lastTradeList_item in lastTradeList]
lastTradeFee = [float(lastTradeList_item['fee']) for lastTradeList_item in lastTradeList]
lastTradeSide = [str(lastTradeList_item['side']) for lastTradeList_item in lastTradeList]
lastTradeSettled = [lastTradeList_item['settled'] for lastTradeList_item in lastTradeList]
print 'My Last Trade Price =',lastTradePrice
print 'My Last Trade Size =',lastTradeSize
print 'My Last Trade Fee =',lastTradeFee
print 'My Last Trade Side =',lastTradeSide
print 'My Last Trade Settled =',lastTradeSettled
return
# Work out the value to use to trigger the trail loop
def calculateTrailTriggerValue():
global triggerValue
global stoplossValue
if "buy" in lastTradeSide:
triggerValue = lastTradePrice * ((100+trailTrigger)/100)
stoplossValue = lastTradePrice * ((100-stoploss)/100)
elif "sell" in lastTradeSide:
triggerValue = lastTradePrice * ((100-trailTrigger)/100)
stoplossValue = lastTradePrice * ((100+stoploss)/100)
print 'Trail Trigger $=',triggerValue
print 'Stop Loss $=',stoplossValue
return
When I run the code I get the fault 运行代码时出现错误
triggerValue = lastTradePrice * ((100-trailTrigger)/100)
TypeError: can't multiply sequence by non-int of type 'float'
Its strange because I used this code on a different list and I was able to use the values but It doesn't work with the code i need help with 这很奇怪,因为我在不同的列表上使用了此代码,并且能够使用这些值,但不适用于需要帮助的代码
# Call 24hour stats
def coinBasePastDayStats():
dayStats = requests.get('https://api.pro.coinbase.com/products/BTC-USDC/stats')
dayHigh = float(dayStats.json()['high'])
dayLow = float(dayStats.json()['low'])
dayChange = dayHigh - dayLow
global dayChangePercent
dayChangePercent = (dayChange / dayHigh) * 100
print 'Last 24hr High =', dayHigh
print 'Last 24hr Low =', dayLow
print 'Last 24hr Change =', dayChange
print 'Last 24hr Change Percent =', {:.2f}".format(dayChangePercent),'%'
return
If anyone could help me take a list value and use it in a basic math equation I would be very grateful. 如果有人可以帮助我取得列表值并将其用于基本数学方程式中,我将不胜感激。 Cheers 干杯
If you want to multiply an entire list like this I'd suggest using numpy arrays 如果您想像这样将整个列表相乘,建议使用numpy数组
import numpy as np
arr = np.array([ i for i in range(10)])
arr *= 2
It looks like you are on the right track. 看来您在正确的轨道上。 But as the others have said you are doing the operation on the list itself and python doesn't support that. 但是正如其他人所说,您正在对列表本身进行操作,而python不支持该操作。
Without the use of numpy you have to loop over the list and do the operation on each value in turn. 在不使用numpy的情况下,您必须遍历列表并依次对每个值进行操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.