I'm using the Ruby Money gem (For rails) for my app and i want to save the used currency rate for every order.
My base currency is USD and i give my users the option to pay in EUR, on a order save i want to record the used currency conversion rate.
I just can't find a method to get the used rate from this lib, anybody knows how to do this?
I'm also looking for best practice on this, for now i'm planning on saving the prices in order_lines in the users currency and save the used currency per line. As my original prices are in dollars i'm also saving the price in dollar per order line as reference.
Thanks in advance!
For this example bank:
# config/initializers/money.rb
dev_bank = Money::Bank::VariableExchange.new
dev_bank.add_rate("EUR", "USD", 1.35)
dev_bank.add_rate("USD", "EUR", 1/1.35)
Money.default_bank = dev_bank
you can do this:
# somewhere else in your code
Money.default_bank.get_rate('EUR', 'USD')
See more info in the documentation
It looks like you're supposed to set up exchange rates in your configuration code using the exchange bank object or the money.rb initializer, in which case you already have access to the exchange rate in your code.
If for some reason you have access only to the input and output of the exchange conversion, you should be able to calculate the exchange rate yourself.
Related
My web app is about looking up airplane tickets.
The raw prices is stored in departure country currency and departure city time .
I wonder what's the better practice to convert the price in user's preferable currency.
How about create an currency field in User model, and convert the price according it.
And I want to show the updated time depends on the user's time zone.
If the user comes from Japan it will show in Japan time.
If the user comes from Taiwain it will show Taipei time.
Currently I hard code the time in that way,
flight.updated_at.in_time_zone("Taipei").strftime('%m-%d %H:%M')
Any suggestion or idea, thanks~
Currency:
The Money gem can probably help you. It has methods like these:
Money.new(1000, "USD").exchange_to("EUR").format # => "€906.86"
where "EUR" can be the currency stored in your User model.
Exchange rates can be retrieved from several online services with additional plugins (see the gem's README).
Time zone:
The local_time gem will give you this helper:
<%= local_time(flight.updated_at) %>
It will render a time tag, and update its content with javascript to match each visitor's local time.
I'm attempting to run Interactive Broker's included code sample.
http://www.interactivebrokers.com/download/JavaAPIGettingStarted.pdf
On about page 42 it details how to pull market data feeds. My question is, has anyone successfully put in the parameters needed to pull currency pair data??
public synchronized void reqMktData(int tickerId, Contract contract, String genericTickList, boolean snapshot)
I cannot find the valid inputs that will correct the errors I'm seeing from the client.
Parameters needed
List of values inside Contract class are here : https://www.interactivebrokers.com/en/software/api/apiguide/java/contract.htm
STK == "stock" , should this be set to CASH for Forex data?
IDEALPRO == the exchange according to this page : http://ibkb.interactivebrokers.com/tag/fx-trader
USD.JPY = SYMBOL (this here is a guess on my part)
USD == "underlying currency" , here I am guessing again.. seems the currency needs to match the transaction currency.
the pair in the format Transaction Currency.Settlement Currency (example: EUR.USD). The Underlying column will display only the Transaction Currency.
After scouring IB's forum I have found something that works for FX data feeds. You need to put the TransactionCurrency as the Symbol, and the SettlementCurrency as the underlying in the dialog box.
Here is the resulting data feed
As I can remember, I used: CASH, IDEALPRO, EUR.USD, USD
You can see all parameters example in the TradeStation client. Just find the needed instrument and look at it's properties.
And remember that not all parameters must be necessarily filled
At the worst, show the error.
I am building a rails ecommerce application and would like to integrate a currency exchange system were visitors can click on a check box and a list of countries will be displayed and when clicked on the value of the currencies of products on the site changes to the value of the selected countries currency. Is there a rails gem that connects to a currency exchange server and authomaticall converts the currency for me or any ideas of how i can accomplish this. Thank you.
The first thing to do is have the currency code like USD NGN EUR,so which ever currency the user selects has its currency code saved in a session or cookie having in mind that your default currency is set to which ever one you want to use.
Then Install the google currency gem which gives you access to real time exchange rate.
Assuming all these is set what we will do is
require 'money'
require 'money/bank/google_currency'
bank = Money::Bank::GoogleCurrency.new
rate = bank.get_rate(:NGN, :USD).to_f
For this example, i am simply getting the rate of 1 naira to $1
now that i no the rate all i have to do is find a way to convert the item price to selected currency.
Assuming the user selects $ then the session[:selected_currency] = "USD"
in our product model we have the following
##Assuming default currency is NGN so it defaults to NGN if no currency code
require 'money'
require 'money/bank/google_currency'
def converted_price(currency = :NGN)
bank = Money::Bank::GoogleCurrency.new
rate = bank.get_rate(:NGN, currency).to_f
price = item_amount / rate
price
end
so in our view we can have <%= #product.converted_price(session[:selected_currency])%>
I haven't tested this but this would give you an idea of how i solved the issue.
Check out the money gem
There seems to be some workaround to do exactly what you want to do, however it'll give you a decent base to what you're aiming for.
I am storing a price in my database.
I figured I should store the currency as my application with need to support internationalisation.
I believe the correct way is to store the ISO 4217 currency code, such as USD for US$, AUD for AU$ and EUR for Euros ...
I have a price and a currency attribute in model... Am I doing it right?
How do convert the currency code to it Symbol version? is there a helper for this? do I need to create myself a corresponding hash?
Cheers,
Joel
You should have a look at the Money gem. Handles currency codes and has support for exchange rates and formatting.
https://github.com/RubyMoney/money
I would like to get the adjusted price (adjusting for splits and dividends) for a group of stock symbols using Yahoo! Finance. It looks like the historical prices call is limited to one symbol at a time. Could please let me know if there is a way to get multiple symbols in one call?
I would like to get this data so I can do some back testing on that data. Since I may require quite a few symbols (say 500-1000), it will be easier if I can make just a few batch calls to Yahoo!'s servers instead of making one call per symbol everyday.
Another way of getting the adjusted price is to use their daily stock price api and adjust it manually using dividend and splits information (they allow multiple symbols for their daily stock quotes). Unfortunately I cannot find any way to get splits information from the http call (guessing based on 50% or 200% is one option but if you deal with penny stocks, this can be dangerous and cannot figure out uneven splits). Also, the dividend information returned by it is not easy to decode. They seem to be returning the total over 4 quarters and the dividend date doesn't really correspond with the actual dividend date based on the historical price. The various options for the call can be found here: http://www.gummy-stuff.org/Yahoo-data.htm
Any suggestions on getting adjusted price for multiple symbols? Or Am I unnecessarily worrying about making 100s of calls to Yahoo! everyday? Ideally I would like to download all the required data within a couple of hours each day - that would be 10-20 calls per minute. Is that too much? I couldn't find any documentation on the permissible number of requests per second.
I am open to other places where I can get similar data. However, since I am just trying to learn the basics of quant trading and not trade, I would prefer free downloads.
Thanks
-e
This is an old question, but I did find a source where split data is available. Not sure how comprehensive these announcements are though:
http://biz.yahoo.com/c/09/s1.html
In the url, the "09" part is the year (2009), and the "s1" part is the month (s1 = Jan, s2 = Feb., s3 = Mar., etc.)
It isn't a nice clean CSV, but the format of the page is consistent and should be parseable. Just make a query each day for the current month, parse the page, and process any splits that you didn't see the day before.
ETA: And another source (probably less reliable than Yahoo, but can be queried by ticker):
http://getsplithistory.com/
I am not sure which language you are using but I have a sample in C#. I think it will give you the idea at least or may be help some one else
private string BASE_URL = "http://query.yahooapis.com/v1/public/yql?q=" + "select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20({0})" + "&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
Collection<Quote> quotes;
string symbolList = String.Join("%2C", quotes.Select(w => "%22" + w.Symbol + "%22").ToArray());
string url = string.Format(BASE_URL,symbolList);
XDocument doc = XDocument.Load(url);
Parse(quotes,doc);
What we are doing here is appending "," to each array item then passing that symbol list to yahoo. I have successfully fetched prices for 700 symbols in each call. Hitting yahoo servers for each ticker is a pain. I fetch stock prices for all of 6500+ tickers everyday. Earlier it use to take 3 hours now it is less than 2 mins.....sweet
Source link for that code is here - http://www.jarloo.com/get-yahoo-finance-api-data-via-yql/
P.S. Please get a api key to work smoothly. The above url is a public link where tables are timed out most of the time. Once you get an api key then your url will be (minus "public")
http://query.yahooapis.com/v1/yql