Skip to content

Instantly share code, notes, and snippets.

@ivannp
ivannp / dvi.r
Created September 24, 2014 16:24
DVI Indicator Backtesting with SIT
require(quantmod)
require(SIT)
dvi.indicator = function(prices) {
dvi = TTR:::DVI(prices)[,3]
indicator.long = ifelse(dvi <= 0.5, 1, 0)
indicator.short = ifelse(dvi > 0.5, -1, 0)
indicator = reclass(cbind(indicator.short + indicator.long), prices)
#' Saves symbols (from an environment) to a specified directory
#'
#' The files (one per symbol) are saved in \code{dir}. The file name
#' is the symbol itself, and the file extension is RData.
#'
#' @param symbols The symbols
#' @param dir The destination folder (file system directory). It must exist.
#' @param env The environment containing the symbols
#'
#' @examples
require(quantmod)
prepare.indicator = function(close, indicator, roc.n, normalize=FALSE, func=mean) {
rets = ROC(close, type="discrete", n=roc.n)
if(normalize) {
# Normalize the returns to daily
rets = ((1 + rets) ^ (1/roc.n)) - 1
}
garchAutoTryFit = function(
ll,
data,
trace=FALSE,
forecast.length=1,
with.forecast=TRUE,
ic="AIC",
garch.model="garch" )
{
formula = as.formula( paste( sep="",
@ivannp
ivannp / e1071.R
Created December 1, 2012 03:18
Back-testing SVM with e1071
svmComputeOneForecast = function(
id,
data,
response,
startPoints,
endPoints,
len,
history=500,
trace=FALSE,
kernel="radial",
@ivannp
ivannp / test7.C
Created October 28, 2012 22:31
Maintaining sorted std::vector
// g++ -O3 -std=c++0x -o test test7.C
#include <iostream>
#include <cstdint>
#include <vector>
#include <iterator>
#include <algorithm>
#include <cassert>
using namespace std;
@ivannp
ivannp / armaSearch.R
Created June 12, 2012 00:32
Determine the best ARMA model
armaSearch = function(
xx,
minOrder=c(0,0),
maxOrder=c(5,5),
trace=FALSE )
{
bestAic = 1e9
len = NROW( xx )
for( p in minOrder[1]:maxOrder[1] ) for( q in minOrder[2]:maxOrder[2] )
{
@ivannp
ivannp / computeArmaForcasts.R
Created June 9, 2012 21:27
Compute a day-by-day trading indicator using ARMA models
armaTryFit = function(
ll,
data,
trace=FALSE,
includeMean=TRUE,
withForecast=TRUE,
forecastLength=1 )
{
formula = as.formula( paste( sep="",
"xx ~ arma(", ll[1], ",", ll[2], ")" ) )
@ivannp
ivannp / findBestArma.R
Created June 9, 2012 19:31
findBestArma
findBestArma = function(
xx,
minOrder=c(0,0),
maxOrder=c(5,5),
trace=FALSE )
{
bestAic = 1e9
len = NROW( xx )
for( p in minOrder[1]:maxOrder[1] ) for( q in minOrder[2]:maxOrder[2] )
{