如何从列表中获取一个值,然后将其用于基本数学方程式?
How can I take a value from a list and then use it in a basic math equation?
我正在尝试从 api 提供的列表中获取浮点值。当我打印它时,我能够让它看起来像一个浮点值,但我不认为它是一个真实的数字,因为我不能在我的数学方程式中使用它。还有其他函数可以获取 trailTrigger 和 stopLoss,但我很高兴它们可以工作。我只是无法让 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
当我 运行 代码时,我得到了错误
triggerValue = lastTradePrice * ((100-trailTrigger)/100)
TypeError: can't multiply sequence by non-int of type 'float'
这很奇怪,因为我在不同的列表上使用了这段代码,我能够使用这些值,但它不适用于我需要帮助的代码
# 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
如果有人能帮我列出一个列表值并将其用于基本数学方程式,我将不胜感激。
干杯
如果你想像这样乘以整个列表,我建议使用 numpy 数组
import numpy as np
arr = np.array([ i for i in range(10)])
arr *= 2
看来您的方向是正确的。但正如其他人所说,您正在对列表本身进行操作,python 不支持。
如果不使用 numpy,您必须遍历列表并依次对每个值执行操作。
我正在尝试从 api 提供的列表中获取浮点值。当我打印它时,我能够让它看起来像一个浮点值,但我不认为它是一个真实的数字,因为我不能在我的数学方程式中使用它。还有其他函数可以获取 trailTrigger 和 stopLoss,但我很高兴它们可以工作。我只是无法让 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
当我 运行 代码时,我得到了错误
triggerValue = lastTradePrice * ((100-trailTrigger)/100)
TypeError: can't multiply sequence by non-int of type 'float'
这很奇怪,因为我在不同的列表上使用了这段代码,我能够使用这些值,但它不适用于我需要帮助的代码
# 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
如果有人能帮我列出一个列表值并将其用于基本数学方程式,我将不胜感激。 干杯
如果你想像这样乘以整个列表,我建议使用 numpy 数组
import numpy as np
arr = np.array([ i for i in range(10)])
arr *= 2
看来您的方向是正确的。但正如其他人所说,您正在对列表本身进行操作,python 不支持。
如果不使用 numpy,您必须遍历列表并依次对每个值执行操作。