Skip to content

Instantly share code, notes, and snippets.

View xescuder's full-sized avatar
💻
Making software more reliable

xescuder

💻
Making software more reliable
View GitHub Profile
@xescuder
xescuder / date_utils.py
Last active February 13, 2023 20:32
Python date utils for building ranges and periods
from datetime import date, datetime, timedelta
from dateutil.relativedelta import relativedelta
def build_formatted_range(interval: str, range_start: date | datetime = None, range_end: date | datetime = None,
period: str = None):
new_start = range_start
new_end = range_end
if range_end is None:
@xescuder
xescuder / docker-compose.yml
Last active March 3, 2023 09:35
Wordpress Docker Compose
version: "3"
services:
db:
image: mysql:5.7
volumes:
- ./wp-data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: MyR00tMySQLPa$$w0rD
MYSQL_DATABASE: MyWordPressDatabaseName
@xescuder
xescuder / config.yaml
Created March 19, 2023 23:56
Dependency injector logging configuration
core:
i18n:
language: 'es'
logging:
version: 1
formatters:
standard:
format: "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
error:
format: "%(asctime)s - %(name)s - %(levelname)s <PID %(process)d:%(processName)s>\
@xescuder
xescuder / orm.py
Created March 23, 2023 22:03
Exchanges, indices and stocks imperative mapping
import logging
from sqlalchemy import (Table, Column, Integer, Enum, Float, String, Date, Boolean, ForeignKey)
from sqlalchemy.orm import registry, relationship
from bot.domain import model
"""
Imperative mapping with dataclasses (models are dataclasses)
https://docs.sqlalchemy.org/en/14/orm/mapping_styles.html#imperative-mapping-with-dataclasses-and-attrs
"""
@xescuder
xescuder / config.py
Created June 4, 2023 21:40
Configuration
import os
from os import environ as env
from typing import get_type_hints, Union
from dotenv import load_dotenv
env = os.getenv('ENVIRONMENT')
dotenv_path = f'.env.{env}' if env is not None else '.env'
load_dotenv(dotenv_path=dotenv_path)
@xescuder
xescuder / docker-compose-airflow.yaml
Created December 31, 2023 13:32
Docker Compose Airflow
version: "3"
x-airflow-common: &airflow-common
# In order to add custom dependencies or upgrade provider packages you can use your extended image.
# Comment the image line, place your Dockerfile in the directory where you placed the docker-compose.yaml
# and uncomment the "build" line below, Then run `docker-compose build` to build the images.
image: trading-airflow #${AIRFLOW_IMAGE_NAME:-apache/airflow}
# build: .
environment: &airflow-common-env
AIRFLOW__CORE__EXECUTOR: LocalExecutor
AIRFLOW__DATABASE__SQL_ALCHEMY_CONN: postgresql+psycopg2://airflow:airflow@postgres/airflow
@xescuder
xescuder / load_sectors_etfs.py
Created January 16, 2024 17:45
Load CSV into Table
import os
from pathlib import Path
from sqlalchemy import create_engine
import pandas as pd
from trading_data_pipeline.config import AppConfig
config = AppConfig(os.environ)
connection_url = config.get_postgres_uri()
@xescuder
xescuder / return_changes_1year.sql
Created January 21, 2024 13:13
Return changes between today and 1 year ago
SELECT s.symbol, s.sector, first(close, date) AS first_close, last(close, date) AS last_close,
MIN(date) AS first_date,
MAX(date) AS last_date,
100 * (last(close, date) - first(close, date)) / first(close, date) AS return_change
FROM
daily_bars db, stocks s
WHERE
db.date >= now() - interval '1 year' AND db.symbol = s.symbol
GROUP BY
s.symbol
@xescuder
xescuder / top_instruments.ipynb
Last active February 15, 2024 17:43
Top Instruments - DuckDB and Polars
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@xescuder
xescuder / golden_cross_stop_trailer.py
Created March 3, 2024 11:04
A backtrader multidata example with golden cross strategy and stop trailer (based on strategy entries and exits)
import backtrader as bt
from trading_backtesting.stop_trailer import StopTrailer
class GoldenCrossStrategy(bt.Strategy):
SHORT, NONE, LONG = -1, 0, 1
params = dict(
fast_length=50,