Coder Social home page Coder Social logo

lamonkey / portfolio-monitoring-app Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 301 KB

A web app to monitor your stock portfolio's performance

Home Page: https://huggingface.co/spaces/lamonkey/portfolio_management

License: GNU General Public License v3.0

Dockerfile 0.56% Python 99.19% HTML 0.24%
stock-market stock-portfolio stock-portfolio-site

portfolio-monitoring-app's Introduction

Docker Image CI

Portfolio monitoring app

A web application to monitor portfolio's performance image

Try it yourself using docker

The web app currently only supports Chinese equities, and it is using jqsdk to acquire data thus you need to have a free account with them.

Apple Silicon

docker pull lamonkey/risk-monitor-app:arm64
docker volume create rma_volumne
docker run -p 7860:7860 -e JQDATA_USER=USERNAME -e JQDATA_PASSWORD=PASSWORD -e SECRET_COOKIE=my_super_safe_cookie_secret -v rma_volume:/code/instance lamonkey/risk-monitor-app:arm64

Go to localhost:7860 The default username and password is user and password

Intel Machine

docker pull lamonkey/risk-monitor-app:amd64
docker volume create rma_volumne
docker run -p 7860:7860 -e JQDATA_USER=USERNAME -e JQDATA_PASSWORD=PASSWORD -e SECRET_COOKIE=my_super_safe_cookie_secret -v rma_volume:/code/instance lamonkey/risk-monitor-app:amd64

Currently Supported Analysis

Currently Supported Feature

  • Real-time streaming of PnL, Compound return and Maximum drawdown
  • Interactive plot and table
  • Schedule to update stock price
  • User Login
  • Customizable layout

Tech Stack

panel

Development

git pull https://github.com/Lamonkey/portfolio-monitoring-app.git
cd portfolio-monitoring-app
python -m venv venv
source venvbin/activate
pip install -r requirements.txt

Then create a .env file at the project root

JQDATA_USER=USERNAME
JQDATA_PASSWORD=PASSWORD

Then run the app

cd src
panel serve pages/* --basic-auth ./credential.json --cookie-secret=${SECRET_COOKIE} --setup backgroundTask.py --address 0.0.0.0 --port 7860 --allow-websocket-origin "*"

portfolio-monitoring-app's People

Contributors

dependabot[bot] avatar lamonkey avatar

Stargazers

 avatar

Watchers

 avatar

portfolio-monitoring-app's Issues

Incorrect PnL result

The calculated PnL doesn't match with "actual PnL"
image
image

The "actual PnL" between [2023-09-21 09:47:29.000, 2023-09-26 10:47:48.776] is around 3000, but what I was able to get is only 1493.88 or if I calculated using google sheet is around 1.2k.

google sheet 1
google sheet 2

Notes: Sep,21 should have PnL

Error in calculate portfolio return

Whenever the share changes the return needs to be recalculated.
example

date ticker price shares
2023.1.1 600425 30 100
2023.1.2 600425 31 100
2023.1.3 600425 32 200
2023.1.4 600425 32 200

since the adjustment only occurred on Tuesday, thus it is okay to use cumprod(return+1) -1 to calculate the periodical return,
however for periods longer than that, I need to use PnL to calculate the accumulative return,

cum_return = cum_pnl / initial_cash

For the accumulative return of the whole portfolio, using cum_pnl / initial_cash

also whenever the stock holding position changes minus their PnL by processing fee 1/1000 * pnl

Improve sector view

image

maybe add a star plot to view the active weight, $W_a$, between portfolio and benchmark

if $W_a$ is 0 it show as 1,
if $W_b$ is 0 and $W_p$ is 0 then it is not shown on star plot
if $W_b$ is 0 and $W_a$ is not 0 then it show as $W_a$
if $W_a$ is positive then it show as 1 + $W_a$

image

$W_a= W_p - W_b$

handle exception when no portfolio

2023-09-28 09:24:51,141 Exception in callback functools.partial(<bound method IOLoop._discard_future_result of <tornado.platform.asyncio.AsyncIOMainLoop object at 0x151686990>>, <Task finished name='Task-1' coro=<_state._scheduled_cb() done, defined at /Users/lamonkey/miniconda3/envs/risk-dashboard/lib/python3.11/site-packages/panel/io/state.py:394> exception=ValueError('You are trying to merge on datetime64[ns] and object columns. If you wish to proceed you should use pd.concat')>)

optimize db.get_stocks_price

Change to only select stock prices within a window. Currently, the function picks all stock prices' within the DB.

Calculation on MDD is wrong

MDD = $\frac{Trough Value - PeakValue}{PeakValue}$
$TroughValue$: min(capital) after peak
$PeakValue$: max(capital) within a window

I used the wrong through value

backup database

Need to find a method to backup the portfolio holding position
how to backup db

but I don't need to backup the whole database just portfolio table

I could just upload the local.db to a oss storage periodically

post new entry trigger update

Feature: when uploading entry from API force to recalculate if data are available.

Why: It is possible the user didn't upload the new portfolio profile 1 or 2 days after updating the portfolio. In this case, I would like the web app to redo the calculation.

How: just check the portfolio date with the current beijing time. If the date is from yesterday then force to update

deploy using docker

current docker file

FROM python:3.9

# Directory to store the code
RUN mkdir /code

WORKDIR /code

COPY ./requirements.txt /code/requirements.txt
COPY . /code

RUN python3 -m pip install --no-cache-dir --upgrade pip
RUN python3 -m pip install --no-cache-dir --upgrade -r /code/requirements.txt

WORKDIR /code/src/riskMonitoring

# Start the panel app
CMD ["panel", "serve", "pages/*", "--basic-auth", "/code/instance/credential.json", "--cookie-secret=${SECRET_COOKIE}", "--setup","backgroundTask.py", "--address", "0.0.0.0", "--port", "7860", "--allow-websocket-origin", "*", '--admin', '--rest-provider', 'tranquilizer' ]      
# CMD ["panel", "serve", "/code/src/riskMonitoring/pages/*"]

error

/bin/sh: 1: [panel,: not found

However when running interactive container

docker run -t -i lamonkey/risk-monitor-app:arm64 bash

I can run panel like this

overview Card, benchmark missing entry

image

How to replicate this issue:

upload two portfolio records, Then at the trading date portfolio will have one extra entry compared to the benchmark result. Because the benchmark only has the stock price at market close. But a portfolio can have the stock price at trading time.

Proposed fix

backward fill missing value on benchmark result

Admin Page

  • Manage login credential
  • Manage alert event

fix aggregation to accomendate new design

aggregate_sector_daily summed all ts entries on the same date. This works because only one entry is allowed for each stock at each date. But with the new design, an arbitrary number of ts entries are allowed for a date.

  • fix active weight of sector
  • check bestAndWorstStocks.py
  • check maxDrawDown.py
  • check overview.py
  • check returnAnalysis.py
  • check sectorPerformance.py
  • [] check portfolioComposition.py

ERROR: (sqlite3.OperationalError) near "portfolio_profile": syntax error

delete local.db in instance first then run

panel serve pages/* --basic-auth /code/instance/credential.json --cookie-secret=${SECRET_COOKIE} --setup backgroundTask.py --address 0.0.0.0 --port 7860 --admin --rest-provider tranquilizer --allow-websocket-origin "*"

got this error

ERROR: (sqlite3.OperationalError) near "portfolio_profile": syntax error
[SQL: portfolio_profile]
(Background on this error at: https://sqlalche.me/e/20/e3q8)

This happened because table is not created in local.db.

probable cause

  1. backgroundTask.py is not run via the setup
  2. backgroundTask.py invoked but initialize_db.py is not executed.

validate the overview card result

After finishing implementing new feature for the overview card

Compare the local result from the overview component with the cloud's result.

The cloud has the correct result.

Exam Attribute of Sector Return

Use sectors to calculate the attribute, and compare the result to result calculated using individual stock. Both of them should yield the same result.

Preserve Stock's average price from portfolio profile

To include the exact average price of the portfolio's stock.

Currently only using the close price of that day.

This step doesn't contribute to the precision of the PnL calculation.

As the Cum PnL = total_capital_i - total_capital_(i-1)

Error in processing.agg_to_daily

This method should remove all entries within the same date that do not have the maximum time stamp before merging. Currently, it just aggregates by the sum of all the entries within the same date.

Add Alerting Feature

watch events and send emails to alert after market close

  • Individual stock price's total changes and daily percentage changes
  • Total max drawdown and daily max drawdown
  • cumulative return and daily cumulative return
  • Daily capital percentage change and total percentage chagne

Line plot of tracking error and risk

tracking error is the normalized ( std(daily active return)*sqrt(252))
risk is the normalized standard deviation of daily return (std(daily return)) * sqrt(252))

Add these two plot to the overview card

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.