Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
femtotrader committed Apr 23, 2024
0 parents commit fd627fc
Show file tree
Hide file tree
Showing 13 changed files with 284 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/" # Location of package manifests
schedule:
interval: "weekly"
41 changes: 41 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: CI
on:
push:
branches:
- main
tags: ['*']
pull_request:
workflow_dispatch:
concurrency:
# Skip intermediate builds: always.
# Cancel intermediate builds: only if it is a pull request build.
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ startsWith(github.ref, 'refs/pull/') }}
jobs:
test:
name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} - ${{ github.event_name }}
runs-on: ${{ matrix.os }}
timeout-minutes: 60
permissions: # needed to allow julia-actions/cache to proactively delete old caches that it has created
actions: write
contents: read
strategy:
fail-fast: false
matrix:
version:
- '1.10'
- '1.6'
- 'nightly'
os:
- ubuntu-latest
arch:
- x64
steps:
- uses: actions/checkout@v4
- uses: julia-actions/setup-julia@v1
with:
version: ${{ matrix.version }}
arch: ${{ matrix.arch }}
- uses: julia-actions/cache@v1
- uses: julia-actions/julia-buildpkg@v1
- uses: julia-actions/julia-runtest@v1
16 changes: 16 additions & 0 deletions .github/workflows/CompatHelper.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: CompatHelper
on:
schedule:
- cron: 0 0 * * *
workflow_dispatch:
jobs:
CompatHelper:
runs-on: ubuntu-latest
steps:
- name: Pkg.add("CompatHelper")
run: julia -e 'using Pkg; Pkg.add("CompatHelper")'
- name: CompatHelper.main()
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COMPATHELPER_PRIV: ${{ secrets.DOCUMENTER_KEY }}
run: julia -e 'using CompatHelper; CompatHelper.main()'
31 changes: 31 additions & 0 deletions .github/workflows/TagBot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: TagBot
on:
issue_comment:
types:
- created
workflow_dispatch:
inputs:
lookback:
default: "3"
permissions:
actions: read
checks: read
contents: write
deployments: read
issues: read
discussions: read
packages: read
pages: read
pull-requests: read
repository-projects: read
security-events: read
statuses: read
jobs:
TagBot:
if: github.event_name == 'workflow_dispatch' || github.actor == 'JuliaTagBot'
runs-on: ubuntu-latest
steps:
- uses: JuliaRegistries/TagBot@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
ssh: ${{ secrets.DOCUMENTER_KEY }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/Manifest.toml
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 FemtoTrader <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
16 changes: 16 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name = "OnlinePortfolioAnalysis"
uuid = "d39dbdee-9d7b-4a6f-a064-c0eaa0a6e939"
authors = ["FemtoTrader <[email protected]>"]
version = "1.0.0-DEV"

[deps]
OnlineStatsBase = "925886fa-5bf2-5e8e-b522-a9147a512338"

[compat]
julia = "1.6.7"

[extras]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Test"]
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# OnlinePortfolioAnalysis

[![Build Status](https://github.com/femtotrader/OnlinePortfolioAnalysis.jl/actions/workflows/CI.yml/badge.svg?branch=main)](https://github.com/femtotrader/OnlinePortfolioAnalysis.jl/actions/workflows/CI.yml?query=branch%3Amain)
18 changes: 18 additions & 0 deletions src/OnlinePortfolioAnalysis.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module OnlinePortfolioAnalysis

using OnlineStatsBase

export SimpleAssetReturn
export LogAssetReturn
export StdDev

export fit!, value

abstract type PortfolioAnalysis{T} <: OnlineStat{T} end

include("asset_return.jl")
include("std_dev.jl")

include("value_at_risk.jl")

end
61 changes: 61 additions & 0 deletions src/asset_return.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
mutable struct SimpleAssetReturn{T} <: PortfolioAnalysis{T}
value::Union{Missing,T}
n::Int

ready::Bool

period::Int

input_values::CircBuff

function SimpleAssetReturn{T}(; period::Int=1) where {T}
input_values = CircBuff(T, period + 1, rev = false)
new{T}(missing, 0, false, period, input_values)
end
end

function OnlineStatsBase._fit!(stat::SimpleAssetReturn, data)
fit!(stat.input_values, data)
stat.n += 1
if stat.n > stat.period
data_prev = stat.input_values[end-stat.period]
stat.value = (data - data_prev) / data_prev
return stat.value
else
stat.ready = true
stat.value = missing
return stat.value
end
end



mutable struct LogAssetReturn{T} <: PortfolioAnalysis{T}
value::Union{Missing,T}
n::Int

ready::Bool

period::Int

input_values::CircBuff

function LogAssetReturn{T}(; period::Int=1) where {T}
input_values = CircBuff(T, period + 1, rev = false)
new{T}(missing, 0, false, period, input_values)
end
end

function OnlineStatsBase._fit!(stat::LogAssetReturn, data)
fit!(stat.input_values, data)
stat.n += 1
if stat.n > stat.period
data_prev = stat.input_values[end-stat.period]
stat.value = log(data / data_prev) # log=ln (Neperian log not decimal log)
return stat.value
else
stat.ready = true
stat.value = missing
return stat.value
end
end
17 changes: 17 additions & 0 deletions src/std_dev.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
mutable struct StdDev{T} <: PortfolioAnalysis{T}
value::T
n::Int

variance::Variance

function StdDev{T}() where {T}
variance = Variance()
new{T}(1, 0, variance)
end
end

function OnlineStatsBase._fit!(stat::StdDev, data)
fit!(stat.variance, data)
stat.n += 1
stat.value = sqrt(value(stat.variance))
end
Empty file added src/value_at_risk.jl
Empty file.
52 changes: 52 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using OnlinePortfolioAnalysis
using OnlineStatsBase
using Test

const ATOL = 0.0001
const TSLA = [235.22,264.51,225.16,222.64,236.48,208.40,226.56,229.06,245.24,258.49,371.33,381.58,352.26]
const NFLX = [540.73,532.39,538.85,521.66,513.47,502.81,528.21,517.57,569.19,610.34,690.31,641.90,602.44]
const MSFT = [222.42,231.96,232.38,235.77,252.18,249.68,270.90,284.91,301.88,281.92,331.62,330.59,336.32]
const weights = [0.4, 0.4, 0.2]

@testset "OnlinePortfolioAnalysis.jl" begin
@testset "SimpleAssetReturn" begin
stat = SimpleAssetReturn{Float64}()
fit!(stat, TSLA[1])
fit!(stat, TSLA[2])
@test round(value(stat), digits=4) == 0.1245
end

@testset "SimpleAssetReturn with period=3" begin
stat = SimpleAssetReturn{Float64}(period=3)
fit!(stat, TSLA[1])
fit!(stat, TSLA[2])
fit!(stat, TSLA[3])
fit!(stat, TSLA[4])
@test isapprox(value(stat), (222.64 - 235.22) / 235.2, atol=ATOL)
end

@testset "LogAssetReturn" begin
stat = LogAssetReturn{Float64}()
fit!(stat, TSLA[1])
fit!(stat, TSLA[2])
@test isapprox(value(stat), 0.1174, atol=ATOL)
end

@testset "StdDev" begin
stat = StdDev{Float64}()
fit!(stat, TSLA)
@test isapprox(value(stat), 60.5448, atol=ATOL)
end

@testset "MeanReturn" begin
stat = Mean()
fit!(stat, TSLA)
@test isapprox(value(stat), 265.9177, atol=ATOL)
end

@testset "GeometricMeanReturn" begin

end


end

0 comments on commit fd627fc

Please sign in to comment.