Search code examples
pythonlistmultiplication

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. 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. I just can't get lastTradePrice to be a float I can use.

    # 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


Solution

  • If you want to multiply an entire list like this I'd suggest using numpy arrays

    import numpy as np
    arr = np.array([ i for i in range(10)])
    arr *= 2