-4
我使用HTTParty連接stockfighter.io的API並獲取股票的報價,然後我提取價格並將其附加到$averageArr
陣列的末尾。`+'零不能強制到FixNum(TypeError)不會消失
我試圖平均不斷更新數組的最後五個值,所以我想我會讓一個變量sum
將其設置爲零,將最後五個值添加到它,然後除以5得到一個動態和變化的平均值。
這裏是我的代碼:用45行(註釋)
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, #$250.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, #$250.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
我一直有困難,我在其中得到有關FixNum
和nil
錯誤:
/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>'
我可能會嘗試訪問和索引超出範圍在陣列上,但這似乎不是問題。任何幫助,將不勝感激。
代碼有很多錯誤。爲了消除這個錯誤,你可以在這行尾加上''.to_i'',就像這樣:''averageArr.last.to_i''。打賭這對你沒有多大幫助。 –
謝謝!我會試試這個! –
@lurker看看前一行。 'puts''永遠不會被調用。 –