File size: 2,969 Bytes
6f60a15
 
 
 
 
 
 
 
 
3fe0726
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
---
title: Gotti Signal Gen
emoji: 🏢
colorFrom: green
colorTo: gray
sdk: docker
pinned: false
---

# Stock Alchemist Signal Generator

A Dockerized FastAPI application that generates stock trading signals using Ollama (LLaMA 3.1) and performs scheduled fundamental analysis. Designed to run on Hugging Face Spaces.

## Features

- **Daily Signal Generation**: Prompts a local LLaMA 3.1 model to analyze stocks and generates trading signals.
- **Weekly Analysis**: Runs a comprehensive fundamental analysis script every Saturday.
- **Persistent Storage**: Stores results in a TiDB/MySQL database.
- **Automated Scheduling**: Uses GitHub Actions to trigger daily and weekly tasks.

## Setup Instructions

### 1. Hugging Face Space Setup

1. Create a new Space on [Hugging Face](https://huggingface.co/spaces).
2. Select **Docker** as the SDK.
3. Choose "Blank" or just point it to your repository.

### 2. Environment Variables

In your Hugging Face Space "Settings" -> "Variables and secrets", add the following **Secret** variables:

- `API_SECRET`: A strong secret key to protect your endpoints.
- `DB_HOST`: Your TiDB/MySQL host.
- `DB_PORT`: Your TiDB/MySQL port (e.g., 4000 for TiDB, 3306 for MySQL).
- `DB_USERNAME`: Database username.
- `DB_PASSWORD`: Database password.
- `DB_DATABASE`: Database name (e.g., `gotti`).
- `DB_SSL_CA`: (Optional) Path to SSL CA certificate if needed (relative to repository root).

### 3. GitHub Actions Setup

To enable the scheduler:

1. Go to your GitHub repository -> **Settings** -> **Secrets and variables** -> **Actions**.
2. Add the following **Repository secrets**:
   - `HF_SPACE_URL`: The URL of your Hugging Face Space (e.g., `https://username-space-name.hf.space`).
   - `API_SECRET`: The same secret you set in Hugging Face.

### 4. Database Initialization

The application handles table creation automatically in `src/db/local_database.py`. Ensure your database user has `CREATE TABLE` permissions.

## Local Development

### Prerequisites

- Docker
- Python 3.10+
- Ollama (running locally if testing outside Docker)

### Running with Docker

```bash
# Build the image
docker build -t stock-alchemist .

# Run the container
# Note: You need to pass env vars. For local testing, you might want to use --env-file
docker run -p 7860:7860 \
  -e API_SECRET=test_secret \
  -e DB_HOST=host.docker.internal \
  -e DB_USERNAME=root \
  -e DB_PASSWORD=yourpassword \
  stock-alchemist
```

### API Endpoints

- `POST /generate-signal`: Trigger signal generation. Headers: `X-API-Secret: <your_secret>` (or `API_SECRET` in local dev). *Note: The code uses `API_SECRET` env var validation.*
- `POST /saturday-analysis`: Trigger weekly analysis.
- `GET /health`: Health check and system vitals.

## Project Structure

- `Dockerfile` & `entrypoint.sh`: System configuration.
- `src/main.py`: FastAPI application entry point.
- `src/db/`: Database interaction logic.
- `.github/workflows/scheduler.yml`: Automated scheduler.