Rilcon42 Rilcon42 - 3 months ago 11
Ruby Question

passing result of method as global value

Can someone explain what I am doing wrong here? I am attempting to avoid having to type and re-declare all the variables for an account inside the method.

#https://github.com/nukeproof/oanda_api
require 'oanda_api'
#require 'rsruby'
require 'indicators'

def createOrder(instrument="EUR_USD",type="market",units)
order = $client.$account($acct_id).order(instrument: "EUR_USD",type: "market",side: type,units: units).create
puts "time: "+order.time+"\nprice: "+order.price+"\nid: "+order.trade_opened.id
return(order.trade_opened.id)
end

OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE #bad practice...anyone who copies this code dont do this!
$acct_id=123456
$acct_token="abcd"

$client = OandaAPI::Client::TokenClient.new(:practice, $acct_token)
$account = client.account($acct_id).get

createOrder(units:1000)


Error

C:\Users\king\Desktop\_REPOS\misc\stock_analysis\forex\oanda\ruby>ruby basicnukeproof.rb
basicnukeproof.rb:7: syntax error, unexpected tGVAR, expecting '('
order = $client.$account($acct_id).order(instrument: ...

Answer

This part of the code is invalid

$client.$account(...)

If you write $client., then the next thing that Ruby expects to see is the name of a method, and method names cannot begin with $ so $account is invalid. Try removing the dollar sign from $account there.