Skip to content

Instantly share code, notes, and snippets.

View josephdunn's full-sized avatar

Joseph Dunn josephdunn

View GitHub Profile
# original idea: http://mcarreira.typepad.com/mc_notes/2014/03/a-simple-model-for-the-pl-of-a-market-maker-how-many-losing-days-in-a-year.html
ndays <- 1e5; pl <- unlist(lapply(1:ndays, function(x) { sum((rbinom(7*60*10, 1, 0.525) - 0.5) * 2) })) ; 1 / (length(pl[pl < 0]) / ndays * 252)
# updated code demonstrating win vs. loss asymmetry with M/T. thanks @marascio and @MarcosCarreira for the suggestions.
# wins are 1.00, losses are -1.05 (normalized)
ndays <- 1e5; pl <- unlist(lapply(1:ndays, function(x) { sum(ifelse((rbinom(7*60*10, 1, 0.525) - 0.5) * 2 == -1, -1.05, 1)) })) ; 1 / (length(pl[pl < 0]) / ndays * 252)
# same as above - ifelse() is slow compared to writing the vector correctly the first time, but this is more cryptic.
# wins are 1.00, losses are -1.05 (normalized)
ndays <- 1e5; pl <- unlist(lapply(1:ndays, function(x) { sum(((1-rbinom(7*60*10, 1, 0.525))*-2.05)+1) })) ; 1 / (length(pl[pl < 0]) / ndays * 252)
import numpy as np
ndays = int(1e5)
ntradesday = 7*60*10
pl = np.sum((np.random.binomial(1, 0.525, (ndays, ntradesday))-0.5)*2, 1)
1.0 / (len(pl[pl < 0]) / float(ndays) * 252)
winProb <- 0.525
numBets <- c(1, 50, 1000, 10000, 100000)
runTimes <- 1000
pl <- function(bets)
{
sum(rbinom(bets, 1, winProb) - 0.5)
}
runs <- do.call(rbind, lapply(1:runTimes, function(x) { sapply(numBets, pl) }))
hello milktrader!
library(quantmod)
library(PerformanceAnalytics)
getSymbols('HEIA.AS', src='yahoo')
Return.cumulative(Return.calculate(get('HEIA.AS')['2010-12-31::']))
#!/usr/bin/ruby
def winloss
if rand(2) == 1
return true
else
return false
end
end
#!/usr/bin/ruby
def winloss
if rand(2) == 1
return true
else
return false
end
end
require 'rubygems'
require 'eventmachine'
require 'em-http-request'
require 'json'
EventMachine.run {
bid = 0.0
ask = 0.0
spread = 0.0
data <- read.table('TrAEX.xls', header=TRUE, sep='\t', skip=5, stringsAsFactors=FALSE)
data.xts <- xts(data$Quote, order.by=as.POSIXlt(data$Date...time, format='%H:%M:%S'))
require(quantstrat)
# clean up
rm(list=ls(all=TRUE, pos=.blotter), pos=.blotter)
rm(list=ls(all=TRUE, pos=.instrument), pos=.instrument)
rm(list=ls(all=TRUE, pos=.strategy), pos=.strategy)
# set initial values
initDate <- '2011-08-05 06:00:00'
initEq <- 10000