From 67a5545a4ae986551ec8ba1b9bf408fc1540709f Mon Sep 17 00:00:00 2001 From: Kokechacho <67198515+Kokechacho@users.noreply.github.com> Date: Mon, 12 Feb 2024 10:29:02 +0100 Subject: [PATCH 01/21] Add files via upload --- streamPair.q | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 streamPair.q diff --git a/streamPair.q b/streamPair.q new file mode 100644 index 0000000..a2e6b1e --- /dev/null +++ b/streamPair.q @@ -0,0 +1,80 @@ +// load tables +tab1: 40000#1_ flip `dateTime`bid`ask`bidVol`askVol!("*FFFF";",") 0: `:C:/q/dash/sample/data/stocks/USA500IDXUSD.csv; +tab2: 40000#1_ flip `dateTime`bid`ask`bidVol`askVol!("*FFFF";",") 0: `:C:/q/dash/sample/data/stocks/USATECHIDXUSD.csv; +//tab1: 1_ flip `dateTime`bid`ask`bidVol`askVol`Vol!("DFFFFF";",") 0: `:C:/q/dash/sample/data/stocks/EWA2.csv; +//tab2: 1_ flip `dateTime`bid`ask`bidVol`askVol`Vol!("DFFFFF";",") 0: `:C:/q/dash/sample/data/stocks/EWC2.csv; +tab3: flip `dateTime`spread`mean`up`low`operation!("P"$();"F"$();"F"$();"F"$();"F"$();"F"$()); + + +// Fix Time +update dateTime:"P"$@[;19;:;"."] each dateTime from `tab1; +update dateTime:"P"$@[;19;:;"."] each dateTime from `tab2; + +// Take date and log(prices) +priceX: distinct select distinct dateTime, log bid, log ask from tab1; +priceY: distinct select distinct dateTime, log bid, log ask from tab2; +spreads: 200#select from tab3; + +// Eliminate nulls and filter data +update delta:0f^deltas dateTime from `priceX; +update delta:0f^deltas dateTime from `priceY; +priceX: 0!1_priceX; +priceY: 0!1_priceY; + +// Create an empty auxiliary table +tAux: 1_1#priceX; +profit: 0; + +// Alpha and beta functions +betaF:{dot:{sum x*y}; + ((n*dot[x;y])-(*/)(sum')(x;y))% + ((n:count[x])*dot[x;x])-sum[x]xexp 2}; +alphaF: {avg[y]-(betaF[x;y]*avg[x])}; + +// Calculate alpha y beta and spreads +px: exec bid from priceX; +py: exec bid from priceY; +s1: py - ((px * beta[px;py])-alpha[px;py]); +spreads: update dateTime: priceX[`dateTime], spread: s1, mean: avg[100#s1] ,up: avg[100#s1]+1.96*dev[100#s1], low: avg[100#s1]-1.96*dev[100#s1] , operation:0 from spreads; + +/ load and initialize kdb+tick +/ all tables in the top level namespace (.) become publish-able +\l C:/q/dash/sample/tick/u.q +.u.init[]; + +// Read and write on buffer functions +.ringBuffer.read:{[t;i] $[i<=count t; i#t; i rotate t] } +.ringBuffer.write:{[t;r;i] @[t;(i mod count value t)+til 1;:;r];} + +// Initialize index and empty tables (We will access directly to these objects from dashboards) +.streamPair.i:-1; +.streamPair.priceX: 1000#tAux; +.streamPair.priceY: 1000#tAux; +.streamPair.spreads: 1000#tab3; +//.streamPair.spreads: update spread:0 from .streamPair.spreads; +// Timer function +timer:{t:.z.p;while[.z.p Date: Mon, 12 Feb 2024 10:48:00 +0100 Subject: [PATCH 02/21] Add files via upload --- linear_regression.q | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 linear_regression.q diff --git a/linear_regression.q b/linear_regression.q new file mode 100644 index 0000000..b83aba2 --- /dev/null +++ b/linear_regression.q @@ -0,0 +1,6 @@ +// Alpha and beta functions +betaF:{dot:{sum x*y}; + ((n*dot[x;y])-(*/)(sum')(x;y))% + ((n:count[x])*dot[x;x])-sum[x]xexp 2}; + +alphaF: {avg[y]-(betaF[x;y]*avg[x])}; \ No newline at end of file From a235bc27cd94f0f8df72ddbc6e62823f60546b96 Mon Sep 17 00:00:00 2001 From: Kokechacho <67198515+Kokechacho@users.noreply.github.com> Date: Mon, 12 Feb 2024 11:51:45 +0100 Subject: [PATCH 03/21] Add files via upload --- linear_regression.q | 18 +++++++++++++++--- streamPair.q | 36 +++++++++++------------------------- 2 files changed, 26 insertions(+), 28 deletions(-) diff --git a/linear_regression.q b/linear_regression.q index b83aba2..8ae2845 100644 --- a/linear_regression.q +++ b/linear_regression.q @@ -1,6 +1,18 @@ -// Alpha and beta functions -betaF:{dot:{sum x*y}; +// Alpha and beta functions +// These formulas are derived from Ordinary Least Squares (OLS) regression for one variable +// OLS regression is a method used to estimate the relationship between a dependent variable (y) and one or more independent variables (x). + +// Function to calculate the beta coefficient (slope) using OLS +// The beta coefficient (slope) represents the change in the dependent variable for a one-unit change in the independent variable. +// This function computes the beta coefficient using the formula: +// beta = ((n * Σ(x*y)) - (Σx * Σy)) / ((n * Σ(x^2)) - (Σx)^2) +betaF:{dot:'[sum;*]; ((n*dot[x;y])-(*/)(sum')(x;y))% ((n:count[x])*dot[x;x])-sum[x]xexp 2}; - + + +// Function to calculate the alpha coefficient (intercept) using OLS +// The alpha coefficient (intercept) represents the value of the dependent variable when the independent variable is zero. +// This function computes the alpha coefficient using the formula: +// alpha = Mean(y) - beta * Mean(x) alphaF: {avg[y]-(betaF[x;y]*avg[x])}; \ No newline at end of file diff --git a/streamPair.q b/streamPair.q index a2e6b1e..84beb5d 100644 --- a/streamPair.q +++ b/streamPair.q @@ -1,41 +1,27 @@ +// import linear regression module +\l C:/q/dash/sample/linear_regression.q + // load tables tab1: 40000#1_ flip `dateTime`bid`ask`bidVol`askVol!("*FFFF";",") 0: `:C:/q/dash/sample/data/stocks/USA500IDXUSD.csv; tab2: 40000#1_ flip `dateTime`bid`ask`bidVol`askVol!("*FFFF";",") 0: `:C:/q/dash/sample/data/stocks/USATECHIDXUSD.csv; -//tab1: 1_ flip `dateTime`bid`ask`bidVol`askVol`Vol!("DFFFFF";",") 0: `:C:/q/dash/sample/data/stocks/EWA2.csv; -//tab2: 1_ flip `dateTime`bid`ask`bidVol`askVol`Vol!("DFFFFF";",") 0: `:C:/q/dash/sample/data/stocks/EWC2.csv; tab3: flip `dateTime`spread`mean`up`low`operation!("P"$();"F"$();"F"$();"F"$();"F"$();"F"$()); - -// Fix Time -update dateTime:"P"$@[;19;:;"."] each dateTime from `tab1; -update dateTime:"P"$@[;19;:;"."] each dateTime from `tab2; - -// Take date and log(prices) -priceX: distinct select distinct dateTime, log bid, log ask from tab1; -priceY: distinct select distinct dateTime, log bid, log ask from tab2; +// Fix data and take log(prices) +priceX: 0!1_(update delta:0f^deltas dateTime from distinct select distinct dateTime, log bid, log ask from update dateTime:"P"$@[;19;:;"."] each dateTime from tab1); +priceY: 0!1_(update delta:0f^deltas dateTime from distinct select distinct dateTime, log bid, log ask from update dateTime:"P"$@[;19;:;"."] each dateTime from tab2); spreads: 200#select from tab3; -// Eliminate nulls and filter data -update delta:0f^deltas dateTime from `priceX; -update delta:0f^deltas dateTime from `priceY; -priceX: 0!1_priceX; -priceY: 0!1_priceY; - // Create an empty auxiliary table tAux: 1_1#priceX; profit: 0; -// Alpha and beta functions -betaF:{dot:{sum x*y}; - ((n*dot[x;y])-(*/)(sum')(x;y))% - ((n:count[x])*dot[x;x])-sum[x]xexp 2}; -alphaF: {avg[y]-(betaF[x;y]*avg[x])}; - // Calculate alpha y beta and spreads px: exec bid from priceX; py: exec bid from priceY; -s1: py - ((px * beta[px;py])-alpha[px;py]); -spreads: update dateTime: priceX[`dateTime], spread: s1, mean: avg[100#s1] ,up: avg[100#s1]+1.96*dev[100#s1], low: avg[100#s1]-1.96*dev[100#s1] , operation:0 from spreads; +s1: py - ((px * betaF[px;py])-alphaF[px;py]); +mean: avg[100#s1]; +std: dev[100#s1]; +spreads: update dateTime: priceX[`dateTime], spread: s1, mean: mean ,up: mean+1.96*std, low: mean-1.96*std , operation:0 from spreads; / load and initialize kdb+tick / all tables in the top level namespace (.) become publish-able @@ -53,7 +39,7 @@ spreads: update dateTime: priceX[`dateTime], spread: s1, mean: avg[100#s1] ,up: .streamPair.spreads: 1000#tab3; //.streamPair.spreads: update spread:0 from .streamPair.spreads; // Timer function -timer:{t:.z.p;while[.z.p Date: Mon, 12 Feb 2024 16:29:15 +0100 Subject: [PATCH 04/21] Update streamPair.q --- streamPair.q | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/streamPair.q b/streamPair.q index 84beb5d..78f807a 100644 --- a/streamPair.q +++ b/streamPair.q @@ -1,9 +1,9 @@ // import linear regression module -\l C:/q/dash/sample/linear_regression.q +\l `:data/linear_regression.q // load tables -tab1: 40000#1_ flip `dateTime`bid`ask`bidVol`askVol!("*FFFF";",") 0: `:C:/q/dash/sample/data/stocks/USA500IDXUSD.csv; -tab2: 40000#1_ flip `dateTime`bid`ask`bidVol`askVol!("*FFFF";",") 0: `:C:/q/dash/sample/data/stocks/USATECHIDXUSD.csv; +tab1: 40000#1_ flip `dateTime`bid`ask`bidVol`askVol!("*FFFF";",") 0: `:data/USA500IDXUSD.csv; +tab2: 40000#1_ flip `dateTime`bid`ask`bidVol`askVol!("*FFFF";",") 0: `:data/USATECHIDXUSD.csv; tab3: flip `dateTime`spread`mean`up`low`operation!("P"$();"F"$();"F"$();"F"$();"F"$();"F"$()); // Fix data and take log(prices) @@ -25,7 +25,7 @@ spreads: update dateTime: priceX[`dateTime], spread: s1, mean: mean ,up: mean+1. / load and initialize kdb+tick / all tables in the top level namespace (.) become publish-able -\l C:/q/dash/sample/tick/u.q +\l `:tick/u.q .u.init[]; // Read and write on buffer functions @@ -63,4 +63,4 @@ timer:{t:.z.p;while[.z.p Date: Mon, 12 Feb 2024 16:29:43 +0100 Subject: [PATCH 05/21] Update streamPair.q --- streamPair.q | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/streamPair.q b/streamPair.q index 78f807a..32b1789 100644 --- a/streamPair.q +++ b/streamPair.q @@ -37,7 +37,7 @@ spreads: update dateTime: priceX[`dateTime], spread: s1, mean: mean ,up: mean+1. .streamPair.priceX: 1000#tAux; .streamPair.priceY: 1000#tAux; .streamPair.spreads: 1000#tab3; -//.streamPair.spreads: update spread:0 from .streamPair.spreads; + // Timer function timer:{t:.z.p;while[.z.p Date: Tue, 13 Feb 2024 20:04:26 +0100 Subject: [PATCH 06/21] Added doc to linear regression functions and fix minor bug in local path --- linear_regression.q | 25 ++++++++++++++++--------- streamPair.q | 2 +- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/linear_regression.q b/linear_regression.q index 8ae2845..0e27de4 100644 --- a/linear_regression.q +++ b/linear_regression.q @@ -2,17 +2,24 @@ // These formulas are derived from Ordinary Least Squares (OLS) regression for one variable // OLS regression is a method used to estimate the relationship between a dependent variable (y) and one or more independent variables (x). -// Function to calculate the beta coefficient (slope) using OLS -// The beta coefficient (slope) represents the change in the dependent variable for a one-unit change in the independent variable. -// This function computes the beta coefficient using the formula: -// beta = ((n * Σ(x*y)) - (Σx * Σy)) / ((n * Σ(x^2)) - (Σx)^2) +// @kind function +// @desc Function to calculate the beta coefficient (slope) using OLS +// The beta coefficient (slope) represents the change in the dependent variable for a one-unit change in the independent variable. +// This function computes the beta coefficient using the formula: +// beta = ((n * Σ(x*y)) - (Σx * Σy)) / ((n * Σ(x^2)) - (Σx)^2) +// @param x {number[]} Independent variable +// @param y {number[]} Dependent variable +// @return {number} Beta (slope) betaF:{dot:'[sum;*]; ((n*dot[x;y])-(*/)(sum')(x;y))% ((n:count[x])*dot[x;x])-sum[x]xexp 2}; - -// Function to calculate the alpha coefficient (intercept) using OLS -// The alpha coefficient (intercept) represents the value of the dependent variable when the independent variable is zero. -// This function computes the alpha coefficient using the formula: -// alpha = Mean(y) - beta * Mean(x) +// @kind function +// @desc Function to calculate the alpha coefficient (intercept) using OLS +// The alpha coefficient (intercept) represents the value of the dependent variable when the independent variable is zero. +// This function computes the alpha coefficient using the formula: +// alpha = Mean(y) - beta * Mean(x) +// @param x {number[]} Independent variable +// @param y {number[]} Dependent variable +// @return {number} alpha (intercept) alphaF: {avg[y]-(betaF[x;y]*avg[x])}; \ No newline at end of file diff --git a/streamPair.q b/streamPair.q index 32b1789..271d2d6 100644 --- a/streamPair.q +++ b/streamPair.q @@ -1,5 +1,5 @@ // import linear regression module -\l `:data/linear_regression.q +\l `:linear_regression.q // load tables tab1: 40000#1_ flip `dateTime`bid`ask`bidVol`askVol!("*FFFF";",") 0: `:data/USA500IDXUSD.csv; From 18d285b9250eb2806a35626f24ee0abb10834e03 Mon Sep 17 00:00:00 2001 From: Kokechacho <67198515+Kokechacho@users.noreply.github.com> Date: Wed, 14 Feb 2024 13:56:33 +0100 Subject: [PATCH 07/21] Add files via upload --- linear_regression.q | 27 +++++++++---------------- streamPair.q | 49 +++++++++++++++++++++++++++++++-------------- 2 files changed, 44 insertions(+), 32 deletions(-) diff --git a/linear_regression.q b/linear_regression.q index 0e27de4..f7daf02 100644 --- a/linear_regression.q +++ b/linear_regression.q @@ -2,24 +2,17 @@ // These formulas are derived from Ordinary Least Squares (OLS) regression for one variable // OLS regression is a method used to estimate the relationship between a dependent variable (y) and one or more independent variables (x). -// @kind function -// @desc Function to calculate the beta coefficient (slope) using OLS -// The beta coefficient (slope) represents the change in the dependent variable for a one-unit change in the independent variable. -// This function computes the beta coefficient using the formula: -// beta = ((n * Σ(x*y)) - (Σx * Σy)) / ((n * Σ(x^2)) - (Σx)^2) -// @param x {number[]} Independent variable -// @param y {number[]} Dependent variable -// @return {number} Beta (slope) -betaF:{dot:'[sum;*]; +// Function to calculate the beta coefficient (slope) using OLS +// The beta coefficient (slope) represents the change in the dependent variable for a one-unit change in the independent variable. +// This function computes the beta coefficient using the formula: +// beta = ((n * Σ(x*y)) - (Σx * Σy)) / ((n * Σ(x^2)) - (Σx)^2) +betaF:{dot:{sum x*y}; ((n*dot[x;y])-(*/)(sum')(x;y))% ((n:count[x])*dot[x;x])-sum[x]xexp 2}; -// @kind function -// @desc Function to calculate the alpha coefficient (intercept) using OLS -// The alpha coefficient (intercept) represents the value of the dependent variable when the independent variable is zero. -// This function computes the alpha coefficient using the formula: -// alpha = Mean(y) - beta * Mean(x) -// @param x {number[]} Independent variable -// @param y {number[]} Dependent variable -// @return {number} alpha (intercept) + +// Function to calculate the alpha coefficient (intercept) using OLS +// The alpha coefficient (intercept) represents the value of the dependent variable when the independent variable is zero. +// This function computes the alpha coefficient using the formula: +// alpha = Mean(y) - beta * Mean(x) alphaF: {avg[y]-(betaF[x;y]*avg[x])}; \ No newline at end of file diff --git a/streamPair.q b/streamPair.q index 271d2d6..40584dc 100644 --- a/streamPair.q +++ b/streamPair.q @@ -1,31 +1,33 @@ // import linear regression module -\l `:linear_regression.q +\l C:/q/dash/sample/linear_regression.q +\l C:/q/dash/sample/kalman_filter.q // load tables -tab1: 40000#1_ flip `dateTime`bid`ask`bidVol`askVol!("*FFFF";",") 0: `:data/USA500IDXUSD.csv; -tab2: 40000#1_ flip `dateTime`bid`ask`bidVol`askVol!("*FFFF";",") 0: `:data/USATECHIDXUSD.csv; +tab1: 40000#1_ flip `dateTime`bid`ask`bidVol`askVol!("*FFFF";",") 0: `:C:/q/dash/sample/data/stocks/USA500IDXUSD.csv; +tab2: 40000#1_ flip `dateTime`bid`ask`bidVol`askVol!("*FFFF";",") 0: `:C:/q/dash/sample/data/stocks/USATECHIDXUSD.csv; tab3: flip `dateTime`spread`mean`up`low`operation!("P"$();"F"$();"F"$();"F"$();"F"$();"F"$()); +historial_tab1: 40000#1_ flip `open`high`low`close`adjClose`vol!("FFFFFF";",") 0: `:C:/q/dash/sample/data/stocks/NASDAQ100_hist.csv; +historial_tab2: 40000#1_ flip `open`high`low`close`adjClose`vol!("FFFFFF";",") 0: `:C:/q/dash/sample/data/stocks/SP500_hist.csv; // Fix data and take log(prices) priceX: 0!1_(update delta:0f^deltas dateTime from distinct select distinct dateTime, log bid, log ask from update dateTime:"P"$@[;19;:;"."] each dateTime from tab1); priceY: 0!1_(update delta:0f^deltas dateTime from distinct select distinct dateTime, log bid, log ask from update dateTime:"P"$@[;19;:;"."] each dateTime from tab2); -spreads: 200#select from tab3; +spreads: 40000#select from tab3; +spreadskf: 40000#select from tab3; // Create an empty auxiliary table tAux: 1_1#priceX; profit: 0; -// Calculate alpha y beta and spreads -px: exec bid from priceX; -py: exec bid from priceY; -s1: py - ((px * betaF[px;py])-alphaF[px;py]); -mean: avg[100#s1]; -std: dev[100#s1]; -spreads: update dateTime: priceX[`dateTime], spread: s1, mean: mean ,up: mean+1.96*std, low: mean-1.96*std , operation:0 from spreads; +// Calculate alpha and beta from historical values +px: exec close from historial_tab1; +py: exec close from historial_tab2; +beta: betaF[px;py]; +alpha: alphaF[px;py]; / load and initialize kdb+tick / all tables in the top level namespace (.) become publish-able -\l `:tick/u.q +\l C:/q/dash/sample/tick/u.q .u.init[]; // Read and write on buffer functions @@ -37,6 +39,11 @@ spreads: update dateTime: priceX[`dateTime], spread: s1, mean: mean ,up: mean+1. .streamPair.priceX: 1000#tAux; .streamPair.priceY: 1000#tAux; .streamPair.spreads: 1000#tab3; +.streamPair.kfspreads: 1000#tab3; + +// Initial values for Kalman outside the loop +m0: zeros 2; +c0: eye 2; // Timer function timer:{t:.z.p;while[.z.p Date: Wed, 14 Feb 2024 13:57:42 +0100 Subject: [PATCH 08/21] Update streamPair.q --- streamPair.q | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/streamPair.q b/streamPair.q index 40584dc..4c060ca 100644 --- a/streamPair.q +++ b/streamPair.q @@ -1,13 +1,13 @@ // import linear regression module -\l C:/q/dash/sample/linear_regression.q -\l C:/q/dash/sample/kalman_filter.q +\l linear_regression.q +\l kalman_filter.q // load tables -tab1: 40000#1_ flip `dateTime`bid`ask`bidVol`askVol!("*FFFF";",") 0: `:C:/q/dash/sample/data/stocks/USA500IDXUSD.csv; -tab2: 40000#1_ flip `dateTime`bid`ask`bidVol`askVol!("*FFFF";",") 0: `:C:/q/dash/sample/data/stocks/USATECHIDXUSD.csv; +tab1: 40000#1_ flip `dateTime`bid`ask`bidVol`askVol!("*FFFF";",") 0: `:data/USA500IDXUSD.csv; +tab2: 40000#1_ flip `dateTime`bid`ask`bidVol`askVol!("*FFFF";",") 0: `:dataUSATECHIDXUSD.csv; tab3: flip `dateTime`spread`mean`up`low`operation!("P"$();"F"$();"F"$();"F"$();"F"$();"F"$()); -historial_tab1: 40000#1_ flip `open`high`low`close`adjClose`vol!("FFFFFF";",") 0: `:C:/q/dash/sample/data/stocks/NASDAQ100_hist.csv; -historial_tab2: 40000#1_ flip `open`high`low`close`adjClose`vol!("FFFFFF";",") 0: `:C:/q/dash/sample/data/stocks/SP500_hist.csv; +historial_tab1: 40000#1_ flip `open`high`low`close`adjClose`vol!("FFFFFF";",") 0: `:data/NASDAQ100_hist.csv; +historial_tab2: 40000#1_ flip `open`high`low`close`adjClose`vol!("FFFFFF";",") 0: `:data/SP500_hist.csv; // Fix data and take log(prices) priceX: 0!1_(update delta:0f^deltas dateTime from distinct select distinct dateTime, log bid, log ask from update dateTime:"P"$@[;19;:;"."] each dateTime from tab1); @@ -27,7 +27,7 @@ alpha: alphaF[px;py]; / load and initialize kdb+tick / all tables in the top level namespace (.) become publish-able -\l C:/q/dash/sample/tick/u.q +\l tick/u.q .u.init[]; // Read and write on buffer functions @@ -82,4 +82,4 @@ timer:{t:.z.p;while[.z.p Date: Wed, 14 Feb 2024 15:39:34 +0100 Subject: [PATCH 09/21] Update linear_regression.q --- linear_regression.q | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/linear_regression.q b/linear_regression.q index f7daf02..1858865 100644 --- a/linear_regression.q +++ b/linear_regression.q @@ -2,17 +2,25 @@ // These formulas are derived from Ordinary Least Squares (OLS) regression for one variable // OLS regression is a method used to estimate the relationship between a dependent variable (y) and one or more independent variables (x). -// Function to calculate the beta coefficient (slope) using OLS -// The beta coefficient (slope) represents the change in the dependent variable for a one-unit change in the independent variable. -// This function computes the beta coefficient using the formula: -// beta = ((n * Σ(x*y)) - (Σx * Σy)) / ((n * Σ(x^2)) - (Σx)^2) +// @kind function +// @desc Function to calculate the beta coefficient (slope) using OLS +// The beta coefficient (slope) represents the change in the dependent variable for a one-unit change in the independent variable. +// This function computes the beta coefficient using the formula: +// beta = ((n * Σ(x*y)) - (Σx * Σy)) / ((n * Σ(x^2)) - (Σx)^2) +// @param x {number[]} Independent variable +// @param y {number[]} Dependent variable +// @return {number} Beta (slope) betaF:{dot:{sum x*y}; ((n*dot[x;y])-(*/)(sum')(x;y))% ((n:count[x])*dot[x;x])-sum[x]xexp 2}; -// Function to calculate the alpha coefficient (intercept) using OLS -// The alpha coefficient (intercept) represents the value of the dependent variable when the independent variable is zero. -// This function computes the alpha coefficient using the formula: -// alpha = Mean(y) - beta * Mean(x) -alphaF: {avg[y]-(betaF[x;y]*avg[x])}; \ No newline at end of file +// @kind function +// @desc Function to calculate the alpha coefficient (intercept) using OLS +// The alpha coefficient (intercept) represents the value of the dependent variable when the independent variable is zero. +// This function computes the alpha coefficient using the formula: +// alpha = Mean(y) - beta * Mean(x) +// @param x {number[]} Independent variable +// @param y {number[]} Dependent variable +// @return {number} alpha (intercept) +alphaF: {avg[y]-(betaF[x;y]*avg[x])}; From 95f3ff5dcd2d728bb45895cc4c636ea2436234de Mon Sep 17 00:00:00 2001 From: Kokechacho <67198515+Kokechacho@users.noreply.github.com> Date: Wed, 14 Feb 2024 15:42:11 +0100 Subject: [PATCH 10/21] Update streamPair.q --- streamPair.q | 25 ++++++------------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/streamPair.q b/streamPair.q index 4c060ca..83acaf0 100644 --- a/streamPair.q +++ b/streamPair.q @@ -1,19 +1,18 @@ // import linear regression module \l linear_regression.q -\l kalman_filter.q // load tables -tab1: 40000#1_ flip `dateTime`bid`ask`bidVol`askVol!("*FFFF";",") 0: `:data/USA500IDXUSD.csv; -tab2: 40000#1_ flip `dateTime`bid`ask`bidVol`askVol!("*FFFF";",") 0: `:dataUSATECHIDXUSD.csv; +tab1: 1_ flip `dateTime`bid`ask`bidVol`askVol!("*FFFF";",") 0: `:data/USA500IDXUSD.csv; +tab2: 1_ flip `dateTime`bid`ask`bidVol`askVol!("*FFFF";",") 0: `:dataUSATECHIDXUSD.csv; tab3: flip `dateTime`spread`mean`up`low`operation!("P"$();"F"$();"F"$();"F"$();"F"$();"F"$()); -historial_tab1: 40000#1_ flip `open`high`low`close`adjClose`vol!("FFFFFF";",") 0: `:data/NASDAQ100_hist.csv; -historial_tab2: 40000#1_ flip `open`high`low`close`adjClose`vol!("FFFFFF";",") 0: `:data/SP500_hist.csv; +historial_tab1: 1_ flip `open`high`low`close`adjClose`vol!("FFFFFF";",") 0: `:data/NASDAQ100_hist.csv; +historial_tab2: 1_ flip `open`high`low`close`adjClose`vol!("FFFFFF";",") 0: `:data/SP500_hist.csv; // Fix data and take log(prices) priceX: 0!1_(update delta:0f^deltas dateTime from distinct select distinct dateTime, log bid, log ask from update dateTime:"P"$@[;19;:;"."] each dateTime from tab1); priceY: 0!1_(update delta:0f^deltas dateTime from distinct select distinct dateTime, log bid, log ask from update dateTime:"P"$@[;19;:;"."] each dateTime from tab2); -spreads: 40000#select from tab3; -spreadskf: 40000#select from tab3; +spreads: select from tab3; +spreadskf: select from tab3; // Create an empty auxiliary table tAux: 1_1#priceX; @@ -39,11 +38,6 @@ alpha: alphaF[px;py]; .streamPair.priceX: 1000#tAux; .streamPair.priceY: 1000#tAux; .streamPair.spreads: 1000#tab3; -.streamPair.kfspreads: 1000#tab3; - -// Initial values for Kalman outside the loop -m0: zeros 2; -c0: eye 2; // Timer function timer:{t:.z.p;while[.z.p Date: Wed, 14 Feb 2024 15:43:08 +0100 Subject: [PATCH 11/21] Update streamPair.q --- streamPair.q | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/streamPair.q b/streamPair.q index 83acaf0..51d713d 100644 --- a/streamPair.q +++ b/streamPair.q @@ -19,9 +19,7 @@ tAux: 1_1#priceX; profit: 0; // Calculate alpha and beta from historical values -px: exec close from historial_tab1; -py: exec close from historial_tab2; -beta: betaF[px;py]; +beta: betaF[px:historial_tab1`close;py:historial_tab2`close]; alpha: alphaF[px;py]; / load and initialize kdb+tick From caecef870d722a1b522d6760cc3416a0fb45ec2f Mon Sep 17 00:00:00 2001 From: Kokechacho <67198515+Kokechacho@users.noreply.github.com> Date: Wed, 14 Feb 2024 15:47:04 +0100 Subject: [PATCH 12/21] Create u.q --- tick/u.q | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 tick/u.q diff --git a/tick/u.q b/tick/u.q new file mode 100644 index 0000000..21f42e4 --- /dev/null +++ b/tick/u.q @@ -0,0 +1,18 @@ +/2019.06.17 ensure sym has g attr for schema returned to new subscriber +/2008.09.09 .k -> .q +/2006.05.08 add + +\d .u +init:{w::t!(count t::tables`.)#()} + +del:{w[x]_:w[x;;0]?y};.z.pc:{del[;x]each t}; + +sel:{$[`~y;x;select from x where sym in y]} + +pub:{[t;x]{[t;x;w]if[count x:sel[x]w 1;(neg first w)(`upd;t;x)]}[t;x]each w t} + +add:{$[(count w x)>i:w[x;;0]?.z.w;.[`.u.w;(x;i;1);union;y];w[x],:enlist(.z.w;y)];(x;$[99=type v:value x;sel[v]y;@[0#v;`sym;`g#]])} + +sub:{if[x~`;:sub[;y]each t];if[not x in t;'x];del[x].z.w;add[x;y]} + +end:{(neg union/[w[;;0]])@\:(`.u.end;x)} From 444bb695d6b8842e2a19480e21040252cef83e63 Mon Sep 17 00:00:00 2001 From: Kokechacho Date: Thu, 15 Feb 2024 12:58:30 +0100 Subject: [PATCH 13/21] modified: streamPair.q ola --- streamPair.q | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/streamPair.q b/streamPair.q index 51d713d..674e040 100644 --- a/streamPair.q +++ b/streamPair.q @@ -3,10 +3,10 @@ // load tables tab1: 1_ flip `dateTime`bid`ask`bidVol`askVol!("*FFFF";",") 0: `:data/USA500IDXUSD.csv; -tab2: 1_ flip `dateTime`bid`ask`bidVol`askVol!("*FFFF";",") 0: `:dataUSATECHIDXUSD.csv; +tab2: 1_ flip `dateTime`bid`ask`bidVol`askVol!("*FFFF";",") 0: `:data/USATECHIDXUSD.csv; tab3: flip `dateTime`spread`mean`up`low`operation!("P"$();"F"$();"F"$();"F"$();"F"$();"F"$()); -historial_tab1: 1_ flip `open`high`low`close`adjClose`vol!("FFFFFF";",") 0: `:data/NASDAQ100_hist.csv; -historial_tab2: 1_ flip `open`high`low`close`adjClose`vol!("FFFFFF";",") 0: `:data/SP500_hist.csv; +historial_tab1: 1_ flip `open`high`low`close`adjClose`vol!("FFFFFF";",") 0: `:data/SP500_hist.csv; +historial_tab2: 1_ flip `open`high`low`close`adjClose`vol!("FFFFFF";",") 0: `:data/NASDAQ100_hist.csv; // Fix data and take log(prices) priceX: 0!1_(update delta:0f^deltas dateTime from distinct select distinct dateTime, log bid, log ask from update dateTime:"P"$@[;19;:;"."] each dateTime from tab1); @@ -19,8 +19,8 @@ tAux: 1_1#priceX; profit: 0; // Calculate alpha and beta from historical values -beta: betaF[px:historial_tab1`close;py:historial_tab2`close]; -alpha: alphaF[px;py]; +beta_lr: betaF[px:-100#log historial_tab1`close;py:-100#log historial_tab2`close]; // we only take most recent 100 values for the alpha and beta +alpha_lr: alphaF[px;py]; / load and initialize kdb+tick / all tables in the top level namespace (.) become publish-able @@ -50,7 +50,7 @@ timer:{t:.z.p;while[.z.p Date: Thu, 15 Feb 2024 13:01:44 +0100 Subject: [PATCH 14/21] Update streamPair.q --- streamPair.q | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/streamPair.q b/streamPair.q index 674e040..055458b 100644 --- a/streamPair.q +++ b/streamPair.q @@ -50,7 +50,7 @@ timer:{t:.z.p;while[.z.p Date: Fri, 16 Feb 2024 12:10:06 +0100 Subject: [PATCH 15/21] modified: streamPair.q --- streamPair.q | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/streamPair.q b/streamPair.q index 674e040..ee578b2 100644 --- a/streamPair.q +++ b/streamPair.q @@ -12,7 +12,6 @@ historial_tab2: 1_ flip `open`high`low`close`adjClose`vol!("FFFFFF";",") 0: `:da priceX: 0!1_(update delta:0f^deltas dateTime from distinct select distinct dateTime, log bid, log ask from update dateTime:"P"$@[;19;:;"."] each dateTime from tab1); priceY: 0!1_(update delta:0f^deltas dateTime from distinct select distinct dateTime, log bid, log ask from update dateTime:"P"$@[;19;:;"."] each dateTime from tab2); spreads: select from tab3; -spreadskf: select from tab3; // Create an empty auxiliary table tAux: 1_1#priceX; @@ -21,6 +20,8 @@ profit: 0; // Calculate alpha and beta from historical values beta_lr: betaF[px:-100#log historial_tab1`close;py:-100#log historial_tab2`close]; // we only take most recent 100 values for the alpha and beta alpha_lr: alphaF[px;py]; +// We calculate an historical standard deviation +std_lr: dev[(1000#exec bid from priceY) - (1000#exec bid from priceX)]; / load and initialize kdb+tick / all tables in the top level namespace (.) become publish-able @@ -33,6 +34,7 @@ alpha_lr: alphaF[px;py]; // Initialize index and empty tables (We will access directly to these objects from dashboards) .streamPair.i:-1; +.streamPair.iEWMA:-1; .streamPair.priceX: 1000#tAux; .streamPair.priceY: 1000#tAux; .streamPair.spreads: 1000#tab3; @@ -49,9 +51,11 @@ timer:{t:.z.p;while[.z.p Date: Fri, 16 Feb 2024 12:16:04 +0100 Subject: [PATCH 16/21] modified: streamPair.q --- streamPair.q | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/streamPair.q b/streamPair.q index 8d1d24a..e2cbc05 100644 --- a/streamPair.q +++ b/streamPair.q @@ -4,7 +4,7 @@ // load tables tab1: 1_ flip `dateTime`bid`ask`bidVol`askVol!("*FFFF";",") 0: `:data/USA500IDXUSD.csv; tab2: 1_ flip `dateTime`bid`ask`bidVol`askVol!("*FFFF";",") 0: `:data/USATECHIDXUSD.csv; -tab3: flip `dateTime`spread`mean`up`low`operation!("P"$();"F"$();"F"$();"F"$();"F"$();"F"$()); +tab3: flip `dateTime`spread`mean`up`low`ewma!("P"$();"F"$();"F"$();"F"$();"F"$();"F"$()); historial_tab1: 1_ flip `open`high`low`close`adjClose`vol!("FFFFFF";",") 0: `:data/SP500_hist.csv; historial_tab2: 1_ flip `open`high`low`close`adjClose`vol!("FFFFFF";",") 0: `:data/NASDAQ100_hist.csv; @@ -55,7 +55,7 @@ timer:{t:.z.p;while[.z.p Date: Fri, 16 Feb 2024 12:38:55 +0100 Subject: [PATCH 17/21] modified: streamPair.q --- streamPair.q | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/streamPair.q b/streamPair.q index e2cbc05..d9515f7 100644 --- a/streamPair.q +++ b/streamPair.q @@ -55,7 +55,7 @@ timer:{t:.z.p;while[.z.p Date: Fri, 16 Feb 2024 13:01:06 +0100 Subject: [PATCH 18/21] Update streamPair.q --- streamPair.q | 1 - 1 file changed, 1 deletion(-) diff --git a/streamPair.q b/streamPair.q index d9515f7..f5e3142 100644 --- a/streamPair.q +++ b/streamPair.q @@ -51,7 +51,6 @@ timer:{t:.z.p;while[.z.p Date: Fri, 16 Feb 2024 13:02:59 +0100 Subject: [PATCH 19/21] Update streamPair.q --- streamPair.q | 1 + 1 file changed, 1 insertion(+) diff --git a/streamPair.q b/streamPair.q index f5e3142..433ee23 100644 --- a/streamPair.q +++ b/streamPair.q @@ -53,6 +53,7 @@ timer:{t:.z.p;while[.z.p Date: Wed, 28 Feb 2024 13:42:51 +0100 Subject: [PATCH 20/21] Update streamPair.q EWMA and moving stds Fixed EWMA values, now Spreads table has up and up2 columns refered to static std and moving std respectively, and also added an ewma column for ewma deviations values --- streamPair.q | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/streamPair.q b/streamPair.q index 433ee23..efc901c 100644 --- a/streamPair.q +++ b/streamPair.q @@ -4,7 +4,7 @@ // load tables tab1: 1_ flip `dateTime`bid`ask`bidVol`askVol!("*FFFF";",") 0: `:data/USA500IDXUSD.csv; tab2: 1_ flip `dateTime`bid`ask`bidVol`askVol!("*FFFF";",") 0: `:data/USATECHIDXUSD.csv; -tab3: flip `dateTime`spread`mean`up`low`ewma!("P"$();"F"$();"F"$();"F"$();"F"$();"F"$()); +tab3: flip `dateTime`spread`mean`up`low`ewma`up2`low2!("P"$();"F"$();"F"$();"F"$();"F"$();"F"$();"F"$();"F"$()); historial_tab1: 1_ flip `open`high`low`close`adjClose`vol!("FFFFFF";",") 0: `:data/SP500_hist.csv; historial_tab2: 1_ flip `open`high`low`close`adjClose`vol!("FFFFFF";",") 0: `:data/NASDAQ100_hist.csv; @@ -52,10 +52,9 @@ timer:{t:.z.p;while[.z.p999;.streamPair.iEWMA:998;.streamPair.iEWMA+:1]; + resSpread: enlist `dateTime`spread`mean`up`low`ewma`up2`low2!("p"$(priceX[.streamPair.i][`dateTime]);"f"$(s);"f"$(0);"f"$(1.96*std_lr);"f"$(-1.96*std_lr);"f"$0f^(ewma); "f"$(0f^(1.96*(last 1000 mdev (exec spread from .streamPair.spreads)))); "f"$0f^(-1.96*(last 1000 mdev (exec spread from .streamPair.spreads)))); // We update our buffer tables with those values .ringBuffer.write[`.streamPair.priceX;resX;.streamPair.i]; From f0efad1201a91ceb0bd3706dd9ba1f50f87e377d Mon Sep 17 00:00:00 2001 From: Kokechacho <67198515+Kokechacho@users.noreply.github.com> Date: Mon, 8 Apr 2024 13:08:23 +0200 Subject: [PATCH 21/21] Update streamPair.q Take away some innecesary lines --- streamPair.q | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/streamPair.q b/streamPair.q index efc901c..4421cba 100644 --- a/streamPair.q +++ b/streamPair.q @@ -11,11 +11,6 @@ historial_tab2: 1_ flip `open`high`low`close`adjClose`vol!("FFFFFF";",") 0: `:da // Fix data and take log(prices) priceX: 0!1_(update delta:0f^deltas dateTime from distinct select distinct dateTime, log bid, log ask from update dateTime:"P"$@[;19;:;"."] each dateTime from tab1); priceY: 0!1_(update delta:0f^deltas dateTime from distinct select distinct dateTime, log bid, log ask from update dateTime:"P"$@[;19;:;"."] each dateTime from tab2); -spreads: select from tab3; - -// Create an empty auxiliary table -tAux: 1_1#priceX; -profit: 0; // Calculate alpha and beta from historical values beta_lr: betaF[px:-100#log historial_tab1`close;py:-100#log historial_tab2`close]; // we only take most recent 100 values for the alpha and beta @@ -35,7 +30,7 @@ std_lr: dev[(1000#exec bid from priceY) - (1000#exec bid from priceX)]; // Initialize index and empty tables (We will access directly to these objects from dashboards) .streamPair.i:-1; .streamPair.iEWMA:-1; -.streamPair.priceX: 1000#tAux; +.streamPair.priceX: 1000#tAux: 1_1#priceX; .streamPair.priceY: 1000#tAux; .streamPair.spreads: 1000#tab3;