`+' nil 不能被强制转换为 FixNum (TypeError) 不会消失

`+' nil can't be coerced into FixNum (TypeError) won't go away

我正在使用 HTTParty 连接到 stockfighter.io 的 API 并获取股票报价,然后我从中提取每股价格并将其附加到$averageArr数组。

我正在尝试对不断更新的数组 $averageArr 的最后五个值取平均值,所以我想我可以创建一个变量 sum 将其设置为零,然后将最后五个值相加值,然后除以 5 以获得动态和变化的平均值。

这是我的代码:

require 'rubygems'
require 'httparty'
require 'json'

apikey = 'API_KEY_FOR_LOGIN_HERE'
venue = "VENUEX"
stock = "FOOBAR"

base_url = "https://api.stockfighter.io/ob/api"

account = "MY_ACCOUNT_HERE"

$averageArr = []

$counter = 0

$currAve = 0


def getQuote(stock, venue, account)

    response = HTTParty.get("https://api.stockfighter.io/ob/api/venues/#{venue}/stocks/#{stock}/quote")

    orderbook = response.parsed_response

    puts orderbook["ok"]

    return orderbook["last"]
    puts orderbook["lastTrade"]

end


def getAverage(stock, venue, account)

    $averageArr.push(getQuote(stock, venue, account))
    $counter += 1

    if $counter > 5

        sum = 0

        #line 43
        for i in 1..5 do
            # this one is line 45
            sum += $averageArr[$averageArr.count - i]

        end


        return sum/5
        $currAve = sum/5

    else

        return 'WAITING FOR QUOTE. CURRENT:'

    end
end


# line 62
for i in 1..10 do

    # line 64
    getAverage(stock, venue, account)

    if $counter > 5

        if getQuote(stock, venue, account) < $currAve - 25 and numShares < 999

            order = {

                    "account" => account,
                    "venue" => venue,
                    "symbol" => stock,
                    "price" => 1,  #0.00 -- probably ludicrously high
                    "qty" => 1,
                    "direction" => "buy",
                    "orderType" => "market"  # See the order docs for what a limit order is

                  }

            response = HTTParty.post("#{base_url}/venues/#{venue}/stocks/#{stock}/orders",
                                   :body => JSON.dump(order),
                                   :headers => {"X-Starfighter-Authorization" => apikey}
                                   )

        elsif getQuote(stock, venue, account) > $currAve + 25 and numShares > 0

            order = {

                    "account" => account,
                    "venue" => venue,
                    "symbol" => stock,
                    "price" => 1,  #0.00 -- probably ludicrously high
                    "qty" => 1,
                    "direction" => "sell",
                    "orderType" => "market"  # See the order docs for what a limit order is

                  }

            response = HTTParty.post("#{base_url}/venues/#{venue}/stocks/#{stock}/orders",
                                   :body => JSON.dump(order),
                                   :headers => {"X-Starfighter-Authorization" => apikey}
                                   )

        end
    end
end

我在第 45 行(已注释)遇到困难,其中我收到有关 FixNumnil:

的错误
/Users/kaichristensen/Dropbox/Kai/Stockfighter/level_three_selling.rb:45:in `+': nil can't be coerced into Fixnum (TypeError)
from /Users/kaichristensen/Dropbox/Kai/Stockfighter/level_three_selling.rb:45:in `block in getAverage'
from /Users/kaichristensen/Dropbox/Kai/Stockfighter/level_three_selling.rb:43:in `each'
from /Users/kaichristensen/Dropbox/Kai/Stockfighter/level_three_selling.rb:43:in `getAverage'
from /Users/kaichristensen/Dropbox/Kai/Stockfighter/level_three_selling.rb:64:in `block in <main>'
from /Users/kaichristensen/Dropbox/Kai/Stockfighter/level_three_selling.rb:62:in `each'
from /Users/kaichristensen/Dropbox/Kai/Stockfighter/level_three_selling.rb:62:in `<main>'

我可能试图在数组的范围外访问和索引,但这似乎不是问题所在。任何帮助将不胜感激。

你的语法没问题。问题是您的方法没有验证数据。

a += b 扩展为 a = a + b,当 bnil.

时,这将抛出您面临的错误

您需要确保 getQuote 总是 returns 一个数字,或者在执行数学运算之前将您想要求平均的任何值都转换为一个数字。

对于前者,可以将return orderbook["last"]改为return orderbook["last"].to_f

对于后者,sum += $averageArr[$averageArr.count - i].to_f