Skip to content

Instantly share code, notes, and snippets.

@creative-quant
creative-quant / ib.eurusd.R
Last active August 29, 2015 14:13
R - IBrokers - Historical Currency Data
library(IBrokers)
library(twsInstrument)
tws <- twsConnect(host='127.0.0.1', port=4001)
contract <- getContract("EUR.USD")
reqHistoricalData( tws, contract, verbose=T, whatToShow="BID" )
reqHistoricalData( tws, contract, verbose=T, whatToShow="ASK" )
twsDisconnect(tws)
@creative-quant
creative-quant / cadvswti.R
Last active August 29, 2015 14:14
CAD vs WTI
#sources
#https://www.quandl.com/CURRFX/USDCAD-Currency-Exchange-Rates-USD-vs-CAD
#curl https://www.quandl.com/api/v1/datasets/CURRFX/USDCAD.csv -o /tmp/USDCAD.csv
#https://www.quandl.com/DOE/RWTC-WTI-Crude-Oil-Spot-Price-Cushing-OK-FOB
#curl https://www.quandl.com/api/v1/datasets/DOE/RWTC.csv -o /tmp/RWTC.csv
library(xts)
library(ggplot2)
usd.cad <- read.csv("/tmp/USDCAD.csv")
@creative-quant
creative-quant / cot.cad.R
Created February 4, 2015 18:40
COT - CAD
############################################################
## Content of the Traders in Financial Futures (TFF) Report
## http://www.cftc.gov/ucm/groups/public/@commitmentsoftraders/documents/file/tfmexplanatorynotes.pdf
############################################################
# Dealer/Intermediary
# These participants are what are typically described as the "sell side” of the market. Though they
# may not predominately sell futures, they do design and sell various financial assets to clients.
# They tend to have matched books or offset their risk across markets and clients. Futures
# contracts are part of the pricing and balancing of risk associated with the products they sell and
# their activities. These include large banks (U.S. and non-U.S.) and dealers in securities, swaps
@creative-quant
creative-quant / cal.R
Created February 5, 2015 23:30
Forex Economic Calendar
#curl http://www.dailyfx.com/files/Calendar-02-01-2015.csv -o /tmp/Calendar-02-01-2015.csv
cal <- read.csv("/tmp/Calendar-02-01-2015.csv")
@creative-quant
creative-quant / nfp.R
Last active June 4, 2017 21:04
USD Change in Non-farm Payrolls
library(ggplot2)
#curl 'http://research.stlouisfed.org/fred2/series/PAYEMS/downloaddata' --data 'form%5Bnative_frequency%5D=Monthly&form%5Bunits%5D=lin&form%5Bfrequency%5D=Monthly&form%5Baggregation%5D=Average&form%5Bobs_start_date%5D=1939-01-01&form%5Bobs_end_date%5D=2015-01-01&form%5Bfile_format%5D=csv&form%5Bdownload_data_2%5D=' -o /tmp/PAYEMS.csv
nfp <- read.csv("/tmp/PAYEMS.csv")
nfp$DATE <- as.Date( nfp$DATE, "%Y-%m-%d" )
df <- data.frame( change=diff(nfp$VALUE), date=tail(nfp$DATE,-1) )
plot <- ggplot(df,aes(x=date,y=change)) +
geom_area()
plot
@creative-quant
creative-quant / cad.employment.R
Created February 6, 2015 22:41
CAD Net Change in Employment
#http://www20.statcan.gc.ca/tables-tableaux/cansim/csv/02820087-eng.zip
@creative-quant
creative-quant / AlmostEqual.h
Last active August 29, 2015 14:19
Comparing floating point numbers
/*
* AlmostEqual.h
*
*/
#ifndef ALMOSTEQUAL_H_
#define ALMOSTEQUAL_H_
// Usable AlmostEqual function - http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
bool AlmostEqual(float A, float B) {
[alias]
sstash = "!f() { git stash save $1; }; f"
sshow = "!f() { echo $@; git stash show stash^{/$*} -p; }; f"
sapply = "!f() { git stash apply stash^{/$*}; }; f"
@creative-quant
creative-quant / cad.cpi.R
Created January 26, 2016 00:54
CAD CPI plot
#$ curl -o /tmp/cad.cpi.csv http://www.statcan.gc.ca/daily-quotidien/160122/cg160122a001-eng.csv
# "The 12-month change in the Consumer Price Index (CPI) and the CPI excluding gasoline, 12-month % change"
# ,"CPI","CPI excluding gasoline"
library(ggplot2)
cad.cpi <- read.table( "/tmp/cad.cpi.csv", sep = ",", col.names=c("date", "cpi", "exgas"), skip=2, fill=T )
cad.cpi <- cad.cpi[1:(nrow( cad.cpi ) - 1),]
cad.cpi$date = as.Date( paste( cad.cpi$date, '01' ), "%B %Y %d" )
df <- data.frame(cad.cpi)
library(xts)
library(ggplot2)
#cad gdp
#curl http://www20.statcan.gc.ca/tables-tableaux/cansim/csv/03790031-eng.zip -o /tmp/ca.gpd.zip && unzip /tmp/ca.gpd.zip -d /tmp
#curl http://www.statcan.gc.ca/cgi-bin/sum-som/fl/cstsaveascsv.cgi?filename=gdps04a-eng.htm&lan=eng -o
#http://www20.statcan.gc.ca/tables-tableaux/cansim/csv/03790031-eng.zip
#Ref_Date,GEO,SEAS,PRICES,NAICS,Vector,Coordinate,Value
if( !exists( "ca.gdp" ) ) {
ca.gdp <- read.csv("/tmp/03790031-eng.csv")