freqtrade in new homelab

This commit is contained in:
Luigi Oliveira 2024-11-24 20:40:28 -03:00
parent f2b7bd4c74
commit 690b2b01c4
11 changed files with 370 additions and 36 deletions

37
.gitignore vendored
View file

@ -1 +1,36 @@
.vscode/
.vscode/
# Local .terraform directories
**/.terraform/*
# .tfstate files
*.tfstate
*.tfstate.*
.terraform.lock.hcl
# Crash log files
crash.log
crash.*.log
# Exclude all .tfvars files, which are likely to contain sensitive data, such as
# password, private keys, and other secrets. These should not be part of version
# control as they are data points which are potentially sensitive and subject
# to change depending on the environment.
*.tfvars
*.tfvars.json
# Ignore override files as they are usually used to override resources locally and so
# are not checked in
override.tf
override.tf.json
*_override.tf
*_override.tf.json
# Include override files you do wish to add to version control using negated pattern
# !example_override.tf
# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
# example: *tfplan*
# Ignore CLI configuration files
.terraformrc
terraform.rc

View file

@ -42,4 +42,11 @@ Right now I'm using redis for nothing.
My instnace of [Twitch-Channel-Points-Miner-v2](https://github.com/rdavydov/Twitch-Channel-Points-Miner-v2)
# What I host in hetzner?
At hetzner I "share" some personal services with my friends projetcs. But the basic infrascructure is running proxmox with NAT for an instance of pfSense for router. A traefik in a LXC for reverse proxy. And a nomad server with some jobs for running containers.
At hetzner I "share" some personal services with my friends projetcs. But the basic infrascructure is running proxmox with NAT for an instance of pfSense for router. A traefik in a LXC for reverse proxy. And a nomad server with some jobs for running containers.
## Setup terraform:
```
read -s PGPASSWORD
export PGPASSWORD
```

View file

@ -1,34 +0,0 @@
# Local .terraform directories
**/.terraform/*
# .tfstate files
*.tfstate
*.tfstate.*
.terraform.lock.hcl
# Crash log files
crash.log
crash.*.log
# Exclude all .tfvars files, which are likely to contain sensitive data, such as
# password, private keys, and other secrets. These should not be part of version
# control as they are data points which are potentially sensitive and subject
# to change depending on the environment.
*.tfvars
*.tfvars.json
# Ignore override files as they are usually used to override resources locally and so
# are not checked in
override.tf
override.tf.json
*_override.tf
*_override.tf.json
# Include override files you do wish to add to version control using negated pattern
# !example_override.tf
# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
# example: *tfplan*
# Ignore CLI configuration files
.terraformrc
terraform.rc

14
terraform-lab/main.tf Normal file
View file

@ -0,0 +1,14 @@
module "freqtrade" {
source = "./modules/freqtrade"
providers = {
nomad = nomad
}
exchange_key = var.exchange_key
exchange_secret = var.exchange_secret
exchange_password = var.exchange_password
telegram_token = var.telegram_token
telegram_chat_id = var.telegram_chat_id
jwt_secret_key = var.jwt_secret_key
username = var.username
password = var.password
}

View file

@ -0,0 +1,76 @@
{
"$schema": "https://schema.freqtrade.io/schema.json",
"max_open_trades": 3,
"stake_currency": "USDT",
"stake_amount": "unlimited",
"tradable_balance_ratio": 0.99,
"fiat_display_currency": "USD",
"dry_run": true,
"dry_run_wallet": 1000,
"cancel_open_orders_on_exit": false,
"trading_mode": "spot",
"margin_mode": "",
"unfilledtimeout": {
"entry": 10,
"exit": 10,
"exit_timeout_count": 0,
"unit": "minutes"
},
"entry_pricing": {
"price_side": "same",
"use_order_book": true,
"order_book_top": 1,
"price_last_balance": 0.0,
"check_depth_of_market": {
"enabled": false,
"bids_to_ask_delta": 1
}
},
"exit_pricing":{
"price_side": "same",
"use_order_book": true,
"order_book_top": 1
},
"exchange": {
"name": "kucoin",
"key": "${exchange_key}",
"secret": "${exchange_secret}",
"password": "${exchange_password}",
"ccxt_config": {},
"ccxt_async_config": {},
"pair_whitelist": [],
"pair_blacklist": []
},
"pairlists": [
{
"method": "VolumePairList",
"number_assets": 20,
"sort_key": "quoteVolume",
"min_value": 0,
"refresh_period": 1800
}
],
"telegram": {
"enabled": false,
"token": "${telegram_token}",
"chat_id": "${telegram_chat_id}"
},
"api_server": {
"enabled": true,
"listen_ip_address": "0.0.0.0",
"listen_port": 8080,
"verbosity": "error",
"enable_openapi": false,
"jwt_secret_key": "${jwt_secret_key}",
"ws_token": "gp5SbWEjm4IMSU6uKKw7tVtcWt6tgWJsug",
"CORS_origins": [],
"username": "${username}",
"password": "${password}"
},
"bot_name": "freqtrade",
"initial_state": "running",
"force_entry_enable": false,
"internals": {
"process_throttle_secs": 5
}
}

View file

@ -0,0 +1,88 @@
job "freqtrade" {
datacenters = ["marioverde"]
type = "service"
group "freqtrade" {
count = 1
network {
port "api" {
static = 9696
to = 8080
}
}
restart {
attempts = 3
interval = "2m"
delay = "30s"
mode = "delay"
}
ephemeral_disk {
migrate = true
sticky = true
size = 500
}
task "freqtrade" {
driver = "podman"
config {
image = "docker.io/freqtradeorg/freqtrade:stable"
ports = ["api"]
volumes = [
"${NOMAD_ALLOC_DIR}:/freqtrade/user_data/"
]
args = [
"trade",
"--logfile", "/freqtrade/user_data/logs/freqtrade.log",
"--db-url", "sqlite:////freqtrade/user_data/tradesv3.sqlite",
"--config", "/freqtrade/user_data/config.json",
"--strategy", "MyStrategy"
]
}
template {
data = <<EOF
${CONFIG_JSON}
EOF
destination = "${NOMAD_ALLOC_DIR}/config.json"
change_mode = "restart"
}
template {
data = <<EOF
${STRATEGY_NAME}
EOF
destination = "${NOMAD_ALLOC_DIR}/strategies/strategy.py"
change_mode = "restart"
}
resources {
cpu = 1000
memory = 2024
}
service {
name = "freqtrade"
port = "api"
provider = "nomad"
check {
type = "http"
path = "/api/v1/ping"
interval = "10s"
timeout = "2s"
}
}
logs {
max_files = 5
max_file_size = 15
}
}
}
}

View file

@ -0,0 +1,33 @@
from freqtrade.strategy import IStrategy
from pandas import DataFrame
import talib.abstract as ta
class MyStrategy(IStrategy):
# set the initial stoploss to -10%
stoploss = -0.10
# exit profitable positions at any time when the profit is greater than 1%
minimal_roi = {"0": 0.01}
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
# generate values for technical analysis indicators
dataframe['rsi'] = ta.RSI(dataframe, timeperiod=14)
return dataframe
def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
# generate entry signals based on indicator values
dataframe.loc[
(dataframe['rsi'] < 30),
'enter_long'] = 1
return dataframe
def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
# generate exit signals based on indicator values
dataframe.loc[
(dataframe['rsi'] > 70),
'exit_long'] = 1
return dataframe

View file

@ -0,0 +1,46 @@
resource "nomad_job" "app" {
jobspec = templatefile("${path.module}/conf/freqtrade.hcl", {
NOMAD_ALLOC_DIR = "./private",
CONFIG_JSON = templatefile("${path.module}/conf/config.json.tpl", {
exchange_key = var.exchange_key,
exchange_secret = var.exchange_secret,
exchange_password = var.exchange_password,
telegram_token = var.telegram_token,
telegram_chat_id = var.telegram_chat_id
})
STRATEGY_NAME = file("${path.module}/conf/strategy.py")
})
}
variable "exchange_key" {
type = string
}
variable "exchange_secret" {
type = string
}
variable "exchange_password" {
type = string
}
variable "telegram_token" {
type = string
}
variable "telegram_chat_id" {
type = string
}
variable "jwt_secret_key" {
type = string
}
variable "username" {
type = string
}
variable "password" {
type = string
}

View file

@ -0,0 +1,9 @@
terraform {
required_providers {
nomad = {
source = "hashicorp/nomad"
version = "2.4.0"
}
}
required_version = ">= 0.14"
}

View file

@ -0,0 +1,29 @@
terraform {
required_providers {
nomad = {
source = "hashicorp/nomad"
version = "2.4.0"
}
}
backend "pg" {
conn_str = "postgres://postgres@100.64.0.7:5432/terraform_backend"
#conn_str = "postgres://postgres@192.168.5.4:5432/terraform_backend"
}
required_version = ">= 0.14"
}
/*
VOCE PRECISA FAZER UM TUNEL PRA ACESSAR O SQL DO LAB. E TESTAR O INIT DO TERRAFORM. BOLAR UM JEITO DE SEMPRE RODAR SAPORRA DE TF DO LAB
PASSWORD:
read -s PGPASSWORD
export PGPASSWORD
*/
# Configure the Nomad provider.
provider "nomad" {
address = "http://100.64.0.5:4646"
}

View file

@ -0,0 +1,31 @@
variable "exchange_key" {
type = string
}
variable "exchange_secret" {
type = string
}
variable "exchange_password" {
type = string
}
variable "telegram_token" {
type = string
}
variable "telegram_chat_id" {
type = string
}
variable "jwt_secret_key" {
type = string
}
variable "username" {
type = string
}
variable "password" {
type = string
}