Compare commits
109 Commits
3e412615b9
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| a2848481eb | |||
| 06bfc85485 | |||
| 6aec58b05e | |||
| 412d09cbe1 | |||
| b0c7d0192a | |||
| 0652ef3a7e | |||
| 0181800430 | |||
| 2fb220ccfb | |||
| c70713356d | |||
| 4a02ad0e51 | |||
| b2bf0ad683 | |||
| af1497c1c2 | |||
| 099a42d214 | |||
| c728795a9a | |||
| 78f9b15413 | |||
| 29e04d9533 | |||
| 4dc63bd6ab | |||
| 58c2895238 | |||
| adb75b4e22 | |||
| 3415280a33 | |||
| 3cd119dda1 | |||
| 0ca094d353 | |||
| 6c0c786c70 | |||
| a8132e3992 | |||
| 705df375c9 | |||
| 5326e3f5bc | |||
| 9f72619f51 | |||
| b279735ae8 | |||
| 76f556b1ad | |||
| 21125e84e5 | |||
| 0109799a4c | |||
| 8b83d450d0 | |||
| 6b51e610ff | |||
| 4328807975 | |||
| 8f307438cf | |||
| b79313d1e4 | |||
| 694184a25f | |||
| 0a6d7264cb | |||
| f66f1934a1 | |||
| f0c3303246 | |||
| 4935a317a6 | |||
| 5adfc2920f | |||
| c835b51245 | |||
| e2be72b43b | |||
| 4122d60f34 | |||
| 544fdb2a7a | |||
| e1a4bb4a46 | |||
| cd55084d54 | |||
| 4e2c90fe0b | |||
| afe83c4811 | |||
| a7a94fdd18 | |||
| bae56de644 | |||
| 5536abbd0b | |||
| fb0c346e2f | |||
| d3352c4d28 | |||
| c9cc555b42 | |||
| fa4fec677b | |||
| 092bf62581 | |||
| 4f3f7fdbec | |||
| 8b17a7053b | |||
| 2cea87bce7 | |||
| bf0bb1e77e | |||
| 98d185067b | |||
| 655c3b4032 | |||
| edfa828c2a | |||
| c0a3e85888 | |||
| f69d75b1b7 | |||
| c3e0060347 | |||
| d71f344777 | |||
| dcf5336a75 | |||
| f36485f1eb | |||
| 16bb308c4b | |||
| d1805e5fc4 | |||
| a274818463 | |||
| 5338017760 | |||
| 26cc075984 | |||
| 7e21738be2 | |||
| d6cefa56d1 | |||
| ae70ddfb48 | |||
| ed92ab87af | |||
| a3b1387022 | |||
| 8123deb54c | |||
| 2261635fb5 | |||
| a2c90f47e3 | |||
| 6b9c31bf2b | |||
| 335c1ee349 | |||
| 53861b2a8e | |||
| 21cddf372b | |||
| 55a27389a2 | |||
| 7680e1fca8 | |||
| 4cbff23680 | |||
| a45b4e5f51 | |||
| ee77fa4522 | |||
| d7087c738d | |||
| ec677cc41c | |||
| cab4d7e0df | |||
| 62ad7ce2da | |||
| 244667d11a | |||
| 71ff0b28a8 | |||
| f34c5ddf89 | |||
| e991042eaa | |||
| d3714630b2 | |||
| 170ad50652 | |||
| e15740900a | |||
| e3602cc7b8 | |||
| 0dcdc33e7b | |||
| a1c60d1696 | |||
| ff46ae8e8a | |||
| 61c805ebfd |
7
.gitignore
vendored
7
.gitignore
vendored
@@ -9,10 +9,13 @@
|
||||
/dabo/.*-secrets
|
||||
/home/.ssh
|
||||
/home/.viminfo
|
||||
/home/.bash_history
|
||||
/home/.dabo-bot.sh.lock
|
||||
.*history
|
||||
.wget-hsts
|
||||
/analyze-*
|
||||
*.del
|
||||
*.tmp
|
||||
*local*
|
||||
test*.sh
|
||||
/env
|
||||
/genpw.sh
|
||||
/watch-assets.csv*
|
||||
|
||||
365
README.md
365
README.md
@@ -13,7 +13,7 @@ It is still under development and is currently only partially functional.
|
||||
|
||||
## Copyright / License
|
||||
|
||||
Copyright (c) 2022-2024 Oliver Bohlen (aka olli)
|
||||
Copyright (c) 2022-2024 Oliver Bohlen (aka olli/egabosh)
|
||||
|
||||
The software provided here is called dabo (crypto bot)
|
||||
|
||||
@@ -24,20 +24,24 @@ dabo is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY
|
||||
You should have received a copy of the GNU General Public License along with dabo (see COPYING file). If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
## Data sources
|
||||
Various data sources such as finance.yahoo.com and crypto exchanges available via ccxt are used. please check whether this is legal in your region before use.
|
||||
- query1.finance.yahoo.com (economic data,...)
|
||||
- 30rates.com (forecast)
|
||||
- api.coingecko.com (Market-Cap)
|
||||
- fapi.binance.com (OpenInterest,...)
|
||||
- api.alternative.me (Fear and Greed)
|
||||
Various data sources such as finance.yahoo.com and crypto exchanges available via ccxt are used. Please check whether this is legal in your region before use.
|
||||
- https://query1.finance.yahoo.com (economic data,...)
|
||||
- https://api.coinmarketcap.com (crypto data)
|
||||
- https://api.bls.gov (CPI, unemployment rate)
|
||||
- https://fred.stlouisfed.org (fed funds rate)
|
||||
- https://30rates.com (forecast)
|
||||
- https://fapi.binance.com (OpenInterest,...)
|
||||
- https://api.alternative.me (Fear and Greed)
|
||||
- https://production.dataviz.cnn.io (Fear and Greed CNN)
|
||||
- https://unpkg.com/lightweight-charts/dist/lightweight-charts.standalone.production.js (TradingView Lightweitgt Charts)
|
||||
- ...
|
||||
|
||||
## dependencies on other software products
|
||||
## dependencies to other software
|
||||
- CCXT (https://www.ccxt.com | https://github.com/ccxt/ccxt)
|
||||
- TradingView Lightweitgt Charts (https://www.tradingview.com/lightweight-charts/ | https://github.com/tradingview/lightweight-charts)
|
||||
- bash, python
|
||||
- several linux programs like bc, wget,...
|
||||
- several default linux programs like bc, wget,...
|
||||
- gaboshlib (https://github.com/egabosh/gaboshlib)
|
||||
- ...
|
||||
|
||||
## Objective
|
||||
@@ -54,9 +58,9 @@ https://memory-alpha.fandom.com/wiki/Dabo
|
||||
I thought this fits quite well to the cryptotrading world and that's why I chose this name ;-)
|
||||
|
||||
## Structure
|
||||
The Bot is splitted in the following parts:
|
||||
The Bot is splitted in the following parts/containers:
|
||||
|
||||
- dabo-bot: Basic Bot woth buy and sell decisions based on self-definable strategies
|
||||
- dabo-bot: Basic Bot with buy and sell decisions based on self-definable strategies
|
||||
- dabo-symbols_ticker: Ticker for current symbols and prices
|
||||
- dabo-ohlcv-candles: Continuously collect OHLCV data
|
||||
- dabo-indicators: Continuously calculate indicators and other data
|
||||
@@ -65,22 +69,21 @@ The Bot is splitted in the following parts:
|
||||
- dabo-webpage: Continuously creates webpage overview (readonly)
|
||||
- dabo-web: Webserver for webpage overview
|
||||
|
||||
For cummunicating with the exchange APIs CCXT is used: https://github.com/ccxt/ccxt
|
||||
|
||||
gaboshlib
|
||||
|
||||
Each part runs parallel to the others in its own docker-container.
|
||||
|
||||
## Features
|
||||
### General:
|
||||
- theoretically compatible to supported CCXT exchanges. Tested with Phemex. Please let me know if there are problems with other exchanges.
|
||||
- parallel processing in separate docker containers
|
||||
- theoretically compatible to supported CCXT exchanges. Tested with Phemex Contract trading. Please let me know your experiences with other exchanges.
|
||||
- parallel processing in separate non-root docker containers
|
||||
- Notifications via Matrix Messenger to groups
|
||||
|
||||
### Dabo Bot
|
||||
- place limit and market orders and supports ...
|
||||
- ... leverage trading
|
||||
- ... short trades
|
||||
- ... margins cross and isolated
|
||||
- ... stoploss and takeprofit
|
||||
- multiple different strategies possible at the same time
|
||||
### Dabo Symbol Ticker
|
||||
- automatically finds all supported tokens and pairs and fetches continiously ...
|
||||
- ... symbols
|
||||
@@ -89,49 +92,29 @@ Each part runs parallel to the others in its own docker-container.
|
||||
OHLCV = Open, High, Low, Close and Volume of a time unit
|
||||
- time units 1w, 1d, 4h, 1h, 15m and 5m
|
||||
- 4h, 1h, 15m and 5m from the respective stock exchange
|
||||
- 1d and 1w data from yahoo finace to have longer terms
|
||||
- 1d and 1w data from coinmarketcap to have longer terms
|
||||
- economic data from yahoo finance
|
||||
### Dabo Indicators
|
||||
- data per time unit
|
||||
- time units 1w, 1d, 4h, 1h, 15m and 5m
|
||||
- self-calculated EMA12, 26 ,50, 100, 200, 400 and 800
|
||||
- self-calculated RSI5, 14, 21
|
||||
- self-calculated MACD
|
||||
- self-calculated significant levels (support/resist)
|
||||
### Dabo Market Data
|
||||
- Yahoo Finance
|
||||
- CoinMarketCap
|
||||
- BLS.gov
|
||||
- alternative.me
|
||||
- CNN
|
||||
- Fed
|
||||
### Dabo Orders
|
||||
## Dabo Transaction History
|
||||
### Dabo Transaction History
|
||||
- Support of additional Exchnages/Brokers JustTrade (CSV-Import) and Bitpanda (API+CSV-Import)
|
||||
- German tax calculation
|
||||
### Dabo Webpage
|
||||
- ReadOnly Overview
|
||||
|
||||
- Consideration of trading fees
|
||||
- Automatic selection of cryptocurrencies to invest in
|
||||
- Filtering of cryptocurrencies by market capitalization (data via coingecko API) e.g. only trade the 50 largest by market capitalization if available on the exchange
|
||||
- Notifications via Matrix Messenger to groups
|
||||
- Individually selectable percentage of available capital per trade
|
||||
- automatic definition of time intervals (e.g. every minute / every five minutes) based on the change of the rates
|
||||
- Monitoring of overall market performance via own market performance index based for example on the MSCI World, leading currencies like BTC and ETH and their forecasts.
|
||||
- Emergency stop if balance falls below defined value
|
||||
- Recording of available price values incl. RSI and MACD indicators of available cryptocurrencies
|
||||
- Analysis tool for collected historical values to try out buy or sell conditions based on them
|
||||
- ReadOnly web interface for overview
|
||||
- Runnable in a non-root docker containe
|
||||
- multiple different buy and sell strategies possible at the same time
|
||||
|
||||
- Hedge mode (long and short positions the same time) not supported
|
||||
|
||||
### Buy conditions
|
||||
- definable RSI Indicator signals min/max (RSI5, 14 and 21)
|
||||
- definable MACD signals min/max
|
||||
- definable minimum growth in a definable time period
|
||||
|
||||
### Sell conditions
|
||||
- sell at defined loss limit
|
||||
- hold if the result would be negative
|
||||
- sell after certain time even if loss exists
|
||||
- definable RSI Indicator signals min/max
|
||||
- definable MACD signals min/max
|
||||
|
||||
## Why bash?
|
||||
Yes, bash is not a language in which you write something like this in a normal way.
|
||||
|
||||
@@ -143,28 +126,88 @@ Originally this project was supposed to be a simple script to monitor prices of
|
||||
Finally, it's a hobby project and I have to see how and when I can find time for it, because there also has to be time for family, friends, work and other hobbies.
|
||||
If there is someone who would like to rewrite this bot in e.g. Python, I would be happy to support them as best I can with this task. Just let me know.
|
||||
|
||||
## How to use/install
|
||||
Linux knowledge required!
|
||||
## How to install (basic Linux knowledge required!)
|
||||
|
||||
Needed a running Docker install. Traefik suggested, see
|
||||
Should run on every system with docker.
|
||||
|
||||
https://gitea.ds9.dedyn.io/olli/debian.ansible.docker
|
||||
### 1: Operating System
|
||||
Tested and running with Debian 12 (Bookworm).
|
||||
https://www.debian.org/download
|
||||
https://www.raspberrypi.com/software/operating-systems/
|
||||
|
||||
https://gitea.ds9.dedyn.io/olli/debian.ansible.traefik.server
|
||||
### 2: Way 1: Istall via anssible playbooks
|
||||
On a fresh Debian 12 system you can run my Ansible Playbooks to use the same environment the bot is developed and running.
|
||||
Please have a look what exactly the playbooks are doing if you are unsure.
|
||||
|
||||
If the web interface (or matrix/turn) should be accessible via the internet, the server must be accessible on port 80/tcp (acme/letsencrypt) and 443/tcp.
|
||||
For private/home internet access, this may require port forwarding on your router and registration with a dyndns (DDNS) service. The DDNS service should allow subdomains.
|
||||
|
||||
### Download
|
||||
When using my playbooks and the services should be available to the internet, the hostname must correspond to the ddns name.
|
||||
Here is an example for the dynu-DDNS service:
|
||||
- DDNS name: myhost.mywire.com
|
||||
- dabo-bot-name: dabo.myhost.mywire.com
|
||||
- matrix-name: matrix.myhost.mywire.com
|
||||
|
||||
You also can use IPv6 if your ISP and router supports this and if you have multiple servers/services on Ports (80/443) but only one IPv4 address to the internet.
|
||||
|
||||
#### 2.1 Download basic install script
|
||||
```
|
||||
git clone https://gitea.ds9.dedyn.io/olli/dabo.git
|
||||
wget https://raw.githubusercontent.com/egabosh/linux-setups/refs/heads/main/debian/install.sh -O install.sh
|
||||
```
|
||||
#### 2.2 define Playbooks
|
||||
- debian/basics/basics.yml (https://github.com/egabosh/linux-setups/tree/main/debian/basics) - Basic Debian configuration
|
||||
- Optional: debian/firewall/firewall.yml (https://github.com/egabosh/linux-setups/tree/main/debian/firewall) - Firewall for the server based on ufw
|
||||
- Optional: debian/runchecks/runchecks.yml (https://github.com/egabosh/linux-setups/tree/main/debian/runchecks) - System checks and notification
|
||||
- Optional: debian/backup/backup.yml (https://github.com/egabosh/linux-setups/tree/main/debian/backup) - Backup framework
|
||||
- Optional: debian/autoupdate/autoupdate.yml (https://github.com/egabosh/linux-setups/tree/main/debian/autoupdate) - Automatic System Updates
|
||||
- debian/docker/docker.yml (https://github.com/egabosh/linux-setups/tree/main/debian/docker) - Docker Installation
|
||||
- debian/traefik.server/traefik.yml (https://github.com/egabosh/linux-setups/tree/main/debian/traefik.server) - Traefik Reverse Proxy for Web UI and Letsencrypt Certs
|
||||
- Optional: debian/turn.server/turn.yml (https://github.com/egabosh/linux-setups/tree/main/debian/turn.server) - Turn Server fpr Audio/Video conferences in Matrix
|
||||
- Optional: debian/matrix.server/matrix.yml (https://github.com/egabosh/linux-setups/tree/main/debian/matrix.server) - Notifications with own Martix Server
|
||||
- https://github.com/egabosh/dabo/raw/refs/heads/main/dabo-ansible.yml: The Bot itself
|
||||
|
||||
for example:
|
||||
```
|
||||
PLAYBOOKS="debian/basics/basics.yml
|
||||
debian/firewall/firewall.yml
|
||||
debian/runchecks/runchecks.yml
|
||||
debian/backup/backup.yml
|
||||
debian/autoupdate/autoupdate.yml
|
||||
debian/docker/docker.yml
|
||||
debian/traefik.server/traefik.yml
|
||||
https://github.com/egabosh/dabo/raw/refs/heads/main/dabo-ansible.yml"
|
||||
export PLAYBOOKS
|
||||
```
|
||||
#### 2.3 Install ansible and run Playbooks
|
||||
If you run this as user and not as root, the script will install sudo and enable the user to execute commands as root via sudo. To do this, the root password is requested (several times).
|
||||
```
|
||||
bash install.sh
|
||||
```
|
||||
|
||||
### 3. Manual installation without ansible playbooks
|
||||
Not necessary if you use the dabo Playbook
|
||||
|
||||
#### Install docker
|
||||
See: https://docs.docker.com/engine/install/debian/
|
||||
|
||||
You can also use other containerizations like podman or kubernetes. But here I stick with docker
|
||||
|
||||
#### Download
|
||||
Not necessary if you use the dabo Playbook
|
||||
```
|
||||
git clone https://github.com/egabosh/dabo.git
|
||||
cd dabo
|
||||
```
|
||||
|
||||
### Build container
|
||||
#### Build container
|
||||
Not necessary if you use the dabo Playbook
|
||||
```
|
||||
docker -l warn compose --ansi never build --progress=plain --pull --no-cache --force-rm
|
||||
```
|
||||
|
||||
### Configure
|
||||
### 3. Make Webinterface available
|
||||
Not necessary if you use the dabo Playbook with traefik and letsencrypt
|
||||
|
||||
Edit docker-compose.yml or create docker-compose.override.yml to fit yout needs e.g. domain and network settings or basic auth, e.g. for traefik and letsencrypt:
|
||||
```
|
||||
echo '
|
||||
@@ -206,8 +249,18 @@ networks:
|
||||
' >docker-compose.override.yml
|
||||
```
|
||||
|
||||
If you use the ansible Playbook but don't want to use traefik/letsencrypt or availability over the internet you can add the following to `docker-compose.override.yml` below "# END ANSIBLE MANAGED BLOCK".
|
||||
ATTENTION: ACCESS IS UNENCRYPTED!!!
|
||||
```
|
||||
ports:
|
||||
- 8080:80
|
||||
```
|
||||
You have to restart the container(s) to apply changes. See operational commands.
|
||||
|
||||
Optional: If you use matrix/matrix-commander (https://gitea.ds9.dedyn.io/olli/debian.ansible.matrix.server) and want do receive Matrix-Messages from the bot you can create an SSH-Key to allow sending Matrix-Messages e.g.:
|
||||
### 4. Optional: Matrix connection
|
||||
|
||||
Optional: If you use matrix/matrix-commander (https://github.com/egabosh/linux-setups/tree/main/debian/matrix.server) and want do receive Matrix-Messages from the bot you can create an SSH-Key to allow sending Matrix-Messages e.g.:
|
||||
Automatically done by playbooks.
|
||||
```
|
||||
mkdir -p home/.ssh
|
||||
ssh-keygen -f home/.ssh/id_ed25519 -N "" -t ed25519
|
||||
@@ -216,76 +269,34 @@ cat home/.ssh/id_ed25519.pub
|
||||
```
|
||||
and add Key on your matrix-Server to the authorized_keys of the matrix-User
|
||||
|
||||
|
||||
### 5. Add Exchange
|
||||
Create Secrets file for your API Key(s)
|
||||
|
||||
- file: dabo/.CCXT-ID-secrets
|
||||
|
||||
CCXI-ID see: https://github.com/ccxt/ccxt
|
||||
|
||||
|
||||
Examples:
|
||||
```
|
||||
# for Phemex
|
||||
echo 'local API_SECRET="YOUR_API_SECRET_FROM_PHEMEX"
|
||||
local API_KEY="YOUR_API_KEY_FROM_BINANCE"
|
||||
local API_KEY="YOUR_API_KEY_FROM_PHEMEX"' >dabo/.phemex-secrets
|
||||
chmod 400 dabo/.phemex-secrets
|
||||
|
||||
# for Binance
|
||||
echo 'local API_SECRET="YOUR_API_SECRET_FROM_BINANCE"
|
||||
local API_KEY="YOUR_API_KEY_FROM_BINANCE" '>dabo/.binance-secrets
|
||||
local API_KEY="YOUR_API_KEY_FROM_BINANCE"' >dabo/.binance-secrets
|
||||
chmod 400 dabo/.binance-secrets
|
||||
```
|
||||
|
||||
Create Config
|
||||
Especially set URL, STOCK_EXCHANGE, FEE, CURRENCY,... to fit your needs.
|
||||
If you want to use the a testnet of an exchnage if available use TESTNET=true which is the default
|
||||
```
|
||||
vim dabo-bot.conf
|
||||
```
|
||||
Defaults in dabo/dabo-bot.conf
|
||||
|
||||
|
||||
### Prepare/Create a stretegy
|
||||
|
||||
IMPORTANT!!!
|
||||
|
||||
THE DEFAULT STRATEGY MAY NOT FIT YOUR NEEDS OR WORK PROPERLY. SO YOU CAN LOOSE ALL YOUR MONEY!!! USE ON YOUR OWN RISK!!!
|
||||
|
||||
TEST YOUR OWN STRATEGY COMPREHENSIVELY AND OVER A LOGNER PERIOD OF TIME WITH analyze.sh!!! USE ON YOUR OWN RISK!!!
|
||||
|
||||
Strategie files can be put in the "strategies"-directory the defaults
|
||||
|
||||
There is an example for a buy and a sell strategy file (deactivated by "return 0" in the forst line):
|
||||
```
|
||||
ls strategies/buy.example.conf
|
||||
ls strategies/sell.example.conf
|
||||
```
|
||||
Aditional strategies can be created with Name
|
||||
```
|
||||
strategies/buy.<name>.conf
|
||||
strategies/sell.<name>.conf
|
||||
```
|
||||
e.g named "mannover-sulu-1" for buy strategy and "command-kirk-3" for sell strategy
|
||||
```
|
||||
strategies/buy.mannover-sulu-1.conf
|
||||
strategies/sell.command-kirk-3.conf
|
||||
```
|
||||
|
||||
### Create individual watch-assets.csv
|
||||
```
|
||||
cp dabo/watch-assets.csv watch-assets.csv
|
||||
```
|
||||
Optional:
|
||||
You can edit this file if you want do generate warnings or track your asstes/trades.
|
||||
```
|
||||
nano watch-assets.csv
|
||||
```
|
||||
|
||||
### Set Rights
|
||||
Set Rights (UID 10000 for non-root-User in running container):
|
||||
```
|
||||
chown -R 10000:10000 dabo data home strategies dabo-bot.conf watch-assets.csv
|
||||
```
|
||||
## How to use
|
||||
|
||||
### Operational commands
|
||||
Run/Restart:
|
||||
@@ -294,7 +305,7 @@ docker compose down # if an old instance is running
|
||||
docker compose up -d
|
||||
```
|
||||
|
||||
Check
|
||||
List and state of containers:
|
||||
```
|
||||
docker compose ps
|
||||
```
|
||||
@@ -303,37 +314,76 @@ Logs/Output:
|
||||
```
|
||||
docker compose logs -f
|
||||
```
|
||||
|
||||
Update:
|
||||
Logs/Oputput of a specific container for example dabo-bot:
|
||||
```
|
||||
# Optinal: Remove local data
|
||||
docker compose logs -f dabo-bot
|
||||
```
|
||||
|
||||
|
||||
### Update
|
||||
Not necessary if you use the playbooks
|
||||
```
|
||||
# Optional: Remove local data
|
||||
git reset --hard HEAD^ # Remove local commits
|
||||
git clean -fd # Remove local uncommited files
|
||||
|
||||
# Update and restart
|
||||
git pull https://gitea.ds9.dedyn.io/olli/dabo.git main -f
|
||||
git pull https://github.com/egabosh/dabo.git main -f
|
||||
docker compose down
|
||||
docker compose up -d
|
||||
|
||||
```
|
||||
|
||||
### Strategies
|
||||
|
||||
dabo-bot.sh is the bot that trades and collects the quotes and analyze.sh is the tool with which you can try out strategies with the historical data.
|
||||
The configuration files are called dabo-bot.conf and analyze.conf. analyze.sh also uses bot.conf but its variables are overwritten by analyze.conf if duplicated.
|
||||
IMPORTANT!!!
|
||||
|
||||
A Binance or OneTrading.com (ex BitpandaPro) account must exist and the API must be enabled.
|
||||
The access data is stored in the file .binance-secrets in the project directory in the variables API_SECRET and API_KEY.
|
||||
The access rights to this file should be set to the minimum necessary for security reasons.
|
||||
THE EXAMPLE STRATEGIES MAY NOT FIT YOUR NEEDS OR WORK PROPERLY. SO YOU CAN LOOSE ALL YOUR MONEY!!! USE ON YOUR OWN RISK!!!
|
||||
|
||||
## Strategies
|
||||
TEST YOUR OWN STRATEGY COMPREHENSIVELY AND OVER A LOGNER PERIOD OF TIME BEST RISK-FREE IN A TESTNET!!! USE ON YOUR OWN RISK!!!
|
||||
|
||||
Strategies are needed for the bot to trade.
|
||||
|
||||
Strategies are located in die stretegies subdir.
|
||||
|
||||
You can put your own code into the strategies it will be sourced by the bot.
|
||||
If you want, you can also use other programming languages or binary code as a strategy and simply start it using your own strategy.
|
||||
|
||||
You can use available variables to read (and set) values.
|
||||
#### Example strategies
|
||||
There are examples for strategy files (deactivated by "return 0" in the beginning):
|
||||
```
|
||||
cat strategies/example.strategy.sh
|
||||
cat strategies/example_manage_positions.strategy.sh
|
||||
```
|
||||
"example.strategy.sh" creates a score by defined tests and opens a long or short order with stoploss and takeprofit if the score has a defined value.
|
||||
|
||||
### Variables with current market values
|
||||
"example_manage_positions.strategy.sh" watches open positions and switches the stoploss into profit if position is in profit to secure it.
|
||||
|
||||
#### Large associative arrays v and vr (reverse)
|
||||
You can use them and change them to fit your needs. To avoid resets on updates copy them to your own strategies for example:
|
||||
|
||||
```
|
||||
cp -p strategies/example.strategy.sh strategies/my-own-trading.strategy.sh
|
||||
cp -p strategies/example_manage_positions.strategy.sh strategies/my-own-position.strategy.sh
|
||||
```
|
||||
|
||||
#### Own strategies
|
||||
Aditional strategies can be created with naming convention *.strategy.sh
|
||||
```
|
||||
strategies/my-new.strategy.sh
|
||||
strategies/yet-another-new.strategy.sh
|
||||
```
|
||||
|
||||
Strategy files should have specific rights - must be readable by the bot:
|
||||
```
|
||||
chown -R 10000:10000 strategies
|
||||
chmod -R 640 strategies
|
||||
```
|
||||
|
||||
#### Variables during runtime of the strategies
|
||||
|
||||
You can use available variables/arrays during runtime to read (and set) values.
|
||||
This arrays are available to runtime in the strategies
|
||||
|
||||
##### Large associative arrays v and vr (reverse)
|
||||
|
||||
${v[SYMBOL_TIMEFRAME_ITEM_NUMBER]}
|
||||
|
||||
@@ -349,12 +399,13 @@ ${v[ECONOMY_SP500_rsi14_0]}
|
||||
${v[ETHUSDT_1w_ema200_0]}
|
||||
${v[ETHUSDT_1w_macd_histogram_signal_1]}
|
||||
${v[SOLUSDT_levels_1d]}
|
||||
$v[$[SOLUSDT_levels_1d_next_up]}
|
||||
$v[$[ETHUSDT_levels_1w_next_down]}
|
||||
${v[$[SOLUSDT_levels_1d_next_up]}
|
||||
${v[$[ETHUSDT_levels_1w_next_down]}
|
||||
```
|
||||
You can find a complete list of available values in the file `data/botdata/values` whic is creates in the runtime of the bot.
|
||||
An example you can find in example-values.
|
||||
#### Current price from exchange
|
||||
You can find a complete list of available values in the file `data/botdata/values` which is created in the runtime of the bot.
|
||||
An long example list you can find in example-values. https://github.com/egabosh/dabo/blob/main/example-values
|
||||
|
||||
##### Current price from exchange
|
||||
|
||||
${f_tickers_array[SYMBOL]}
|
||||
```
|
||||
@@ -362,10 +413,68 @@ ${f_tickers_array[SOLUSDT]}
|
||||
${f_tickers_array[ETH${CURRENCY}]}
|
||||
```
|
||||
|
||||
##### Open Orders
|
||||
|
||||
```
|
||||
${o[ETHUSDT_present]}=sl_close_long tp_close_long
|
||||
${o[ETHUSDT_sl_close_long_amount]}=0
|
||||
${o[ETHUSDT_sl_close_long_entry_price]}=null
|
||||
${o[ETHUSDT_sl_close_long_id]}=36b2f404-0b20-4806-bdcc-0dad0daf57e9
|
||||
${o[ETHUSDT_sl_close_long_side]}=sell
|
||||
${o[ETHUSDT_sl_close_long_stoplossprice]}=0
|
||||
${o[ETHUSDT_sl_close_long_stopprice]}=3305.98
|
||||
${o[ETHUSDT_sl_close_long_takeprofitprice]}=0
|
||||
${o[ETHUSDT_sl_close_long_type]}=Stop
|
||||
${o[ETHUSDT_tp_close_long_amount]}=0
|
||||
${o[ETHUSDT_tp_close_long_entry_price]}=null
|
||||
${o[ETHUSDT_tp_close_long_id]}=850dd169-7387-4be3-ac68-bfce73cfa47d
|
||||
${o[ETHUSDT_tp_close_long_side]}=sell
|
||||
${o[ETHUSDT_tp_close_long_stoplossprice]}=0
|
||||
${o[ETHUSDT_tp_close_long_stopprice]}=3710.04
|
||||
${o[ETHUSDT_tp_close_long_takeprofitprice]}=0
|
||||
${o[ETHUSDT_tp_close_long_type]}=MarketIfTouched
|
||||
```
|
||||
|
||||
You can find a complete list of available values in the file `data/botdata/values-orders` which is created in the runtime of the bot.
|
||||
|
||||
|
||||
## Future ideas/featrues
|
||||
- Crypto preferences (While/Blacklist for certain currencies)
|
||||
- Overview trades/profits/losses for tax declaration
|
||||
- Support for decentralized exchanges like uniswap to gain more "security"
|
||||
##### Open Positions
|
||||
|
||||
```
|
||||
${p[ETHUSDT_currency_amount]}=9509.25
|
||||
${p[ETHUSDT_current_price]}=3573.79
|
||||
${p[ETHUSDT_entry_price]}=3673.31
|
||||
${p[ETHUSDT_leverage]}=5
|
||||
${p[ETHUSDT_liquidation_price]}=2953.43
|
||||
${p[ETHUSDT_pnl]}=-1288.50
|
||||
${p[ETHUSDT_pnl_percentage]}=-13.55
|
||||
${p[ETHUSDT_side]}=long
|
||||
${p[ETHUSDT_stoploss_price]}=3305.98
|
||||
${p[ETHUSDT_takeprofit_price]}=3710.04
|
||||
```
|
||||
|
||||
You can find a complete list of available values in the file `data/botdata/values-positions` which is created in the runtime of the bot.
|
||||
|
||||
|
||||
## Support/Community
|
||||
New Telegram group for the dabo community.
|
||||
https://t.me/dabobotcrypto
|
||||
|
||||
|
||||
## Future ideas/featrues and todos
|
||||
- PSR Indicator https://de.tradingview.com/script/w4U2xUN7-Pivot-Support-Resistance/
|
||||
- public order data if there is such a thing. At what price do people want to enter (limit order)? Where do they want to exit (TakePofit/StopLoss)? Maybe free Binance data helps (https://developers.binance.com/docs/derivatives/usds-margined-futures/market-data/rest-api/Order-Book)? https://www.coinglass.com/de/mergev2/BTC-USDT
|
||||
- AI implementation. What data can be prepared for training?
|
||||
- example Grid trading strategy (user specific)
|
||||
- Fibonacci Levels (which timeframe(s)?)
|
||||
- Forecasts
|
||||
- Chart improvements
|
||||
- Volumeindicator and for example RSI on volume values
|
||||
- Support for decentralized exchanges like uniswap
|
||||
- Archive/compress old or large CSV-History-files
|
||||
- Hedge mode (long and short positions the same time)
|
||||
- Emergency stop if balance falls below defined value
|
||||
- Analysis tool for collected historical values to try out buy or sell conditions based on them
|
||||
- Consideration of trading and funding fees
|
||||
- Liquidation Heatmap (https://www.coinglass.com/pro/futures/LiquidationHeatMap)
|
||||
|
||||
|
||||
@@ -13,14 +13,14 @@
|
||||
|
||||
- name: Git checkout
|
||||
ansible.builtin.git:
|
||||
repo: 'https://gitea.ds9.dedyn.io/olli/dabo.git'
|
||||
repo: 'https://github.com/egabosh/dabo.git'
|
||||
dest: /home/docker/dabo.{{inventory_hostname}}
|
||||
force: true
|
||||
notify: Restart dabo
|
||||
|
||||
- name: /home/docker/dabo.{{inventory_hostname}}/genpw.sh (generate Random PW)
|
||||
- name: /home/docker/dabo.{{inventory_hostname}}/initiate.sh
|
||||
blockinfile:
|
||||
path: /home/docker/dabo.{{inventory_hostname}}/genpw.sh
|
||||
path: /home/docker/dabo.{{inventory_hostname}}/initiate.sh
|
||||
create: yes
|
||||
mode: 0550
|
||||
owner: root
|
||||
@@ -29,41 +29,22 @@
|
||||
block: |
|
||||
cd /home/docker/dabo.{{inventory_hostname}}
|
||||
|
||||
if [ -f env ]
|
||||
then
|
||||
. ./env
|
||||
echo "${WEBPASSWDCRYPT}"
|
||||
else
|
||||
webpassword=$(pwgen -s 32 1)
|
||||
webuser=bot
|
||||
webpasswordcrypted=$(htpasswd -nb ${webuser} ${webpassword} | sed -e 's/\\$/\\$\\$/g')
|
||||
|
||||
echo "WEBUSER=${webuser}
|
||||
WEBPASSWD='${webpassword}'
|
||||
WEBPASSWDCRYPT='${webpasswordcrypted}'
|
||||
" >env
|
||||
|
||||
chmod 440 env
|
||||
chown root:docker env
|
||||
echo "${webpasswordcrypted}"
|
||||
fi
|
||||
|
||||
if ! [ -d home/.ssh ]
|
||||
then
|
||||
mkdir -p home/.ssh
|
||||
ssh-keygen -f home/.ssh/id_ed25519 -N "" -t ed25519 >/dev/null
|
||||
chmod 700 home/.ssh
|
||||
fi
|
||||
|
||||
[ -f watch-assets.csv ] || cp dabo/watch-assets.csv watch-assets.csv
|
||||
chown -R 10000:10000 dabo data home strategies dabo-bot.conf watch-assets.csv
|
||||
mkdir -p data/botdata strategies
|
||||
chown -R 10000:10000 dabo data home strategies dabo-bot.conf
|
||||
|
||||
backup: yes
|
||||
validate: /bin/bash -n %s
|
||||
notify: run initiate.sh
|
||||
|
||||
- name: /home/docker/dabo.{{inventory_hostname}}/genpw.sh shebang
|
||||
- name: /home/docker/dabo.{{inventory_hostname}}/initiate.sh shebang
|
||||
lineinfile:
|
||||
path: /home/docker/dabo.{{inventory_hostname}}/genpw.sh
|
||||
path: /home/docker/dabo.{{inventory_hostname}}/initiate.sh
|
||||
insertbefore: BOF
|
||||
line: "#!/bin/bash -e"
|
||||
|
||||
@@ -84,10 +65,10 @@
|
||||
- Restart dabo
|
||||
|
||||
|
||||
- name: Get crypted PW
|
||||
shell: bash /home/docker/dabo.{{inventory_hostname}}/genpw.sh
|
||||
register: cryptpw
|
||||
changed_when: false
|
||||
#- name: Get crypted PW
|
||||
# shell: bash /home/docker/dabo.{{inventory_hostname}}/initiate.sh
|
||||
# register: cryptpw
|
||||
# changed_when: false
|
||||
|
||||
- name: /home/docker/dabo.{{inventory_hostname}}/docker-compose.override.yml Container Configuration
|
||||
blockinfile:
|
||||
@@ -98,6 +79,14 @@
|
||||
group: docker
|
||||
marker: "# {mark} ANSIBLE MANAGED BLOCK"
|
||||
block: |
|
||||
networks:
|
||||
dabo-network:
|
||||
driver: bridge
|
||||
driver_opts:
|
||||
com.docker.network.bridge.name: br-dabo
|
||||
traefik:
|
||||
external: true
|
||||
|
||||
services:
|
||||
|
||||
dabo-bot:
|
||||
@@ -118,21 +107,11 @@
|
||||
# cert via letsencrypt
|
||||
- traefik.http.routers.dabo.tls.certresolver=letsencrypt
|
||||
# activate secHeaders@file
|
||||
- traefik.http.routers.dabo.middlewares=secHeaders@file,dabo-basicauth
|
||||
- traefik.http.middlewares.dabo-basicauth.basicauth.users={{ cryptpw.stdout }}
|
||||
- traefik.http.routers.dabo.middlewares=secHeaders@file,default-basic-auth@file
|
||||
# Traefik network
|
||||
- traefik.docker.network=traefik
|
||||
networks:
|
||||
- traefik
|
||||
|
||||
networks:
|
||||
dabo-network:
|
||||
driver: bridge
|
||||
driver_opts:
|
||||
com.docker.network.bridge.name: br-dabo
|
||||
traefik:
|
||||
external: true
|
||||
|
||||
backup: yes
|
||||
notify: Restart dabo
|
||||
|
||||
@@ -142,11 +121,26 @@
|
||||
chdir: /home/docker/dabo.{{inventory_hostname}}
|
||||
creates: /home/docker/dabo.{{inventory_hostname}}/data/botdata/MARKET_PERFORMANCE
|
||||
|
||||
- name: autoupdate dabo-bot
|
||||
blockinfile:
|
||||
path: /usr/local/sbin/autoupdate.d/dabo.update
|
||||
create: yes
|
||||
mode: 0550
|
||||
owner: root
|
||||
group: root
|
||||
marker: "# {mark} ANSIBLE MANAGED BLOCK"
|
||||
block: |
|
||||
g_echo_ok "Dabo update"
|
||||
rm -f /tmp/dabo-ansible.yml
|
||||
wget https://raw.githubusercontent.com/egabosh/dabo/refs/heads/main/dabo-ansible.yml -O /tmp/dabo-ansible.yml
|
||||
ansible-playbook --connection=local --inventory $(hostname), --limit $(hostname) /tmp/dabo-ansible.yml
|
||||
backup: yes
|
||||
validate: /bin/bash -n %s
|
||||
|
||||
handlers:
|
||||
|
||||
- name: run genpw.sh
|
||||
ansible.builtin.shell: ./genpw.sh
|
||||
- name: run initiate.sh
|
||||
ansible.builtin.shell: ./initiate.sh
|
||||
args:
|
||||
chdir: /home/docker/dabo.{{inventory_hostname}}
|
||||
notify: Restart dabo
|
||||
@@ -155,3 +149,4 @@
|
||||
ansible.builtin.shell: docker compose up -d --force-recreate
|
||||
args:
|
||||
chdir: /home/docker/dabo.{{inventory_hostname}}
|
||||
|
||||
|
||||
@@ -30,8 +30,9 @@ do
|
||||
# Reload Config
|
||||
. ../../dabo-bot.conf
|
||||
. ../../dabo-bot.override.conf
|
||||
# get orders
|
||||
# get all indicators first only latest until EMA800
|
||||
get_indicators_all 810
|
||||
get_indicators_all
|
||||
rm asset-histories/*.history.*.csv.indicators-calculating
|
||||
rm -f asset-histories/*.history.*.csv.indicators-calculating
|
||||
done
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
. /dabo/dabo-prep.sh
|
||||
|
||||
rm -f asset-histories/*.history.*.csv.levels-calculating
|
||||
sleep 12
|
||||
sleep 120
|
||||
|
||||
while true
|
||||
do
|
||||
@@ -33,6 +33,7 @@ do
|
||||
# get levels
|
||||
get_levels_all
|
||||
rm -f asset-histories/*.history.*.csv.levels-calculating
|
||||
sleep 900
|
||||
# recalc every 6 hours
|
||||
sleep 21600
|
||||
done
|
||||
|
||||
|
||||
@@ -2,22 +2,18 @@
|
||||
|
||||
# Webpage URL
|
||||
URL="mydabobot.mydomain"
|
||||
URL="dabo.$(hostname)"
|
||||
|
||||
# The exchange we use for using the correct API (BINANCE or ONETRADING)
|
||||
# The exchange we use for using the correct API (CCXI-ID see: https://github.com/ccxt/ccxt)
|
||||
# for Phemex it is "phemex" for example
|
||||
STOCK_EXCHANGE="NONE"
|
||||
|
||||
# fee per trade in percentage on exchange (taker and maker added)
|
||||
FEE="0.5"
|
||||
|
||||
# Interval in seconds - Should not be lower then 300
|
||||
# Interval in seconds - Should not be lower then 300 seconds (5m is the lowest timeframe
|
||||
INTERVAL="300"
|
||||
|
||||
## Currency used for trading
|
||||
CURRENCY="USDT"
|
||||
TRANSFER_CURRENCY="USD"
|
||||
|
||||
# Only use currencies under the first X currencies sorted by market capitalization
|
||||
LARGEST_MARKETCAP="250"
|
||||
TRANSFER_CURRENCY="EUR"
|
||||
|
||||
# symbols that should be traded
|
||||
SYMBOLS="ETH SOL"
|
||||
@@ -25,16 +21,12 @@ SYMBOLS="ETH SOL"
|
||||
## Signal Group for Notifications
|
||||
NOTIFICATION_GROUP="Dabo-Bot"
|
||||
|
||||
## Percent from balance per invest.
|
||||
# Overwritten by MIN_NOTIONAL+X% from stock if lower
|
||||
INVEST="5"
|
||||
|
||||
# Stop all trading and sell everything if the complete balance shrinks under this value in ${CURRENCY}
|
||||
EMERGENCY_STOP="1000"
|
||||
|
||||
# Leverage
|
||||
LEVERAGE=""
|
||||
LEVERAGE="2"
|
||||
|
||||
# margin mode (isolated or cross)
|
||||
MARGIN_MODE="isolated"
|
||||
|
||||
# testnet of exchange if available (true or false). false for trades with real money
|
||||
TESTNET=true
|
||||
|
||||
|
||||
@@ -71,42 +71,14 @@ do
|
||||
# Timestamp
|
||||
export f_timestamp=$(g_date_print)
|
||||
|
||||
|
||||
####### TODO -> Funktionen überarbeiten ############
|
||||
#
|
||||
# # get minute interval for find -mmin (used by get_marketdata market_performance
|
||||
# INTERVAL_MIN=$(echo "${INTERVAL}/60-1" | bc -l | sed -r 's/^(-?)\./\10./' | cut -d\. -f1)
|
||||
# [ -z "${INTERVAL_MIN}" ] && INTERVAL_MIN=1
|
||||
#
|
||||
# ### get general market data
|
||||
# # Get coingecko data
|
||||
# get_coingecko_data
|
||||
#
|
||||
# # Get current MarketData
|
||||
# get_marketdata
|
||||
#
|
||||
# # Check the situation on the market
|
||||
# if ! market_performance
|
||||
# then
|
||||
# f_market_performance=$(cat MARKET_PERFORMANCE_LATEST)
|
||||
# fi
|
||||
####### TODO -> Funktionen überarbeiten ENDE ###########
|
||||
|
||||
|
||||
## watch some manual defined assets
|
||||
#watch_assets
|
||||
|
||||
if [ "${STOCK_EXCHANGE}" = "NONE" ]
|
||||
then
|
||||
## stop here if STOCK_EXCHANGE not present
|
||||
continue
|
||||
fi
|
||||
|
||||
# Get current symbols
|
||||
#[ ${FULL_LOOP} = 1 ] && get_symbols_ticker
|
||||
|
||||
# Sell something?
|
||||
#check_for_sell
|
||||
# clean old data
|
||||
[ ${FULL_LOOP} = 1 ] && find asset-histories -maxdepth 1 ! -type d ! -name "*.csv" ! -name "*.levels" ! -name "*.zones" ! -name "*.indicators-calculated" -mtime +1 -delete
|
||||
|
||||
# Get current balance
|
||||
[ ${FULL_LOOP} = 1 ] && get_balance || continue
|
||||
|
||||
34
dabo/fetch-coinmarketcapids.sh
Executable file
34
dabo/fetch-coinmarketcapids.sh
Executable file
@@ -0,0 +1,34 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Copyright (c) 2022-2024 olli
|
||||
#
|
||||
# This file is part of dabo (crypto bot).
|
||||
#
|
||||
# dabo is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# dabo is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with dabo. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
|
||||
. /dabo/dabo-prep.sh
|
||||
|
||||
while true
|
||||
do
|
||||
g_echo_note "Next loop"
|
||||
[ -s COINMARKETCAPIDS ] || get_marketdata_coinmarketcap_ids
|
||||
sleeptime=$(($(TZ=UTC date +%s -d "next monday 0:00") - $(date +%s) +2 ))
|
||||
g_echo_note "Waiting $sleeptime seconds until next run"
|
||||
sleep $sleeptime
|
||||
get_marketdata_coinmarketcap_ids
|
||||
|
||||
done
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
. /dabo/dabo-prep.sh
|
||||
|
||||
interval=$1
|
||||
[ -z "$interval" ] && return 1
|
||||
[ -z "$interval" ] && exit 1
|
||||
seconds=$2
|
||||
|
||||
while true
|
||||
@@ -31,16 +31,25 @@ do
|
||||
# Reload Config
|
||||
. ../../dabo-bot.conf
|
||||
. ../../dabo-bot.override.conf
|
||||
# notify failed yahoo downloads
|
||||
[ "$interval" = "1d" ] && cat FAILED_YAHOO/*USD_* FAILED_YAHOO/*ECONOMY* 2>/dev/null | notify.sh -s "Failed Yahoo downloads"
|
||||
# notify failed downloads
|
||||
if [ "$interval" = "1h" ]
|
||||
then
|
||||
cat FAILED_*/* 2>/dev/null | notify.sh -s "Failed downloads"
|
||||
mkdir -p REPORTED_FAILED
|
||||
mv FAILED_*/* REPORTED_FAILED/ 2>/dev/null
|
||||
fi
|
||||
# Timestamp
|
||||
export f_timestamp=$(g_date_print)
|
||||
# get candles and indicators
|
||||
get_ohlcv-candles $interval
|
||||
[[ $interval != 1w ]] && get_marketdata_all $interval
|
||||
[ -n "$seconds" ] && sleeptime=$(( ( ($seconds - $(TZ=UTC printf "%(%s)T") % $seconds) % $seconds + 2 )))
|
||||
#[[ $interval = 4h ]] && sleeptime=??
|
||||
[ "$interval" = "1d" ] && sleeptime=$(($(TZ=UTC date +%s -d "tomorrow 0:00") - $(date +%s) +2 ))
|
||||
[ "$interval" = "1w" ] && sleeptime=$(($(TZ=UTC date +%s -d "next monday 0:00") - $(date +%s) +2 ))
|
||||
if [ "$interval" = "1d" ]
|
||||
then
|
||||
sleeptime=$(($(TZ=UTC date +%s -d "tomorrow 0:01") - $(date +%s) +2 ))
|
||||
fi
|
||||
[ "$interval" = "1w" ] && sleeptime=$(($(TZ=UTC date +%s -d "next monday 0:01") - $(date +%s) +2 ))
|
||||
g_echo_note "Waiting $sleeptime seconds until next run"
|
||||
sleep $sleeptime
|
||||
done
|
||||
|
||||
@@ -24,8 +24,19 @@
|
||||
sleep 1800
|
||||
while true
|
||||
do
|
||||
>ALL_TRANSACTIONS_OVERVIEW.csv.tmp
|
||||
g_echo_note "Next loop"
|
||||
transactions_overview
|
||||
get_bitpanda_api_transactions
|
||||
get_justtrade_csv_transactions
|
||||
get_onetrading_csv_transactions
|
||||
get_phemex_csv_transactions
|
||||
get_transactions
|
||||
for transaction_csv in TRANSACTIONS-*.csv
|
||||
do
|
||||
calc_fifo_pnl "$transaction_csv"
|
||||
done
|
||||
mv ALL_TRANSACTIONS_OVERVIEW.csv.tmp ALL_TRANSACTIONS_OVERVIEW.csv
|
||||
webpage_transactions
|
||||
sleep 3600
|
||||
done
|
||||
|
||||
|
||||
@@ -40,23 +40,26 @@ function calc_ema {
|
||||
# check if there is a column (i from loop through array)
|
||||
[ -z "$f_column" ] && return 3
|
||||
|
||||
# check for enough positions/values to calculate (enough values)
|
||||
[ $f_position -ge $f_period ] || return 0
|
||||
|
||||
# get ema column
|
||||
[ -z "$f_target_column" ] && local f_target_column="ema$f_period"
|
||||
|
||||
local f_last_value=${v_csv_array_associative[${f_column}_${f_position}]}
|
||||
[ -z "$f_target_column" ] && return 4
|
||||
|
||||
local v
|
||||
local f_v
|
||||
|
||||
# reset old ema var
|
||||
unset f_ema
|
||||
|
||||
# find last EMA
|
||||
local f_last_ema_position=$((f_position-1))
|
||||
local f_last_ema=${v_csv_array_associative[${f_target_column}_${f_last_ema_pos}]}
|
||||
local f_last_ema=${v_csv_array_associative[${f_target_column}_${f_last_ema_position}]}
|
||||
|
||||
# check for enough positions/values to calculate (enough values) if SMA needed
|
||||
if [ -z "$f_last_ema" ]
|
||||
then
|
||||
[ $f_position -ge $f_period ] || return 5
|
||||
fi
|
||||
|
||||
# check if last EMA is given
|
||||
if [ -n "$f_last_ema" ]
|
||||
@@ -66,15 +69,17 @@ function calc_ema {
|
||||
else
|
||||
## calc SMA if previous EMA is not given (only needed on first EMA calc)
|
||||
# get last $f_period values
|
||||
g_echo_note "calc SMA - previous EMA is not given"
|
||||
local f_last_period_values_from=$((f_position-$f_period+1))
|
||||
local f_last_period_values
|
||||
for ((v=$f_last_period_values_from; v<=${f_position}; v++))
|
||||
for ((f_v=$f_last_period_values_from; f_v<=${f_position}; f_v++))
|
||||
do
|
||||
if [ -z ${f_last_period_values} ]
|
||||
then
|
||||
f_last_period_values=${v_csv_array_associative[${f_column}_${v}]}
|
||||
f_last_period_values=${v_csv_array_associative[${f_column}_${f_v}]}
|
||||
else
|
||||
f_last_period_values="$f_last_period_values+${v_csv_array_associative[${f_column}_${v}]}"
|
||||
g_calc "${f_last_period_values}+${v_csv_array_associative[${f_column}_${f_v}]}"
|
||||
f_last_period_values=$g_calc_result
|
||||
fi
|
||||
done
|
||||
# calc SMA (EMA=SMA in this special first case)
|
||||
@@ -82,9 +87,16 @@ function calc_ema {
|
||||
fi
|
||||
|
||||
# write back EMA
|
||||
if [[ $g_calc_result =~ ^- ]]
|
||||
then
|
||||
if ! [[ $f_period = 9 ]]
|
||||
then
|
||||
g_echo_warn "${FUNCNAME} $@: EMA can not be negative ($g_calc_result)"
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
v_csv_array_associative[${f_target_column}_${f_position}]=$g_calc_result
|
||||
f_ema=$g_calc_result
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
|
||||
525
dabo/functions/calc_fifo_pnl.sh
Normal file
525
dabo/functions/calc_fifo_pnl.sh
Normal file
@@ -0,0 +1,525 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Copyright (c) 2022-2024 olli
|
||||
#
|
||||
# This file is part of dabo (crypto bot).
|
||||
#
|
||||
# dabo is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# dabo is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with dabo. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
function calc_fifo_pnl {
|
||||
# Initialize variables
|
||||
local f_csv_file="$1"
|
||||
local f_current_year=$(date +%Y)
|
||||
declare -A f_holdings
|
||||
local f_date f_action f_symbol f_crypto_amount f_fiat_currency f_fiat_amount f_exchange f_fee_currency f_fee_amount f_note f_fiat_amount_tax_currency
|
||||
|
||||
# Read CSV file line by line
|
||||
while IFS=',' read -r f_date f_action f_symbol f_crypto_amount f_fiat_currency f_fiat_amount f_exchange f_fee_currency f_fee_amount f_note
|
||||
do
|
||||
|
||||
## Debug
|
||||
#[ "$f_symbol" == "ETH" ] || continue
|
||||
#[ "$f_note" == "short" ] && continue
|
||||
|
||||
# ignore stable coins
|
||||
[[ $f_symbol == USDT || $f_symbol == EUR ]] && continue
|
||||
|
||||
# Extract year from date
|
||||
local f_year=${f_date:0:4}
|
||||
|
||||
## Debug
|
||||
#[ "$f_year" == "2024" ] || continue
|
||||
#[ "$f_action" == "fundingfee" ] && continue
|
||||
|
||||
# add exchange
|
||||
[[ "$f_exchanges" != *"$f_exchange "* ]] && f_exchanges+="$f_exchange "
|
||||
|
||||
# prevent exponential numbers
|
||||
g_num_exponential2normal $f_crypto_amount
|
||||
f_crypto_amount=$g_num_exponential2normal_result
|
||||
g_num_exponential2normal $f_fiat_amount
|
||||
f_fiat_amount=$g_num_exponential2normal_result
|
||||
g_num_exponential2normal $f_fee_amount
|
||||
f_fee_amount=$g_num_exponential2normal_result
|
||||
|
||||
## Debug
|
||||
#echo "f_fiat_amount=$f_fiat_amount"
|
||||
|
||||
# convert f_fiat_currency/f_fiat_amount to TRANSFER_CURRENCY/f_fiat_amount_tax_currency if they are not equal
|
||||
if ! [ "$f_fiat_currency" == "$TRANSFER_CURRENCY" ] && [ "$f_fiat_amount" != "0" ]
|
||||
then
|
||||
currency_converter $f_fiat_amount "$f_fiat_currency" $TRANSFER_CURRENCY "$f_date" >/dev/null
|
||||
f_fiat_amount_tax_currency=$f_currency_converter_result
|
||||
else
|
||||
f_fiat_amount_tax_currency=$f_fiat_amount
|
||||
fi
|
||||
|
||||
## Debug
|
||||
#echo "f_fiat_amount_tax_currency=$f_fiat_amount_tax_currency"
|
||||
|
||||
# convert f_fee_currency/f_fee_amount to TRANSFER_CURRENCY/f_fiat_amount_tax_currency if present
|
||||
if [ -n "$f_fee_amount" ]
|
||||
then
|
||||
currency_converter $f_fee_amount $f_fee_currency $TRANSFER_CURRENCY "$f_date" >/dev/null
|
||||
f_fee_amount=$f_currency_converter_result
|
||||
[[ $f_action == "buy" || $f_action == "leverage-buy" ]] && g_calc "$f_fiat_amount_tax_currency + $f_fee_amount"
|
||||
[[ $f_action == "sell" || $f_action == "leverage-sell" || $f_action == "liquidation" ]] && g_calc "$f_fiat_amount_tax_currency - $f_fee_amount"
|
||||
f_fiat_amount_tax_currency=$g_calc_result
|
||||
fi
|
||||
|
||||
## Debug
|
||||
#echo "f_fiat_amount_tax_currency=$f_fiat_amount_tax_currency"
|
||||
|
||||
# no space in date (prevent problems mit $f_holdings)
|
||||
f_date="${f_date/ /T}"
|
||||
|
||||
# get current holfings for determining if this is a long or short trade (f_holdings_amount)
|
||||
get_holdings_amount
|
||||
|
||||
## Debug
|
||||
#echo "f_crypto_amount=$f_crypto_amount"
|
||||
|
||||
# Process fundingfee action
|
||||
if [[ $f_action == "fundingfee" ]]
|
||||
then
|
||||
process_fundingfee "$f_symbol" "$f_crypto_amount" "$f_fee_amount" "$f_date" "$f_year"
|
||||
# Process buy actions
|
||||
elif [[ $f_action == "buy" || $f_action == "leverage-buy" || $f_action == "reward-staking" || $f_action == "giveaway" || $f_action == "instant_trade_bonus" ]]
|
||||
then
|
||||
if g_num_is_higher_equal $f_holdings_amount 0
|
||||
then
|
||||
# long
|
||||
## Debug
|
||||
#echo process_buy "$f_symbol" "$f_crypto_amount" "$f_fiat_amount_tax_currency" "$f_date"
|
||||
process_buy "$f_symbol" "$f_crypto_amount" "$f_fiat_amount_tax_currency" "$f_date"
|
||||
elif g_num_is_lower_equal "$f_holdings_amount" "-$f_crypto_amount"
|
||||
then
|
||||
# short
|
||||
## Debug
|
||||
#echo process_sell "$f_symbol" "$f_crypto_amount" "$f_fiat_amount_tax_currency" "$f_date" "$f_year" short
|
||||
process_sell "$f_symbol" "$f_crypto_amount" "$f_fiat_amount_tax_currency" "$f_date" "$f_year" short
|
||||
else
|
||||
# long+short (partial)
|
||||
# calc long/short parts
|
||||
g_percentage-diff "-$f_crypto_amount" "$f_holdings_amount"
|
||||
g_calc "$f_fiat_amount_tax_currency/100*($g_percentage_diff_result+100)"
|
||||
f_fiat_amount_tax_currency_long=$g_calc_result
|
||||
g_calc "$f_fiat_amount_tax_currency/100*(($g_percentage_diff_result*-1))"
|
||||
f_fiat_amount_tax_currency_short=$g_calc_result
|
||||
g_calc "$f_crypto_amount/100*($g_percentage_diff_result+100)"
|
||||
f_crypto_amount_long=$g_calc_result
|
||||
g_calc "$f_crypto_amount/100*($g_percentage_diff_result*-1)"
|
||||
f_crypto_amount_short=$g_calc_result
|
||||
# part short-sell
|
||||
# part long-sell
|
||||
## Debug
|
||||
#echo PART: process_sell process_sell "$f_symbol" "$f_crypto_amount_long" "$f_fiat_amount_tax_currency_long" "$f_date" "$f_year"
|
||||
process_sell "$f_symbol" "$f_crypto_amount_long" "$f_fiat_amount_tax_currency_long" "$f_date" "$f_year"
|
||||
|
||||
## Debug
|
||||
#echo PART: process_buy "$f_symbol" "$f_crypto_amount_long" "$f_fiat_amount_tax_currency_long" "$f_date" "$f_year"
|
||||
process_buy "$f_symbol" "$f_crypto_amount_long" "$f_fiat_amount_tax_currency_long" "$f_date" "$f_year"
|
||||
fi
|
||||
# Process sell actions
|
||||
elif [[ $f_action == "sell" || $f_action == "leverage-sell" || $f_action == "liquidation" ]]
|
||||
then
|
||||
# check for long or short or log+short
|
||||
if g_num_is_higher_equal "$f_holdings_amount" "$f_crypto_amount"
|
||||
then
|
||||
# long
|
||||
## Debug
|
||||
#echo process_sell "$f_symbol" "$f_crypto_amount" "$f_fiat_amount_tax_currency" "$f_date" "$f_year"
|
||||
process_sell "$f_symbol" "$f_crypto_amount" "$f_fiat_amount_tax_currency" "$f_date" "$f_year"
|
||||
elif g_num_is_higher "$f_holdings_amount" 0
|
||||
then
|
||||
# long+short (partial)
|
||||
# calc long/short parts
|
||||
g_percentage-diff "$f_crypto_amount" "$f_holdings_amount"
|
||||
g_calc "$f_fiat_amount_tax_currency/100*($g_percentage_diff_result+100)"
|
||||
f_fiat_amount_tax_currency_long=$g_calc_result
|
||||
g_calc "$f_fiat_amount_tax_currency/100*(($g_percentage_diff_result*-1))"
|
||||
f_fiat_amount_tax_currency_short=$g_calc_result
|
||||
g_calc "$f_crypto_amount/100*($g_percentage_diff_result+100)"
|
||||
f_crypto_amount_long=$g_calc_result
|
||||
g_calc "$f_crypto_amount/100*($g_percentage_diff_result*-1)"
|
||||
f_crypto_amount_short=$g_calc_result
|
||||
# part long-sell
|
||||
## Debug
|
||||
#echo PART: process_sell "$f_symbol" "$f_crypto_amount_long" "$f_fiat_amount_tax_currency_long" "$f_date" "$f_year"
|
||||
process_sell "$f_symbol" "$f_crypto_amount_long" "$f_fiat_amount_tax_currency_long" "$f_date" "$f_year"
|
||||
# part short-sell
|
||||
## Debug
|
||||
#echo PART: process_buy "$f_symbol" "$f_crypto_amount_short" "$f_fiat_amount_tax_currency_short" "$f_date" short
|
||||
process_buy "$f_symbol" "$f_crypto_amount_short" "$f_fiat_amount_tax_currency_short" "$f_date" short
|
||||
elif [[ $f_action == "liquidation" ]]
|
||||
then
|
||||
# short sell/liquidation
|
||||
## Debug
|
||||
#echo process_sell "$f_symbol" "$f_crypto_amount" "$f_fiat_amount_tax_currency" "$f_date" $f_year short
|
||||
process_sell "$f_symbol" "$f_crypto_amount" "$f_fiat_amount_tax_currency" "$f_date" $f_year short
|
||||
else
|
||||
# short buy
|
||||
## Debug
|
||||
#echo process_buy "$f_symbol" "$f_crypto_amount" "$f_fiat_amount_tax_currency" "$f_date" short
|
||||
process_buy "$f_symbol" "$f_crypto_amount" "$f_fiat_amount_tax_currency" "$f_date" short
|
||||
fi
|
||||
fi
|
||||
|
||||
## DEBUG output
|
||||
#get_holdings_amount
|
||||
#echo "f_holdings_amount=$f_holdings_amount"
|
||||
#echo "============================"
|
||||
|
||||
done < "$f_csv_file"
|
||||
}
|
||||
|
||||
function process_buy {
|
||||
local f_symbol="$1" f_amount="$2" f_price="$3" f_date="$4" f_short="$5"
|
||||
local f_tax_type f_trade_tax
|
||||
# Add to holdings
|
||||
# long
|
||||
[ -z "$f_short" ] && f_holdings[$f_symbol]+="$f_amount:$f_price:$f_date "
|
||||
# short
|
||||
if [ -n "$f_short" ]
|
||||
then
|
||||
f_holdings[$f_symbol]+="-$f_amount:$f_price:$f_date "
|
||||
f_action="${f_action}-short"
|
||||
## Debug
|
||||
#echo ACTION:$f_action
|
||||
elif [[ $f_action == "reward-staking" ]]
|
||||
then
|
||||
f_tax_type="Sonst-Einkünfte-Staking"
|
||||
elif [[ $f_action == "giveaway" ]]
|
||||
then
|
||||
f_tax_type="Sonst-Einkünfte-Giveaway"
|
||||
elif [[ $f_action == "instant_trade_bonus" ]]
|
||||
then
|
||||
f_tax_type="Kapitalertrag-Instant-Trade-Bonus"
|
||||
fi
|
||||
|
||||
if [ -n "$f_tax_type" ]
|
||||
then
|
||||
f_trade_tax=$f_price
|
||||
f_fiat_amount=0
|
||||
f_fiat_amount_tax_currency=0
|
||||
fi
|
||||
|
||||
|
||||
echo "$f_date,$f_exchange,$f_action,$f_symbol,$f_amount,$f_fiat_currency,-$f_price,,,,,,$f_tax_type,$f_trade_tax,-$f_price,,,,,,,,," >>ALL_TRANSACTIONS_OVERVIEW.csv.tmp
|
||||
}
|
||||
|
||||
function process_sell {
|
||||
local f_symbol="$1" f_sell_amount="$2" f_sell_price="$3" f_sell_date="$4" f_year="$5" f_short="$6"
|
||||
f_remaining_sell=$f_sell_amount
|
||||
local f_profit=0 f_loss=0 f_profit_tax=0 f_loss_tax=0 f_trade_tax=0
|
||||
local f_pnl
|
||||
|
||||
# define tax type
|
||||
local f_tax_type="Kapitalertrag-Derivat"
|
||||
local f_trade_result
|
||||
[[ $f_action == "sell" ]] && f_tax_type="Veräußerungsgeschäft"
|
||||
local f_buy_amount f_buy_price f_buy_date
|
||||
|
||||
# Process each holding using FIFO
|
||||
while [[ $f_remaining_sell > 0 && -n "${f_holdings[$f_symbol]}" ]]
|
||||
do
|
||||
|
||||
IFS=':' read -r f_buy_amount f_buy_price f_buy_date < <(echo "${f_holdings[$f_symbol]%% *}")
|
||||
|
||||
# Calculate amount to sell from this holding
|
||||
f_sell_from_holding=$f_buy_amount
|
||||
[ -n "$f_short" ] && f_remaining_sell=-${f_remaining_sell#-}
|
||||
[ -z "$f_short" ] && g_num_is_lower $f_remaining_sell $f_buy_amount && f_sell_from_holding=$f_remaining_sell
|
||||
[ -n "$f_short" ] && g_num_is_higher $f_remaining_sell $f_buy_amount && f_sell_from_holding=$f_remaining_sell
|
||||
|
||||
# calculate sell percentage of buy trade
|
||||
## Debug
|
||||
#echo "f_sell_from_holding=$f_sell_from_holding"
|
||||
[ -z "$f_short" ] && g_percentage-diff $f_buy_amount $f_sell_from_holding
|
||||
[ -n "$f_short" ] && g_percentage-diff $f_buy_amount $f_sell_from_holding
|
||||
g_calc "100+$g_percentage_diff_result"
|
||||
f_percentage_of_buy=${g_calc_result#-}
|
||||
|
||||
## Debug
|
||||
#echo "f_percentage_of_buy=$f_percentage_of_buy"
|
||||
|
||||
# Calculate profit/loss (pnl)
|
||||
## Debug
|
||||
#echo "f_sell_price=$f_sell_price"
|
||||
#echo "f_buy_price=$f_buy_price"
|
||||
# if not first iteration (f_pnl is already set from previous iteration) and partial sell
|
||||
if [ -n "$f_pnl" ]
|
||||
then
|
||||
# on multiple iteration partial sell
|
||||
g_calc "$f_pnl - ($f_buy_price/100*$f_percentage_of_buy)"
|
||||
else
|
||||
# on first interation partial sell
|
||||
g_calc "$f_sell_price - ($f_buy_price/100*$f_percentage_of_buy)"
|
||||
fi
|
||||
local f_pnl=$g_calc_result
|
||||
## Debug
|
||||
#echo "f_pnl=$f_pnl"
|
||||
|
||||
# Check if trade is tax-free (held for more than a year)
|
||||
local f_is_taxable=true
|
||||
if [ "$f_tax_type" == "Veräußerungsgeschäft" ]
|
||||
then
|
||||
local f_days_held=$(( ($(date -d "$f_sell_date" +%s) - $(date -d "$f_buy_date" +%s)) / 86400 ))
|
||||
[[ $f_days_held -gt 365 && ${f_tax_type} == "Veräußerungsgeschäft" ]] && f_is_taxable=false
|
||||
fi
|
||||
|
||||
# Update remaining sell amount and holdings
|
||||
g_calc "$f_remaining_sell - $f_sell_from_holding"
|
||||
f_remaining_sell=$g_calc_result
|
||||
## Debug
|
||||
#echo "f_remaining_sell=$f_remaining_sell"
|
||||
#echo "HOLDINGS1: ${f_holdings[$f_symbol]}"
|
||||
f_holdings[$f_symbol]="${f_holdings[$f_symbol]#* }"
|
||||
|
||||
# If there's remaining amount in the holding, add it back
|
||||
[ -z "$f_short" ] && g_calc "$f_buy_amount - $f_sell_from_holding"
|
||||
[ -n "$f_short" ] && g_calc "$f_buy_amount + $f_sell_from_holding"
|
||||
g_calc "$f_buy_amount - $f_sell_from_holding"
|
||||
local f_remaining_buy_amount=$g_calc_result
|
||||
## Debug
|
||||
#echo "f_remaining_buy_amount=$g_calc_result"
|
||||
#if g_num_is_higher $f_remaining_buy_amount 0
|
||||
if [ "$f_remaining_buy_amount" != "0" ]
|
||||
then
|
||||
g_calc "$f_buy_price/100*(100-$f_percentage_of_buy)"
|
||||
f_remaining_buy_price=$g_calc_result
|
||||
f_holdings[$f_symbol]="$f_remaining_buy_amount:$f_remaining_buy_price:$f_buy_date ${f_holdings[$f_symbol]}"
|
||||
fi
|
||||
## Debug
|
||||
#echo "HOLDINGS2: ${f_holdings[$f_symbol]}"
|
||||
done
|
||||
|
||||
# Update profit/loss
|
||||
[ -n "$f_short" ] && g_calc "$f_pnl * -1" && f_pnl=$g_calc_result
|
||||
if g_num_is_higher $f_pnl 0
|
||||
then
|
||||
g_calc "$f_profit + $f_pnl"
|
||||
f_profit=$g_calc_result
|
||||
else
|
||||
g_calc "$f_loss - $f_pnl"
|
||||
f_loss=$g_calc_result
|
||||
fi
|
||||
|
||||
# calculate result of trade
|
||||
g_calc "$f_profit - $f_loss"
|
||||
f_trade_result=$g_calc_result
|
||||
|
||||
# calculate taxable part of trade
|
||||
if [[ $f_is_taxable == true ]]
|
||||
then
|
||||
g_calc "$f_trade_tax + $f_pnl"
|
||||
f_trade_tax=$g_calc_result
|
||||
fi
|
||||
|
||||
## DEBUG output
|
||||
#get_holdings_amount
|
||||
#echo "f_holdings_amount=$f_holdings_amount"
|
||||
#echo "Result: $f_trade_result ; taxable=$f_is_taxable ; REMAINING: $f_holdings_amount"
|
||||
|
||||
# write to csv
|
||||
if [ -n "$f_short" ]
|
||||
then
|
||||
f_action="${f_action}-short"
|
||||
## Debug
|
||||
#echo ACTION:$f_action
|
||||
fi
|
||||
[ "$f_trade_tax" == "0" ] && [ "$f_tax_type" == "Veräußerungsgeschäft" ] && f_tax_type="Veräußerungsgeschäft Spekulationsfrist > 1 Jahr"
|
||||
echo "$f_date,$f_exchange,$f_action,$f_symbol,-$f_sell_amount,$f_fiat_currency,$f_sell_price,,,,,,$f_tax_type,$f_trade_tax,$f_sell_price,,$f_trade_result,,,,,,," >>ALL_TRANSACTIONS_OVERVIEW.csv.tmp
|
||||
|
||||
[ -z "$f_trade_result" ] && g_echo_error "No trade result!!! Someting wrong $f_date,$f_symbol,$f_action $f_short"
|
||||
|
||||
}
|
||||
|
||||
function get_holdings_amount {
|
||||
local block first_value
|
||||
f_holdings_amount=0
|
||||
|
||||
# Durch jeden Block iterieren
|
||||
IFS=" "
|
||||
for block in ${f_holdings[$f_symbol]}
|
||||
do
|
||||
IFS=$origIFS
|
||||
# Den ersten Wert vor dem Doppelpunkt extrahieren
|
||||
first_value=${block%%:*}
|
||||
|
||||
# Zum Gesamtwert addieren
|
||||
g_calc "$f_holdings_amount + $first_value"
|
||||
f_holdings_amount=$g_calc_result
|
||||
done
|
||||
IFS=$origIFS
|
||||
}
|
||||
|
||||
function process_fundingfee {
|
||||
local f_symbol="$1" f_amount="$2" f_fiat_amount_tax_currency="$3" f_date="$4" f_year="$5"
|
||||
|
||||
## Debug
|
||||
#echo "adding fundingfee: $f_fiat_amount_tax_currency"
|
||||
|
||||
## add fundingfee
|
||||
[[ $f_fiat_amount_tax_currency == -* ]] && f_tax="${f_fiat_amount_tax_currency#-}"
|
||||
[[ $f_fiat_amount_tax_currency == -* ]] || f_tax="-${f_fiat_amount_tax_currency}"
|
||||
|
||||
echo "$f_date,$f_exchange,$f_action,$f_symbol,$f_amount,,,,,,,,Kapitalertrag-Derivat,$f_tax,$f_tax,,$f_tax,,,,,,," >>ALL_TRANSACTIONS_OVERVIEW.csv.tmp
|
||||
}
|
||||
|
||||
#function transaction_csv_validity_ckecks {
|
||||
# local f_buy f_sell f_liquidation f_liquidation_short
|
||||
# local f_complete_result=0
|
||||
# declare -A transaction_csv_validity_ckeck_buy_sell_diff
|
||||
#
|
||||
# f_symbols=$(cut -d, -f3 $f_csv_file | sort -u)
|
||||
# local f_buy_amount f_sell_amount f_tax_type
|
||||
#
|
||||
# # go through symbols and male some pre-checks
|
||||
# for f_symbol in $f_symbols
|
||||
# do
|
||||
#
|
||||
# ## check asset amount
|
||||
# g_echo_note "Initial checks for $f_symbol"
|
||||
# # add all buys and sells of a symbols amount
|
||||
# f_buy=$(\
|
||||
# egrep "buy,${f_symbol},|,reward-staking,${f_symbol}|,giveaway,${f_symbol},instant_trade_bonus,${f_symbol}" "$f_csv_file" | \
|
||||
# cut -d, -f4 | \
|
||||
# awk '{ SUM += $1} END { printf("%.12f\n", SUM) }' \
|
||||
# )
|
||||
# f_sell=$(\
|
||||
# egrep "sell,${f_symbol}," "$f_csv_file" | \
|
||||
# cut -d, -f4 | \
|
||||
# awk '{ SUM += $1} END { printf("%.12f\n", SUM) }' \
|
||||
# )
|
||||
# f_liquidation=$(\
|
||||
# egrep "liquidation,${f_symbol}," "$f_csv_file" | \
|
||||
# grep -v ",short" | \
|
||||
# cut -d, -f4 | \
|
||||
# awk '{ SUM += $1} END { printf("%.12f\n", SUM) }' \
|
||||
# )
|
||||
# f_liquidation_short=$(\
|
||||
# egrep "liquidation,${f_symbol},.+,short" "$f_csv_file" | \
|
||||
# cut -d, -f4 | \
|
||||
# awk '{ SUM += $1} END { printf("%.12f\n", SUM) }' \
|
||||
# )
|
||||
#
|
||||
# # add liquidations to sell
|
||||
# # long
|
||||
# g_calc "$f_sell + $f_liquidation - $f_liquidation_short"
|
||||
# f_sell=$g_calc_result
|
||||
#
|
||||
# # buy should be same as sell sum to be fine - if not:
|
||||
# g_calc "$f_buy == $f_sell"
|
||||
# if ! [[ $g_calc_result == 1 ]]
|
||||
# then
|
||||
# g_echo_note "buy ($f_buy) and sell ($f_sell) amount sums are different for ${f_symbol}. Open Positions!?"
|
||||
# g_calc "$f_sell - ($f_buy)"
|
||||
# transaction_csv_validity_ckecks[$f_symbol]=$g_calc_result
|
||||
# else
|
||||
# transaction_csv_validity_ckeck_buy_sell_diff[$f_symbol]=0
|
||||
# fi
|
||||
#
|
||||
# done
|
||||
#}
|
||||
|
||||
function print_results {
|
||||
|
||||
local f_csv=ALL_TRANSACTIONS_OVERVIEW.csv
|
||||
local f_exchange_symbol f_exchange_symbol_year_tax f_amount f_result
|
||||
#transaction_csv_validity_ckecks
|
||||
|
||||
echo ""
|
||||
echo "Open Positions:"
|
||||
echo "==============="
|
||||
|
||||
local f_exchanges_symbols=$(cut -d, -f 2,4 "$f_csv" | sort -u)
|
||||
for f_exchange_symbol in $f_exchanges_symbols
|
||||
do
|
||||
f_exchange=${f_exchange_symbol%%,*}
|
||||
f_symbol=${f_exchange_symbol#*,}
|
||||
f_amount=$(\
|
||||
egrep ",${f_exchange},.+,$f_symbol" "$f_csv" | \
|
||||
cut -d, -f5 | \
|
||||
awk '{ SUM += $1} END { printf("%.12f\n", SUM) }' \
|
||||
)
|
||||
|
||||
f_result=$(\
|
||||
egrep ",${f_exchange},.*sell,$f_symbol|,${f_exchange},.*buy,$f_symbol" "$f_csv" | \
|
||||
#egrep "${f_exchange},.+,$f_symbol" "$f_csv" \
|
||||
cut -d, -f17 | \
|
||||
awk '{ SUM += $1} END { printf("%.2f\n", SUM) }' \
|
||||
)
|
||||
|
||||
g_calc "$f_amount == 0"
|
||||
if ! [[ $g_calc_result == 1 ]]
|
||||
then
|
||||
echo "$f_exchange/$f_symbol: $f_amount"
|
||||
fi
|
||||
|
||||
done
|
||||
|
||||
echo ""
|
||||
echo "Profit and Loss (Tax):"
|
||||
echo "======================"
|
||||
|
||||
declare -A f_taxes f_pnls
|
||||
local f_total_tax
|
||||
local f_exchanges_symbols_years_tax=$(sed 's/-/,/' "$f_csv" | cut -d, -f 1,3,5,14 | sort -u)
|
||||
for f_exchange_symbol_year_tax in $f_exchanges_symbols_years_tax
|
||||
do
|
||||
IFS=',' read -r f_year f_exchange f_symbol f_tax_type < <(echo "$f_exchange_symbol_year_tax")
|
||||
[ -z "$f_tax_type" ] && continue
|
||||
f_tax=$(\
|
||||
egrep "^$f_year-.+,${f_exchange},.+,${f_symbol},.+,$f_tax_type" "$f_csv" | \
|
||||
cut -d, -f14 | \
|
||||
awk '{ SUM += $1} END { printf("%.2f\n", SUM) }' \
|
||||
)
|
||||
f_pnl=$(\
|
||||
egrep "^$f_year-.+,${f_exchange},.+,${f_symbol},.+,$f_tax_type" "$f_csv" | \
|
||||
cut -d, -f17 | \
|
||||
awk '{ SUM += $1} END { printf("%.2f\n", SUM) }' \
|
||||
)
|
||||
echo "$f_year/$f_exchange/$f_symbol/$f_tax_type: $f_tax $TRANSFER_CURRENCY"
|
||||
[ -z "${f_taxes[${f_year}_${f_exchange}_${f_tax_type}]}" ] && f_taxes[${f_year}_${f_exchange}_${f_tax_type}]=0
|
||||
[ -z "${f_pnls[${f_year}_${f_exchange}]}" ] && f_pnls[${f_year}_${f_exchange}]=0
|
||||
g_calc "${f_taxes[${f_year}_${f_exchange}_${f_tax_type}]} + ($f_tax)"
|
||||
f_taxes[${f_year}_${f_exchange}_${f_tax_type}]=$g_calc_result
|
||||
g_calc "${f_pnls[${f_year}_${f_exchange}]} + ($f_pnl)"
|
||||
f_pnls[${f_year}_${f_exchange}]=$g_calc_result
|
||||
|
||||
done
|
||||
|
||||
echo ""
|
||||
echo "Profit and Loss (Tax per exchange):"
|
||||
echo "==================================="
|
||||
|
||||
for f_tax_year in "${!f_taxes[@]}"
|
||||
do
|
||||
echo "$f_tax_year: ${f_taxes[$f_tax_year]} $TRANSFER_CURRENCY"
|
||||
done | sort
|
||||
|
||||
echo ""
|
||||
echo "Profit and Loss:"
|
||||
echo "================"
|
||||
|
||||
for f_pnl_year in "${!f_pnls[@]}"
|
||||
do
|
||||
echo "$f_pnl_year: ${f_pnls[$f_pnl_year]} $TRANSFER_CURRENCY"
|
||||
done | sort
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -26,14 +26,20 @@ function currency_converter {
|
||||
local f_currency=$2
|
||||
local f_currency_target=$3
|
||||
local f_currency_date=$4
|
||||
|
||||
local f_return
|
||||
|
||||
unset f_currency_converter_result
|
||||
|
||||
local f_line f_rate f_histfile f_date_array f_stablecoin f_reverse
|
||||
# check for cached result
|
||||
local f_args=$@
|
||||
[ -f CACHE_CURRENCY_CONVERTER ] && f_currency_converter_result=$(egrep "^${f_args}=" CACHE_CURRENCY_CONVERTER | cut -d= -f2)
|
||||
[[ -n $f_currency_converter_result ]] && g_num_valid_number "$f_currency_converter_result" && return 0
|
||||
|
||||
local f_line f_rate f_histfile f_date_array f_stablecoin f_reverse f_file f_link_file f_timeframe
|
||||
|
||||
if [[ $f_currency_target =~ ^20.*-.*: ]]
|
||||
then
|
||||
g_echo_warn "${FUNCNAME}: Invalid target $f_currency_target"
|
||||
g_echo_warn "${FUNCNAME} $@: Invalid target"
|
||||
g_traceback
|
||||
return 1
|
||||
fi
|
||||
@@ -51,27 +57,30 @@ function currency_converter {
|
||||
f_currency_date_day=$(date -d "${f_currency_date}" "+%Y-%m-%d")
|
||||
|
||||
# month failback
|
||||
f_currency_date_month=$(date -d "${f_currency_date}" "+%Y-%m")
|
||||
if [ $(date -d "${f_currency_date}" "+%d") = "01" ]
|
||||
then
|
||||
# on first day in month use month before because no date from current month
|
||||
f_currency_date_month=$(date -d "${f_currency_date} yesterday" "+%Y-%m")
|
||||
else
|
||||
f_currency_date_month=$(date -d "${f_currency_date}" "+%Y-%m")
|
||||
fi
|
||||
|
||||
# path to history files for the converting rate
|
||||
[ -d asset-histories ] && f_asset_histories="asset-histories/"
|
||||
[ -d asset-histories ] || mkdir asset-histories
|
||||
f_asset_histories="asset-histories/"
|
||||
|
||||
# map USD-Stablecoins to USD
|
||||
local f_stablecoins="USDT BUSD"
|
||||
for f_stablecoin in $f_stablecoins
|
||||
do
|
||||
# Link USD Stablecoin files to USD
|
||||
if [ -s ${f_asset_histories}${f_currency}${f_stablecoin}.history-raw.csv ]
|
||||
then
|
||||
[ -e ${f_asset_histories}${f_currency}USD.history-raw.csv ] || \
|
||||
ln ${f_asset_histories}${f_currency}${f_stablecoin}.history-raw.csv ${f_asset_histories}${f_currency}USD.history-raw.csv
|
||||
fi
|
||||
|
||||
if [ -s ${f_asset_histories}${f_currency_target}${f_stablecoin}.history-raw.csv ]
|
||||
then
|
||||
[ -e ${f_asset_histories}USD${f_currency_target}.history-raw.csv ] || \
|
||||
ln ${f_asset_histories}${f_currency_target}${f_stablecoin}.history-raw.csv ${f_asset_histories}USD${f_currency_target}.history-raw.csv
|
||||
fi
|
||||
cd "$f_asset_histories"
|
||||
find . -maxdepth 1 -mindepth 1 -name "*${f_stablecoin}.history.*.csv" | while read f_file
|
||||
do
|
||||
f_link_file=${f_file/${f_stablecoin}/USD}
|
||||
ln -sf "$f_file" "$f_link_file"
|
||||
done
|
||||
cd - >/dev/null
|
||||
# use USD
|
||||
if [[ $f_currency_target = $f_stablecoin ]]
|
||||
then
|
||||
@@ -82,6 +91,29 @@ function currency_converter {
|
||||
f_currency=USD
|
||||
fi
|
||||
done
|
||||
|
||||
# map EUR-Stablecoins to EUR
|
||||
local f_stablecoins="EURC"
|
||||
for f_stablecoin in $f_stablecoins
|
||||
do
|
||||
# Link EUR Stablecoin files to EUR
|
||||
cd "$f_asset_histories"
|
||||
find . -maxdepth 1 -mindepth 1 -name "*${f_stablecoin}.history.*.csv" | while read f_file
|
||||
do
|
||||
f_link_file=${f_file/${f_stablecoin}/EUR}
|
||||
ln -sf "$f_file" "$f_link_file"
|
||||
done
|
||||
cd - >/dev/null
|
||||
# use USD
|
||||
if [[ $f_currency_target = $f_stablecoin ]]
|
||||
then
|
||||
f_currency_target=EUR
|
||||
fi
|
||||
if [[ $f_currency = $f_stablecoin ]]
|
||||
then
|
||||
f_currency=EUR
|
||||
fi
|
||||
done
|
||||
|
||||
# if there is no currency change (USD USD or USDT USD)
|
||||
if [[ $f_currency == $f_currency_target ]]
|
||||
@@ -89,54 +121,72 @@ function currency_converter {
|
||||
f_currency_converter_result=$f_currency_amount
|
||||
return 0
|
||||
fi
|
||||
|
||||
# try direct pair
|
||||
get_marketdata_yahoo "${f_currency_target}-${f_currency}" "${f_currency_target}${f_currency}" || get_marketdata_yahoo "${f_currency}-${f_currency_target}" "${f_currency}${f_currency_target}"
|
||||
local f_histfile_default="${f_asset_histories}${f_currency_target}${f_currency}.history"
|
||||
local f_histfile_yahoo="${f_asset_histories}${f_currency_target}${f_currency}.history"
|
||||
# reverse as backup
|
||||
local f_histfile_default_reverse="${f_asset_histories}${f_currency}${f_currency_target}.history"
|
||||
local f_histfile_yahoo_reverse="${f_asset_histories}${f_currency}${f_currency_target}.history"
|
||||
|
||||
# search for rate by date
|
||||
for f_histfile in "$f_histfile_default" "$f_histfile_default_reverse" "$f_histfile_yahoo" "$f_histfile_yahoo_reverse"
|
||||
# define possiblefiles
|
||||
local f_histfile_default="${f_asset_histories}${f_currency_target}${f_currency}.history"
|
||||
local f_histfile_default_reverse="${f_asset_histories}${f_currency}${f_currency_target}.history"
|
||||
|
||||
for f_histfile in "$f_histfile_default" "$f_histfile_default_reverse"
|
||||
do
|
||||
# histfile has to exist
|
||||
#if [ -s "${f_histfile}*.csv" ]
|
||||
#then
|
||||
# search for most precise date
|
||||
f_line=$(egrep "^$f_currency_date_minute" "$f_histfile".*m.csv 2>/dev/null | tail -n1)
|
||||
[ -z "$f_line" ] && f_line=$(egrep "^$f_currency_date_hour" "$f_histfile".*h.csv 2>/dev/null | tail -n1)
|
||||
[ -z "$f_line" ] && f_line=$(egrep "^$f_currency_date_day" "$f_histfile".*d.csv 2>/dev/null | tail -n1)
|
||||
[ -z "$f_line" ] && f_line=$(egrep "^$f_currency_date_month" "$f_histfile"*.csv 2>/dev/null | tail -n1)
|
||||
[ -n "$f_line" ] && f_rate=$(echo "$f_line" | cut -d, -f2)
|
||||
f_reverse=false
|
||||
if [ -n "$f_rate" ]
|
||||
then
|
||||
[[ $f_histfile =~ ${f_currency}${f_currency_target} ]] && f_reverse=true
|
||||
[ $f_currency_target = "USD" ] && f_reverse=true
|
||||
[ $f_currency = "USD" ] && f_reverse=false
|
||||
[ $f_currency_target = "EUR" ] && [ $f_currency = "USD" ] && f_reverse=true
|
||||
[[ $f_line =~ ^$f_currency_date_hour ]] && break
|
||||
fi
|
||||
#fi
|
||||
# search for most precise date
|
||||
f_line=$(egrep "^$f_currency_date_minute" "$f_histfile"*m.csv 2>/dev/null | sort | tail -n1)
|
||||
[ -z "$f_line" ] && f_line=$(egrep "^$f_currency_date_hour" "$f_histfile"*m.csv 2>/dev/null | sort | tail -n1)
|
||||
[ -z "$f_line" ] && f_line=$(egrep "^$f_currency_date_day" "$f_histfile"*h.csv 2>/dev/null | sort | tail -n1)
|
||||
done
|
||||
|
||||
# end if no rate found
|
||||
# download from coinmarketcap if nothing found
|
||||
[ -z "$f_line" ] && for f_histfile in "$f_histfile_default" "$f_histfile_default_reverse"
|
||||
do
|
||||
# download data from coinmarketcap
|
||||
for f_timeframe in 1d 1w
|
||||
do
|
||||
[ "${f_currency}" = "USD" ] && get_marketdata_coinmarketcap "${f_currency_target}-${f_currency}" "${f_currency_target}${f_currency}" $f_timeframe
|
||||
[ "${f_currency_target}" = "USD" ] && get_marketdata_coinmarketcap "${f_currency}-${f_currency_target}" "${f_currency}${f_currency_target}" $f_timeframe
|
||||
done
|
||||
f_line=$(egrep "^$f_currency_date_minute" "$f_histfile"*m.csv 2>/dev/null | sort | tail -n1)
|
||||
[ -z "$f_line" ] && f_line=$(egrep "^$f_currency_date_hour" "$f_histfile"*m.csv 2>/dev/null | sort | tail -n1)
|
||||
[ -z "$f_line" ] && f_line=$(egrep "^$f_currency_date_day" "$f_histfile"*h.csv 2>/dev/null | sort | tail -n1)
|
||||
[ -z "$f_line" ] && f_line=$(egrep "^$f_currency_date_month" "$f_histfile"*.csv 2>/dev/null | sort | tail -n1)
|
||||
[ -n "$f_line" ] && break
|
||||
done
|
||||
|
||||
# extract rate/price
|
||||
[ -n "$f_line" ] && f_rate=$(echo "$f_line" | cut -d, -f2)
|
||||
f_reverse=false
|
||||
if [ -n "$f_rate" ]
|
||||
then
|
||||
[[ $f_histfile =~ ${f_currency}${f_currency_target} ]] && f_reverse=true
|
||||
[ $f_currency_target = "USD" ] && f_reverse=true
|
||||
[ $f_currency = "USD" ] && f_reverse=false
|
||||
[ $f_currency_target = "EUR" ] && [ $f_currency = "USD" ] && f_reverse=false
|
||||
[[ $f_line =~ ^$f_currency_date_hour ]] && break
|
||||
fi
|
||||
|
||||
# if no rate found
|
||||
if [ -z "$f_rate" ]
|
||||
then
|
||||
# try workaround over USD if EUR
|
||||
if [[ ${f_currency_target} = EUR ]] && [[ $f_currency != USD ]]
|
||||
# if EUR source or traget try way over USD as workaround
|
||||
if [[ ${f_currency_target} = EUR ]] && [[ $f_currency != USD ]] && [[ $f_currency != EUR ]]
|
||||
then
|
||||
#g_echo_note "trying way over USD (workaround)"
|
||||
if currency_converter $f_currency_amount $f_currency USD $f_currency_date
|
||||
g_echo_note "trying way over USD (workaround) Target EUR"
|
||||
if currency_converter $f_currency_amount $f_currency USD "$f_currency_date"
|
||||
then
|
||||
f_currency_amount=$f_currency_converter_result
|
||||
currency_converter $f_currency_amount USD EUR $f_currency_date
|
||||
return $?
|
||||
currency_converter $f_currency_converter_result USD EUR "$f_currency_date" && f_return=$?
|
||||
[[ $f_return == 0 ]] && echo "$@=$f_currency_converter_result" >>CACHE_CURRENCY_CONVERTER
|
||||
return $f_return
|
||||
fi
|
||||
elif [[ ${f_currency_target} != USD ]] && [[ ${f_currency_target} != EUR ]] && [[ $f_currency = EUR ]]
|
||||
then
|
||||
g_echo_note "trying way over USD (workaround) Source EUR"
|
||||
if currency_converter $f_currency_amount EUR USD "$f_currency_date"
|
||||
then
|
||||
currency_converter $f_currency_converter_result USD ${f_currency_target} "$f_currency_date" && f_return=$?
|
||||
[[ $f_return == 0 ]] && echo "$@=$f_currency_converter_result" >>CACHE_CURRENCY_CONVERTER
|
||||
return $f_return
|
||||
fi
|
||||
fi
|
||||
g_echo_note "didn't find rate for ${f_currency}-${f_currency_target} - '${FUNCNAME} $@'"
|
||||
# end if no rate found
|
||||
g_echo_error "didn't find rate for ${f_currency}-${f_currency_target} - '${FUNCNAME} $@'"
|
||||
return 1
|
||||
fi
|
||||
|
||||
@@ -145,5 +195,6 @@ function currency_converter {
|
||||
[[ $f_reverse = false ]] && g_calc "1/${f_rate}*${f_currency_amount}"
|
||||
f_currency_converter_result=$g_calc_result
|
||||
|
||||
}
|
||||
echo "$@=$f_currency_converter_result" >>CACHE_CURRENCY_CONVERTER
|
||||
|
||||
}
|
||||
|
||||
@@ -58,7 +58,7 @@ function get_bitpanda_api_transactions {
|
||||
select(.effective_leverage!= null) |
|
||||
.time.date_iso8601 + ",leverage-" + .type + "," + .cryptocoin_symbol + "," + .amount_cryptocoin + ",EUR," + .amount_fiat + ",Bitpanda"
|
||||
' BITPANDA_trades.json >>BITPANDA.csv.tmp
|
||||
# Workaround fpr staking-rewards (not availabpe per API yet (https://help.blockpit.io/hc/de-at/articles/360011790820-Wie-importiere-ich-Daten-mittels-Bitpanda-API-Key)
|
||||
# Workaround for staking-rewards (not availabpe per API yet (https://help.blockpit.io/hc/de-at/articles/360011790820-Wie-importiere-ich-Daten-mittels-Bitpanda-API-Key)
|
||||
[ -s bitpanda-export.csv ] && cat bitpanda-export.csv | grep reward,incoming | awk -F, '{print $2",reward-staking,"$8","$7",EUR,"$5",Bitpanda"}' >>BITPANDA.csv.tmp
|
||||
|
||||
cat BITPANDA.csv.tmp | grep -v ",reward.best," | sort >TRANSACTIONS-BITPANDA.csv
|
||||
|
||||
@@ -26,7 +26,8 @@ function get_indicators_all {
|
||||
|
||||
local f_histfile f_symbol
|
||||
|
||||
find asset-histories -maxdepth 1 -name "ECONOMY-*.history.[0-5][5dhwm]*.csv" | sort | while read f_histfile
|
||||
# ECONOMY and MARKETDATA
|
||||
find asset-histories -maxdepth 1 -name "ECONOMY-*.history.[0-5][5dhwm]*.csv" -o -name "MARKETDATA_*.history.[0-5][5dhwm]*.csv" | sort | while read f_histfile
|
||||
do
|
||||
if [ -s "${f_histfile}.fetching" ] || [ -s "${f_histfile}.indicators-calculating" ]
|
||||
then
|
||||
@@ -35,10 +36,10 @@ function get_indicators_all {
|
||||
fi
|
||||
|
||||
# do the job
|
||||
printf '%(%Y-%m-%d %H:%M:%S)T' >"${f_histfile}.indicators-calculating"
|
||||
get_indicators "${f_histfile}" ${f_last_intervals} && printf '%(%Y-%m-%d %H:%M:%S)T' >>"$f_histfile.indicators-calculated"
|
||||
printf "$0 %(%Y-%m-%d %H:%M:%S)T" >"${f_histfile}.indicators-calculating"
|
||||
get_indicators "${f_histfile}" ${f_last_intervals} && printf "$0 %(%Y-%m-%d %H:%M:%S)T\n" >>"$f_histfile.indicators-calculated"
|
||||
# add missing intervals for example from weekends from non-24h-assets like economic data - interval from filename
|
||||
f_add_missing_ohlcv_intervals "${f_histfile}"
|
||||
#f_add_missing_ohlcv_intervals "${f_histfile}"
|
||||
rm -f "${f_histfile}.indicators-calculating"
|
||||
done
|
||||
|
||||
@@ -62,14 +63,14 @@ function get_indicators_all {
|
||||
fi
|
||||
|
||||
# do the job
|
||||
printf '%(%Y-%m-%d %H:%M:%S)T' >"${f_histfile}.indicators-calculating"
|
||||
get_indicators "${f_histfile}" ${f_last_intervals} && printf '%(%Y-%m-%d %H:%M:%S)T' >>"$f_histfile.indicators-calculated"
|
||||
printf "$0 %(%Y-%m-%d %H:%M:%S)T" >"${f_histfile}.indicators-calculating"
|
||||
get_indicators "${f_histfile}" ${f_last_intervals} && printf "$0 %(%Y-%m-%d %H:%M:%S)T\n" >>"$f_histfile.indicators-calculated"
|
||||
rm -f "${f_histfile}.indicators-calculating"
|
||||
|
||||
fi
|
||||
done
|
||||
done
|
||||
shopt +s nullglob
|
||||
shopt -u nullglob
|
||||
|
||||
}
|
||||
|
||||
@@ -82,17 +83,27 @@ function get_indicators {
|
||||
local f_fill_missing_ohlcv_intervals=$3
|
||||
local f_line
|
||||
|
||||
# check if the job is already done
|
||||
if [ $(wc -l <"${f_histfile}") -gt 801 ]
|
||||
then
|
||||
if ! tail +801 "${f_histfile}" | egrep -vq "^....-..-..*,[0-9\.]+,[0-9\.]+,[0-9\.]+,[0-9\.]+,[0-9\.]+,[0-9\.\-]+,[0-9\.]+,[0-9\.]+,[0-9\.]+,[0-9\.]+,[0-9\.]+,[0-9\.]+,[0-9\.]+,[0-9\.]+,[0-9\.]+,[0-9\.]+,[0-9\.]+,[0-9\.\-]+,[0-9\.\-]+,[0-9\.\-]+,[a-z]*,[0-9\.\-]+,[0-9\.\-]+"
|
||||
then
|
||||
g_echo_note "${f_histfile} seems to be indicator-complete"
|
||||
return 0
|
||||
fi
|
||||
fi
|
||||
|
||||
# history
|
||||
local f_columns="date,open,high,low,close,volume,change,ath,ema12,ema26,ema50,ema100,ema200,ema400,ema800,rsi5,rsi14,rsi21,macd,macd_ema9_signal,macd_histogram,macd_histogram_signal,macd_histogram_max,macd_histogram_strength"
|
||||
local f_emas="12 26 50 100 200 400 800"
|
||||
local f_rsis="5 14 21"
|
||||
local f_ema f_change f_changed f_line f_valid_data f_ath
|
||||
local f_ema f_change f_changed f_line f_valid_data f_ath f_last_year
|
||||
local f_columns_space="${f_columns//,/ }"
|
||||
g_read_csv "${f_histfile}" "${f_last_intervals}" "$f_columns"
|
||||
for ((i=0; i<=${#g_csv_array[@]}-1; i++))
|
||||
do
|
||||
|
||||
if [ -z "${v_csv_array_associative[date_${i}]}" ]
|
||||
if [ -z "${v_csv_array_associative[date_${i}]}" ] || [ -z "${v_csv_array_associative[open_${i}]}" ]
|
||||
then
|
||||
g_echo_note "No data $f_histfile:${v_csv_array_associative[date_${i}]}"
|
||||
return 0
|
||||
@@ -105,16 +116,42 @@ function get_indicators {
|
||||
### check for unfilled fields
|
||||
f_change=""
|
||||
|
||||
# check olhc data
|
||||
if [ -z "${v_csv_array_associative[high_${i}]}" ] && [ -z "${v_csv_array_associative[low_${i}]}" ] && [ -z "${v_csv_array_associative[close_${i}]}" ]
|
||||
then
|
||||
g_echo_note "fixing OHLC Data"
|
||||
# open is close
|
||||
v_csv_array_associative[close_${i}]=${v_csv_array_associative[open_${i}]}
|
||||
# open is previous close
|
||||
[ -n "${v_csv_array_associative[close_${p}]}" ] && v_csv_array_associative[open_${i}]="${v_csv_array_associative[close_${p}]}"
|
||||
# calc high/low from open/close
|
||||
if g_num_is_higher_equal ${v_csv_array_associative[open_${i}]} ${v_csv_array_associative[close_${i}]}
|
||||
then
|
||||
v_csv_array_associative[high_${i}]=${v_csv_array_associative[open_${i}]}
|
||||
v_csv_array_associative[low_${i}]=${v_csv_array_associative[close_${i}]}
|
||||
else
|
||||
v_csv_array_associative[high_${i}]=${v_csv_array_associative[close_${i}]}
|
||||
v_csv_array_associative[low_${i}]=${v_csv_array_associative[open_${i}]}
|
||||
fi
|
||||
f_change=1
|
||||
fi
|
||||
|
||||
# check for missing percentage change
|
||||
if [ -z "${v_csv_array_associative[change_${i}]}" ]
|
||||
then
|
||||
#if ! [ $p -lt 0 ]
|
||||
#then
|
||||
#echo "g_percentage-diff ${v_csv_array_associative[close_${p}]} ${v_csv_array_associative[close_${i}]}"
|
||||
#g_percentage-diff ${v_csv_array_associative[close_${p}]} ${v_csv_array_associative[close_${i}]} && f_change=1
|
||||
g_percentage-diff ${v_csv_array_associative[open_${i}]} ${v_csv_array_associative[close_${i}]} && f_change=1
|
||||
v_csv_array_associative[change_${i}]=${g_percentage_diff_result}
|
||||
#fi
|
||||
# special for changes watched per year like CPI,...
|
||||
if [[ $f_histfile = "asset-histories/MARKETDATA_US_CONSUMER_PRICE_INDEX_CPI.history.1d.csv" ]] || \
|
||||
[[ $f_histfile = "asset-histories/MARKETDATA_US_UNEMPLOYMENT_RATE.history.1d.csv" ]]
|
||||
then
|
||||
if [ $i -ge 12 ]
|
||||
then
|
||||
f_last_year=$((i-12))
|
||||
g_percentage-diff ${v_csv_array_associative[open_${f_last_year}]} ${v_csv_array_associative[close_${i}]}
|
||||
v_csv_array_associative[year_change_${i}]=${g_percentage_diff_result}
|
||||
fi
|
||||
fi
|
||||
g_percentage-diff ${v_csv_array_associative[open_${i}]} ${v_csv_array_associative[close_${i}]} && f_change=1
|
||||
v_csv_array_associative[change_${i}]=${g_percentage_diff_result}
|
||||
fi
|
||||
|
||||
# ath (all-time-high) of present data
|
||||
@@ -135,10 +172,20 @@ function get_indicators {
|
||||
# check for missing EMAs
|
||||
for f_ema_column in $f_emas
|
||||
do
|
||||
# check for enough values/lines to calculate EMA
|
||||
[ $i -ge $f_ema_column ] || continue
|
||||
# check for enough values/lines to calculate EMA if no previous EMA given
|
||||
if [ -z "${v_csv_array_associative[ema${f_ema_column}_${p}]}" ]
|
||||
then
|
||||
if ! [ $i -ge $f_ema_column ]
|
||||
then
|
||||
#echo "not enough lines $i -ge $f_ema_column"
|
||||
continue
|
||||
fi
|
||||
fi
|
||||
# calculate EMA
|
||||
[ -z "${v_csv_array_associative[ema${f_ema_column}_${i}]}" ] && calc_ema ${f_ema_column} close && f_change=1
|
||||
if [ -z "${v_csv_array_associative[ema${f_ema_column}_${i}]}" ]
|
||||
then
|
||||
calc_ema ${f_ema_column} close && f_change=1
|
||||
fi
|
||||
done
|
||||
|
||||
# check for missing RSI
|
||||
@@ -164,6 +211,11 @@ function get_indicators {
|
||||
# build line
|
||||
for f_column in $f_columns
|
||||
do
|
||||
# special for changes watched per year like CPI,...
|
||||
if [[ $f_column = change ]]
|
||||
then
|
||||
[ -n "${v_csv_array_associative[year_change_${i}]}" ] && v_csv_array_associative[change_${i}]=${v_csv_array_associative[year_change_${i}]}
|
||||
fi
|
||||
if [ -z "$f_line" ]
|
||||
then
|
||||
f_line="${v_csv_array_associative[${f_column}_${i}]}"
|
||||
@@ -178,6 +230,8 @@ function get_indicators {
|
||||
fi
|
||||
|
||||
done
|
||||
|
||||
|
||||
# cleanup large arrays
|
||||
unset v vr v_csv_array_associative v_csv_array_associative_reverse
|
||||
}
|
||||
|
||||
|
||||
@@ -70,6 +70,7 @@ function get_levels {
|
||||
|
||||
# reset old levels var
|
||||
unset f_levels
|
||||
unset f_zones
|
||||
|
||||
local f_levelsfile=$1
|
||||
if ! [ -s "$f_levelsfile" ]
|
||||
|
||||
@@ -17,95 +17,146 @@
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with dabo. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
function get_marketdata_all {
|
||||
local f_interval=$1
|
||||
|
||||
# daily garketdata jobs
|
||||
if [[ $f_interval = 1d ]]
|
||||
then
|
||||
# FEAR_AND_GREED_ALTERNATIVEME
|
||||
get_marketdata FEAR_AND_GREED_ALTERNATIVEME 'https://api.alternative.me/fng/?limit=0&format=json' '.data[] | (.timestamp | tonumber | strftime("%Y-%m-%d")) + "," + .value + ",,,,0"' "" 1d
|
||||
|
||||
# FEAR AND GREED COINMARKETCAP
|
||||
get_marketdata FEAR_AND_GREED_COINMARKETCAP "https://api.coinmarketcap.com/data-api/v3/fear-greed/chart?start=1&end=$(date +%s)" '.data.dataList[] | (.timestamp | tonumber | strftime("%Y-%m-%d")) + "," + (.score|tostring) + ",,,,0"'
|
||||
|
||||
# FEAR_AND_GREED_CNN
|
||||
get_marketdata FEAR_AND_GREED_CNN 'https://production.dataviz.cnn.io/index/fearandgreed/graphdata' '.fear_and_greed_historical.data[] | (.x/1000 | strftime("%Y-%m-%d")) + "," + (.y|tostring) + ",,,,0"' "" 1d
|
||||
|
||||
# Altcoin-Saison-Index COINMARKETCAP Top 100 Altcoins
|
||||
get_marketdata ALTCOIN_SEASON_INDEX_COINMARKETCAP "https://api.coinmarketcap.com/data-api/v3/altcoin-season/chart?start=1&end=$(date +%s)" '.data.points[:-1][] | (.timestamp | tonumber | strftime("%Y-%m-%d")) + "," + (.altcoinIndex|tostring) + ",,,,0"' "" 1d
|
||||
|
||||
# monthly US consumer price index CPI data
|
||||
get_marketdata US_CONSUMER_PRICE_INDEX_CPI "https://api.bls.gov/publicAPI/v2/timeseries/data/CUUR0000SA0?startyear=$(date -d 'now -8 years' '+%Y')&endyear=$(date '+%Y')" '.Results.series[0].data[] | .year + "-" + (.period | gsub("M"; "")) + "-01," + .value + ",,,,0"' "" 1d
|
||||
|
||||
# monthly US unemployment rate
|
||||
get_marketdata US_UNEMPLOYMENT_RATE "https://api.bls.gov/publicAPI/v2/timeseries/data/LNU03000000?startyear=$(date -d 'now -8 years' '+%Y')&endyear=$(date '+%Y')" '.Results.series[0].data[] | .year + "-" + (.period | gsub("M"; "")) + "-01," + .value + ",,,,0"' "" 1d
|
||||
|
||||
# US FED funds rate
|
||||
get_marketdata US_FED_FUNDS_RATE 'https://fred.stlouisfed.org/graph/fredgraph.csv?id=DFF' "" "" 1d
|
||||
|
||||
fi
|
||||
|
||||
# Binance Long Short Ration Account / Taker and Open Interest per symbol
|
||||
get_symbols_ticker
|
||||
local f_symbol f_asset f_time
|
||||
for f_symbol in BTC/$CURRENCY "${f_symbols_array_trade[@]}"
|
||||
do
|
||||
f_asset=${f_symbol//:$CURRENCY/}
|
||||
f_asset=${f_asset//\//}
|
||||
|
||||
# week not available
|
||||
[[ $f_interval = 1w ]] && continue
|
||||
|
||||
f_time='%Y-%m-%d %H:%M:00'
|
||||
[[ $f_interval = 1d ]] && f_time='%Y-%m-%d'
|
||||
|
||||
# BINANCE_LONG_SHORT_RATIO_ACCOUNT per symbol
|
||||
get_marketdata BINANCE_LONG_SHORT_RATIO_ACCOUNT_$f_asset "https://fapi.binance.com/futures/data/globalLongShortAccountRatio?symbol=${f_asset}&limit=500&period=${f_interval}" ".[] | (.timestamp/1000 | strftime(\"${f_time}\")) + \",\" + .longShortRatio + \",,,,0\"" "" ${f_interval}
|
||||
|
||||
# BINANCE_LONG_SHORT_RATIO_Taker per symbol
|
||||
get_marketdata BINANCE_LONG_SHORT_RATIO_TAKER_$f_asset "https://fapi.binance.com/futures/data/takerlongshortRatio?symbol=${f_asset}&limit=500&period=${f_interval}" ".[] | (.timestamp/1000 | strftime(\"${f_time}\")) + \",\" + .buySellRatio + \",,,,0\"" "" ${f_interval}
|
||||
|
||||
# BINANCE_OPEN_INTEREST per symbol
|
||||
get_marketdata BINANCE_OPEN_INTEREST_$f_asset "https://fapi.binance.com/futures/data/openInterestHist?symbol=${f_asset}&limit=500&period=${f_interval}" ".[] | (.timestamp/1000 | strftime(\"${f_time}\")) + \",\" + .sumOpenInterest + \",,,,0\"" "" ${f_interval}
|
||||
|
||||
done
|
||||
}
|
||||
|
||||
|
||||
function get_marketdata {
|
||||
|
||||
|
||||
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
|
||||
|
||||
local f_name=$1
|
||||
local f_wget=$2
|
||||
local f_jq=$3
|
||||
local f_other=$4
|
||||
local f_timeframe=$5
|
||||
[ -z "$f_timeframe" ] && f_timeframe=1d
|
||||
local f_histfile="asset-histories/MARKETDATA_${f_name}.history.${f_timeframe}.csv"
|
||||
local f_dataline f_failed
|
||||
|
||||
### CPI Data: curl hapi.bls.gov/publicAPI/v2/timeseries/data/cpi | jq -a
|
||||
### release dates: https://www.bls.gov/schedule/news_release/bls.ics
|
||||
|
||||
get_marketdata_from_url https://www.investing.com/economic-calendar/unemployment-rate-300/ US-UNEMPLOYMENT-INDEX
|
||||
get_marketdata_from_url https://www.investing.com/economic-calendar/cpi-733 US-CONSUMER-PRICE-INDEX
|
||||
get_marketdata_from_url https://www.investing.com/indices/fed-funds-composite-interest-rate-opinion US-FED-FEDERAL-FUNDS-RATE-INVERTED-INDEX
|
||||
get_marketdata_from_url '"https://fapi.binance.com/futures/data/globalLongShortAccountRatio?symbol=BTCUSDT&period=5m" | jq -r .[].longShortRatio | tail -n1' BINANCE-BTC-GlobalLongShortAccountRatio
|
||||
get_marketdata_from_url '"https://fapi.binance.com/futures/data/takerlongshortRatio?symbol=BTCUSDT&period=5m" | jq -r .[].buySellRatio | tail -n1' BINANCE-BTC-TakerLongShortRatio
|
||||
get_marketdata_from_url '"https://fapi.binance.com/futures/data/openInterestHist?symbol=BTCUSDT&period=5m" | jq -r .[].sumOpenInterest | tail -n1' BINANCE-BTC-OpenInterest
|
||||
get_marketdata_from_url '"https://api.alternative.me/fng/?limit=1&format=json" | jq -r .data[].value' CRYPTO_FEAR_AND_GREED-INDEX
|
||||
# clear old stuff
|
||||
find asset-histories/*INDEX* -type f -mtime +6 -delete
|
||||
}
|
||||
|
||||
function get_marketdata_from_url {
|
||||
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
|
||||
|
||||
local f_url="$1"
|
||||
local f_name="$2"
|
||||
|
||||
## get data for analysis
|
||||
if find asset-histories/${f_name}.history.csv -mmin -${INTERVAL_MIN} 2>/dev/null | grep -q "asset-histories/${f_name}.history.csv"
|
||||
# download
|
||||
g_wget -O "${f_histfile}.wget.tmp" $f_wget 2>"${f_histfile}.err.tmp" || f_failed=wget
|
||||
[ -s "${f_histfile}.wget.tmp" ] || f_failed=wget
|
||||
if [ -n "$f_failed" ]
|
||||
then
|
||||
g_echo_note "asset-histories/${f_name}.history.csv already downloaded in the last ${INTERVAL_MIN} minutes"
|
||||
f_get_marketdata_price=$(cat MARKET_DATA_CMD_OUT-${f_name})
|
||||
return 0
|
||||
echo "g_wget -O \"${f_histfile}.wget.tmp\" $f_wget 2>\"${f_histfile}.err\"" >"${f_histfile}.err"
|
||||
fi
|
||||
|
||||
# check source platform for parsing parameters, prepare and run wget command
|
||||
>MARKET_DATA_CMD
|
||||
if echo "${f_url}" | grep -q "boerse.de"
|
||||
# jd
|
||||
if [ -z "$f_failed" ] && [ -n "$f_jq" ]
|
||||
then
|
||||
echo "wget ${g_wget_opts} -q -O - ${f_url} | egrep 'itemprop=\"price\" content=\"[0-9]+\.[0-9]+\"' | sed s'#\"#\n#g' | egrep '^[0-9]+\.[0-9]+'" >MARKET_DATA_CMD
|
||||
elif echo "${f_url}" | egrep -q "investing.com.+economic-calendar"
|
||||
then
|
||||
echo "wget ${g_wget_opts} -q -O - ${f_url} | egrep 'Actual.+Forecast.+Previous' | cut -d'>' -f7,11 | cut -d'<' -f1,2 | sed 's#,##g' | sed 's/\%//g' | sed 's#</div>#,#' | grep '[0-9]' | cut -d, -f1" >MARKET_DATA_CMD
|
||||
elif echo "${f_url}" | egrep -q "investing.com.+indices"
|
||||
then
|
||||
echo "wget ${g_wget_opts} -q -O - ${f_url} | sed 's#</div>#\n#g' | grep 'text-5xl.*font-bold.*md:text-' | sed 's#^.*>##; s#,##g' | grep '[0-9]'" >MARKET_DATA_CMD
|
||||
elif echo "${f_url}" | egrep -q '^"https://'
|
||||
then
|
||||
echo "wget -q -O - ${g_wget_opts} ${f_url}" >MARKET_DATA_CMD
|
||||
else
|
||||
# default to Yahoo Finace Symbols via API
|
||||
echo "wget ${g_wget_opts} -q -O - https://query1.finance.yahoo.com/v8/finance/chart/${f_url} | jq -r '.[].result[].meta.regularMarketPrice'" >MARKET_DATA_CMD
|
||||
fi
|
||||
|
||||
g_runcmd g_retrycmd sh MARKET_DATA_CMD >MARKET_DATA_CMD_OUT-${f_name}.tmp 2>MARKET_DATA_CMD_OUT-${f_name}.tmp.err
|
||||
|
||||
# check output
|
||||
local f_get_marketdata_price_tmp=$(cat MARKET_DATA_CMD_OUT-${f_name}.tmp)
|
||||
if g_num_valid_number ${f_get_marketdata_price_tmp}
|
||||
then
|
||||
if egrep -q "^0\.00" MARKET_DATA_CMD_OUT-${f_name}.tmp
|
||||
if ! jq -r "$f_jq" "${f_histfile}.wget.tmp" >"${f_histfile}.tmp" 2>"${f_histfile}.err.tmp"
|
||||
then
|
||||
g_echo_note "Ignoring ${f_name} $(tail -n 10 MARKET_DATA_CMD_OUT-${f_name}.tmp) - maybe out of business day"
|
||||
echo jq -r "$f_jq" "${f_histfile}.wget.tmp" >"${f_histfile}.err"
|
||||
f_failed=jq
|
||||
else
|
||||
g_echo_note "${f_name}: ${f_get_marketdata_price_tmp}"
|
||||
mv MARKET_DATA_CMD_OUT-${f_name}.tmp MARKET_DATA_CMD_OUT-${f_name}
|
||||
mv "${f_histfile}.tmp" "${f_histfile}.wget.tmp"
|
||||
fi
|
||||
fi
|
||||
|
||||
# other/additional processing
|
||||
if [ -z "$f_failed" ] && [ -n "$f_other" ]
|
||||
then
|
||||
if ! cat "${f_histfile}.wget.tmp" | eval $f_other
|
||||
then
|
||||
echo "cat \"${f_histfile}.wget.tmp\" | $f_other" >"${f_histfile}.err"
|
||||
f_failed=other
|
||||
fi
|
||||
else
|
||||
g_echo_warn "MARKET_DATA_CMD_OUT-${f_name}.tmp has wrong Syntax. - Not updating ${f_name} Index.
|
||||
|
||||
CMD:
|
||||
$(tail -n 10 MARKET_DATA_CMD)
|
||||
|
||||
Output:
|
||||
$(tail -n 10 MARKET_DATA_CMD_OUT-${f_name}.tmp | cat -t)
|
||||
|
||||
Error:
|
||||
$(tail -n 10 MARKET_DATA_CMD_OUT-${f_name}.tmp.err | cat -t)"
|
||||
mv "${f_histfile}.wget.tmp" "${f_histfile}.tmp"
|
||||
fi
|
||||
if ! [ -e "MARKET_DATA_CMD_OUT-${f_name}" ]
|
||||
|
||||
|
||||
# cleanup
|
||||
rm -f "${f_histfile}.wget.tmp" "${f_histfile}.err.tmp"
|
||||
|
||||
# error if no csvfile available
|
||||
if [ -n "$f_failed" ] || ! [ -s "${f_histfile}.tmp" ]
|
||||
then
|
||||
local f_old_value=$(tail -n 1 asset-histories/${f_name}.history.csv | cut -d, -f2)
|
||||
if echo ${f_old_value} | egrep -q "^[0-9]*\.[0-9]+"
|
||||
cat "${f_histfile}.err.tmp" >>"${f_histfile}.err"
|
||||
cat "${f_histfile}.wget.tmp" >>"${f_histfile}.err"
|
||||
cat "${f_histfile}.err" 1>&2
|
||||
mkdir -p FAILED_MARKETDATA
|
||||
mv "${f_histfile}.err" "FAILED_MARKETDATA/MARKETDATA-${f_name}" 2>/dev/null
|
||||
return 1
|
||||
fi
|
||||
|
||||
# on first download
|
||||
if ! [ -s "${f_histfile}" ]
|
||||
then
|
||||
grep ^[2-9] "${f_histfile}.tmp" | sort -k1,1 -t, -u >"${f_histfile}"
|
||||
else
|
||||
# merge data
|
||||
egrep -h ^[0-9][0-9][0-9][0-9]-[0-9][0-9] "${f_histfile}" "${f_histfile}.tmp" | sort -k1,1 -t, -u >"${g_tmp}/${FUNCNAME}.tmp"
|
||||
|
||||
# if there is new dataline add it
|
||||
if ! cmp -s "${g_tmp}/${FUNCNAME}.tmp" "${f_histfile}"
|
||||
then
|
||||
echo ${f_old_value} >MARKET_DATA_CMD_OUT-${f_name}
|
||||
else
|
||||
echo 0 >MARKET_DATA_CMD_OUT-${f_name}
|
||||
cat "${g_tmp}/${FUNCNAME}.tmp" >"${f_histfile}"
|
||||
fi
|
||||
fi
|
||||
f_get_marketdata_price=$(cat MARKET_DATA_CMD_OUT-${f_name})
|
||||
echo "${f_timestamp},${f_get_marketdata_price}" >>asset-histories/${f_name}.history.csv
|
||||
rm "${f_histfile}.tmp"
|
||||
|
||||
# calc indicators and if 1d then generate 1w histfile
|
||||
if [[ $f_interval = 1d ]]
|
||||
then
|
||||
get_indicators "${f_histfile}" 51
|
||||
convert_ohlcv_1d_to_1w "${f_histfile}" "${f_histfile/.1d./.1w.}"
|
||||
get_indicators "${f_histfile/.1d./.1w.}" 51
|
||||
else
|
||||
get_indicators "${f_histfile}" 51
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
167
dabo/functions/get_marketdata_coinmarketcap.sh
Normal file
167
dabo/functions/get_marketdata_coinmarketcap.sh
Normal file
@@ -0,0 +1,167 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Copyright (c) 2022-2024 olli
|
||||
#
|
||||
# This file is part of dabo (crypto bot).
|
||||
#
|
||||
# dabo is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# dabo is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with dabo. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
function get_marketdata_coinmarketcap {
|
||||
|
||||
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
|
||||
local f_item="$1"
|
||||
local f_name="$2"
|
||||
local f_timeframe="$3"
|
||||
|
||||
local f_targetcsvtmp="${g_tmp}/${f_name}.history-coinmarketcap.csv"
|
||||
local f_targetjsontmp="${g_tmp}/${f_name}.history-coinmarketcap.json"
|
||||
rm -f "$f_targetcsvtmp" "$f_targetjsontmp"
|
||||
|
||||
[ -z "$f_timeframe" ] && f_timeframe="1d"
|
||||
local f_targetcsv="asset-histories/${f_name}.history-coinmarketcap.${f_timeframe}.csv"
|
||||
[ "$f_timeframe" = "1w" ] && f_timeframe="7d"
|
||||
f_histfile_coinmarketcap="$f_targetcsv"
|
||||
|
||||
# use EUR EURC stable coin fo EUR
|
||||
f_item=${f_item//EUR-/EURC-}
|
||||
|
||||
# renamed cryptos
|
||||
f_item=${f_item//RNDR-/RENDER-}
|
||||
|
||||
# remove -
|
||||
f_item=${f_item//-//}
|
||||
|
||||
# USDT to USD
|
||||
f_item=${f_item//USDT/USD}
|
||||
# BUSD to USD
|
||||
f_item=${f_item//BUSD/USD}
|
||||
|
||||
|
||||
if ! [[ $f_item =~ /USD ]]
|
||||
then
|
||||
g_echo_error "${FUNCNAME} $@: Only USD supported"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# transform CCXT symbols to CoinmarketCap IDs
|
||||
if [[ $f_item =~ / ]]
|
||||
then
|
||||
# remove /*
|
||||
f_item=${f_item///*/}
|
||||
fi
|
||||
|
||||
local f_id
|
||||
# get id -> If multiple take the one with the largest marketcap
|
||||
f_id=$(egrep "^${f_item},[1-9]" COINMARKETCAPIDS COINMARKETCAPIDS.tmp 2>/dev/null | sort -n -t, -k4 | tail -n1 | cut -d, -f2)
|
||||
[[ $f_item = EURC ]] && f_id=20641
|
||||
if [ -z "$f_id" ]
|
||||
then
|
||||
g_echo_error "${FUNCNAME} $@: No CoinMarketCap ID for $f_item"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# end if already failed the last 5 minutes
|
||||
if [ -f "FAILED_COINMARKETCAP/${f_name}_HISTORIC_DOWNLOAD" ]
|
||||
then
|
||||
find "FAILED_COINMARKETCAP/${f_name}_HISTORIC_DOWNLOAD" -mmin +5 -delete
|
||||
if [ -f "FAILED_COINMARKETCAP/${f_name}_HISTORIC_DOWNLOAD" ]
|
||||
then
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# end if already exists and modified under given time
|
||||
if [ -s "${f_targetcsv}" ] && find "${f_targetcsv}" -mmin -2 | grep -q "${f_targetcsv}"
|
||||
then
|
||||
return 0
|
||||
fi
|
||||
|
||||
# cleanup
|
||||
rm -f "$f_targetcsvtmp" "${f_targetcsvtmp}".err ${f_targetjsontmp} "${f_targetjsontmp}".err
|
||||
|
||||
if [ "$f_timeframe" = "1d" ] || [ "$f_timeframe" = "7d" ]
|
||||
then
|
||||
# Download data from coinmarketcap
|
||||
g_wget -O "${f_targetjsontmp}" "https://api.coinmarketcap.com/data-api/v3.1/cryptocurrency/historical?id=${f_id}&interval=${f_timeframe}" 2>"${f_targetjsontmp}".err
|
||||
jq -r '.data.quotes[] | .quote.timestamp[0:10] + "," + (.quote.open|tostring) + "," + (.quote.high|tostring) + "," + (.quote.low|tostring) + "," + (.quote.close|tostring) + "," + (.quote.volume|tostring)' "${f_targetjsontmp}" | egrep -v ',0$|,$' >"${f_targetcsvtmp}" 2>"${f_targetjsontmp}".err
|
||||
else
|
||||
g_echo_error "${FUNCNAME} $@: Timeframe $f_timeframe in CoinMarketCap not supported."
|
||||
return 1
|
||||
fi
|
||||
|
||||
# error if no csvfile available
|
||||
if ! [ -s "${f_targetcsvtmp}" ]
|
||||
then
|
||||
mkdir -p FAILED_COINMARKETCAP
|
||||
cat "${f_targetcsvtmp}.err" "${f_targetjsontmp}.err" > "FAILED_COINMARKETCAP/${f_name}_HISTORIC_DOWNLOAD" 2>/dev/null
|
||||
f_get_marketdata_coinmarketcap_error=$(cat "${f_targetcsvtmp}.err" "${f_targetjsontmp}.err" 2>/dev/null)
|
||||
return 1
|
||||
fi
|
||||
|
||||
# put the csvs together
|
||||
if [ -s "${f_targetcsv}" ] && [ -s "${f_targetcsvtmp}" ]
|
||||
then
|
||||
egrep -h "^[1-9][0-9][0-9][0-9]-[0-1][0-9]-[0-9][0-9].*,[0-9]" "${f_targetcsv}" "${f_targetcsvtmp}" | sort -k1,2 -t, -u | sort -k1,1 -t, -u >"${f_targetcsv}.tmp"
|
||||
mv "${f_targetcsv}.tmp" "${f_targetcsv}"
|
||||
else
|
||||
egrep -h "^[1-9][0-9][0-9][0-9]-[0-1][0-9]-[0-9][0-9].*,[0-9]" "${f_targetcsvtmp}" | sort -k1,2 -t, -u >"$f_targetcsv"
|
||||
fi
|
||||
|
||||
# change exponential notation to normal notation
|
||||
g_num_exponential2normal_file "${f_targetcsv}"
|
||||
|
||||
}
|
||||
|
||||
function get_marketdata_coinmarketcap_ids {
|
||||
|
||||
# get symbol ids from coinmarketcap
|
||||
|
||||
local f_target=COINMARKETCAPIDS
|
||||
local f_target_tmp="${f_target}.tmp"
|
||||
local f_target_loop=$f_target_tmp
|
||||
local f_latest_date
|
||||
local f_latest_date_seconds
|
||||
local f_latest_date_seconds_now=$(date -d "now - 8 days" +%s)
|
||||
|
||||
# write direct to target if not exists or empty
|
||||
[ -s "$f_target" ] || f_target_loop=$f_target
|
||||
|
||||
for f_id in $(seq 1 50000)
|
||||
do
|
||||
#echo "checking COINMARKETCAPID $f_id - Writing to $f_target_loop" 1>&2
|
||||
|
||||
sleep 0.3
|
||||
# download
|
||||
curl -s --request GET --url "https://api.coinmarketcap.com/data-api/v3.1/cryptocurrency/historical?id=${f_id}&interval=1d" >"$g_tmp/get_marketdata_coinmarketcap_ids.json"
|
||||
|
||||
# check latest date
|
||||
f_latest_date=$(jq -r '.data.quotes[] | .quote.timestamp[0:10]' "$g_tmp/get_marketdata_coinmarketcap_ids.json" | tail -n1)
|
||||
[ -z "$f_latest_date" ] && continue
|
||||
|
||||
# check for up-to-date data
|
||||
f_latest_date_seconds=$(date -d "$f_latest_date" +%s)
|
||||
if [ $f_latest_date_seconds_now -lt $f_latest_date_seconds ]
|
||||
then
|
||||
jq -r '.data | .symbol + "," + (.id|tostring) + "," + .name + "," + (.quotes[].quote|.marketCap|tostring)' "$g_tmp/get_marketdata_coinmarketcap_ids.json" | grep -vi ",0e-" | head -n 1
|
||||
fi
|
||||
done | egrep --line-buffered '^.+,[0-9]*,' >"$f_target_loop"
|
||||
|
||||
if [ -s "$f_target_tmp" ]
|
||||
then
|
||||
cp -p "$f_target" "${f_target}.$(date +%F)"
|
||||
mv "$f_target_tmp" "$f_target"
|
||||
fi
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@ function get_marketdata_yahoo {
|
||||
|
||||
[ -z "$f_timeframe" ] && f_timeframe="1d"
|
||||
local f_targetcsv="asset-histories/${f_name}.history-yahoo.${f_timeframe}.csv"
|
||||
local f_targetbotcsv="asset-histories/${f_name}.history.${f_timeframe}.csv"
|
||||
[ "$f_timeframe" = "1w" ] && f_timeframe="1wk"
|
||||
f_histfile_yahoo="$f_targetcsv"
|
||||
|
||||
@@ -65,27 +66,6 @@ function get_marketdata_yahoo {
|
||||
[[ $f_item = "USD-EUR" ]] && f_item="USDEUR=X"
|
||||
[[ $f_item = "EUR-USD" ]] && f_item="EURUSD=X"
|
||||
|
||||
# special names of some coins/currencies of yahoo finance
|
||||
[[ $f_item = "ARB-USD" ]] && f_item="ARB11841-USD"
|
||||
[[ $f_item = "DUEL-USD" ]] && f_item="DUEL28868-USD"
|
||||
[[ $f_item = "GMX-USD" ]] && f_item="GMX11857-USD"
|
||||
[[ $f_item = "MEW-USD" ]] && f_item="MEW30126-USD"
|
||||
[[ $f_item = "TAO-USD" ]] && f_item="TAO22974-USD"
|
||||
[[ $f_item = "UNI-USD" ]] && f_item="UNI7083-USD"
|
||||
[[ $f_item = "SUI-USD" ]] && f_item="SUI20947-USD"
|
||||
[[ $f_item = "BLAZE-USD" ]] && f_item="BLAZE30179-USD"
|
||||
[[ $f_item = "BEER-USD" ]] && f_item="BEER31337-USD"
|
||||
[[ $f_item = "TAI-USD" ]] && f_item="TAI20605-USD"
|
||||
[[ $f_item = "DEFI-USD" ]] && f_item="DEFI29200-USD"
|
||||
[[ $f_item = "TON-USD" ]] && f_item="TON11419-USD"
|
||||
[[ $f_item = "BRETT-USD" ]] && f_item="BRETT29743-USD"
|
||||
#[[ $f_item = "ADS-USD" ]] && f_item="%24ADS-USD"
|
||||
[[ $f_item = "AIT-USD" ]] && f_item="AIT28882-USD"
|
||||
[[ $f_item = "PT-USD" ]] && f_item="PT28582-USD"
|
||||
[[ $f_item = "BLAST-USD" ]] && f_item="BLAST28480-USD"
|
||||
[[ $f_item = "GRT-USD" ]] && f_item="GRT6719-USD"
|
||||
[[ $f_item = "ARTY-USD" ]] && f_item="ARTY23751-USD"
|
||||
|
||||
# end if already failed the last 5 minutes
|
||||
if [ -f "FAILED_YAHOO/${f_name}_HISTORIC_DOWNLOAD" ]
|
||||
then
|
||||
@@ -107,46 +87,60 @@ function get_marketdata_yahoo {
|
||||
# cleanup
|
||||
rm -f "$f_targetcsvtmp" "${f_targetcsvtmp}".err ${f_targetjsontmp} "${f_targetjsontmp}".err
|
||||
|
||||
if [ "$f_timeframe" = "1d" ] || [ "$f_timeframe" = "1wk" ] || [ "$f_timeframe" = "1mo" ]
|
||||
then
|
||||
# restrict to finished candles
|
||||
[ "$f_timeframe" = "1d" ] && f_sec=$(TZ=US/NY date -d 'last day 0:00' '+%s')
|
||||
[ "$f_timeframe" = "1wk" ] && f_sec=$(TZ=US/NY date -d 'last monday 0:00' '+%s')
|
||||
[ "$f_timeframe" = "1mo" ] && f_sec=$(TZ=US/NY date -d "$(date -d "@$(date -d "last month" +%s)" +'%Y-%m-01')" +%s)
|
||||
# Download historical data from yahoo
|
||||
g_wget -O "${f_targetcsvtmp}" "https://query1.finance.yahoo.com/v7/finance/download/${f_item}?period1=0&period2=${f_sec}&interval=${f_timeframe}&events=history" 2>"${f_targetcsvtmp}".err
|
||||
else
|
||||
# Download data from yahoo
|
||||
g_wget -O "${f_targetjsontmp}" "https://query1.finance.yahoo.com/v7/finance/chart/${f_item}?interval=${f_timeframe}&period2=${f_sec}" 2>"${f_targetjsontmp}".err
|
||||
jq -r '.chart.result[0] as $result | range(0; $result.timestamp | length) | [$result.timestamp[.], $result.indicators.quote[0].open[.], $result.indicators.quote[0].high[.], $result.indicators.quote[0].low[.], $result.indicators.quote[0].close[.], $result.indicators.quote[0].volume[.]] | @csv' "${f_targetjsontmp}" >"${f_targetcsvtmp}.unixtime" 2>"${f_targetjsontmp}".err
|
||||
local f_from
|
||||
[ "$f_timeframe" = "5m" ] && f_from=$(date -d "now -86000 minutes" +%s)
|
||||
[ "$f_timeframe" = "15m" ] && f_from=$(date -d "now -86000 minutes" +%s)
|
||||
[ "$f_timeframe" = "1h" ] && f_from=$(date -d "now -17510 hour" +%s)
|
||||
[ "$f_timeframe" = "1d" ] && f_from=1
|
||||
[ "$f_timeframe" = "1wk" ] && f_from=1
|
||||
[ "$f_timeframe" = "1mo" ] && f_from=1
|
||||
|
||||
# change unix time to human readable and fill unfilled lines, ignore lines not with 00 secolds (last line)
|
||||
local date_time open high low close lastopen lasthigh lastlow lastclose volume
|
||||
while IFS=, read -r timestamp open high low close volume; do
|
||||
date_time=$(printf "%(%Y-%m-%d %H:%M:%S)T" $timestamp)
|
||||
[ -z "$open" ] && open=$lastopen
|
||||
[ -z "$high" ] && high=$lasthigh
|
||||
[ -z "$low" ] && low=$lastlow
|
||||
[ -z "$close" ] && close=$lastclose
|
||||
[ -z "$volume" ] && volume=0
|
||||
lastopen=$open
|
||||
lasthigh=$high
|
||||
lastlow=$low
|
||||
lastclose=$close
|
||||
echo "$date_time,$open,$high,$low,$close,$volume"
|
||||
done < "${f_targetcsvtmp}.unixtime" | grep ":00," >${f_targetcsvtmp}
|
||||
fi
|
||||
# Download data from yahoo
|
||||
g_wget -O "${f_targetjsontmp}" "https://query1.finance.yahoo.com/v8/finance/chart/${f_item}?interval=${f_timeframe}&period1=${f_from}&period2=${f_sec}" 2>"${f_targetjsontmp}".err
|
||||
|
||||
# Create csv from json
|
||||
jq -r '.chart.result[0] as $result | range(0; $result.timestamp | length) | [$result.timestamp[.], $result.indicators.quote[0].open[.], $result.indicators.quote[0].high[.], $result.indicators.quote[0].low[.], $result.indicators.quote[0].close[.], $result.indicators.quote[0].volume[.]] | @csv' "${f_targetjsontmp}" >"${f_targetcsvtmp}.unixtime" 2>"${f_targetjsontmp}".err
|
||||
|
||||
# remove last/open timeframe (use only closed)
|
||||
sed -i '$d' "${f_targetcsvtmp}.unixtime"
|
||||
|
||||
# change unix time to human readable and fill unfilled lines, ignore lines not with 00 secolds (last line)
|
||||
local date_time open high low close lastopen lasthigh lastlow lastclose volume
|
||||
while IFS=, read -r timestamp open high low close volume
|
||||
do
|
||||
if [ "$f_timeframe" = "1d" ] || [ "$f_timeframe" = "1mo" ]
|
||||
then
|
||||
printf -v date_time "%(%Y-%m-%d)T" $timestamp
|
||||
elif [ "$f_timeframe" = "1wk" ]
|
||||
then
|
||||
# on week 1 day back like crypto assets
|
||||
date_time=$(date -d "yesterday $(date -d "@$timestamp" "+%Y-%m-%d")" "+%Y-%m-%d")
|
||||
else
|
||||
printf -v date_time "%(%Y-%m-%d %H:%M:%S)T" $timestamp
|
||||
fi
|
||||
[ -z "$open" ] && open=$lastopen
|
||||
[ -z "$high" ] && high=$lasthigh
|
||||
[ -z "$low" ] && low=$lastlow
|
||||
[ -z "$close" ] && close=$lastclose
|
||||
[ -z "$volume" ] && volume=0
|
||||
lastopen=$open
|
||||
lasthigh=$high
|
||||
lastlow=$low
|
||||
lastclose=$close
|
||||
echo "$date_time,$open,$high,$low,$close,$volume"
|
||||
done < "${f_targetcsvtmp}.unixtime" >${f_targetcsvtmp}
|
||||
|
||||
# error if no csvfile available
|
||||
if ! [ -s "${f_targetcsvtmp}" ]
|
||||
then
|
||||
mkdir -p FAILED_YAHOO
|
||||
cat "${f_targetcsvtmp}.err" "${f_targetjsontmp}.err" > "FAILED_YAHOO/${f_name}_HISTORIC_DOWNLOAD" 2>&1
|
||||
cat "${f_targetcsvtmp}.err" "${f_targetjsontmp}.err" > "FAILED_YAHOO/${f_name}_HISTORIC_DOWNLOAD" 2>/dev/null
|
||||
f_get_marketdata_yahoo_error=$(cat "${f_targetcsvtmp}.err" "${f_targetjsontmp}.err" 2>/dev/null)
|
||||
return 1
|
||||
fi
|
||||
|
||||
# put the csvs together
|
||||
# history-yahoo file
|
||||
if [ -s "${f_targetcsv}" ] && [ -s "${f_targetcsvtmp}" ]
|
||||
then
|
||||
egrep -h "^[1-9][0-9][0-9][0-9]-[0-1][0-9]-[0-9][0-9].*,[0-9]" "${f_targetcsv}" "${f_targetcsvtmp}" | sort -k1,2 -t, -u | sort -k1,1 -t, -u >"${f_targetcsv}.tmp"
|
||||
@@ -155,5 +149,14 @@ function get_marketdata_yahoo {
|
||||
egrep -h "^[1-9][0-9][0-9][0-9]-[0-1][0-9]-[0-9][0-9].*,[0-9]" "${f_targetcsvtmp}" | sort -k1,2 -t, -u >"$f_targetcsv"
|
||||
fi
|
||||
|
||||
}
|
||||
# bots history file
|
||||
if [ -s "${f_targetbotcsv}" ] && [ -s "${f_targetcsv}" ]
|
||||
then
|
||||
egrep -h "^[1-9][0-9][0-9][0-9]-[0-1][0-9]-[0-9][0-9].*,[0-9]" "${f_targetbotcsv}" "${f_targetcsv}" | sort -k1,2 -t, -u | sort -k1,1 -t, -u >"${f_targetbotcsv}.tmp"
|
||||
mv "${f_targetbotcsv}.tmp" "${f_targetbotcsv}"
|
||||
else
|
||||
egrep -h "^[1-9][0-9][0-9][0-9]-[0-1][0-9]-[0-9][0-9].*,[0-9]" "${f_targetcsv}" | sort -k1,2 -t, -u >"$f_targetbotcsv"
|
||||
fi
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ function get_ohlcv-candles {
|
||||
|
||||
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
|
||||
|
||||
local f_histfile f_symbol f_timeframe f_1h_histfile
|
||||
local f_histfile f_symbol f_timeframe f_1h_histfile f_1d_histfile
|
||||
local f_timeframes="1w 1d 4h 1h 15m 5m"
|
||||
[ -n $1 ] && f_timeframes=$1
|
||||
|
||||
@@ -35,16 +35,18 @@ function get_ohlcv-candles {
|
||||
g_echo_note "Fetching/Refreshing $f_eco_asset $f_timeframe"
|
||||
f_histfile="asset-histories/ECONOMY-${f_eco_asset}.history.${f_timeframe}.csv"
|
||||
|
||||
# 4h timefrage does not exist on yahoo finance so calc from 1h
|
||||
# 4h timeframe does not exist on yahoo finance so calc from 1h
|
||||
if [ "$f_timeframe" = "4h" ]
|
||||
then
|
||||
f_1h_histfile="asset-histories/ECONOMY-${f_eco_asset}.history.1h.csv"
|
||||
[ -s "$f_1h_histfile" ] && convert_ohlcv_1h_to_4h "$f_1h_histfile" "$f_histfile"
|
||||
f_add_missing_ohlcv_intervals "$f_histfile" 4h
|
||||
else
|
||||
get_ohlcv-candle "${f_eco_asset}" ${f_timeframe} "${f_histfile}" "ECONOMY-${f_eco_asset}"
|
||||
#get_ohlcv-candle "${f_eco_asset}" ${f_timeframe} "${f_histfile}" "ECONOMY-${f_eco_asset}"
|
||||
get_marketdata_yahoo ${f_eco_asset} ECONOMY-${f_eco_asset} ${f_timeframe}
|
||||
fi
|
||||
# refresh latest indicators
|
||||
[ -s "${f_histfile}" ] && get_indicators "${f_histfile}" 900
|
||||
[ -s "${f_histfile}" ] && get_indicators "${f_histfile}" 51
|
||||
done
|
||||
done
|
||||
|
||||
@@ -73,7 +75,7 @@ function get_ohlcv-candles {
|
||||
get_ohlcv-candle "$f_symbol" $f_timeframe "${f_histfile}" && printf '%(%Y-%m-%d %H:%M:%S)T' >>"$f_histfile.fetched"
|
||||
|
||||
# refresh latest indicators
|
||||
get_indicators "${f_histfile}" 900
|
||||
get_indicators "${f_histfile}" 51
|
||||
|
||||
rm -f "${f_histfile}.fetching"
|
||||
|
||||
@@ -86,28 +88,42 @@ function get_ohlcv-candle {
|
||||
|
||||
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
|
||||
|
||||
local f_yahoo f_date f_unit_date f_data f_data_array f_data_unit f_open f_high f_low f_close f_volume f_last_unit_date f_last_unit_close
|
||||
local f_extdata f_date f_unit_date f_data f_data_array f_data_unit f_open f_high f_low f_close f_volume f_last_unit_date f_last_unit_close
|
||||
local f_symbol="$1"
|
||||
local f_timeframe=$2
|
||||
local f_histfile="$3"
|
||||
local f_asset=$4
|
||||
unset f_histfile_yahoo
|
||||
#[ -n "$f_asset" ] && f_yahoo=1a
|
||||
unset f_histfile_yahoo f_histfile_coinmarketcap
|
||||
#[ -n "$f_asset" ] && f_extdata=1
|
||||
#local f_histfile_week="$4"
|
||||
|
||||
# fetch >=1d from yahoo finance
|
||||
# fetch >=1d from coinmarketcap
|
||||
if [ "$f_timeframe" = "1d" ] || [ "$f_timeframe" = "1w" ] || [ "$f_timeframe" = "1mo" ] || [ -n "$f_asset" ]
|
||||
then
|
||||
f_yahoo=1
|
||||
f_extdata=1
|
||||
if [ -z "$f_asset" ]
|
||||
then
|
||||
f_asset=${f_symbol///}
|
||||
f_asset=${f_asset//:*}
|
||||
fi
|
||||
if ! get_marketdata_yahoo "$f_symbol" "$f_asset" $f_timeframe
|
||||
|
||||
if [[ $f_asset =~ ^ECONOMY- ]]
|
||||
then
|
||||
g_echo_error "$f_get_marketdata_yahoo_error"
|
||||
return 1
|
||||
# economy from yahoo finance
|
||||
if ! get_marketdata_yahoo "$f_symbol" "$f_asset" $f_timeframe
|
||||
then
|
||||
g_echo_error "$f_get_marketdata_coinmarketcap_error"
|
||||
return 1
|
||||
fi
|
||||
f_histfile_extdata=$f_histfile_yahoo
|
||||
else
|
||||
# crypto from coinmarketcap
|
||||
if ! get_marketdata_coinmarketcap "$f_symbol" "$f_asset" $f_timeframe
|
||||
then
|
||||
g_echo_error "$f_get_marketdata_coinmarketcap_error"
|
||||
return 1
|
||||
fi
|
||||
f_histfile_extdata=$f_histfile_coinmarketcap
|
||||
fi
|
||||
fi
|
||||
|
||||
@@ -115,7 +131,7 @@ function get_ohlcv-candle {
|
||||
while true
|
||||
do
|
||||
# fetch data
|
||||
if [ -z "$f_yahoo" ]
|
||||
if [ -z "$f_extdata" ]
|
||||
then
|
||||
# find latest time which is not fetched already create f_since
|
||||
get_ohlcv-candle-latest "$f_symbol" "$f_histfile"
|
||||
@@ -131,28 +147,30 @@ function get_ohlcv-candle {
|
||||
f_data=${f_data//],/+}
|
||||
g_array $f_data f_data_ref +
|
||||
else
|
||||
# from yahoo finance
|
||||
g_array "$f_histfile_yahoo" f_data_ref
|
||||
# from coinmarketcap/yahoo
|
||||
|
||||
g_array "$f_histfile_extdata" f_data_ref
|
||||
fi
|
||||
|
||||
f_data_array=("${f_data_ref[@]}")
|
||||
|
||||
# check if last data already in history file and end if already present
|
||||
g_array "${f_data_array[-1]}" f_last_data_unit_ref ,
|
||||
[ -z "$f_yahoo" ] && printf -v f_last_unit_date '%(%Y-%m-%d %H:%M:%S)T' ${f_last_data_unit_ref[0]::-3}
|
||||
[ -n "$f_yahoo" ] && f_last_unit_date="${f_last_data_unit_ref[0]}"
|
||||
|
||||
#echo "grep -q ^\"$f_last_unit_date\" \"$f_histfile\""
|
||||
[ -z "$f_extdata" ] && printf -v f_last_unit_date '%(%Y-%m-%d %H:%M:%S)T' ${f_last_data_unit_ref[0]::-3}
|
||||
[ -n "$f_extdata" ] && f_last_unit_date="${f_last_data_unit_ref[0]}"
|
||||
# exit if we have already in the newest date
|
||||
[ -s "$f_histfile" ] && grep -q ^"${f_last_unit_date}," "$f_histfile" && break
|
||||
|
||||
|
||||
|
||||
# go through data and write to history file if new units available
|
||||
for f_data_unit in "${f_data_array[@]}"
|
||||
do
|
||||
|
||||
# use array for each unit and assigned values to vars
|
||||
g_array "$f_data_unit" f_data_unit_ref ,
|
||||
[ -z "$f_yahoo" ] && printf -v f_unit_date '%(%Y-%m-%d %H:%M:%S)T' ${f_data_unit_ref[0]::-3}
|
||||
[ -n "$f_yahoo" ] && f_unit_date="${f_last_data_unit_ref[0]}"
|
||||
[ -z "$f_extdata" ] && printf -v f_unit_date '%(%Y-%m-%d %H:%M:%S)T' ${f_data_unit_ref[0]::-3}
|
||||
[ -n "$f_extdata" ] && f_unit_date="${f_last_data_unit_ref[0]}"
|
||||
|
||||
# check if date is already in history file
|
||||
[ -s "$f_histfile" ] && grep -q ^"$f_unit_date" "$f_histfile" && continue
|
||||
@@ -163,7 +181,7 @@ function get_ohlcv-candle {
|
||||
then
|
||||
f_open=${f_data_unit_ref[1]}
|
||||
fi
|
||||
g_num_exponential2normal "$f_open" && f_open=$g_num_exponential2normal_resul
|
||||
g_num_exponential2normal "$f_open" && f_open=$g_num_exponential2normal_result
|
||||
f_high=${f_data_unit_ref[2]}
|
||||
g_num_exponential2normal "$f_high" && f_high=$g_num_exponential2normal_result
|
||||
f_low=${f_data_unit_ref[3]}
|
||||
@@ -172,7 +190,7 @@ function get_ohlcv-candle {
|
||||
g_num_exponential2normal "$f_close" && f_close=$g_num_exponential2normal_result
|
||||
f_last_unit_close=$f_close
|
||||
f_volume=${f_data_unit_ref[5]}
|
||||
# yahoo historic volume col 6
|
||||
# coinmarketcap historic volume col 6
|
||||
[ -n "${f_data_unit_ref[6]}" ] && f_volume=${f_data_unit_ref[6]}
|
||||
g_num_exponential2normal "$f_volume" && f_volume=$g_num_exponential2normal_result
|
||||
|
||||
@@ -197,8 +215,8 @@ function get_ohlcv-candle {
|
||||
|
||||
done
|
||||
|
||||
# end if yahoo (complete file and not time chunks)
|
||||
[ -n "$f_yahoo" ] && break
|
||||
# end if coinmarketcap (complete file and not time chunks)
|
||||
[ -n "$f_extdata" ] && break
|
||||
|
||||
# end if lates refresh is this day
|
||||
printf -v f_date '%(%Y-%m-%d)T\n'
|
||||
@@ -245,6 +263,9 @@ function get_ohlcv-candle-latest {
|
||||
|
||||
|
||||
function convert_ohlcv_1h_to_4h {
|
||||
|
||||
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
|
||||
|
||||
local f_input_file="$1"
|
||||
local f_output_file="$2"
|
||||
|
||||
@@ -267,7 +288,7 @@ function convert_ohlcv_1h_to_4h {
|
||||
fi
|
||||
|
||||
# Read the input file line by line
|
||||
while IFS=',' read -r f_date f_1h_open f_1h_high f_1h_low f_1h_close f_1h_volume f_rest
|
||||
grep -h "$f_latest_date" -A99999 "$f_input_file" | while IFS=',' read -r f_date f_1h_open f_1h_high f_1h_low f_1h_close f_1h_volume f_rest
|
||||
do
|
||||
|
||||
# check for already converted lines
|
||||
@@ -324,10 +345,206 @@ function convert_ohlcv_1h_to_4h {
|
||||
g_calc "$f_volume + $f_1h_volume"
|
||||
f_volume=$g_calc_result
|
||||
|
||||
done < "$f_input_file" >>"$f_output_file"
|
||||
done >>"$f_output_file.4htmp"
|
||||
egrep -h "^[1-9][0-9][0-9][0-9]-[0-1][0-9]-[0-9][0-9].*,[0-9]" "$f_output_file" "$f_output_file.4htmp" | sort -k1,2 -t, -u | sort -k1,1 -t, -u >"$f_output_file.tmp"
|
||||
mv "$f_output_file.tmp" "$f_output_file"
|
||||
rm -f "$f_output_file.4htmp"
|
||||
|
||||
}
|
||||
|
||||
|
||||
#function convert_ohlcv_1h_to_1d {
|
||||
#
|
||||
# g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
|
||||
#
|
||||
# local f_input_file="$1"
|
||||
# local f_output_file="$2"
|
||||
#
|
||||
# local f_latestdate f_nextdate f_mytimezone f_line f_date f_open f_high f_low f_close f_volume f_inday i
|
||||
#
|
||||
# if ! [ -s "$f_input_file" ]
|
||||
# then
|
||||
# g_echo_error "$f_input_file"
|
||||
# return 0
|
||||
# fi
|
||||
#
|
||||
# # crypto timezone UTC
|
||||
# local f_target_timezone=UTC
|
||||
# # US economy timezone America/New_York
|
||||
# [[ $f_input_file =~ ECONOMY ]] && f_target_timezone="America/New_York"
|
||||
#
|
||||
# [ -s "$f_output_file" ] && f_latestdate=$(tail -n1 "$f_output_file" | cut -d, -f1)
|
||||
# [ -z "$f_latestdate" ] && f_latestdate=$(date -d "$(head -n1 "$f_input_file" | cut -d, -f1)" +%Y-%m-%d)
|
||||
# f_latestdate=$(TZ="$f_target_timezone" date -d "$f_latestdate $f_mytimezone" "+%Y-%m-%d")
|
||||
# f_nextdate=$(date -d "$f_latestdate +1day" "+%Y-%m-%d")
|
||||
#
|
||||
# # mytimezone, respecting summer/winter time
|
||||
# f_mytimezone=$(date -d "$_latestdate" +%Z)
|
||||
#
|
||||
# local f_today=$(TZ="$f_target_timezone" date "+%Y-%m-%d")
|
||||
# # check if there is a $f_latestdate
|
||||
# grep -A9999 -B24 "^$f_latestdate" "$f_input_file" >"$g_tmp/convert_ohlcv_1h_to_1d_nextlines"
|
||||
# if ! [ -s "$g_tmp/convert_ohlcv_1h_to_1d_nextlines" ]
|
||||
# then
|
||||
# cat "$f_input_file" >"$g_tmp/convert_ohlcv_1h_to_1d_nextlines"
|
||||
# f_nextdate=$(date -d "$(head -n1 "$g_tmp/convert_ohlcv_1h_to_1d_nextlines" | cut -d, -f1)" +%Y-%m-%d)
|
||||
# fi
|
||||
#
|
||||
# # go through lines and switch to $f_target_timezone
|
||||
# cat "$g_tmp/convert_ohlcv_1h_to_1d_nextlines" | grep ':00:00,' | cut -d, -f1,2,3,4,5,6 | while read f_line
|
||||
# do
|
||||
# g_array "$f_line" g_line_array ,
|
||||
# # calculate day in target timezone
|
||||
# g_line_array[0]=$(TZ="$f_target_timezone" date -d "${g_line_array[0]} $f_mytimezone" "+%Y-%m-%d")
|
||||
# [[ ${g_line_array[0]} = $f_today ]] && break
|
||||
# echo "${g_line_array[0]},${g_line_array[1]},${g_line_array[2]},${g_line_array[3]},${g_line_array[4]},${g_line_array[5]}"
|
||||
# done >"${f_output_file}.tmp"
|
||||
#
|
||||
# # check if $f_nextdate really exists in $f_target_timezone if not add a day until it exists
|
||||
# # useful for weekends
|
||||
# i=1
|
||||
# until grep -q "^$f_nextdate" "${f_output_file}.tmp"
|
||||
# do
|
||||
# #echo $f_nextdate
|
||||
# [[ $f_nextdate = $f_today ]] && return 0
|
||||
# f_nextdate=$(date -d "$f_nextdate +1day" "+%Y-%m-%d")
|
||||
# i=$((i++))
|
||||
# if [ $i -gt 10 ]
|
||||
# then
|
||||
# g_echo_warn "${FUNCNAME} $@: no nextdate found after >10 iterations"
|
||||
# return 1
|
||||
# fi
|
||||
# done
|
||||
#
|
||||
# # set ent mark to store latest complete day
|
||||
# echo END >>"${f_output_file}.tmp"
|
||||
#
|
||||
# # go through converted lines
|
||||
# cat "${f_output_file}.tmp" | while read f_line
|
||||
# do
|
||||
# g_array "$f_line" g_line_array ,
|
||||
# [[ ${g_line_array[0]} = $f_today ]] && break
|
||||
#
|
||||
# # wait until next day in target file reached
|
||||
# if [[ ${g_line_array[0]} = $f_nextdate ]]
|
||||
# then
|
||||
# f_end_reached=1
|
||||
# else
|
||||
# [ -z $f_end_reached ] && continue
|
||||
# fi
|
||||
#
|
||||
# # if dayend
|
||||
# if [ -n "$f_inday" ] && [[ $f_latestdate != ${g_line_array[0]} ]]
|
||||
# then
|
||||
# # day end
|
||||
# echo "$f_date,$f_open,$f_high,$f_low,$f_close,$f_volume"
|
||||
# f_inday=""
|
||||
# fi
|
||||
#
|
||||
# # calc values if inday
|
||||
# if [ -n "$f_inday" ]
|
||||
# then
|
||||
# #echo "in day $f_date" 1>&2
|
||||
# # in day
|
||||
# # add volume
|
||||
# g_calc "$f_volume+${g_line_array[5]}"
|
||||
# f_volume=$g_calc_result
|
||||
# # look for higher high
|
||||
# g_num_is_higher ${g_line_array[2]} $f_high && f_high=${g_line_array[2]}
|
||||
# # look for lower low
|
||||
# g_num_is_lower ${g_line_array[3]} $f_low && f_low=${g_line_array[3]}
|
||||
# fi
|
||||
#
|
||||
# # if newday
|
||||
# if [ -z "$f_inday" ]
|
||||
# then
|
||||
# #echo "day begin ${g_line_array[0]}" 1>&2
|
||||
# # day begin
|
||||
# f_inday=1
|
||||
# f_date=${g_line_array[0]}
|
||||
# f_latestdate=$f_date
|
||||
# f_open=${g_line_array[1]}
|
||||
# f_high=${g_line_array[2]}
|
||||
# f_low=${g_line_array[3]}
|
||||
# f_close=${g_line_array[4]}
|
||||
# f_volume=${g_line_array[5]}
|
||||
# fi
|
||||
#
|
||||
# done >>"$f_output_file"
|
||||
#
|
||||
#}
|
||||
|
||||
function convert_ohlcv_1d_to_1w {
|
||||
|
||||
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
|
||||
|
||||
local f_input_file=$1
|
||||
local f_output_file=$2
|
||||
|
||||
local f_week_date f_day f_month f_year f_other f_line f_data
|
||||
local -A f_open_prices f_high_prices f_low_prices f_close_prices f_volume_prices
|
||||
|
||||
# get lastest date to continue from here and create output file if not exists
|
||||
if [ -s "$f_output_file" ]
|
||||
then
|
||||
f_latestdate=$(tail -n1 "$f_output_file" | cut -d, -f1)
|
||||
else
|
||||
touch "$f_output_file"
|
||||
fi
|
||||
# if not exists use first date as latest date
|
||||
[ -z "$f_latestdate" ] && f_latestdate=$(date -d "$(head -n1 "$f_input_file" | cut -d, -f1)" +%Y-%m-%d)
|
||||
|
||||
# check if there is a $f_latestdate
|
||||
grep -A9999 -B9 "^$f_latestdate" "$f_input_file" >"$g_tmp/convert_ohlcv_1d_to_1w_nextlines"
|
||||
if ! [ -s "$g_tmp/convert_ohlcv_1d_to_1w_nextlines" ]
|
||||
then
|
||||
cat "$f_input_file" >"$g_tmp/convert_ohlcv_1d_to_1w_nextlines"
|
||||
fi
|
||||
|
||||
# go through lines
|
||||
for f_line in $(cat "$g_tmp/convert_ohlcv_1d_to_1w_nextlines")
|
||||
do
|
||||
IFS=',' read -r f_date f_open f_high f_low f_close f_volume f_other <<< "$f_line"
|
||||
IFS='-' read -r f_year f_month f_day <<< "$f_date"
|
||||
|
||||
[ -z "$f_high" ] && f_high=$f_open
|
||||
[ -z "$f_low" ] && f_low=$f_open
|
||||
[ -z "$f_close" ] && f_close=$f_open
|
||||
[ -z "$f_volume" ] && f_volume=0
|
||||
|
||||
# use week-number to sort day data in weeks
|
||||
f_week_number=$(date -d "$f_year-$f_month-$f_day" +%U)
|
||||
f_week_number=${f_week_number##0}
|
||||
f_week_year=$f_year$f_week_number
|
||||
|
||||
# calculate week ohlcv and write to arrays sortet by f_week_year
|
||||
g_calc "${f_open_prices[$f_week_year]:-$f_open}"
|
||||
f_open_prices[$f_week_year]=$g_calc_result
|
||||
|
||||
g_num_is_higher "$f_high" "${f_high_prices[$f_week_year]:-0}" && f_high_prices[$f_week_year]=$f_high
|
||||
|
||||
[ -z "${f_low_prices[$f_week_year]}" ] && f_low_prices[$f_week_year]=$f_low
|
||||
g_num_is_lower "$f_low" "${f_low_prices[$f_week_year]:-0}" && f_low_prices[$f_week_year]=$f_low
|
||||
|
||||
f_close_prices[$f_week_year]=$f_close
|
||||
|
||||
[ -z "$f_volume" ] && f_volume=0
|
||||
g_calc "${f_volume_prices[$f_week_year]:-0}+$f_volume"
|
||||
f_volume_prices[$f_week_year]=$g_calc_result
|
||||
done
|
||||
|
||||
# go through array(s) and write down missing week data
|
||||
for f_week_year in "${!f_open_prices[@]}"
|
||||
do
|
||||
f_week_date=$(date -d "${f_week_year:0:4}-01-01 +$((${f_week_year:4})) week -1day" +%F)
|
||||
# ignore if date alerady exists
|
||||
grep -q ^$f_week_date, "$f_output_file" && continue
|
||||
echo "$f_week_date,${f_open_prices[$f_week_year]},${f_high_prices[$f_week_year]},${f_low_prices[$f_week_year]},${f_close_prices[$f_week_year]},${f_volume_prices[$f_week_year]}"
|
||||
done | sort >>"$f_output_file"
|
||||
}
|
||||
|
||||
|
||||
|
||||
function f_add_missing_ohlcv_intervals {
|
||||
|
||||
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
|
||||
@@ -354,12 +571,16 @@ function f_add_missing_ohlcv_intervals {
|
||||
[[ $f_interval = 1w ]] && return 0
|
||||
[[ $f_histfile =~ \.1w\. ]] && return 0
|
||||
|
||||
local f_prev_date f_prev_vals f_curr_date f_curr_vals f_missing_date f_open f_high f_low f_close f_volume f_percent
|
||||
|
||||
local f_prev_date f_prev_vals f_curr_date f_curr_vals f_missing_date f_open f_high f_low f_close f_volume f_percent f_open f_counter
|
||||
|
||||
# go through csv per line
|
||||
while IFS=',' read -r f_curr_date f_open f_high f_low f_close f_volume f_percent f_curr_vals
|
||||
do
|
||||
# Wegnn das vorherige Datum nicht leer ist
|
||||
|
||||
#echo "$f_curr_date" 1>&2
|
||||
|
||||
# if prev date is not empty
|
||||
if [ -z "$f_prev_date" ]
|
||||
then
|
||||
f_prev_date=$f_curr_date
|
||||
@@ -367,24 +588,38 @@ function f_add_missing_ohlcv_intervals {
|
||||
continue
|
||||
fi
|
||||
|
||||
while true
|
||||
#echo "$f_curr_date x" 1>&2
|
||||
|
||||
# only 10 interations to prevelt endless loop
|
||||
f_counter=0
|
||||
while [ $f_counter -lt 10 ]
|
||||
#while true
|
||||
do
|
||||
|
||||
((f_counter++))
|
||||
#echo "$f_curr_date xx $f_counter" 1>&2
|
||||
|
||||
# get second timestamps
|
||||
f_prev_date_in_seconds=$(date -d"$f_prev_date" +%s)
|
||||
f_curr_date_in_seconds=$(date -d"$f_curr_date" +%s)
|
||||
|
||||
# calculate/check the next timestamp from previ15ms
|
||||
# and check for summer/winter time
|
||||
# echo [ "$f_prev_date_in_seconds" -gt "$f_curr_date_in_seconds" ] # && break
|
||||
|
||||
# calculate/check the next timestamp from previous
|
||||
# and check for summer/winter time in 4h or greater interval
|
||||
if [ $f_interval -gt 3600 ]
|
||||
then
|
||||
# reduce an hour because of possible summer/winter time change
|
||||
#g_calc "$f_curr_date_in_seconds - ($f_counter * $f_prev_date_in_seconds - 3600)"
|
||||
g_calc "$f_curr_date_in_seconds - $f_prev_date_in_seconds - 3600"
|
||||
else
|
||||
#g_calc "$f_curr_date_in_seconds - $f_counter * $f_prev_date_in_seconds"
|
||||
g_calc "$f_curr_date_in_seconds - $f_prev_date_in_seconds"
|
||||
fi
|
||||
if [ $g_calc_result -gt $f_interval ]
|
||||
then
|
||||
# calc missing timestamp in seconds
|
||||
#f_curr_date_in_seconds=$(( f_prev_date_in_seconds + f_interval * f_counter ))
|
||||
f_curr_date_in_seconds=$(( f_prev_date_in_seconds + f_interval ))
|
||||
# and calculate next timestamp
|
||||
g_calc "$f_curr_date_in_seconds - $f_prev_date_in_seconds"
|
||||
@@ -396,15 +631,28 @@ function f_add_missing_ohlcv_intervals {
|
||||
else
|
||||
f_missing_date=$(date -d"@$f_curr_date_in_seconds" +"%F")
|
||||
fi
|
||||
|
||||
|
||||
# prevent endless loop if something goes wrong (strange errors in 1d ohlcv!)
|
||||
f_missing_date_in_seconds=$(date -d"$f_missing_date" +%s)
|
||||
if [ $f_missing_date_in_seconds -lt $f_curr_date_in_seconds ]
|
||||
then
|
||||
[ -z "$f_curr_vals" ] && echo "$f_curr_date,$f_open,$f_high,$f_low,$f_close,$f_volume,$f_percent"
|
||||
[ -n "$f_curr_vals" ] && echo "$f_curr_date,$f_open,$f_high,$f_low,$f_close,$f_volume,$f_percent,$f_curr_vals"
|
||||
f_prev_date=$f_curr_date
|
||||
break
|
||||
fi
|
||||
|
||||
# write missing line
|
||||
echo "$f_missing_date,$f_close,$f_close,$f_close,$f_close,0,0.00,$f_curr_vals"
|
||||
[ -z "$f_curr_vals" ] && echo "$f_missing_date,$f_open,$f_open,$f_open,$f_open,0,0.00"
|
||||
[ -n "$f_curr_vals" ] && echo "$f_missing_date,$f_open,$f_open,$f_open,$f_open,0,0.00,$f_curr_vals"
|
||||
f_prev_date=$f_missing_date
|
||||
else
|
||||
f_prev_date=$f_curr_date
|
||||
echo "$f_curr_date,$f_open,$f_high,$f_low,$f_close,$f_volume,$f_percent,$f_curr_vals"
|
||||
[ -z "$f_curr_vals" ] && echo "$f_curr_date,$f_open,$f_high,$f_low,$f_close,$f_volume,$f_percent"
|
||||
[ -n "$f_curr_vals" ] && echo "$f_curr_date,$f_open,$f_high,$f_low,$f_close,$f_volume,$f_percent,$f_curr_vals"
|
||||
break
|
||||
fi
|
||||
|
||||
done
|
||||
|
||||
done < "$f_histfile" > $g_tmp/f_add_missing_ohlcv_intervals_result
|
||||
@@ -417,5 +665,3 @@ function f_add_missing_ohlcv_intervals {
|
||||
cat "$g_tmp/f_add_missing_ohlcv_intervals_result" >"$f_histfile"
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -53,7 +53,7 @@ function get_orders {
|
||||
echo $f_ccxt_result | tee "CCXT_ORDERS_${f_symbol_file}_RAW" | jq -r "
|
||||
.[] |
|
||||
select(.status==\"open\") |
|
||||
.symbol + \",\" + .type + \",\" + .side + \",\" + (.price|tostring) + \",\" + (.amount|tostring) + \",\" + .id + \",\" + (.stopLossPrice|tostring) + \",\" + (.takeProfitPrice|tostring)
|
||||
.symbol + \",\" + .type + \",\" + .side + \",\" + (.price|tostring) + \",\" + (.amount|tostring) + \",\" + .id + \",\" + (.stopLossPrice|tostring) + \",\" + (.takeProfitPrice|tostring) + \",\" + (.stopPrice|tostring)
|
||||
" >"CCXT_ORDERS_${f_symbol_file}"
|
||||
else
|
||||
rm -f "CCXT_ORDERS_${f_symbol_file}_RAW" "CCXT_ORDERS_${f_symbol_file}"
|
||||
@@ -61,25 +61,75 @@ select(.status==\"open\") |
|
||||
fi
|
||||
done
|
||||
cat CCXT_ORDERS_*${CURRENCY} >CCXT_ORDERS 2>/dev/null
|
||||
return 0
|
||||
|
||||
get_orders_array
|
||||
}
|
||||
|
||||
function get_orders_array {
|
||||
g_array CCXT_ORDERS f_get_ordes_rarray
|
||||
local f_order
|
||||
|
||||
# clear/create assoziative array o
|
||||
unset o
|
||||
declare -Ag o
|
||||
|
||||
# build array from lines in CCXT_ORDERS
|
||||
g_array CCXT_ORDERS f_get_ordes_array
|
||||
for f_order in ${f_get_ordes_array[@]}
|
||||
do
|
||||
get_order_line_vars "$f_order"
|
||||
done
|
||||
|
||||
# write values to file
|
||||
for i in "${!o[@]}"
|
||||
do
|
||||
echo "\${o[$i]}=${o[$i]}"
|
||||
done | sort >values-orders.new
|
||||
mv values-orders.new values-orders
|
||||
|
||||
}
|
||||
|
||||
function get_orders_line_vars {
|
||||
function get_order_line_vars {
|
||||
local f_order_line=$1
|
||||
|
||||
g_array $f_order_line f_order_array ,
|
||||
|
||||
f_order_symbol=${f_order_array[0]}
|
||||
f_order_type=${f_order_array[1]}
|
||||
f_order_side=${f_order_array[2]}
|
||||
f_order_entry_price=${f_order_array[3]}
|
||||
f_order_amount=${f_order_array[4]}
|
||||
f_order_id=${f_order_array[5]}
|
||||
f_order_stoplossprice=${f_order_array[6]}
|
||||
f_order_takeprofitprice=${f_order_array[7]}
|
||||
local f_asset=${f_order_symbol//:$CURRENCY/}
|
||||
f_asset=${f_asset//\//}
|
||||
|
||||
local f_order_type=${f_order_array[1]}
|
||||
local f_order_side=${f_order_array[2]}
|
||||
local f_type
|
||||
if [[ $f_order_type = limit ]]
|
||||
then
|
||||
[[ $f_order_side = buy ]] && f_type="open_long"
|
||||
[[ $f_order_side = sell ]] && f_type="open_short"
|
||||
fi
|
||||
if [[ $f_order_type = Stop ]]
|
||||
then
|
||||
[[ $f_order_side = buy ]] && f_type="sl_close_short"
|
||||
[[ $f_order_side = sell ]] && f_type="sl_close_long"
|
||||
fi
|
||||
if [[ $f_order_type = MarketIfTouched ]]
|
||||
then
|
||||
[[ $f_order_side = buy ]] && f_type="tp_close_short"
|
||||
[[ $f_order_side = sell ]] && f_type="tp_close_long"
|
||||
fi
|
||||
|
||||
if [ -z "${o[${f_asset}_present]}" ]
|
||||
then
|
||||
o[${f_asset}_present]=${f_type}
|
||||
else
|
||||
o[${f_asset}_present]="${o[${f_asset}_present]} ${f_type}"
|
||||
fi
|
||||
o[${f_asset}_${f_type}_type]=${f_order_array[1]}
|
||||
o[${f_asset}_${f_type}_side]=${f_order_array[2]}
|
||||
o[${f_asset}_${f_type}_entry_price]=${f_order_array[3]}
|
||||
o[${f_asset}_${f_type}_amount]=${f_order_array[4]}
|
||||
o[${f_asset}_${f_type}_id]=${f_order_array[5]}
|
||||
o[${f_asset}_${f_type}_stoplossprice]=${f_order_array[6]}
|
||||
o[${f_asset}_${f_type}_takeprofitprice]=${f_order_array[7]}
|
||||
o[${f_asset}_${f_type}_stopprice]=${f_order_array[8]}
|
||||
}
|
||||
|
||||
|
||||
|
||||
63
dabo/functions/get_phemex_csv_transactions.sh
Normal file
63
dabo/functions/get_phemex_csv_transactions.sh
Normal file
@@ -0,0 +1,63 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Copyright (c) 2022-2024 olli
|
||||
#
|
||||
# This file is part of dabo (crypto bot).
|
||||
#
|
||||
# dabo is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# dabo is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with dabo. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
function get_phemex_csv_transactions {
|
||||
|
||||
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
|
||||
|
||||
# PHEMEX Export format:
|
||||
# Time (UTC),Symbol,Exec Type,Exec. Size,Direction,Exec. Price,Order Size,Order Price,Exec Value,Fee Rate,Fee Paid,Type,"ID"
|
||||
|
||||
if [ -s phemex-export.csv ]
|
||||
then
|
||||
# explicit long
|
||||
cat phemex-export.csv | egrep '^[0-9].+,Trade,.+ Long,' | sort | sed 's/,Long,/,Open Long,/; s/Open Long/leverage-buy/; s/Close Long/leverage-sell/; s/ /,/g' | awk -F, '{print $1" "$2","$7","$6","$5","$13","$12",phemex,"$16","$15","$18}' >TRANSACTIONS-phemex-LONG.csv.tmp
|
||||
# explicit short
|
||||
cat phemex-export.csv | egrep '^[0-9].+,Trade,.+ Short,' | sort | sed 's/,Short,/,Open Short,/; s/Open Short/leverage-sell/; s/Close Short/leverage-buy/; s/ /,/g' | awk -F, '{print $1" "$2","$7","$6","$5","$13","$12",phemex,"$16","$15","$18}' >TRANSACTIONS-phemex-SHORT.csv.tmp
|
||||
# buy/sell
|
||||
cat phemex-export.csv | egrep '^[0-9].+,Trade,.+,Short,|^[0-9].+,Trade,.+,Long,' | sort | sed 's/Short/leverage-sell/; s/Long/leverage-buy/; s/ /,/g' | awk -F, '{print $1" "$2","$7","$6","$5","$13","$12",phemex,"$16","$15","$18}' >TRANSACTIONS-phemex.csv.tmp
|
||||
# liquidations long
|
||||
cat phemex-export.csv | egrep '^[0-9].+,Liquidation,.+Long,' | sort | sed 's/ /,/g' | awk -F, '{print $1" "$2",liquidation,"$6","$5","$14","$13",phemex,"$17","$16","$19}' >TRANSACTIONS-phemex-liquidations-LONG.csv.tmp
|
||||
# liquidations short
|
||||
cat phemex-export.csv | egrep '^[0-9].+,Liquidation,.+Short,' | sort | sed 's/ /,/g' | awk -F, '{print $1" "$2",liquidation,"$6","$5","$14","$13",phemex,"$17","$16","$19}' >TRANSACTIONS-phemex-liquidations-SHORT.csv.tmp
|
||||
# fundingfees seeem to be included in sell
|
||||
cat TRANSACTIONS-phemex/*.csv | egrep 'funding' | sort -u >>TRANSACTIONS-phemex.csv.tmp
|
||||
|
||||
# put together
|
||||
local f_line f_id
|
||||
touch "TRANSACTIONS-phemex.csv"
|
||||
|
||||
cat TRANSACTIONS-phemex-LONG.csv.tmp TRANSACTIONS-phemex.csv.tmp TRANSACTIONS-phemex-liquidations-LONG.csv.tmp | sort | while read f_line
|
||||
do
|
||||
f_id=$(echo "$f_line" | cut -d, -f10)
|
||||
grep -q "$f_id" "TRANSACTIONS-phemex.csv" || echo $f_line >>"TRANSACTIONS-phemex.csv"
|
||||
done
|
||||
|
||||
cat TRANSACTIONS-phemex-SHORT.csv.tmp TRANSACTIONS-phemex-liquidations-SHORT.csv.tmp | sort | while read f_line
|
||||
do
|
||||
f_id=$(echo "$f_line" | cut -d, -f10)
|
||||
grep -q "$f_id" "TRANSACTIONS-phemex.csv" || echo $f_line >>"TRANSACTIONS-phemex.csv"
|
||||
done
|
||||
|
||||
# cleanup
|
||||
rm -f TRANSACTIONS-phemex-LONG.csv.tmp TRANSACTIONS-phemex-SHORT.csv.tmp TRANSACTIONS-phemex.csv.tmp TRANSACTIONS-phemex-liquidations-LONG.csv.tmp TRANSACTIONS-phemex-liquidations-SHORT.csv.tmp
|
||||
fi
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ function get_positions {
|
||||
|
||||
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
|
||||
|
||||
local f_symbol f_symbols
|
||||
local f_symbol f_symbols f_asset f_stoploss f_takeprofit
|
||||
|
||||
get_symbols_ticker
|
||||
|
||||
@@ -39,50 +39,132 @@ function get_positions {
|
||||
|
||||
[ -z "$f_symbols" ] && return 1
|
||||
f_ccxt "print($STOCK_EXCHANGE.fetchPositions(symbols=[${f_symbols}]))" && echo $f_ccxt_result >CCXT_POSITIONS_RAW
|
||||
|
||||
jq -r "
|
||||
.[] |
|
||||
select(.entryPrice != 0) |
|
||||
.symbol + \",\" + (.notional|tostring) + \",\" + (.entryPrice|tostring) + \",\" + (.markPrice|tostring) + \",\" + .side + \",\" + (.leverage|tostring) + \",\" + (.contracts|tostring) + \",\" + (.contractSize|tostring) + \",\" + (.liquidationPrice|tostring) + \",\" + (.unrealizedPnl|tostring)
|
||||
.symbol + \",\" + (.collateral|tostring) + \",\" + (.entryPrice|tostring) + \",\" + .side + \",\" + (.leverage|tostring) + \",\" + (.liquidationPrice|tostring) + \",\" + (.stopLossPrice|tostring) + \",\" + (.takeProfitPrice|tostring) + \",\" + (.contracts|tostring)
|
||||
" CCXT_POSITIONS_RAW >CCXT_POSITIONS
|
||||
|
||||
# check for takeprofit/stoploss orders if not in CCXT output (needed for phememx and maybe more exchanges)
|
||||
get_position_array
|
||||
for f_symbol in ${f_symbols_array_trade[@]}
|
||||
do
|
||||
f_asset=${f_symbol//:$CURRENCY/}
|
||||
f_asset=${f_asset//\//}
|
||||
# only continue if position for symbol exists and stoploss or takeprofit is empty
|
||||
[ -z "${p[${f_asset}_entry_price]}" ] && continue
|
||||
[ -n "${p[${f_asset}_stoploss_price]}" ] && continue
|
||||
[ -n "${p[${f_asset}_takeprofit_price]}" ] && continue
|
||||
|
||||
# check for position side
|
||||
[ "${p[${f_asset}_side]}" = "long" ] && f_action=sell
|
||||
[ "${p[${f_asset}_side]}" = "short" ] && f_action=buy
|
||||
if [[ ${p[${f_asset}_side]} =~ long|short ]]
|
||||
then
|
||||
# search for stoploss and takeprofit
|
||||
f_stoploss=$(egrep "^$f_symbol,Stop,$f_action,null,0," CCXT_ORDERS | cut -d , -f9)
|
||||
f_takeprofit=$(egrep "^$f_symbol,MarketIfTouched,$f_action,null,0," CCXT_ORDERS | cut -d , -f9)
|
||||
# escape : and / for sed and edit CCXT_POSITIONS if stoploss or takeprofit order found
|
||||
f_symbol=${f_symbol//\//\\\/}
|
||||
f_symbol=${f_symbol//:/\\:}
|
||||
[ -n "$f_stoploss" ] && sed -i "/^$f_symbol,.*,${p[${f_asset}_side]},/s/^\(\([^,]*,\)\{6\}\)[^,]*/\1$f_stoploss/" CCXT_POSITIONS
|
||||
[ -n "$f_takeprofit" ] && sed -i "/^$f_symbol,.*,${p[${f_asset}_side]},/s/^\(\([^,]*,\)\{7\}\)[^,]*/\1$f_takeprofit/" CCXT_POSITIONS
|
||||
fi
|
||||
done
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
function get_position_array {
|
||||
local f_position
|
||||
|
||||
# clear/create assoziative array p
|
||||
unset p
|
||||
declare -Ag p
|
||||
get_symbols_ticker
|
||||
# build array from lines in CCXT_POSITIONS
|
||||
g_array CCXT_POSITIONS f_get_positions_array
|
||||
for f_position in ${f_get_positions_array[@]}
|
||||
do
|
||||
get_position_line_vars "$f_position"
|
||||
done
|
||||
|
||||
# write values to file
|
||||
for i in "${!p[@]}"
|
||||
do
|
||||
echo "\${p[$i]}=${p[$i]}"
|
||||
done | sort >values-positions.new
|
||||
mv values-positions.new values-positions
|
||||
|
||||
}
|
||||
|
||||
function get_position_line_vars {
|
||||
local f_pos_line=$1
|
||||
|
||||
g_array $f_pos_line f_position_array ,
|
||||
|
||||
f_position_symbol=${f_position_array[0]}
|
||||
f_position_currency_amount=${f_position_array[1]}
|
||||
local f_asset=${f_position_symbol//:$CURRENCY/}
|
||||
f_asset=${f_asset//\//}
|
||||
|
||||
printf -v f_position_currency_amount %.2f ${f_position_array[1]}
|
||||
p[${f_asset}_currency_amount]=$f_position_currency_amount
|
||||
|
||||
f_position_entry_price=${f_position_array[2]}
|
||||
f_position_current_price=${f_position_array[3]}
|
||||
f_position_side=${f_position_array[4]}
|
||||
p[${f_asset}_entry_price]=$f_position_entry_price
|
||||
|
||||
# mark price seems not lates price in very case so take the ticker
|
||||
p[${f_asset}_current_price]=${v[${f_asset}_price]}
|
||||
f_position_current_price=${v[${f_asset}_price]}
|
||||
|
||||
f_position_side=${f_position_array[3]}
|
||||
[ -z "$f_position_side" ] && f_position_side="long"
|
||||
f_position_leverage=${f_position_array[5]}
|
||||
[ -z "$f_position_leverage" ] && f_position_leverage="1"
|
||||
f_position_contracts=${f_position_array[6]}
|
||||
f_position_contract_size=${f_position_array[7]}
|
||||
f_position_liquidation_price=${f_position_array[8]}
|
||||
f_position_unrealized_pnl=${f_position_array[9]}
|
||||
p[${f_asset}_side]=$f_position_side
|
||||
|
||||
g_percentage-diff $f_position_entry_price $f_position_current_price
|
||||
[ "$f_position_side" = short ] && g_percentage-diff $f_position_current_price $f_position_entry_price
|
||||
f_position_pnl_percentage=$g_percentage_diff_result
|
||||
if [ -n $f_position_leverage ]
|
||||
f_position_leverage=${f_position_array[4]}
|
||||
[[ $f_position_leverage = null ]] && f_position_leverage="1"
|
||||
p[${f_asset}_leverage]=$f_position_leverage
|
||||
|
||||
p[${f_asset}_liquidation_price]=${f_position_array[5]}
|
||||
f_position_liquidation_price=${f_position_array[5]}
|
||||
|
||||
if [[ ${f_position_array[6]} = null ]]
|
||||
then
|
||||
g_calc "$f_position_pnl_percentage*$f_position_leverage"
|
||||
f_position_pnl_percentage=$g_calc_result
|
||||
|
||||
g_calc "$f_position_currency_amount/$f_position_leverage"
|
||||
f_position_currency_amount=$g_calc_result
|
||||
|
||||
g_calc "$f_position_currency_amount/100*$f_position_pnl_percentage"
|
||||
f_position_pnl=$g_calc_result
|
||||
unset p[${f_asset}_stoploss_price]
|
||||
unset f_position_stoploss_price
|
||||
else
|
||||
p[${f_asset}_stoploss_price]=${f_position_array[6]}
|
||||
f_position_stoploss_price=${f_position_array[6]}
|
||||
fi
|
||||
|
||||
if [[ ${f_position_array[7]} = null ]]
|
||||
then
|
||||
unset p[${f_asset}_takeprofit_price]
|
||||
unset f_position_takeprofit_price
|
||||
else
|
||||
p[${f_asset}_takeprofit_price]=${f_position_array[7]}
|
||||
f_position_takeprofit_price=${f_position_array[7]}
|
||||
fi
|
||||
|
||||
#printf -v f_position_asset_amount %.2f ${f_position_array[8]}
|
||||
#p[${f_asset}_asset_amount]=$f_position_asset_amount
|
||||
p[${f_asset}_asset_amount]=${f_position_array[8]}
|
||||
|
||||
# calc pnl percentage
|
||||
if [[ $f_position_side = long ]]
|
||||
then
|
||||
g_percentage-diff $f_position_entry_price $f_position_current_price
|
||||
else
|
||||
g_percentage-diff $f_position_current_price $f_position_entry_price
|
||||
fi
|
||||
g_calc "$g_percentage_diff_result*$f_position_leverage"
|
||||
f_position_pnl_percentage=$g_calc_result
|
||||
p[${f_asset}_pnl_percentage]=$g_calc_result
|
||||
|
||||
# calc pnl
|
||||
g_calc "$f_position_currency_amount/100*$f_position_pnl_percentage"
|
||||
printf -v f_position_pnl %.2f $g_calc_result
|
||||
p[${f_asset}_pnl]=$f_position_pnl
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -87,6 +87,7 @@ function get_symbols_ticker {
|
||||
local f_ticker f_symbol f_price
|
||||
declare -Ag f_tickers_array
|
||||
declare -Ag v
|
||||
declare -Ag vr
|
||||
for f_ticker in "${f_tickers_array_ref[@]}"
|
||||
do
|
||||
f_symbol=${f_ticker%%:*}
|
||||
@@ -95,6 +96,7 @@ function get_symbols_ticker {
|
||||
f_price=${f_ticker/*,/}
|
||||
g_num_exponential2normal $f_price && f_price=$g_num_exponential2normal_result
|
||||
f_tickers_array[$f_symbol]=$f_price
|
||||
vr[${f_symbol}_price]=$f_price
|
||||
v[${f_symbol}_price]=$f_price
|
||||
done
|
||||
fi
|
||||
|
||||
@@ -113,7 +113,7 @@ function get_transactions {
|
||||
f_ccxt "print(${STOCK_EXCHANGE}.fetchFundingHistory('$f_symbol', limit=200, params={'paginate': True}))" && echo -n $f_ccxt_result >"${f_symbol_file}.FundingFees"
|
||||
cat ${f_symbol_file}.FundingFees | jq -r "
|
||||
.[] |
|
||||
.datetime + \",fundingfee,$f_asset,0,\" + .code + \",0\" + \",$f_exchange,\" + .code + \",\" + (.amount|tostring)
|
||||
.datetime + \",fundingfee,$f_asset,0,\" + .code + \",0\" + \",$f_exchange,\" + .code + \",\" + (.amount|tostring)
|
||||
" >>$f_symbol_file_csv_tmp
|
||||
|
||||
# remove the ':' in f_currency
|
||||
@@ -127,46 +127,27 @@ function get_transactions {
|
||||
cat "$f_symbol_file" | jq -r "
|
||||
.[] |
|
||||
select(.side==\"buy\" or .side==\"sell\") |
|
||||
select(.info.posSide=null) |
|
||||
select(.symbol!= null) |
|
||||
.datetime + \",$f_leverage\" + .side + \",$f_asset,\" + (.amount|tostring) + \",$f_currency,\" + (.cost|tostring) + \",$f_exchange,\" + .fee.currency + \",\" + (.fee.cost|tostring)
|
||||
" >>"$f_symbol_file_csv_tmp"
|
||||
|
||||
# get longs (posSide=="1")
|
||||
cat "$f_symbol_file" | jq -r "
|
||||
.[] |
|
||||
select(.side==\"buy\" or .side==\"sell\") |
|
||||
select(.info.posSide=\"1\") |
|
||||
select(.symbol!= null) |
|
||||
.datetime + \",$f_leverage\" + .side + \",$f_asset,\" + (.amount|tostring) + \",$f_currency,\" + (.cost|tostring) + \",$f_exchange,\" + .fee.currency + \",\" + (.fee.cost|tostring)
|
||||
" >>"$f_symbol_file_csv_tmp"
|
||||
|
||||
# get shorts (posSide=="2") sell first, then buy (https://github.com/ccxt/ccxt/issues/22518)
|
||||
cat "$f_symbol_file" | jq -r "
|
||||
.[] |
|
||||
select(.side==\"buy\" or .side==\"sell\") |
|
||||
select(.info.posSide==\"2\") |
|
||||
select(.symbol!=null) |
|
||||
.datetime + \",$f_leverage\" + .side + \",$f_asset,\" + (.amount|tostring) + \",$f_currency,\" + (.cost|tostring) + \",$f_exchange,\" + .fee.currency + \",\" + (.fee.cost|tostring) + \",short\"
|
||||
select(.symbol != null) |
|
||||
select(.type != null) |
|
||||
.datetime + \",$f_leverage\" + .side + \",$f_asset,\" + (.amount|tostring) + \",$f_currency,\" + (.cost|tostring) + \",$f_exchange,\" + .fee.currency + \",\" + (.fee.cost|tostring) + \",\" + .id
|
||||
" >>"$f_symbol_file_csv_tmp"
|
||||
|
||||
# # get longs (posSide=="1")
|
||||
# cat "$f_symbol_file" | jq -r "
|
||||
#.[] |
|
||||
# select(.side==\"buy\" or .side==\"sell\") |
|
||||
# select(.info.posSide=\"1\") |
|
||||
# select(.symbol!= null) |
|
||||
#.datetime + \",$f_leverage\" + .side + \",$f_asset,\" + (.amount|tostring) + \",$f_currency,\" + (.cost|tostring) + \",$f_exchange,\" + .fee.currency + \",\" + (.fee.cost|tostring) + \",\" + .id
|
||||
#" >>"$f_symbol_file_csv_tmp"
|
||||
#
|
||||
# # get shorts (posSide=="2") sell first, then buy (https://github.com/ccxt/ccxt/issues/22518)
|
||||
# cat "$f_symbol_file" | jq -r "
|
||||
#.[] |
|
||||
# select(.side==\"sell\") |
|
||||
# select(.side==\"buy\" or .side==\"sell\") |
|
||||
# select(.info.posSide==\"2\") |
|
||||
# select(.symbol!=null) |
|
||||
# .side = \"sell\" |
|
||||
#.datetime + \",$f_leverage\" + .side + \",$f_asset,\" + (.amount|tostring) + \",$f_currency,\" + (.cost|tostring) + \",$f_exchange,\" + .fee.currency + \",\" + (.fee.cost|tostring) + \",short\"
|
||||
#" >>"$f_symbol_file_csv_tmp"
|
||||
#
|
||||
# cat "$f_symbol_file" | jq -r "
|
||||
#.[] |
|
||||
# select(.side==\"buy\") |
|
||||
# select(.info.posSide==\"2\") |
|
||||
# select(.symbol!=null) |
|
||||
# .side = \"buy\" |
|
||||
#.datetime + \",$f_leverage\" + .side + \",$f_asset,\" + (.amount|tostring) + \",$f_currency,\" + (.cost|tostring) + \",$f_exchange,\" + .fee.currency + \",\" + (.fee.cost|tostring) + \",short\"
|
||||
#.datetime + \",$f_leverage\" + .side + \",$f_asset,\" + (.amount|tostring) + \",$f_currency,\" + (.cost|tostring) + \",$f_exchange,\" + .fee.currency + \",\" + (.fee.cost|tostring) + \",\" + .id
|
||||
#" >>"$f_symbol_file_csv_tmp"
|
||||
|
||||
if [ -s "$f_symbol_file_csv_tmp" ]
|
||||
@@ -226,14 +207,20 @@ function get_transactions {
|
||||
done
|
||||
fi
|
||||
|
||||
# put all sorted n one file
|
||||
if [ -s TRANSACTIONS-$f_exchange.csv ]
|
||||
then
|
||||
cat "TRANSACTIONS-$f_exchange/"*.csv TRANSACTIONS-$f_exchange.csv | sort -u >TRANSACTIONS-$f_exchange.csv.tmp
|
||||
mv TRANSACTIONS-$f_exchange.csv.tmp TRANSACTIONS-$f_exchange.csv
|
||||
else
|
||||
cat "TRANSACTIONS-$f_exchange/"*.csv | sort -u >TRANSACTIONS-$f_exchange.csv
|
||||
fi
|
||||
# put all sorted in one file. duplicates check with id
|
||||
local f_line f_date f_sym
|
||||
touch "TRANSACTIONS-$f_exchange.csv"
|
||||
grep -vh ,fundingfee, "TRANSACTIONS-$f_exchange/"*.csv | while read f_line
|
||||
do
|
||||
f_id=$(echo "$f_line" | cut -d, -f10)
|
||||
grep -q "$f_id" "TRANSACTIONS-$f_exchange.csv" || echo $f_line >>"TRANSACTIONS-$f_exchange.csv"
|
||||
done
|
||||
grep -h ,fundingfee, "TRANSACTIONS-$f_exchange/"*.csv | while read f_line
|
||||
do
|
||||
f_date=$(echo "$f_line" | cut -d: -f1)
|
||||
f_sym=$(echo "$f_line" | cut -d, -f3)
|
||||
egrep -q "^$f_date:.+,$f_sym," "TRANSACTIONS-$f_exchange.csv" || echo $f_line >>"TRANSACTIONS-$f_exchange.csv"
|
||||
done
|
||||
|
||||
# Switch sides if Fiat is in Krypto side
|
||||
f_fiats="USD EUR"
|
||||
@@ -247,7 +234,7 @@ function get_transactions {
|
||||
g_echo_note "Switched some fiat/krypto sides"
|
||||
#cat TRANSACTIONS-$f_exchange.csv.tmp
|
||||
cat TRANSACTIONS-$f_exchange.csv | egrep -v ",sell,$f_fiat,|,buy,$f_fiat," >>TRANSACTIONS-$f_exchange.csv.tmp
|
||||
cat TRANSACTIONS-$f_exchange.csv.tmp | sort >TRANSACTIONS-$f_exchange.csv
|
||||
cat TRANSACTIONS-$f_exchange.csv.tmp >TRANSACTIONS-$f_exchange.csv
|
||||
fi
|
||||
done
|
||||
|
||||
|
||||
@@ -22,9 +22,19 @@ function get_values {
|
||||
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
|
||||
|
||||
local f_assets="$@"
|
||||
local f_asset_histories
|
||||
|
||||
f_assets=${f_assets//:$CURRENCY/}
|
||||
f_assets=${f_assets//\//}
|
||||
|
||||
for f_asset in $f_assets
|
||||
do
|
||||
f_asset_histories+="$f_asset "
|
||||
f_asset_histories+="MARKETDATA_BINANCE_OPEN_INTEREST_$f_asset "
|
||||
f_asset_histories+="MARKETDATA_BINANCE_LONG_SHORT_RATIO_TAKER_$f_asset "
|
||||
f_asset_histories+="MARKETDATA_BINANCE_LONG_SHORT_RATIO_ACCOUNT_$f_asset "
|
||||
done
|
||||
|
||||
local f_eco_asset f_eco_assets f_asset f_time f_prefix f_histfile f_columns f_return f_levelsfile f_tmp_levels f_first
|
||||
|
||||
for f_eco_asset in $ECO_ASSETS
|
||||
@@ -40,25 +50,34 @@ function get_values {
|
||||
# get current prices from exchange
|
||||
get_symbols_ticker
|
||||
# get values from csv files
|
||||
#f_first=true
|
||||
for f_asset in $f_assets BTC${CURRENCY} $f_eco_assets
|
||||
|
||||
for f_asset in $f_asset_histories\
|
||||
BTC${CURRENCY}\
|
||||
$f_eco_assets\
|
||||
MARKETDATA_BINANCE_OPEN_INTEREST_BTC${CURRENCY}\
|
||||
MARKETDATA_BINANCE_LONG_SHORT_RATIO_TAKER_BTC${CURRENCY}\
|
||||
MARKETDATA_BINANCE_LONG_SHORT_RATIO_ACCOUNT_BTC${CURRENCY}\
|
||||
MARKETDATA_FEAR_AND_GREED_ALTERNATIVEME\
|
||||
MARKETDATA_FEAR_AND_GREED_CNN\
|
||||
MARKETDATA_US_CONSUMER_PRICE_INDEX_CPI\
|
||||
MARKETDATA_US_FED_FUNDS_RATE MARKETDATA_US_UNEMPLOYMENT_RATE
|
||||
do
|
||||
|
||||
# read latest ohlcv data and indicators per timeframe to vars
|
||||
for f_time in 5m 15m 1h 4h 1d 1w
|
||||
do
|
||||
#f_prefix="${f_asset}_${f_time}_"
|
||||
#[ "$f_first" = "true" ] && f_prefix="${f_time}_"
|
||||
# special on ECONOMY data
|
||||
f_prefix="${f_time}_"
|
||||
if [[ "$f_asset" =~ ^ECONOMY- ]]
|
||||
then
|
||||
f_prefix="${f_asset}_${f_time}_"
|
||||
f_prefix=${f_prefix//-/_}
|
||||
fi
|
||||
|
||||
# histfile
|
||||
f_histfile="asset-histories/${f_asset}.history.${f_time}.csv"
|
||||
if ! [ -s "$f_histfile" ]
|
||||
then
|
||||
g_echo_warn "file $f_histfile empty or does not exist"
|
||||
f_return=1
|
||||
continue
|
||||
fi
|
||||
@@ -67,7 +86,6 @@ function get_values {
|
||||
done
|
||||
|
||||
# read current levels
|
||||
#v[${f_asset}_price]=${f_tickers_array[$f_asset]}
|
||||
for f_time in 1w 1d
|
||||
do
|
||||
f_levelsfile="asset-histories/${f_asset}.history.${f_time}.csv.levels"
|
||||
@@ -75,30 +93,36 @@ function get_values {
|
||||
then
|
||||
# get levels
|
||||
read -r -a f_levels <"$f_levelsfile"
|
||||
v[${f_asset}_levels_$f_time]="${f_levels[*]}"
|
||||
vr[${f_asset}_levels_$f_time]="${f_levels[*]}"
|
||||
|
||||
# add current price and sort
|
||||
f_levels+=("${v[${f_asset}_price]}")
|
||||
f_levels+=("${vr[${f_asset}_price]}")
|
||||
oldIFS="$IFS"
|
||||
IFS=$'\n' f_levels_sorted=($(sort -n <<<"${f_levels[*]}"))
|
||||
IFS="$oldIFS"
|
||||
|
||||
# find current price and +- one for upper lower price
|
||||
for ((i=0; i<${#f_levels_sorted[@]}; i++)); do
|
||||
if [ "${f_levels_sorted[$i]}" = "${v[${f_asset}_price]}" ]
|
||||
if [ "${f_levels_sorted[$i]}" = "${vr[${f_asset}_price]}" ]
|
||||
then
|
||||
v[${f_asset}_levels_${f_time}_next_up]=${f_levels_sorted[i+1]}
|
||||
v[${f_asset}_levels_${f_time}_next_down]=${f_levels_sorted[i-1]}
|
||||
vr[${f_asset}_levels_${f_time}_next_up]=${f_levels_sorted[i+1]}
|
||||
vr[${f_asset}_levels_${f_time}_next_down]=${f_levels_sorted[i-1]}
|
||||
break
|
||||
fi
|
||||
done
|
||||
fi
|
||||
done
|
||||
|
||||
#unset f_first
|
||||
|
||||
done
|
||||
|
||||
# use reverse as default to be 0 latest, 1 pre latest,...
|
||||
unset v
|
||||
declare -Ag v
|
||||
for key in "${!vr[@]}"; do
|
||||
v[$key]=${vr[$key]}
|
||||
done
|
||||
unset vr
|
||||
|
||||
# write values file for overview
|
||||
for i in "${!v[@]}"
|
||||
do
|
||||
echo "\${v[$i]}=${v[$i]}"
|
||||
|
||||
@@ -24,50 +24,89 @@ function order {
|
||||
|
||||
# needed vars
|
||||
local f_symbol=$1
|
||||
local f_amount=$2 # amount in $CURRENCY / if crypto_amount:XXX then amount in crypto
|
||||
local f_amount=$2 # amount in $CURRENCY / if asset_amount:XXX then amount in invested asset
|
||||
local f_side=$3 # buy/sell long/short
|
||||
local f_price=$4 # price for limit order - if not given do market order
|
||||
local f_price=$4 # price for limit order - if "0" do market order - "stoploss" for pure StopLoss Order and "takeprofit" for pure TakeProfit Order
|
||||
local f_stoploss=$5
|
||||
local f_takeprofit=$6
|
||||
local f_params="params={"
|
||||
local f_type
|
||||
|
||||
local f_type f_side_opposite f_pos_side f_side_opposide f_trigger_sl f_trigger_tp
|
||||
|
||||
### validity checks ###
|
||||
|
||||
|
||||
if [ -z "$f_symbol" ] || [ -z "$f_amount" ] || [ -z "$f_side" ] || [ -z "$f_price" ]
|
||||
then
|
||||
g_echo_error "Missing values!
|
||||
Usage: order symbol amount side price [stoploss] [takeprofit]
|
||||
Given: ${FUNCNAME} $@"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# check symbol XXX/$CURRENCY[:$CURRENCY]
|
||||
[[ $f_symbol =~ /$CURRENCY ]] || return 1
|
||||
|
||||
# check side
|
||||
[ "$f_side" = "long" ] && f_side="buy"
|
||||
[ "$f_side" = "short" ] && f_side="sell"
|
||||
if [ "$f_side" = "long" ] || [ "$f_side" = "buy" ]
|
||||
then
|
||||
f_side="buy"
|
||||
f_pos_side="Long"
|
||||
f_side_opposide="sell"
|
||||
f_trigger_sl="down"
|
||||
f_trigger_tp="up"
|
||||
fi
|
||||
if [ "$f_side" = "short" ] || [ "$f_side" = "sell" ]
|
||||
then
|
||||
f_side="sell"
|
||||
f_pos_side="Short"
|
||||
f_side_opposide="buy"
|
||||
f_trigger_sl="up"
|
||||
f_trigger_tp="down"
|
||||
fi
|
||||
[[ $f_side =~ ^buy$|^sell$ ]] || return 1
|
||||
|
||||
# check order type limit/market
|
||||
if [ -z "$f_price" ]
|
||||
if [[ $f_price = 0 ]]
|
||||
then
|
||||
f_type="market"
|
||||
f_price=0
|
||||
elif [[ $f_price = stoploss ]]
|
||||
then
|
||||
f_type="stoploss"
|
||||
f_price="None"
|
||||
elif [[ $f_price = takeprofit ]]
|
||||
then
|
||||
f_type="takeprofit"
|
||||
f_price="None"
|
||||
else
|
||||
f_type="limit"
|
||||
fi
|
||||
|
||||
### validity checks end###
|
||||
### validity checks end ###
|
||||
|
||||
# get amount in crypto asset
|
||||
if [[ $f_amount =~ ^crypto_amount: ]]
|
||||
|
||||
# get amount in target asset
|
||||
if [[ $f_amount =~ ^asset_amount: ]]
|
||||
then
|
||||
# if given in crypto
|
||||
f_amount=${f_amount//crypto_amount:}
|
||||
# if given in target
|
||||
f_amount=${f_amount//asset_amount:}
|
||||
else
|
||||
# if given in $CURRENCY
|
||||
local f_asset=${f_symbol///*}
|
||||
currency_converter $f_amount $CURRENCY $f_asset || return 1
|
||||
local f_amount=$f_currency_converter_result
|
||||
fi
|
||||
# on market order use current price
|
||||
if [[ $f_type = market ]]
|
||||
then
|
||||
# if given in $CURRENCY
|
||||
local f_asset=${f_symbol///*}
|
||||
currency_converter $f_amount $CURRENCY $f_asset || return 1
|
||||
f_amount=$f_currency_converter_result
|
||||
# on limit order use limit price
|
||||
elif [[ $f_type = limit ]]
|
||||
then
|
||||
g_calc "1/${f_price}*${f_amount}"
|
||||
f_amount=$g_calc_result
|
||||
fi
|
||||
fi
|
||||
|
||||
|
||||
# check for swap/margin trades
|
||||
if [ -n "$LEVERAGE" ]
|
||||
if [ -n "$LEVERAGE" ]
|
||||
then
|
||||
# do some margin things
|
||||
|
||||
@@ -80,26 +119,80 @@ function order {
|
||||
# set leverage
|
||||
f_ccxt "$STOCK_EXCHANGE.setLeverage($LEVERAGE, '$f_symbol')" || return 1
|
||||
|
||||
# define margibn mode isolated/cross
|
||||
# define margin mode isolated/cross
|
||||
#[[ $f_type =~ limit|market ]] &&
|
||||
f_params="${f_params}'marginMode': '$MARGIN_MODE', "
|
||||
|
||||
|
||||
# calculate amount with leverage
|
||||
if [[ $f_type != stoploss ]] && [[ $f_type != takeprofit ]]
|
||||
then
|
||||
g_calc "${f_amount}*${LEVERAGE}"
|
||||
f_amount=$g_calc_result
|
||||
fi
|
||||
else
|
||||
# short/sell not possible in spot market
|
||||
[[ $f_side =~ ^sell$ ]] || return 1
|
||||
fi
|
||||
|
||||
# Add stoploos and take profit if available
|
||||
if [ -n "$f_stoploss" ]
|
||||
|
||||
# Add stoploss and take profit if available
|
||||
if [ -n "$f_stoploss" ]
|
||||
then
|
||||
if [[ $f_type = limit ]]
|
||||
then
|
||||
# check for long
|
||||
if [[ $f_side = buy ]] && g_num_is_higher_equal $f_stoploss $f_price
|
||||
then
|
||||
g_echo_warn "Long Order not possible: Stoploss ($f_stoploss) higher then buy price ($f_price)"
|
||||
return 1
|
||||
fi
|
||||
# check for short
|
||||
if [[ $f_side = sell ]] && g_num_is_lower_equal $f_stoploss $f_price
|
||||
then
|
||||
g_echo_warn "Short Order not possible: Stoploss ($f_stoploss) lower then buy price ($f_price)"
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
f_ccxt "print($STOCK_EXCHANGE.priceToPrecision('${f_symbol}', ${f_stoploss}))"
|
||||
f_stoploss=$f_ccxt_result
|
||||
f_params="${f_params}'stopLossPrice': '$f_stoploss', "
|
||||
# market or limit order with stoploss
|
||||
if [[ $f_type =~ limit|market ]]
|
||||
then
|
||||
f_params="${f_params}'stopLoss': { 'triggerPrice': $f_stoploss, 'type': 'market' }, "
|
||||
# stoploss (change) for open position
|
||||
elif [[ $f_type = "stoploss" ]]
|
||||
then
|
||||
f_type="market"
|
||||
f_price=$f_stoploss
|
||||
f_params="${f_params}'reduceOnly': True, 'triggerPrice': $f_stoploss, 'triggerDirection': '$f_trigger_sl', "
|
||||
fi
|
||||
fi
|
||||
if [ -n "$f_takeprofit" ]
|
||||
if [ -n "$f_takeprofit" ]
|
||||
then
|
||||
# check for long
|
||||
if [[ $f_type = limit ]]
|
||||
then
|
||||
if [[ $f_side = buy ]] && g_num_is_lower_equal $f_takeprofit $f_price
|
||||
then
|
||||
g_echo_warn "Long Order not possible:TakeProfit ($f_takeprofit) lower then buy price ($f_price)"
|
||||
return 1
|
||||
fi
|
||||
# check for short
|
||||
if [[ $f_side = sell ]] && g_num_is_higher_equal $f_takeprofit $f_price
|
||||
then
|
||||
g_echo_warn "Short Order not possible:TakeProfit ($f_takeprofit) higher then buy price ($f_price)"
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
f_ccxt "print($STOCK_EXCHANGE.priceToPrecision('${f_symbol}', ${f_takeprofit}))"
|
||||
f_takeprofit=$f_ccxt_result
|
||||
f_params="${f_params}'takeProfitPrice': '$f_takeprofit', "
|
||||
[[ $f_type =~ limit|market ]] && f_params="${f_params}'takeProfit': { 'triggerPrice': $f_takeprofit, 'type': 'limit', 'price': $f_takeprofit, }, "
|
||||
if [[ $f_type = "takeprofit" ]]
|
||||
then
|
||||
f_type="limit"
|
||||
f_price=$f_takeprofit
|
||||
f_params="${f_params}'reduceOnly': True, 'triggerPrice': $f_takeprofit, 'triggerDirection': '$f_trigger_tp', "
|
||||
fi
|
||||
fi
|
||||
|
||||
# end up params syntax with "}"
|
||||
@@ -108,15 +201,26 @@ function order {
|
||||
# calculate price amount precision
|
||||
f_ccxt "print($STOCK_EXCHANGE.amountToPrecision('${f_symbol}', ${f_amount}))"
|
||||
f_amount=$f_ccxt_result
|
||||
f_ccxt "print($STOCK_EXCHANGE.priceToPrecision('${f_symbol}', ${f_price}))"
|
||||
f_price=$f_ccxt_result
|
||||
if [[ $f_type = limit ]]
|
||||
then
|
||||
f_ccxt "print($STOCK_EXCHANGE.priceToPrecision('${f_symbol}', ${f_price}))"
|
||||
f_price=$f_ccxt_result
|
||||
fi
|
||||
|
||||
# do the order
|
||||
local f_order="symbol='${f_symbol}', type='$f_type', price=$f_price, amount=${f_amount}, side='${f_side}', ${f_params}"
|
||||
# market order with or without stoploss/takeprofit
|
||||
[[ $f_type = limit ]] && local f_order="symbol='${f_symbol}', type='$f_type', price=$f_price, amount=${f_amount}, side='${f_side}', ${f_params}"
|
||||
[[ $f_type = market ]] && local f_order="symbol='${f_symbol}', type='$f_type', amount=${f_amount}, side='${f_side}', ${f_params}"
|
||||
|
||||
# takeprofit/stoploss only
|
||||
[[ $f_params =~ reduceOnly ]] && local f_order="symbol='${f_symbol}', type='$f_type', amount=${f_amount}, side='${f_side_opposide}', price=$f_price, ${f_params}"
|
||||
echo "$f_order" | notify.sh -s "ORDER"
|
||||
f_ccxt "print($STOCK_EXCHANGE.createOrder(${f_order}))" || return 1
|
||||
|
||||
# refresh positions
|
||||
# refresh orders and positions
|
||||
get_orders "$f_symbol"
|
||||
get_positions
|
||||
get_position_array
|
||||
get_orders_array
|
||||
}
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
# along with dabo. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
function order_close {
|
||||
function order_cancel {
|
||||
# Info for log
|
||||
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
|
||||
|
||||
@@ -26,15 +26,70 @@ function order_close {
|
||||
local f_order
|
||||
|
||||
get_symbols_ticker
|
||||
get_orders
|
||||
get_orders "$f_symbol"
|
||||
get_orders_array
|
||||
|
||||
local f_asset=${f_symbol//:$CURRENCY/}
|
||||
f_asset=${f_asset//\//}
|
||||
|
||||
if [ -n "${o[${f_asset}_present]}" ]
|
||||
then
|
||||
f_ccxt "print(${STOCK_EXCHANGE}.cancelAllOrders('$f_symbol'))"
|
||||
get_orders "$f_symbol"
|
||||
get_orders_array
|
||||
else
|
||||
g_echo_note "No orders for $f_symbol/$f_asset found"
|
||||
return 0
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
function order_cancel_all {
|
||||
# Info for log
|
||||
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
|
||||
|
||||
local f_order
|
||||
|
||||
get_symbols_ticker
|
||||
get_orders_array
|
||||
|
||||
for f_order in "${f_get_orders_array[@]}"
|
||||
do
|
||||
get_order_line_vars "$f_order"
|
||||
if [ "$f_symbol" = "$f_order_symbol" ]
|
||||
if [[ $f_symbol = $f_order_symbol ]]
|
||||
then
|
||||
f_ccxt "print(${STOCK_EXCHANGE}.cancelAllOrders('$f_symbol'))"
|
||||
get_orders "$f_symbol"
|
||||
get_orders_array
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
function order_cancel_id {
|
||||
# Info for log
|
||||
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
|
||||
|
||||
local f_symbol=$1
|
||||
local f_id=$2
|
||||
local f_order
|
||||
|
||||
get_symbols_ticker
|
||||
get_orders "$f_symbol"
|
||||
get_orders_array
|
||||
|
||||
local f_asset=${f_symbol//:$CURRENCY/}
|
||||
f_asset=${f_asset//\//}
|
||||
|
||||
if grep -q "$f_asset.*$f_id" values-orders
|
||||
then
|
||||
f_ccxt "print(${STOCK_EXCHANGE}.cancelOrder(id='${f_id}', symbol='${f_symbol}'))"
|
||||
get_orders "$f_symbol"
|
||||
get_orders_array
|
||||
else
|
||||
g_echo_note "No orders for $f_symbol/$f_asset with id $f_id found"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -30,14 +30,14 @@ function run_strategies {
|
||||
get_symbols_ticker
|
||||
get_values ${f_symbols_array_trade[*]}
|
||||
|
||||
for f_strategy in $(find /dabo/strategies -type f -name "*strategy*" ! -name "\.*")
|
||||
for f_strategy in $(find /dabo/strategies -type f -name "*.strategy.sh" ! -name "\.*")
|
||||
do
|
||||
if ! bash -n "${f_strategy}" >$g_tmp/strat_bash_error 2>&1
|
||||
then
|
||||
g_echo_error "Error in ${f_strategy} $(cat $g_tmp/strat_bash_error)"
|
||||
continue
|
||||
fi
|
||||
g_echo_note "Runnign strategy ${f_strategy}"
|
||||
g_echo_note "Running strategy ${f_strategy}"
|
||||
. "${f_strategy}" || g_echo_warn "Failed ${f_strategy}"
|
||||
done
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@ function transactions_overview {
|
||||
get_bitpanda_api_transactions
|
||||
get_justtrade_csv_transactions
|
||||
get_onetrading_csv_transactions
|
||||
get_phemex_csv_transactions
|
||||
|
||||
>ALL_TRANSACTIONS_OVERVIEW.csv.tmp
|
||||
>ALL_TRANSACTIONS_OVERVIEW_WARN.csv.tmp
|
||||
@@ -34,7 +35,7 @@ function transactions_overview {
|
||||
|
||||
local f_exchange f_asset f_transactions_array f_transaction f_result f_asset_quantity f_asset_quantity_sold f_currency_quantity f_currency_quantity_sold f_currency_spent f_date f_type f_asset_amount f_currency f_currency_amount f_fee_currency f_fee_amount f_sell_result f_taxable f_tax_type f_one_year_ago f_currency_amount_eur f_currency_spent_eur f_currency_quantity_sold_eur f_note f_asset_quantity_remaining f_currency_remaining f_year f_currency_spent_eur_tax f_currency_quantity_sold_eur_tax f_sell_result_percentage f_sell_result_percentage_eur
|
||||
|
||||
f_assets_per_exchange=$(egrep -h -v '^DATE,TYPE,ASSET,ASSET_AMOUNT,CURRENCY,CURRENCY_AMOUNT,EXCHANGE|^#|^$|^ +$' TRANSACTIONS-*.csv | cut -d, -f3,7 | sort -u)
|
||||
f_assets_per_exchange=$(egrep -h -v '^DATE,TYPE,ASSET,ASSET_AMOUNT,CURRENCY,CURRENCY_AMOUNT,EXCHANGE|^#|^$|^ +$' TRANSACTIONS-*.csv | cut -d, -f3,7 | sort -u | grep JustTrade)
|
||||
|
||||
for f_asset_per_exchange in ${f_assets_per_exchange}
|
||||
do
|
||||
@@ -70,6 +71,7 @@ function transactions_overview {
|
||||
mapfile -t f_transactions_array < <(egrep -h -v '^DATE,TYPE,ASSET,ASSET_AMOUNT,CURRENCY,CURRENCY_AMOUNT,EXCHANGE|^#|^$|^ +$' TRANSACTIONS-*.csv | sort -u | egrep ",${f_asset},.+,.+,${f_exchange}" | sort)
|
||||
for f_transaction in "${f_transactions_array[@]}"
|
||||
do
|
||||
g_echo_note "Transaction: $f_transaction"
|
||||
mapfile -d, -t f_transaction_array < <(echo $f_transaction)
|
||||
f_date=${f_transaction_array[0]}
|
||||
f_type=${f_transaction_array[1]}
|
||||
@@ -154,6 +156,7 @@ function transactions_overview {
|
||||
[[ $f_type =~ stake ]] && continue
|
||||
|
||||
# what did I spent on asset in currency
|
||||
#if [[ $f_type =~ buy|leverage-buy ]]
|
||||
if [[ $f_type =~ buy|leverage-buy|reward-staking|instant_trade_bonus|giveaway ]]
|
||||
then
|
||||
g_calc "$f_currency_spent+$f_currency_amount"
|
||||
@@ -210,7 +213,7 @@ function transactions_overview {
|
||||
echo "$f_date,$f_exchange,$f_type,$f_asset,$f_asset_amount,$f_currency,$f_currency_amount,$f_one_year_ago,$f_currency_spent,$f_asset_quantity,$f_result,$f_sell_result,$f_tax_type,$f_taxable,$f_currency_amount_eur,$f_result_eur,$f_sell_result_eur,$f_asset_quantity_remaining,$f_note,Sell never buyed!? Spent currency on $f_asset is 0" 1>&2
|
||||
continue
|
||||
fi
|
||||
# if sell wahats not exists!?
|
||||
# if sell what not exists!?
|
||||
if [ $f_asset_quantity = 0 ]
|
||||
then
|
||||
#g_echo_warn "!!!!!! Sell never buyed!? Buyed asset $f_asset is 0"
|
||||
@@ -238,6 +241,7 @@ function transactions_overview {
|
||||
## Check for ended trade (asset-quantity=0 or tttt)
|
||||
# if all is sold trade ended and calculate PNL
|
||||
local f_trade_end=0
|
||||
local f_trade_partial_end=0
|
||||
local f_dust=0
|
||||
local f_dust_eur=0
|
||||
g_calc "$f_asset_quantity_sold==$f_asset_quantity"
|
||||
@@ -273,10 +277,11 @@ function transactions_overview {
|
||||
else
|
||||
g_echo_note "Tade not closed - partial sale!? Remaining $f_asset_quantity_remaining $f_asset ($f_currency_remaining $f_currency)!?" >>ALL_TRANSACTIONS_OVERVIEW.log
|
||||
f_note="Trade not closed - partial sale. Remaining $f_asset_quantity_remaining $f_asset ($f_currency_remaining $f_currency)"
|
||||
f_trade_partial_end=1
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ ${f_trade_end} -eq 1 ]
|
||||
if [ ${f_trade_end} -eq 1 ] || [ ${f_trade_partial_end} -eq 1 ]
|
||||
then
|
||||
|
||||
echo "Buy price: $f_currency_spent $f_currency ($f_currency_spent_eur EUR)" >>ALL_TRANSACTIONS_OVERVIEW.log
|
||||
@@ -302,17 +307,20 @@ function transactions_overview {
|
||||
# calculate complete result EUR
|
||||
g_calc "$f_result_eur+($f_sell_result_eur)"
|
||||
f_result_eur=$g_calc_result
|
||||
|
||||
# reset vars
|
||||
f_asset_quantity=0
|
||||
f_asset_quantity_sold=0
|
||||
f_asset_quantity_remaining=0
|
||||
f_currency_spent=0
|
||||
printf -v f_currency_spent_eur_tax %.2f $f_currency_spent_eur
|
||||
f_currency_spent_eur=0
|
||||
f_currency_quantity_sold=0
|
||||
printf -v f_currency_quantity_sold_eur_tax %.2f $f_currency_quantity_sold_eur
|
||||
f_currency_quantity_sold_eur=0
|
||||
|
||||
if [ ${f_trade_end} -eq 1 ]
|
||||
then
|
||||
# reset vars
|
||||
f_asset_quantity=0
|
||||
f_asset_quantity_sold=0
|
||||
f_asset_quantity_remaining=0
|
||||
f_currency_spent=0
|
||||
printf -v f_currency_spent_eur_tax %.2f $f_currency_spent_eur
|
||||
f_currency_spent_eur=0
|
||||
f_currency_quantity_sold=0
|
||||
printf -v f_currency_quantity_sold_eur_tax %.2f $f_currency_quantity_sold_eur
|
||||
f_currency_quantity_sold_eur=0
|
||||
fi
|
||||
fi
|
||||
|
||||
# at leverage always full taxable
|
||||
@@ -373,6 +381,7 @@ function transactions_overview {
|
||||
## completely tax free if over 1 year
|
||||
f_taxable=0
|
||||
f_one_year_ago="yes"
|
||||
f_tax_type="Kauf >1 Jahr zurück"
|
||||
# reduce tax free volume
|
||||
g_calc "$f_asset_amount_tax_free-$f_asset_amount"
|
||||
f_asset_amount_tax_free=$g_calc_result
|
||||
@@ -436,6 +445,8 @@ function transactions_overview {
|
||||
# 24 f_sell_result_percentage_eur
|
||||
echo "$f_date,$f_exchange,$f_type,$f_asset,$f_asset_amount,$f_currency,$f_currency_amount,$f_one_year_ago,$f_currency_spent,$f_asset_quantity,$f_result,$f_sell_result,$f_tax_type,$f_taxable,$f_currency_amount_eur,$f_result_eur,$f_sell_result_eur,$f_asset_quantity_remaining,$f_note,$f_currency_spent_eur,$f_currency_quantity_sold,$f_currency_quantity_sold_eur,$f_sell_result_percentage,$f_sell_result_percentage_eur" | tee -a ALL_TRANSACTIONS_OVERVIEW.csv.tmp >>ALL_TRANSACTIONS_OVERVIEW.log
|
||||
|
||||
echo "$f_date,$f_exchange,$f_type,$f_asset,$f_asset_amount,$f_currency,$f_currency_amount,$f_one_year_ago,$f_currency_spent,$f_asset_quantity,$f_result,$f_sell_result,$f_tax_type,$f_taxable,$f_currency_amount_eur,$f_result_eur,$f_sell_result_eur,$f_asset_quantity_remaining,$f_note,$f_currency_spent_eur,$f_currency_quantity_sold,$f_currency_quantity_sold_eur,$f_sell_result_percentage,$f_sell_result_percentage_eur"
|
||||
|
||||
if [[ $f_type =~ sell|leverage-sell ]]
|
||||
then
|
||||
echo -e "\n" >>ALL_TRANSACTIONS_OVERVIEW.log
|
||||
|
||||
@@ -22,8 +22,9 @@ function webpage {
|
||||
|
||||
g_echo_note "RUNNING FUNCTION ${FUNCNAME} $@"
|
||||
|
||||
charts
|
||||
webpage_transactions
|
||||
get_symbols_ticker
|
||||
charts
|
||||
|
||||
# create status webpage
|
||||
echo "<html>
|
||||
@@ -39,9 +40,7 @@ function webpage {
|
||||
<h1>State of Dabo-Bot! on ${STOCK_EXCHANGE} - ${URL} (ReadOnly)</h1>
|
||||
<h1>Last update $(date '+%F %T')</h1>" >../index.html.tmp
|
||||
|
||||
# historic overview
|
||||
echo '<a href=TRANSACTIONS_OVERVIEWS.html><h2>Historic Overview</h2></a>' >>../index.html.tmp
|
||||
|
||||
# balance
|
||||
local f_USED_BALANCE=$(tail -n1 "asset-histories/BALANCEUSED${CURRENCY}.history.csv" | cut -d, -f2)
|
||||
local f_COMPLETE_BALANCE=$(tail -n1 "asset-histories/BALANCECOMPLETE${CURRENCY}.history.csv" | cut -d, -f2)
|
||||
g_calc "$f_COMPLETE_BALANCE-$f_USED_BALANCE"
|
||||
@@ -63,7 +62,7 @@ function webpage {
|
||||
</table>" >>../index.html.tmp
|
||||
|
||||
echo "<h2>Balance in- outflows</h2>" >>../index.html.tmp
|
||||
echo "<table><tr><td><b>Time ago</b><td><b>Balance</b></td><td><b>in/out</b></td><td><b>Percentage</b></td></tr>" >> ../index.html.tmp
|
||||
echo "<table><tr class=\"headline\"><td><b>Time ago</b><td><b>Balance</b></td><td><b>in/out</b></td><td><b>Percentage</b></td></tr>" >> ../index.html.tmp
|
||||
|
||||
for f_balance_date in Day Week Month 3Month Year
|
||||
do
|
||||
@@ -82,23 +81,73 @@ function webpage {
|
||||
|
||||
|
||||
echo '<h2>Open Positions</h2>' >>../index.html.tmp
|
||||
echo "<table width='100%'><tr><td>Symbol</td><td>Amount</td><td>Entry Price</td><td>Current Price</td><td>Profit/Loss</td><td>Notes</td></tr>" >>../index.html.tmp
|
||||
echo "<table width='100%'><tr class=\"headline\"><td>Symbol</td><td>Amount</td><td>Entry Price</td><td>Current Price</td><td>Profit/Loss</td><td>Liquidation Price</td><td>StopLoss</td><td>TakeProfit</td><td>Notes</td></tr>" >>../index.html.tmp
|
||||
get_position_array
|
||||
for f_position in "${f_get_positions_array[@]}"
|
||||
for f_symbol in ${f_symbols_array_trade[@]}
|
||||
do
|
||||
get_position_line_vars "$f_position"
|
||||
printf -v f_position_currency_amount %.2f $f_position_currency_amount
|
||||
printf -v f_position_entry_price %.2f $f_position_entry_price
|
||||
printf -v f_position_current_price %.2f $f_position_current_price
|
||||
printf -v f_position_pnl %.2f $f_position_pnl
|
||||
echo "<tr><td>$f_position_symbol</td><td>${CURRENCY} $f_position_currency_amount</td><td>${CURRENCY} $f_position_entry_price</td><td>${CURRENCY} $f_position_current_price</td><td>${CURRENCY} $f_position_pnl ( ${f_position_pnl_percentage}%)</td><td>$f_position_side ${f_position_leverage}x</td></tr>" >>../index.html.tmp
|
||||
f_asset=${f_symbol//:$CURRENCY/}
|
||||
f_asset=${f_asset//\//}
|
||||
[ -z "${p[${f_asset}_entry_price]}" ] && continue
|
||||
echo "<tr>
|
||||
<td><a href=\"charts.html?symbol=${f_asset}&time=4h&symbol2=BTCUSDT\" target=\"_blank\" rel=\"noopener noreferrer\">$f_symbol</a></td>
|
||||
<td>${p[${f_asset}_currency_amount]}</td>
|
||||
<td>${p[${f_asset}_entry_price]}</td>
|
||||
<td>${p[${f_asset}_current_price]}</td>
|
||||
<td>${p[${f_asset}_pnl]} ( ${p[${f_asset}_pnl_percentage]}%)</td>
|
||||
<td>${p[${f_asset}_liquidation_price]}</td>
|
||||
<td>${p[${f_asset}_stoploss_price]}</td>
|
||||
<td>${p[${f_asset}_takeprofit_price]}</td>
|
||||
<td>${p[${f_asset}_side]} ${p[${f_asset}_leverage]}x</td>
|
||||
</tr>" >>../index.html.tmp
|
||||
done
|
||||
echo "</table>" >>../index.html.tmp
|
||||
|
||||
|
||||
echo '<h2>Open Orders</h2>' >>../index.html.tmp
|
||||
echo "<table width='100%'><tr class=\"headline\"><td>Symbol</td><td>Amount</td><td>Entry Price</td><td>StopLoss</td><td>TakeProfit</td><td>Notes</td></tr>" >>../index.html.tmp
|
||||
get_orders_array
|
||||
local f_type
|
||||
for f_symbol in ${f_symbols_array_trade[@]}
|
||||
do
|
||||
f_asset=${f_symbol//:$CURRENCY/}
|
||||
f_asset=${f_asset//\//}
|
||||
for f_type in ${o[${f_asset}_present]}
|
||||
do
|
||||
[ -z "${o[${f_asset}_${f_type}_entry_price]}" ] && continue
|
||||
[ "${o[${f_asset}_${f_type}_entry_price]}" = "null" ] && continue
|
||||
echo "<tr>
|
||||
<td><a href=\"charts.html?symbol=${f_asset}&time=4h&symbol2=BTCUSDT\" target=\"_blank\" rel=\"noopener noreferrer\">$f_symbol</a></td>
|
||||
<td>${o[${f_asset}_${f_type}_amount]}</td>
|
||||
<td>${o[${f_asset}_${f_type}_entry_price]}</td>
|
||||
<td>${o[${f_asset}_${f_type}_stoplossprice]}</td>
|
||||
<td>${o[${f_asset}_${f_type}_takeprofitprice]}</td>
|
||||
<td>${o[${f_asset}_${f_type}_type]} ${p[${f_asset}_${f_type}_side]}</td>
|
||||
</tr>" >>../index.html.tmp
|
||||
done
|
||||
done
|
||||
echo "</table>" >>../index.html.tmp
|
||||
|
||||
## charts
|
||||
echo '<h2>Charts with local data</h2><p>Click on time units to open chart</p>' >>../index.html.tmp
|
||||
|
||||
local eco_assets=$(echo " $ECO_ASSETS" | sed 's/ / ECONOMY-/g')
|
||||
for f_symbol in ${f_symbols_array_trade[@]} $eco_assets
|
||||
do
|
||||
f_asset=${f_symbol//:$CURRENCY/}
|
||||
f_asset=${f_asset//\//}
|
||||
echo "$f_asset: " >>../index.html.tmp
|
||||
for f_timeframe in 1w 1d 4h 1h 15m
|
||||
do
|
||||
echo "<td><a href=\"charts.html?symbol=${f_asset}&time=${f_timeframe}&symbol2=BTCUSDT\" target=\"_blank\" rel=\"noopener noreferrer\"}>${f_timeframe}</a>" >>../index.html.tmp
|
||||
done
|
||||
echo "<br>" >>../index.html.tmp
|
||||
done
|
||||
|
||||
## Open Positions
|
||||
echo "<h2>Open Positions - From Trade Histories (daily refresh only)</h2>" >>../index.html.tmp
|
||||
echo "<h2>Open Positions - from other Exchanges</h2>
|
||||
<p>Crypto-Only from Bitpanda and JustTrade - daily refresh</p>" >>../index.html.tmp
|
||||
echo "<table width='100%'>" >>../index.html.tmp
|
||||
echo "<tr><td>Amount</td><td>Spent Amount</td><td>Sold Amount</td><td>Profit/Loss</td><td>Asset Amount</td><td>Exchange</td></tr>" >>../index.html.tmp
|
||||
echo "<tr class=\"headline\"><td>Date</td><td>Amount</td><td>Spent Amount</td><td>Sold Amount</td><td>Profit/Loss</td><td>Asset Amount</td><td>Exchange</td></tr>" >>../index.html.tmp
|
||||
rm -f ../index.html.tmp.tmp
|
||||
local f_result_complete=0
|
||||
local f_spent_complete=0
|
||||
@@ -106,20 +155,19 @@ function webpage {
|
||||
local f_sold_complete=0
|
||||
local f_result_percent_complete=0
|
||||
local f_asset f_exchange f_amount f_spent f_sold f_currency_amount f_result_percent
|
||||
if [ -s ALL_TRANSACTIONS_OVERVIEW.csv ]
|
||||
then
|
||||
for f_asset_per_exchange in $(cat ALL_TRANSACTIONS_OVERVIEW.csv | cut -d, -f2,4 | sort -u)
|
||||
for f_asset_per_exchange in $(cat ALL_TRANSACTIONS_OVERVIEW.csv 2>/dev/null | cut -d, -f2,4 | sort -u)
|
||||
do
|
||||
mapfile -d, -t f_asset_per_exchange_array < <(echo $f_asset_per_exchange)
|
||||
f_asset=${f_asset_per_exchange_array[1]%$'\n'}
|
||||
f_exchange=${f_asset_per_exchange_array[0]}
|
||||
[[ "$f_exchange" =~ JustTrade|Bitpanda ]] || continue
|
||||
f_date=$(egrep "$f_exchange,.+,$f_asset" ALL_TRANSACTIONS_OVERVIEW.csv | tail -n1 | cut -d, -f1)
|
||||
f_amount=$(egrep "$f_exchange,.+,$f_asset" ALL_TRANSACTIONS_OVERVIEW.csv | tail -n1 | cut -d, -f18)
|
||||
f_spent=$(egrep "$f_exchange,.+,$f_asset" ALL_TRANSACTIONS_OVERVIEW.csv | tail -n1 | cut -d, -f20)
|
||||
f_sold=$(egrep "$f_exchange,.+,$f_asset" ALL_TRANSACTIONS_OVERVIEW.csv | tail -n1 | cut -d, -f22)
|
||||
if ! [ "$f_amount" = 0 ]
|
||||
then
|
||||
currency_converter $f_amount $f_asset $TRANSFER_CURRENCY || continue
|
||||
currency_converter "$f_amount" "$f_asset" "$TRANSFER_CURRENCY" || continue
|
||||
f_currency_amount=$f_currency_converter_result
|
||||
g_calc "$f_currency_amount+($f_sold)"
|
||||
#f_currency_amount=$g_calc_result
|
||||
@@ -156,60 +204,33 @@ function webpage {
|
||||
#f_sold_complete=$g_calc_result
|
||||
printf -v f_sold_complete %.2f $g_calc_result
|
||||
|
||||
echo "<tr><td>$f_currency_amount $TRANSFER_CURRENCY</td><td>$f_spent $TRANSFER_CURRENCY</td><td>$f_sold $TRANSFER_CURRENCY</td><td>$f_result $TRANSFER_CURRENCY ( ${f_result_percent}%)</td><td>$f_amount $f_asset</td><td>$f_exchange</td></tr>" >>../index.html.tmp.tmp
|
||||
echo "<tr><td>$f_date</td><td>$f_currency_amount $TRANSFER_CURRENCY</td><td>$f_spent $TRANSFER_CURRENCY</td><td>$f_sold $TRANSFER_CURRENCY</td><td>$f_result $TRANSFER_CURRENCY ( ${f_result_percent}%)</td><td>$f_amount $f_asset</td><td>$f_exchange</td></tr>" >>../index.html.tmp.tmp
|
||||
fi
|
||||
done
|
||||
|
||||
g_percentage-diff $f_spent_complete $f_currency_amount_complete
|
||||
f_result_percent_complete=$g_percentage_diff_result
|
||||
|
||||
# ALL Line
|
||||
echo "<tr><td>-</td><td>$f_currency_amount_complete $TRANSFER_CURRENCY</td><td>$f_spent_complete $TRANSFER_CURRENCY</td><td>$f_sold_complete $TRANSFER_CURRENCY</td><td>$f_result_complete $TRANSFER_CURRENCY ( ${f_result_percent_complete}%)</td><td>ALL</td><td>ALL</td></tr>" >>../index.html.tmp
|
||||
|
||||
echo "<tr><td>$f_currency_amount_complete $TRANSFER_CURRENCY</td><td>$f_spent_complete $TRANSFER_CURRENCY</td><td>$f_sold_complete $TRANSFER_CURRENCY</td><td>$f_result_complete $TRANSFER_CURRENCY ( ${f_result_percent_complete}%)</td><td>ALL</td><td>ALL</td></tr>" >>../index.html.tmp
|
||||
sort -n -k3 -t'>' -r ../index.html.tmp.tmp >>../index.html.tmp
|
||||
# Sort by Spent Amount
|
||||
sort -n -k7 -t'>' -r ../index.html.tmp.tmp >>../index.html.tmp
|
||||
rm ../index.html.tmp.tmp
|
||||
echo "</table>" >>../index.html.tmp
|
||||
fi
|
||||
|
||||
echo "<h2>Market Performance ( $(cat MARKET_PERFORMANCE_LATEST)%)</h2>" >>../index.html.tmp
|
||||
#echo "<table width='100%'><tr><td><details><summary>Charts</summary>" >>../index.html.tmp
|
||||
echo "<details><summary>Charts</summary>" >>../index.html.tmp
|
||||
echo "Krypto" >>../index.html.tmp
|
||||
genchart MARKET_PERFORMANCE.csv 50 2,3,4,5,6,7 red-or-green,gold,royalblue,lightyellow,MediumSlateBlue,Sienna >>../index.html.tmp
|
||||
echo "Commodities" >>../index.html.tmp
|
||||
genchart MARKET_PERFORMANCE.csv 50 2,12,10,13 red-or-green,gold,SandyBrown,Sienna >>../index.html.tmp
|
||||
echo "World Economic data" >>../index.html.tmp
|
||||
genchart MARKET_PERFORMANCE.csv 50 2,11,15 red-or-green,Yellow,Sienna >>../index.html.tmp
|
||||
echo "US Economic data" >>../index.html.tmp
|
||||
genchart MARKET_PERFORMANCE.csv 50 2,8,17,16,18,19,20,21,14,9 red-or-green,OliveDrab,Yellow,Orange,DeepSkyBlue,DarkMagenta,PeachPuff,PaleTurquoise,Pink,PapayaWhip >>../index.html.tmp
|
||||
#echo "</details></td></tr></table>" >>../index.html.tmp
|
||||
echo "</details>" >>../index.html.tmp
|
||||
echo "<details><summary>Table</summary><table width='100%'>" >>../index.html.tmp
|
||||
echo "<tr>" >>../index.html.tmp
|
||||
head -n1 MARKET_PERFORMANCE.csv | perl -pe 's/,/\n/g' | tr [:lower:] [:upper:] | while read f_mperfcol
|
||||
# Closed positions
|
||||
echo "<h2>Closed Positions and (german) tax declaration notes</h2>" >>../index.html.tmp
|
||||
ls ../TRANSACTIONS_OVERVIEW-* | while read f_html
|
||||
do
|
||||
echo "<td><b>${f_mperfcol}</b></td>" >>../index.html.tmp
|
||||
done
|
||||
echo "</tr>" >>../index.html.tmp
|
||||
egrep "^[0-9][0-9]" MARKET_PERFORMANCE.csv | tail -n10 | while read f_mperfline
|
||||
do
|
||||
f_mperfline="$(echo ${f_mperfline} | perl -pe 's/ /_/g; s/,/ /g')"
|
||||
echo "<tr>" >>../index.html.tmp
|
||||
#echo "${mperfline}" | perl -pe 's/,/\n/g' | while read f_mperfcol
|
||||
for f_mperfcol in ${f_mperfline}
|
||||
do
|
||||
if echo "${f_mperfcol}" | grep -q ":"
|
||||
then
|
||||
f_mperfcol="$(echo ${f_mperfcol} | perl -pe 's/_/ /')"
|
||||
echo "<td><b>${f_mperfcol}</b></td>" >>../index.html.tmp
|
||||
else
|
||||
echo "<td> ${f_mperfcol}%</td>" >>../index.html.tmp
|
||||
fi
|
||||
done
|
||||
echo "</tr>" >>../index.html.tmp
|
||||
f_html=$(basename $f_html)
|
||||
f_name=$(echo $f_html | cut -d- -f2,3 | cut -d. -f1)
|
||||
echo "<a href='${f_html}'>$f_name</a><br>" >>../index.html.tmp
|
||||
done
|
||||
echo "$(cat ALL_TRANSACTIONS_OVERVIEW_WARN.csv | cut -d, -f1,2,20)<br>" >>../index.html.tmp
|
||||
|
||||
echo "</table></details>" >>../index.html.tmp
|
||||
echo "<a href=\"botdata/MARKET_PERFORMANCE.csv\">Complete list</a>" >>../index.html.tmp
|
||||
|
||||
# THE END
|
||||
echo "</body></html>" >>../index.html.tmp
|
||||
|
||||
# color magic
|
||||
cat ../index.html.tmp | perl -pe 's/ (\-[0-9]+\.[0-9]+\%)/<font color=red>$1<\/font>/g; s/ ([0-9]+\.[0-9]+\%)/<font color=green>$1<\/font>/g;' >../index.html
|
||||
|
||||
@@ -27,7 +27,7 @@ function webpage_transactions {
|
||||
|
||||
|
||||
#echo -e "\n\n========== Total Results ==========="
|
||||
##echo "Trade Result: $f_trade_result EUR"
|
||||
#echo "Trade Result: $f_trade_result EUR"
|
||||
#echo "Staking Result: $f_staking_rewards EUR"
|
||||
#echo "Giveaway Result: $f_giveaway EUR"
|
||||
#echo -e "Instand Trade Bonus: $f_instant_trade_bonus EUR\n"
|
||||
@@ -37,15 +37,16 @@ function webpage_transactions {
|
||||
local f_tax_year
|
||||
cat ALL_TRANSACTIONS_OVERVIEW.csv | cut -d- -f1 | sort -u | while read f_tax_year
|
||||
do
|
||||
echo "========== Tax year $f_tax_year (German Tax Law) =========="
|
||||
#echo "========== Tax year $f_tax_year (German Tax Law) =========="
|
||||
local f_exchange_tax_type
|
||||
cat ALL_TRANSACTIONS_OVERVIEW.csv | grep "^$f_tax_year-" | cut -d, -f 2,13 | sort -u | egrep -v ',$' | grep -v "Note: " | while read f_exchange_tax_type
|
||||
do
|
||||
#echo "$f_exchange_tax_type"
|
||||
local f_exchange=$(echo $f_exchange_tax_type | cut -d, -f1)
|
||||
local f_tax_type=$(echo $f_exchange_tax_type | cut -d, -f2)
|
||||
|
||||
local f_tax=$(cat ALL_TRANSACTIONS_OVERVIEW.csv | grep "^$f_tax_year-" | cut -d, -f 2,13,14 | egrep -v ',,0$' | grep "$f_exchange_tax_type" | cut -d, -f3 | awk "{ SUM += \$1} END { printf(\"%.2f\", SUM) }")
|
||||
echo "$f_exchange_tax_type: $f_tax"
|
||||
#echo "$f_exchange_tax_type: $f_tax"
|
||||
|
||||
echo "$f_tax_type: $f_tax EUR<br>" >>${g_tmp}/tax_summary_$f_exchange-$f_tax_year
|
||||
|
||||
@@ -70,14 +71,14 @@ $(cat ${g_tmp}/tax_summary_$f_exchange-$f_tax_year)
|
||||
<tr><td>Date</td><td>Type of transaction</td><td>Crypto value</td><td>Fiat value</td><td>Result</td><td>Tax type</td><td>Tax amount</td></tr>
|
||||
" >TRANSACTIONS_OVERVIEW-${f_exchange}-${f_tax_year}.html.tmp
|
||||
|
||||
cat ALL_TRANSACTIONS_OVERVIEW.csv | grep "^${f_tax_year}-" | grep ",${f_exchange}," | awk -F, '
|
||||
{printf "<tr><td>"$1"</td><td>"$3"</td><td>"$5" "$4"</td><td>"}
|
||||
{printf("%.2f", $15)}
|
||||
{printf " EUR </td><td>"}
|
||||
{printf("%.2f", $17)}
|
||||
{printf " EUR</td><td>"$13"</td><td>"}
|
||||
{printf("%.2f", $14)}
|
||||
{print " EUR</td></tr>"}' >>TRANSACTIONS_OVERVIEW-${f_exchange}-${f_tax_year}.html.tmp
|
||||
cat ALL_TRANSACTIONS_OVERVIEW.csv | grep "^${f_tax_year}-" | grep ",${f_exchange}," | awk -F, '
|
||||
{
|
||||
printf "<tr><td>%s</td><td>%s</td><td>%s %s</td><td>%.2f EUR</td><td>", $1, $3, $5, $4, $15
|
||||
if ($17 != "") {
|
||||
printf "%.2f EUR", $17
|
||||
}
|
||||
printf "</td><td>%s</td><td>%.2f EUR</td></tr>\n", $13, $14
|
||||
}' >>TRANSACTIONS_OVERVIEW-${f_exchange}-${f_tax_year}.html.tmp
|
||||
|
||||
echo "</table></body></html>" >>TRANSACTIONS_OVERVIEW-${f_exchange}-${f_tax_year}.html.tmp
|
||||
mv TRANSACTIONS_OVERVIEW-${f_exchange}-${f_tax_year}.html.tmp ../TRANSACTIONS_OVERVIEW-${f_exchange}-${f_tax_year}.html
|
||||
@@ -85,34 +86,8 @@ $(cat ${g_tmp}/tax_summary_$f_exchange-$f_tax_year)
|
||||
|
||||
|
||||
done
|
||||
echo ""
|
||||
#echo ""
|
||||
done
|
||||
|
||||
## Overview over Overviews
|
||||
echo "<html>
|
||||
<head>
|
||||
<meta charset='UTF-8'>
|
||||
<meta http-equiv='refresh' content='${INTERVAL}'>
|
||||
<meta name='viewport' content='width=device-width, initial-scale=1'>
|
||||
<link rel='stylesheet' type='text/css' href='/browser.css'>
|
||||
<link rel='stylesheet' type='text/css' href='/charts.min.css'>
|
||||
<title>Trading Overview</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Transaction Overviews</h1>" >../TRANSACTIONS_OVERVIEWS.html
|
||||
|
||||
|
||||
local f_html f_name
|
||||
ls ../TRANSACTIONS_OVERVIEW-* | while read f_html
|
||||
do
|
||||
f_html=$(basename $f_html)
|
||||
f_name=$(echo $f_html | cut -d- -f2,3 | cut -d. -f1)
|
||||
echo "<a href='${f_html}'>$f_name</a><br>" >>../TRANSACTIONS_OVERVIEWS.html
|
||||
done
|
||||
echo "$(cat ALL_TRANSACTIONS_OVERVIEW_WARN.csv | cut -d, -f1,2,20)<br>" >>../TRANSACTIONS_OVERVIEWS.html
|
||||
|
||||
echo "</body></html>" >>../TRANSACTIONS_OVERVIEWS.html
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -86,63 +86,6 @@ a[id^="btn"] { background-color:#111111; cursor:pointer; display:inline-block; f
|
||||
a[id^="btn"]:last-child { margin-left:-4px; }
|
||||
a[id^="btn"]::-moz-focus-inner { border:0; padding:0; }
|
||||
|
||||
/* Chrome / Android / Tablet
|
||||
html[data-useragent*="Chrome"][data-useragent*="Android"] body { color:#373837; }
|
||||
html[data-useragent*="Chrome"][data-useragent*="Android"] audio { margin-left:4px; width:689px; }
|
||||
html[data-useragent*="Chrome"][data-useragent*="Android"] #audiowrap { background-color:#fafafa; }
|
||||
html[data-useragent*="Chrome"][data-useragent*="Android"] a[id^="btn"] { background-color:#fafafa; color:#373837; }
|
||||
html[data-useragent*="Chrome"][data-useragent*="Android"] a[id^="btn"]:hover { background-color:#eee; }
|
||||
html[data-useragent*="Chrome"][data-useragent*="Android"] #plList li { background-color:#fafafa; }
|
||||
html[data-useragent*="Chrome"][data-useragent*="Android"] #plList li:hover { background-color:#eee; }
|
||||
html[data-useragent*="Chrome"][data-useragent*="Android"] .plSel,
|
||||
html[data-useragent*="Chrome"][data-useragent*="Android"] .plSel:hover { background-color:#eee!important; }
|
||||
*/
|
||||
|
||||
/* Audio Player Media Queries
|
||||
================================================== */
|
||||
|
||||
/* Tablet Portrait
|
||||
@media only screen and (min-width: 768px) and (max-width: 959px) {
|
||||
audio { width:526px; }
|
||||
html[data-useragent*="MSIE 9.0"] audio { width:536px; }
|
||||
html[data-useragent*="MSIE 10.0"] audio { width:543px; }
|
||||
html[data-useragent*="rv:11.0"] audio { width:551px; }
|
||||
html[data-useragent*="OS 7"] audio { width:546px; }
|
||||
html[data-useragent*="OS 8"] audio { width:550px; }
|
||||
html[data-useragent*="OS 9"] audio { width:550px; }
|
||||
html[data-useragent*="Chrome"] audio { width:533px; }
|
||||
html[data-useragent*="Chrome"][data-useragent*="Android"] audio { margin-left:4px; width:545px; }
|
||||
}
|
||||
|
||||
/* Mobile Landscape
|
||||
@media only screen and (min-width: 480px) and (max-width: 767px) {
|
||||
audio { width:390px; }
|
||||
html[data-useragent*="MSIE 9.0"] audio { width:400px; }
|
||||
html[data-useragent*="MSIE 10.0"] audio { width:407px; }
|
||||
html[data-useragent*="rv:11.0"] audio { width:415px; }
|
||||
html[data-useragent*="OS 7"] audio { width:410px; }
|
||||
html[data-useragent*="OS 8"] audio { width:414px; }
|
||||
html[data-useragent*="OS 9"] audio { width:414px; }
|
||||
html[data-useragent*="Chrome"] audio { width:397px; }
|
||||
html[data-useragent*="Chrome"][data-useragent*="Mobile"] audio { margin-left:4px; width:410px; }
|
||||
#npTitle { width:245px; }
|
||||
}
|
||||
|
||||
/* Mobile Portrait
|
||||
@media only screen and (max-width: 479px) {
|
||||
audio { width:270px; }
|
||||
html[data-useragent*="MSIE 9.0"] audio { width:280px; }
|
||||
html[data-useragent*="MSIE 10.0"] audio { width:287px; }
|
||||
html[data-useragent*="rv:11.0"] audio { width:295px; }
|
||||
html[data-useragent*="OS 7"] audio { width:290px; }
|
||||
html[data-useragent*="OS 8"] audio { width:294px; }
|
||||
html[data-useragent*="OS 9"] audio { width:294px; }
|
||||
html[data-useragent*="Chrome"] audio { width:277px; }
|
||||
html[data-useragent*="Chrome"][data-useragent*="Mobile"] audio { margin-left:4px; width:290px; }
|
||||
#npTitle { width:167px; }
|
||||
}
|
||||
*/
|
||||
|
||||
audio { width:92%; }
|
||||
|
||||
/* for VPN Login */
|
||||
@@ -173,12 +116,20 @@ pre,#footer {
|
||||
font:16px sans-serif;
|
||||
}
|
||||
|
||||
.headline {
|
||||
font-size:18px;
|
||||
background-color: #808080;
|
||||
font-weight: bold;
|
||||
color: black;
|
||||
}
|
||||
|
||||
.statusok {
|
||||
font:18px sans-serif;
|
||||
background-color: #00BF40;
|
||||
color: black;
|
||||
}
|
||||
|
||||
|
||||
.statusnok {
|
||||
font:18px sans-serif;
|
||||
background-color: #E10020;
|
||||
|
||||
@@ -82,7 +82,7 @@ const chart = LightweightCharts.createChart(document.getElementById('container')
|
||||
type: 'solid',
|
||||
color: '#222',
|
||||
},
|
||||
textColor: '#DDD',
|
||||
textColor: '#DDD',
|
||||
},
|
||||
|
||||
grid: {
|
||||
|
||||
@@ -166,7 +166,7 @@ services:
|
||||
resources:
|
||||
limits:
|
||||
cpus: '1'
|
||||
memory: 1024M
|
||||
memory: 512M
|
||||
|
||||
dabo-calc-levels:
|
||||
build:
|
||||
@@ -235,6 +235,30 @@ services:
|
||||
cpus: '0.5'
|
||||
memory: 512M
|
||||
|
||||
dabo-fetch-coinmarketcapids:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
restart: unless-stopped
|
||||
user: 10000:10000
|
||||
volumes:
|
||||
- ./dabo:/dabo:ro
|
||||
- ./strategies:/dabo/strategies:ro
|
||||
- ./dabo-bot.conf:/dabo/dabo-bot.override.conf
|
||||
# - ./watch-assets.csv:/dabo/watch-assets.csv
|
||||
- ./data:/dabo/htdocs:rw
|
||||
- ./home:/dabo/home:rw
|
||||
- ./notify.local.conf:/usr/local/etc/notify.conf:ro
|
||||
- /etc/localtime:/etc/localtime:ro
|
||||
entrypoint: /dabo/fetch-coinmarketcapids.sh
|
||||
cpu_shares: 128
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: '0.5'
|
||||
memory: 512M
|
||||
|
||||
|
||||
# dabo-test:
|
||||
# build:
|
||||
# context: .
|
||||
|
||||
@@ -1,56 +0,0 @@
|
||||
|
||||
# remove this line if you are fine with this settings and want to use them on your own risk!!!
|
||||
# It is strongly recommended to test this strategy first via analyze.sh!!!
|
||||
return 1
|
||||
|
||||
|
||||
# GOOD_MARKET_PERFORMANCE_INDEX defines from which growth the market is considered good/favorable for investment.
|
||||
# The market performance is calculated from the average percentage development of various indicators such as development MSCI World, Bitcoin and Ethereum as well as forecasts.
|
||||
# for details see functions/market_performance.sh
|
||||
# If the market performance is under this value no buying will be done
|
||||
local GOOD_MARKET_PERFORMANCE_INDEX="100"
|
||||
|
||||
###### BUY CONDITIONS ######
|
||||
|
||||
### RSI Indicator checks
|
||||
# Don't buy if the RSI-XX value is >= BUY_RSIXX_BUY_SIGNAL_UNTIL
|
||||
local BUY_RSI5_SIGNAL_UNTIL="0"
|
||||
local BUY_RSI14_SIGNAL_UNTIL="0"
|
||||
local BUY_RSI21_SIGNAL_UNTIL="0"
|
||||
local BUY_RSI60_SIGNAL_UNTIL="0"
|
||||
local BUY_RSI120_SIGNAL_UNTIL="0"
|
||||
local BUY_RSI240_SIGNAL_UNTIL="0"
|
||||
local BUY_RSI420_SIGNAL_UNTIL="0"
|
||||
local BUY_RSI720_SIGNAL_UNTIL="0"
|
||||
|
||||
# Don't buy if the RSI-XX value is <= BUY_RSIXX_BUY_SIGNAL_FROM
|
||||
local BUY_RSI5_SIGNAL_FROM="0"
|
||||
local BUY_RSI14_SIGNAL_FROM="0"
|
||||
local BUY_RSI21_SIGNAL_FROM="0"
|
||||
local BUY_RSI60_SIGNAL_FROM="0"
|
||||
local BUY_RSI120_SIGNAL_FROM="0"
|
||||
local BUY_RSI240_SIGNAL_FROM="0"
|
||||
local BUY_RSI420_SIGNAL_FROM="0"
|
||||
local BUY_RSI720_SIGNAL_FROM="0"
|
||||
|
||||
|
||||
### MACD Indicator Checks
|
||||
# Don't buy if MACD Histogram Relation is not between this values.
|
||||
# The ratio is calculated by the difference between the maximum positive Histogram value (of the defined time period) and the current value.
|
||||
# Here you can specify a percentage range.
|
||||
# If only the buy signal should be considered, simply specify the range 0 and 100.
|
||||
# decimal numbers are not allowed here.
|
||||
local BUY_MACD_RELATION_FROM="0"
|
||||
local BUY_MACD_RELATION_TO="0"
|
||||
|
||||
# Don't buy if price change is under this percentage values
|
||||
local BUY_MIN_PRICE_CHANGE_LAST_1_DAY="0.25"
|
||||
local BUY_MIN_PRICE_CHANGE_LAST_7_DAY="-1"
|
||||
local BUY_MIN_PRICE_CHANGE_LAST_14_DAY="-2"
|
||||
local BUY_MIN_PRICE_CHANGE_LAST_30_DAY="-5"
|
||||
|
||||
|
||||
# Dont buy if growth in the last defined time period <= BUY_MINGROWTH
|
||||
local BUY_MINGROWTH_PERIOD="30"
|
||||
local BUY_MINGROWTH="-10"
|
||||
|
||||
272
strategies/example.strategy.sh
Normal file
272
strategies/example.strategy.sh
Normal file
@@ -0,0 +1,272 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Copyright (c) 2022-2024 olli
|
||||
#
|
||||
# This file is part of dabo (crypto bot).
|
||||
#
|
||||
# dabo is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# dabo is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with dabo. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
# Example strategy
|
||||
|
||||
g_echo_note "EXAMPLE Strategy"
|
||||
|
||||
##### WARNING! This strategy is only intended as an example and should not be used with real trades. Please develop your own strategy ######
|
||||
|
||||
# if you want to use this remove the next line with return 0
|
||||
return 0
|
||||
|
||||
# get vars with orders and positions
|
||||
get_position_array
|
||||
get_orders_array
|
||||
|
||||
# reset score
|
||||
unset s_score
|
||||
unset s_score_hist
|
||||
s_score=0
|
||||
|
||||
|
||||
### BEGIN market scoring ###
|
||||
# correlation to crypto
|
||||
for asset in DOWJONES SP500 NASDAQ MSCIEAFE GOLD MSCIWORLD KRE
|
||||
do
|
||||
|
||||
g_echo "scoring ECONOMY_${asset}"
|
||||
|
||||
# bullish? bull market?
|
||||
[ -n "${v[ECONOMY_${asset}_1d_ema200_0]}" ] && if g_num_is_higher ${v[ECONOMY_${asset}_15m_close_0]} ${v[ECONOMY_${asset}_1d_ema200_0]}
|
||||
then
|
||||
score 2 "${asset} EMA200 over last 15m close"
|
||||
else
|
||||
score -2 "${asset} EMA200 under last 15m close"
|
||||
fi
|
||||
|
||||
# RSI14 1d
|
||||
if [ -n "${v[ECONOMY_${asset}_1d_rsi14_0]}" ]
|
||||
then
|
||||
rsi14=${v[ECONOMY_${asset}_1d_rsi14_0]}
|
||||
g_num_is_between $rsi14 80 100 && score -2 "${asset} RSI14 $rsi14"
|
||||
g_num_is_between $rsi14 55 80 && score 2 "${asset} RSI14 $rsi14"
|
||||
g_num_is_between $rsi14 15 45 && score 1 "${asset} RSI14 $rsi14"
|
||||
g_num_is_between $rsi14 0 15 && score -2 "${asset} RSI14 $rsi14"
|
||||
fi
|
||||
|
||||
# macd trend
|
||||
[[ ${v[ECONOMY_${asset}_1d_macd_histogram_signal_0]} = uptrend ]] && score 2 "${asset} MACD uptrend"
|
||||
[[ ${v[ECONOMY_${asset}_1d_macd_histogram_signal_0]} = downtrend ]] && score -2 "${asset} MACD downtrend"
|
||||
|
||||
done
|
||||
|
||||
# inverse correlation to crypto
|
||||
for asset in DXY OILGAS
|
||||
do
|
||||
|
||||
g_echo "scoring ECONOMY_${asset}"
|
||||
|
||||
# bullish? bull market?
|
||||
[ -n "${v[ECONOMY_${asset}_1d_ema200_0]}" ] && if g_num_is_higher ${v[ECONOMY_${asset}_15m_close_0]} ${v[ECONOMY_${asset}_1d_ema200_0]}
|
||||
then
|
||||
score -2 "${asset} EMA200 over last 15m close"
|
||||
else
|
||||
score 2 "${asset} EMA200 under last 15m close"
|
||||
fi
|
||||
|
||||
# RSI14 1d
|
||||
if [ -n "${v[ECONOMY_${asset}_1d_rsi14_0]}" ]
|
||||
then
|
||||
rsi14=${v[ECONOMY_${asset}_1d_rsi14_0]}
|
||||
g_num_is_between $rsi14 80 100 && score 2 "${asset} RSI14 $rsi14"
|
||||
g_num_is_between $rsi14 55 80 && score -2 "${asset} RSI14 $rsi14"
|
||||
g_num_is_between $rsi14 15 45 && score -1 "${asset} RSI14 $rsi14"
|
||||
g_num_is_between $rsi14 0 15 && score 2 "${asset} RSI14 $rsi14"
|
||||
fi
|
||||
|
||||
# macd trend
|
||||
[[ ${v[ECONOMY_${asset}_1d_macd_histogram_signal_0]} = uptrend ]] && score -2 "${asset} MACD uptrend"
|
||||
[[ ${v[ECONOMY_${asset}_1d_macd_histogram_signal_0]} = downtrend ]] && score 2 "${asset} MACD downtrend"
|
||||
|
||||
done
|
||||
|
||||
### Evaluate BTC and ETH
|
||||
for asset in BTC${CURRENCY} ETH${CURRENCY}
|
||||
do
|
||||
|
||||
g_echo "scoring ${asset}"
|
||||
|
||||
# bullish? bull market?
|
||||
[ -n "${v[${asset}_1d_ema200_0]}" ] && if g_num_is_higher ${v[${asset}_15m_close_0]} ${v[${asset}_1d_ema200_0]}
|
||||
then
|
||||
score 2 "${asset} EMA200 over last 15m close"
|
||||
else
|
||||
score -2 "${asset} EMA200 under last 15m close"
|
||||
fi
|
||||
|
||||
# RSI14 1d
|
||||
if [ -n "${v[${asset}_1d_rsi14_0]}" ]
|
||||
then
|
||||
rsi14=${v[${asset}_1d_rsi14_0]}
|
||||
g_num_is_between $rsi14 80 100 && score -2 "${asset} RSI14 $rsi14"
|
||||
g_num_is_between $rsi14 55 80 && score 2 "${asset} RSI14 $rsi14"
|
||||
g_num_is_between $rsi14 15 45 && score 1 "${asset} RSI14 $rsi14"
|
||||
g_num_is_between $rsi14 0 15 && score -2 "${asset} RSI14 $rsi14"
|
||||
fi
|
||||
|
||||
# macd trend
|
||||
[[ ${v[${asset}_1d_macd_histogram_signal_0]} = uptrend ]] && score 2 "${asset} MACD uptrend"
|
||||
[[ ${v[${asset}_1d_macd_histogram_signal_0]} = downtrend ]] && score -2 "${asset} MACD downtrend"
|
||||
|
||||
done
|
||||
|
||||
### END market scoring ###
|
||||
|
||||
# save score until here
|
||||
market_score=$s_score
|
||||
market_score_hist=$s_score_hist
|
||||
|
||||
### Go through trading symbols
|
||||
for symbol in ${f_symbols_array_trade[@]}
|
||||
do
|
||||
|
||||
# restore market score as base
|
||||
s_score=$market_score
|
||||
s_score_hist=$market_score_hist
|
||||
|
||||
asset=${symbol//:$CURRENCY/}
|
||||
asset=${asset//\//}
|
||||
|
||||
### Evaluate symbol
|
||||
g_echo "scoring ${asset}"
|
||||
|
||||
# bullish? bull market?
|
||||
[ -n "${v[${asset}_1d_ema200_0]}" ] && if g_num_is_higher ${v[${asset}_15m_close_0]} ${v[${asset}_1d_ema200_0]}
|
||||
then
|
||||
score 2 "${asset} EMA200 over last 15m close"
|
||||
else
|
||||
score -2 "${asset} EMA200 under last 15m close"
|
||||
fi
|
||||
|
||||
# RSI14 5m
|
||||
if [ -n "${v[${asset}_5m_rsi14_0]}" ]
|
||||
then
|
||||
rsi14=${v[${asset}_5m_rsi14_0]}
|
||||
g_num_is_between $rsi14 80 100 && score -4 "$asset RSI14 $rsi14"
|
||||
g_num_is_between $rsi14 55 80 && score 4 "$asset RSI14 $rsi14"
|
||||
g_num_is_between $rsi14 15 45 && score 2 "$asset RSI14 $rsi14"
|
||||
g_num_is_between $rsi14 0 15 && score -4 "$asset RSI14 $rsi14"
|
||||
fi
|
||||
|
||||
# macd trend
|
||||
[[ ${v[${asset}_5m_macd_histogram_signal_0]} = uptrend ]] && score 2 "$asset MACD uptrend"
|
||||
[[ ${v[${asset}_5m_macd_histogram_signal_0]} = downtrend ]] && score -2 "$asset MACD downtrend"
|
||||
|
||||
|
||||
# go short or go long or better do notghing?
|
||||
side="unclear"
|
||||
g_num_is_higher $s_score 5 && side="long"
|
||||
g_num_is_lower $s_score -5 && side="short"
|
||||
|
||||
g_echo_ok "Score: $s_score"
|
||||
g_echo_ok "Side: $side"
|
||||
g_echo "Scores: $s_score_hist"
|
||||
|
||||
# remove existing orders and do nothing if unclear
|
||||
if [[ $side = "unclear" ]]
|
||||
then
|
||||
g_echo "Situation of $asset unclear - remove existing orders and do nothing!"
|
||||
order_cancel "$symbol"
|
||||
continue
|
||||
fi
|
||||
|
||||
# if no contract trading / no shot possible ignore
|
||||
if [ -z "$LEVERAGE" ] && [[ $side = short ]]
|
||||
then
|
||||
g_echo "No 'short' possible while sport trading - doung nothing with $asset"
|
||||
continue
|
||||
fi
|
||||
|
||||
# Next week level is:
|
||||
g_echo "level_1w_next_up: ${v[${asset}_levels_1w_next_up]}"
|
||||
g_echo "level_1w_next_down: ${v[${asset}_levels_1w_next_down]}"
|
||||
|
||||
# define entry price
|
||||
unset entry_price
|
||||
[[ $side = long ]] && entry_price=${v[${asset}_levels_1w_next_down]}
|
||||
[[ $side = short ]] && entry_price=${v[${asset}_levels_1w_next_up]}
|
||||
|
||||
# if entry price not given set to current price
|
||||
[ -z "$entry_price" ] && entry_price=${v[${asset}_price]}
|
||||
|
||||
# check for updates if order with entry price is already defined
|
||||
if [ -n "${o[${asset}_open_${side}_entry_price]}" ]
|
||||
then
|
||||
if g_num_is_approx ${o[${asset}_open_${side}_entry_price]} $entry_price 0.5 0.5
|
||||
then
|
||||
g_echo "Order of $asset at ${o[${asset}_open_long_entry_price]} fine"
|
||||
else
|
||||
# cancelling order
|
||||
g_echo_warn "Cancelling order because entry price not seems to be up2date anymore: ${o[${asset}_open_${side}_entry_price]} != $entry_price"
|
||||
order_cancel "$symbol"
|
||||
fi
|
||||
fi
|
||||
|
||||
# check for already existing order
|
||||
if [ -n "${o[${asset}_present]}" ]
|
||||
then
|
||||
g_echo_ok "Order(s) of ${asset} already exists"
|
||||
continue
|
||||
fi
|
||||
|
||||
# check for already existing position
|
||||
if [ -n "${p[${asset}_currency_amount]}" ]
|
||||
then
|
||||
g_echo "Position of ${asset} already open"
|
||||
continue
|
||||
fi
|
||||
|
||||
# StopLoss at 20% loss
|
||||
stoplosspercentage=20
|
||||
# calc percentage loss if leverage is set
|
||||
if [ -n "$LEVERAGE" ]
|
||||
then
|
||||
g_calc "$stoplosspercentage/$LEVERAGE"
|
||||
stoplosspercentage=$g_calc_result
|
||||
fi
|
||||
# calc stoploss
|
||||
[[ $side = long ]] && g_calc "$entry_price-($entry_price/100*${stoplosspercentage})"
|
||||
[[ $side = short ]] && g_calc "$entry_price+($entry_price/100*${stoplosspercentage})"
|
||||
stoploss=$g_calc_result
|
||||
|
||||
# TakeProfit at 2% profit
|
||||
takeprofitpercentage=5
|
||||
if [ -n "$LEVERAGE" ]
|
||||
then
|
||||
g_calc "$takeprofitpercentage/$LEVERAGE"
|
||||
takeprofitpercentage=$g_calc_result
|
||||
fi
|
||||
[[ $side = long ]] && g_calc "$entry_price+($entry_price/100*${takeprofitpercentage})"
|
||||
[[ $side = short ]] && g_calc "$entry_price-($entry_price/100*${takeprofitpercentage})"
|
||||
takeprofit=$g_calc_result
|
||||
|
||||
# Use 100 of balace for trade
|
||||
trade_balance=100
|
||||
# altarnative calculate 2 percentage of available balace for trade
|
||||
#get_balance
|
||||
#g_calc "$f_CURRENCY_BALANCE/100*2"
|
||||
#trade_balance=$g_calc_result
|
||||
|
||||
# place the order
|
||||
order "$symbol" "$trade_balance" "$side" "$entry_price" "$stoploss" "$takeprofit"
|
||||
|
||||
done
|
||||
|
||||
87
strategies/example_manage_positions.strategy.sh
Normal file
87
strategies/example_manage_positions.strategy.sh
Normal file
@@ -0,0 +1,87 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Copyright (c) 2022-2024 olli
|
||||
#
|
||||
# This file is part of dabo (crypto bot).
|
||||
#
|
||||
# dabo is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# dabo is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with dabo. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
# Example strategy for managing open positions
|
||||
|
||||
g_echo_note "EXAMPLE Strategy for managing open positions"
|
||||
|
||||
##### WARNING! This strategy is only intended as an example and should not be used with real trades. Please develop your own strategy ######
|
||||
|
||||
# if you want to use this remove the next line with return 0
|
||||
return 0
|
||||
|
||||
# get vars with positions
|
||||
get_position_array
|
||||
|
||||
# go through trading symbols
|
||||
for symbol in ${f_symbols_array_trade[@]}
|
||||
do
|
||||
asset=${symbol//:$CURRENCY/}
|
||||
asset=${asset//\//}
|
||||
|
||||
# adjust stoploss from percentage profit
|
||||
from_profit=0.5
|
||||
if [ -n "$LEVERAGE" ]
|
||||
then
|
||||
g_calc "${from_profit}*${LEVERAGE}"
|
||||
from_profit=$g_calc_result
|
||||
fi
|
||||
|
||||
# save profit by switching stoploss in profit
|
||||
if [ -n "${p[${asset}_pnl]}" ]
|
||||
then
|
||||
|
||||
# what side are we on (long or short)
|
||||
side=${p[${asset}_side]}
|
||||
|
||||
g_echo_note "Checking open $side position for $f_asset"
|
||||
|
||||
if g_num_is_higher ${p[${asset}_pnl_percentage]} $from_profit
|
||||
then
|
||||
# calculate stoploss price with half of current pnl
|
||||
g_calc "${p[${asset}_current_price]}-((${p[${asset}_current_price]}-${p[${asset}_entry_price]})/2)"
|
||||
stoploss_price=$g_calc_result
|
||||
|
||||
# check for already existing stoploss
|
||||
if [ -n "${o[${asset}_sl_close_${side}_id]}" ]
|
||||
then
|
||||
|
||||
# do nothing if current stoploss price is already larger/equal then half of current pnl
|
||||
if [[ $side = long ]]
|
||||
then
|
||||
g_num_is_higher_equal ${o[${asset}_sl_close_${side}_stopprice]} $stoploss_price && continue
|
||||
fi
|
||||
if [[ $side = short ]]
|
||||
then
|
||||
g_num_is_lower_equal ${o[${asset}_sl_close_${side}_stopprice]} $stoploss_price && continue
|
||||
fi
|
||||
|
||||
# cancel existing stoploss order
|
||||
order_cancel_id "$symbol" "${o[${asset}_sl_close_${side}_id]}" || continue
|
||||
fi
|
||||
|
||||
# create new stoploss
|
||||
g_echo_ok "==== New StopLoss in profit for $asset at $stoploss_price"
|
||||
order "$symbol" "asset_amount:${p[${asset}_asset_amount]}" ${side} stoploss "$stoploss_price"
|
||||
|
||||
fi
|
||||
fi
|
||||
|
||||
done
|
||||
|
||||
@@ -1,65 +0,0 @@
|
||||
# remove this line if you are fine with this settings and want to use them on your own risk!!!
|
||||
# It is strongly recommended to test this strategy first via analyze.sh!!!
|
||||
return 1
|
||||
|
||||
|
||||
# GOOD_MARKET_PERFORMANCE_INDEX defines from which growth the market is considered good/favorable for investment.
|
||||
# The market performance is calculated from the average percentage development of various indicators such as development MSCI World, Bitcoin and Ethereum as well as forecasts.
|
||||
# for details see functions/market_performance.sh
|
||||
# If the market performance is under this value no buying will be done
|
||||
local GOOD_MARKET_PERFORMANCE_INDEX="2"
|
||||
|
||||
# Force hold if result negative expect SELL_PERCENTAGE_FROM_LAST_PURCHASE_NEGATIVE. Boolean 1 for true 0 for false.
|
||||
local SELL_HOLD_IF_RESULT_NEGATIVE="1"
|
||||
|
||||
# If the price falls by this percentage value from the purchase price, then sell in any case
|
||||
local SELL_PERCENTAGE_FROM_LAST_PURCHASE_NEGATIVE="-10"
|
||||
|
||||
# from here only if SELL_HOLD_IF_RESULT_NEGATIVE doesn't match
|
||||
|
||||
# If the price falls by this percentage value from the last rate
|
||||
local SELL_IF_LAST_RATE_LOWER_THEN="-0.25"
|
||||
|
||||
### RSI Indicator checks
|
||||
|
||||
# SELL if the RSI-XX value is <= SELL_RSIXX_SELL_SIGNAL_UNTIL
|
||||
local SELL_RSI5_SIGNAL_UNTIL="99"
|
||||
local SELL_RSI14_SIGNAL_UNTIL="99"
|
||||
local SELL_RSI21_SIGNAL_UNTIL="99"
|
||||
local SELL_RSI60_SIGNAL_UNTIL="99"
|
||||
local SELL_RSI120_SIGNAL_UNTIL="99"
|
||||
local SELL_RSI240_SIGNAL_UNTIL="99"
|
||||
local SELL_RSI420_SIGNAL_UNTIL="99"
|
||||
local SELL_RSI720_SIGNAL_UNTIL="99"
|
||||
|
||||
# SELL if the RSI-XX value is >= SELL_RSIXX_SELL_SIGNAL_FROM
|
||||
local SELL_RSI5_SIGNAL_FROM="90"
|
||||
local SELL_RSI14_SIGNAL_FROM="90"
|
||||
local SELL_RSI21_SIGNAL_FROM="90"
|
||||
local SELL_RSI60_SIGNAL_FROM="70"
|
||||
local SELL_RSI120_SIGNAL_FROM="50"
|
||||
local SELL_RSI240_SIGNAL_FROM="50"
|
||||
local SELL_RSI420_SIGNAL_FROM="50"
|
||||
local SELL_RSI720_SIGNAL_FROM="50"
|
||||
|
||||
# If the price after this time period is lower the the trading fee, then sell if SELL_HOLD_IF_RESULT_NEGATIVE is fine with it
|
||||
local SELL_IF_LOWER_THEN_FEE_AFTER_PERIOD="7200"
|
||||
|
||||
### MACD Indicator Checks
|
||||
# Sell if MACD Histogram relation is < this value
|
||||
# The ratio is calculated by the difference between the maximum negative Histogram value (of the defined time period) and the current value.
|
||||
# Here you can specify a percentage range.
|
||||
# If only the sell signal should be considered, simply specify 0
|
||||
# decimal numbers are not allowed here.
|
||||
local SELL_MACD_RELATION_FROM="25"
|
||||
|
||||
### Take profit/loss
|
||||
local TAKE_PROFIT_CHECK_AFTER_POSTITIVE_RESULTS="1"
|
||||
local TAKE_PROFIT_CHECK_AT_FEE_PLUS="0.5"
|
||||
local TAKE_PROFIT_CHECK_AT_FACTOR="1.25"
|
||||
|
||||
local TAKE_LOSS_CHECK_AFTER_NEGATIVE_RESULTS="5"
|
||||
local TAKE_LOSS_CHECK_AT_FEE_PLUS="0"
|
||||
local TAKE_LOSS_CHECK_AT_FACTOR="2"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user