Commit ·
c072ec7
1
Parent(s): ba8e8a9
feat: Add application code and models via Git LFS
Browse files- .gitattributes +1 -0
- .gitignore +3 -0
- Dockerfile +21 -0
- README.md +0 -0
- client.py +39 -0
- config.py +10 -0
- main.py +124 -0
- models/catboost_regressor_target_1d.cbm +3 -0
- models/catboost_regressor_target_1w.cbm +3 -0
- models/catboost_regressor_target_2w.cbm +3 -0
- models/catboost_regressor_target_3d.cbm +3 -0
- requirements.txt +9 -0
- utils.py +1488 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
models/*.cbm filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
.env
|
| 2 |
+
venv/
|
| 3 |
+
__pycache__/
|
Dockerfile
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use an official Python runtime as a parent image
|
| 2 |
+
FROM python:3.9-slim
|
| 3 |
+
|
| 4 |
+
# Set the working directory in the container
|
| 5 |
+
WORKDIR /code
|
| 6 |
+
|
| 7 |
+
# Copy the requirements file into the container
|
| 8 |
+
COPY ./requirements.txt /code/requirements.txt
|
| 9 |
+
|
| 10 |
+
# Install any needed system dependencies
|
| 11 |
+
# (Usually not needed for this stack, but good to know)
|
| 12 |
+
|
| 13 |
+
# Install Python dependencies
|
| 14 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
| 15 |
+
|
| 16 |
+
# Copy the rest of your application code into the container
|
| 17 |
+
COPY . /code/
|
| 18 |
+
|
| 19 |
+
# Command to run your application
|
| 20 |
+
# Expose port 7860 and run uvicorn
|
| 21 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
README.md
CHANGED
|
Binary files a/README.md and b/README.md differ
|
|
|
client.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import pprint
|
| 3 |
+
|
| 4 |
+
# The URL where your local FastAPI server is running
|
| 5 |
+
API_URL = "http://127.0.0.1:8000/run-prediction-batch"
|
| 6 |
+
|
| 7 |
+
def trigger_prediction_job():
|
| 8 |
+
"""
|
| 9 |
+
Sends a POST request to the API to start the prediction batch job.
|
| 10 |
+
"""
|
| 11 |
+
print(f"🚀 Sending request to API endpoint: {API_URL}")
|
| 12 |
+
|
| 13 |
+
try:
|
| 14 |
+
# The request doesn't need a body for this endpoint
|
| 15 |
+
response = requests.post(API_URL, timeout=300) # 5-minute timeout
|
| 16 |
+
|
| 17 |
+
# Raise an exception if the request returned an unsuccessful status code
|
| 18 |
+
response.raise_for_status()
|
| 19 |
+
|
| 20 |
+
print("\n✅ API request successful!")
|
| 21 |
+
print("--- API Response ---")
|
| 22 |
+
|
| 23 |
+
# Pretty-print the JSON response from the server
|
| 24 |
+
pprint.pprint(response.json())
|
| 25 |
+
|
| 26 |
+
except requests.exceptions.HTTPError as http_err:
|
| 27 |
+
print(f"❌ HTTP error occurred: {http_err}")
|
| 28 |
+
print(f"Response Body: {response.text}")
|
| 29 |
+
except requests.exceptions.RequestException as req_err:
|
| 30 |
+
print(f"❌ A critical request error occurred: {req_err}")
|
| 31 |
+
print("Is the FastAPI server running? Start it with 'uvicorn main:app --reload'")
|
| 32 |
+
except Exception as e:
|
| 33 |
+
print(f"❌ An unexpected error occurred: {e}")
|
| 34 |
+
|
| 35 |
+
if __name__ == "__main__":
|
| 36 |
+
# How to use:
|
| 37 |
+
# 1. In one terminal, run your FastAPI server: uvicorn main:app --reload
|
| 38 |
+
# 2. In a second terminal (with venv activated), run this script: python client.py
|
| 39 |
+
trigger_prediction_job()
|
config.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# config.py
|
| 2 |
+
CONFIG = {
|
| 3 |
+
"IDX_TICKERS": ["GOTO.JK",
|
| 4 |
+
"BUMI.JK","BBKP.JK","BKSL.JK","BBRI.JK","BRMS.JK","BREN.JK","BBCA.JK","MBMA.JK","BUKA.JK","TLKM.JK","BRPT.JK","BMRI.JK","TPIA.JK","SCMA.JK","AMMN.JK","AVIA.JK","EMTK.JK","KLBF.JK","BRIS.JK","PGEO.JK","ADMR.JK","DEWA.JK","ASII.JK","UNVR.JK","BBNI.JK","BYAN.JK","ISAT.JK","PNLF.JK","ADRO.JK","SIDO.JK","MAPA.JK","MEDC.JK","ARCI.JK","ENRG.JK","MDKA.JK","PGAS.JK","ANTM.JK","AKRA.JK",
|
| 5 |
+
"INPC.JK","ESSA.JK","ACES.JK","MAPI.JK","ERAA.JK","BBTN.JK","ARTO.JK","SRTG.JK","HRUM.JK","CLEO.JK","PANI.JK","WIRG.JK","JPFA.JK","ICBP.JK","CUAN.JK","NICL.JK","DSNG.JK","INCO.JK","BTPN.JK","PTRO.JK","FILM.JK","SSMS.JK","FORE.JK",
|
| 6 |
+
"INDF.JK","ADHI.JK","INET.JK","AADI.JK","DSSA.JK","BTPS.JK","TINS.JK","LSIP.JK","PTPP.JK","CBDK.JK","INKP.JK","INDY.JK","SSIA.JK","HRTA.JK","RAJA.JK","MINE.JK","RATU.JK","DCII.JK","WIFI.JK","LPPF.JK","DAAZ.JK","DATA.JK","KKGI.JK","PSAB.JK","EXCL.JK","TOWR.JK","SMGR.JK","PTBA.JK","ITMG.JK","AMRT.JK","CTRA.JK","SMRA.JK","CPIN.JK","JSMR.JK","UNTR.JK"
|
| 7 |
+
],
|
| 8 |
+
"PROCESS_TIMEFRAMES": ["1h"],
|
| 9 |
+
"HISTORY_BUFFER_DAYS": 365 # Days of data needed for feature calculation
|
| 10 |
+
}
|
main.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# main.py
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
from fastapi import FastAPI, HTTPException
|
| 7 |
+
from supabase import create_client, Client
|
| 8 |
+
from datetime import datetime, timedelta
|
| 9 |
+
from typing import List, Dict
|
| 10 |
+
|
| 11 |
+
# Local imports
|
| 12 |
+
from config import CONFIG
|
| 13 |
+
from utils import fetch_yahoo, candles_to_dataframe, create_features_for_df
|
| 14 |
+
from catboost import CatBoostRegressor
|
| 15 |
+
|
| 16 |
+
# --- INITIALIZATION (Same as before) ---
|
| 17 |
+
load_dotenv()
|
| 18 |
+
app = FastAPI(title="Stock Prediction API", version="1.0.0")
|
| 19 |
+
url: str = os.environ.get("SUPABASE_URL")
|
| 20 |
+
key: str = os.environ.get("SUPABASE_KEY")
|
| 21 |
+
supabase: Client = create_client(url, key)
|
| 22 |
+
MODELS: Dict[str, CatBoostRegressor] = {}
|
| 23 |
+
TARGETS = ['target_1d', 'target_3d', 'target_1w', 'target_2w']
|
| 24 |
+
MODEL_FEATURE_ORDER: List[str] = []
|
| 25 |
+
|
| 26 |
+
# --- HELPER FUNCTION ---
|
| 27 |
+
def bound_prediction(value: float, min_val: float = 0.0, max_val: float = 1.0) -> float:
|
| 28 |
+
"""Clips a value to be within the specified range [min, max]."""
|
| 29 |
+
return max(min_val, min(value, max_val))
|
| 30 |
+
|
| 31 |
+
# --- STARTUP EVENT (Same as before) ---
|
| 32 |
+
@app.on_event("startup")
|
| 33 |
+
def load_models():
|
| 34 |
+
"""Load all CatBoost models from the /models directory into memory."""
|
| 35 |
+
print("--- Loading models at startup ---")
|
| 36 |
+
for target in TARGETS:
|
| 37 |
+
model_path = os.path.join("models", f"catboost_regressor_{target}.cbm")
|
| 38 |
+
if os.path.exists(model_path):
|
| 39 |
+
model = CatBoostRegressor()
|
| 40 |
+
model.load_model(model_path)
|
| 41 |
+
MODELS[target] = model
|
| 42 |
+
print(f"✅ Model loaded for {target}")
|
| 43 |
+
else:
|
| 44 |
+
print(f"🚨 WARNING: Model file not found at {model_path}")
|
| 45 |
+
if MODELS:
|
| 46 |
+
global MODEL_FEATURE_ORDER
|
| 47 |
+
MODEL_FEATURE_ORDER = list(MODELS.values())[0].feature_names_
|
| 48 |
+
print(f"Feature order set with {len(MODEL_FEATURE_ORDER)} features.")
|
| 49 |
+
|
| 50 |
+
# --- API ENDPOINTS ---
|
| 51 |
+
|
| 52 |
+
@app.get("/")
|
| 53 |
+
def read_root():
|
| 54 |
+
return {"status": "ok", "message": f"Prediction API is live. {len(MODELS)} models loaded."}
|
| 55 |
+
|
| 56 |
+
@app.post("/run-prediction-batch")
|
| 57 |
+
async def run_prediction_batch():
|
| 58 |
+
"""
|
| 59 |
+
Triggers a batch prediction job.
|
| 60 |
+
Fetches data, predicts, bounds the predictions to [0, 1], and saves to Supabase.
|
| 61 |
+
"""
|
| 62 |
+
if not MODELS:
|
| 63 |
+
raise HTTPException(status_code=500, detail="Models are not loaded. Cannot run predictions.")
|
| 64 |
+
|
| 65 |
+
print(f"\n--- Starting new prediction batch at {datetime.now().isoformat()} ---")
|
| 66 |
+
|
| 67 |
+
tickers_to_predict = CONFIG["IDX_TICKERS"]
|
| 68 |
+
all_predictions = []
|
| 69 |
+
|
| 70 |
+
for ticker in tickers_to_predict:
|
| 71 |
+
try:
|
| 72 |
+
# 1. Fetch & Prepare Data (Same as before)
|
| 73 |
+
fetch_days = CONFIG['HISTORY_BUFFER_DAYS']
|
| 74 |
+
start_date = (datetime.now() - timedelta(days=fetch_days)).strftime('%Y-%m-%d')
|
| 75 |
+
end_date = (datetime.now() + timedelta(days=1)).strftime('%Y-%m-%d')
|
| 76 |
+
candles = fetch_yahoo(ticker, CONFIG['PROCESS_TIMEFRAMES'][0], start_date, end_date)
|
| 77 |
+
df_live = candles_to_dataframe(candles)
|
| 78 |
+
|
| 79 |
+
if df_live.empty or len(df_live) < 250:
|
| 80 |
+
print(f"Skipping {ticker}, not enough recent data.")
|
| 81 |
+
continue
|
| 82 |
+
|
| 83 |
+
latest_features_dict = create_features_for_df(df_live, CONFIG['PROCESS_TIMEFRAMES'][0])
|
| 84 |
+
if not latest_features_dict:
|
| 85 |
+
print(f"Skipping {ticker}, feature creation failed.")
|
| 86 |
+
continue
|
| 87 |
+
|
| 88 |
+
# 2. Predict with models
|
| 89 |
+
features_for_pred = pd.DataFrame([latest_features_dict])[MODEL_FEATURE_ORDER]
|
| 90 |
+
prediction_results = {}
|
| 91 |
+
for target, model in MODELS.items():
|
| 92 |
+
pred_score = model.predict(features_for_pred)[0]
|
| 93 |
+
# ✨ CHANGE: Bound the prediction score to the [0, 1] range
|
| 94 |
+
bounded_score = bound_prediction(pred_score)
|
| 95 |
+
prediction_results[f'predicted_{target}'] = bounded_score
|
| 96 |
+
|
| 97 |
+
# 3. Prepare data for Supabase
|
| 98 |
+
db_row = {
|
| 99 |
+
"prediction_time": datetime.now().isoformat(),
|
| 100 |
+
"ticker": ticker,
|
| 101 |
+
"predicted_target_1d": prediction_results.get('predicted_target_1d'),
|
| 102 |
+
"predicted_target_3d": prediction_results.get('predicted_target_3d'),
|
| 103 |
+
"predicted_target_1w": prediction_results.get('predicted_target_1w'),
|
| 104 |
+
"predicted_target_2w": prediction_results.get('predicted_target_2w'),
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
# 4. Save to database
|
| 108 |
+
response = supabase.table('stock_predictions').insert(db_row).execute()
|
| 109 |
+
|
| 110 |
+
if response.data:
|
| 111 |
+
print(f"✅ Successfully predicted and saved for {ticker}")
|
| 112 |
+
all_predictions.append(db_row)
|
| 113 |
+
else:
|
| 114 |
+
print(f"🚨 DB Error for {ticker}: {response.error.message if response.error else 'Unknown error'}")
|
| 115 |
+
|
| 116 |
+
except Exception as e:
|
| 117 |
+
print(f"🚨 An error occurred while processing {ticker}: {e}")
|
| 118 |
+
continue
|
| 119 |
+
|
| 120 |
+
return {
|
| 121 |
+
"status": "success",
|
| 122 |
+
"processed_count": len(all_predictions),
|
| 123 |
+
"data": all_predictions
|
| 124 |
+
}
|
models/catboost_regressor_target_1d.cbm
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:bad09fb9c86e8eac4d8c6df6c3f849c5cfcc6401f54cc333075d1e691c535894
|
| 3 |
+
size 1128440
|
models/catboost_regressor_target_1w.cbm
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fc99160f82057f509fb1f9acdfd345e8450e79472d09d07653ecde10e87014f9
|
| 3 |
+
size 1128584
|
models/catboost_regressor_target_2w.cbm
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:584cfe0e56ba4029b43cb240a48e6654e74be44fbc27ccaf966182639fbe0792
|
| 3 |
+
size 1128288
|
models/catboost_regressor_target_3d.cbm
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c56d1647d0fa2535f178ec07c0540123d72c561f6f32ae1535f9ed06c6d57e8a
|
| 3 |
+
size 1128232
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
+
from datetime import datetime, timedelta
|
| 6 |
+
from typing import Dict
|
| 7 |
+
from tqdm.notebook import tqdm
|
| 8 |
+
import requests
|
| 9 |
+
import time
|
| 10 |
+
|
| 11 |
+
# Generic Type Variable
|
| 12 |
+
T = TypeVar('T')
|
| 13 |
+
|
| 14 |
+
def last(a: List[T]) -> T:
|
| 15 |
+
"""Returns the last element of a list."""
|
| 16 |
+
return a[-1]
|
| 17 |
+
|
| 18 |
+
# Pine rule: in Boolean expressions na is treated as false
|
| 19 |
+
def as_bool(v: Union[float, int, bool, None]) -> bool:
|
| 20 |
+
"""Converts a value to boolean, treating None or NaN as False."""
|
| 21 |
+
if v is None or (isinstance(v, float) and math.isnan(v)):
|
| 22 |
+
return False
|
| 23 |
+
return bool(v)
|
| 24 |
+
|
| 25 |
+
# Helper functions for min/max emulating JavaScript's Math.min/max with NaN behavior
|
| 26 |
+
# JS Math.min(NaN, 5) -> 5 (if only one NaN) or NaN (if all NaN or multiple args with one NaN)
|
| 27 |
+
# JS Math.min(...[NaN, 5]) -> NaN
|
| 28 |
+
# The TS code uses `Math.max(...array)`, which means if any element in `array` is NaN, the result is NaN.
|
| 29 |
+
|
| 30 |
+
def _js_style_list_min(values: List[float]) -> float:
|
| 31 |
+
"""Emulates Math.min(...array) which returns NaN if any element in array is NaN."""
|
| 32 |
+
if not values:
|
| 33 |
+
return math.nan # Or based on specific requirement for empty list
|
| 34 |
+
has_nan = False
|
| 35 |
+
for val in values:
|
| 36 |
+
if math.isnan(val):
|
| 37 |
+
has_nan = True
|
| 38 |
+
break
|
| 39 |
+
if has_nan:
|
| 40 |
+
return math.nan
|
| 41 |
+
return min(values) if values else math.nan
|
| 42 |
+
|
| 43 |
+
def _js_style_list_max(values: List[float]) -> float:
|
| 44 |
+
"""Emulates Math.max(...array) which returns NaN if any element in array is NaN."""
|
| 45 |
+
if not values:
|
| 46 |
+
return math.nan
|
| 47 |
+
has_nan = False
|
| 48 |
+
for val in values:
|
| 49 |
+
if math.isnan(val):
|
| 50 |
+
has_nan = True
|
| 51 |
+
break
|
| 52 |
+
if has_nan:
|
| 53 |
+
return math.nan
|
| 54 |
+
return max(values) if values else math.nan
|
| 55 |
+
|
| 56 |
+
def _js_math_max(a: float, b: float) -> float:
|
| 57 |
+
"""Emulates JS Math.max(a,b) behavior with NaNs (prefers non-NaN)."""
|
| 58 |
+
if math.isnan(a): return b
|
| 59 |
+
if math.isnan(b): return a
|
| 60 |
+
return max(a, b)
|
| 61 |
+
|
| 62 |
+
def _js_math_min(a: float, b: float) -> float:
|
| 63 |
+
"""Emulates JS Math.min(a,b) behavior with NaNs (prefers non-NaN)."""
|
| 64 |
+
if math.isnan(a): return b
|
| 65 |
+
if math.isnan(b): return a
|
| 66 |
+
return min(a, b)
|
| 67 |
+
|
| 68 |
+
# /* ───────── basic rolling helpers ───────── */
|
| 69 |
+
def rolling_mean(src: List[float], length: int) -> List[float]:
|
| 70 |
+
"""Calculates the rolling mean (Simple Moving Average)."""
|
| 71 |
+
if not src or length <= 0:
|
| 72 |
+
return [math.nan] * len(src)
|
| 73 |
+
out = [math.nan] * len(src)
|
| 74 |
+
acc = 0.0
|
| 75 |
+
for i in range(len(src)):
|
| 76 |
+
if not math.isnan(src[i]): # Accumulate if not NaN
|
| 77 |
+
acc += src[i]
|
| 78 |
+
else: # If src[i] is NaN, the sum effectively becomes NaN for this window until enough non-NaNs flush it out or it's handled.
|
| 79 |
+
# To match TS, if src[i] is NaN, acc will also become NaN if not handled.
|
| 80 |
+
# The TS code doesn't check for NaN in src[i] during accumulation. acc += NaN -> acc is NaN.
|
| 81 |
+
# Python: acc += float('nan') -> acc is nan. This matches.
|
| 82 |
+
acc += src[i] # Allow NaN to propagate into acc
|
| 83 |
+
|
| 84 |
+
if i >= length:
|
| 85 |
+
# acc -= src[i - length];
|
| 86 |
+
# If src[i-length] was NaN, acc could already be NaN. Or acc is num, src[i-length] is NaN. num - NaN = NaN.
|
| 87 |
+
acc -= src[i - length] # Allow NaN propagation
|
| 88 |
+
|
| 89 |
+
if i < length - 1:
|
| 90 |
+
out[i] = math.nan
|
| 91 |
+
else:
|
| 92 |
+
if math.isnan(acc): # if accumulator is NaN (due to NaN in src)
|
| 93 |
+
out[i] = math.nan
|
| 94 |
+
else:
|
| 95 |
+
out[i] = acc / length
|
| 96 |
+
return out
|
| 97 |
+
|
| 98 |
+
def rolling_max(src: List[float], length: int) -> List[float]:
|
| 99 |
+
"""Calculates the rolling maximum."""
|
| 100 |
+
if not src or length <= 0:
|
| 101 |
+
return [math.nan] * len(src)
|
| 102 |
+
out = [math.nan] * len(src)
|
| 103 |
+
for i in range(len(src)):
|
| 104 |
+
start_index = max(0, i - length + 1)
|
| 105 |
+
window = src[start_index : i + 1]
|
| 106 |
+
out[i] = _js_style_list_max(window)
|
| 107 |
+
return out
|
| 108 |
+
|
| 109 |
+
def rolling_min(src: List[float], length: int) -> List[float]:
|
| 110 |
+
"""Calculates the rolling minimum."""
|
| 111 |
+
if not src or length <= 0:
|
| 112 |
+
return [math.nan] * len(src)
|
| 113 |
+
out = [math.nan] * len(src)
|
| 114 |
+
for i in range(len(src)):
|
| 115 |
+
start_index = max(0, i - length + 1)
|
| 116 |
+
window = src[start_index : i + 1]
|
| 117 |
+
out[i] = _js_style_list_min(window)
|
| 118 |
+
return out
|
| 119 |
+
|
| 120 |
+
def rolling_std(src: List[float], length: int) -> List[float]:
|
| 121 |
+
"""Calculates the rolling standard deviation with ddof=1."""
|
| 122 |
+
if not src or length <= 1: # std requires at least 2 points for ddof=1
|
| 123 |
+
return [math.nan] * len(src)
|
| 124 |
+
|
| 125 |
+
out = [math.nan] * len(src)
|
| 126 |
+
for i in range(len(src)):
|
| 127 |
+
if i < length - 1:
|
| 128 |
+
out[i] = math.nan
|
| 129 |
+
continue
|
| 130 |
+
|
| 131 |
+
window = src[i - length + 1 : i + 1]
|
| 132 |
+
|
| 133 |
+
# Check for NaNs in window, if any, mean and std dev are NaN
|
| 134 |
+
if any(math.isnan(x) for x in window):
|
| 135 |
+
out[i] = math.nan
|
| 136 |
+
continue
|
| 137 |
+
|
| 138 |
+
m = sum(window) / length
|
| 139 |
+
variance_sum = sum((x - m) ** 2 for x in window)
|
| 140 |
+
|
| 141 |
+
# ddof = 1 means (length - 1) in denominator
|
| 142 |
+
if length - 1 == 0: # Should be caught by length <= 1 check earlier
|
| 143 |
+
out[i] = math.nan
|
| 144 |
+
else:
|
| 145 |
+
variance = variance_sum / (length - 1)
|
| 146 |
+
out[i] = math.sqrt(variance)
|
| 147 |
+
return out
|
| 148 |
+
|
| 149 |
+
# /* ───────── Wilder RMA & EMA ───────── */
|
| 150 |
+
def rma(src: List[float], length: int) -> List[float]:
|
| 151 |
+
"""Calculates Wilder's Recursive Moving Average."""
|
| 152 |
+
if not src: return []
|
| 153 |
+
if length <= 0: return [math.nan] * len(src)
|
| 154 |
+
|
| 155 |
+
alpha = 1.0 / length
|
| 156 |
+
out = [math.nan] * len(src)
|
| 157 |
+
|
| 158 |
+
i0 = -1
|
| 159 |
+
for idx, val in enumerate(src):
|
| 160 |
+
if not math.isnan(val):
|
| 161 |
+
i0 = idx
|
| 162 |
+
break
|
| 163 |
+
|
| 164 |
+
if i0 == -1: # All NaNs in src
|
| 165 |
+
return [math.nan] * len(src)
|
| 166 |
+
|
| 167 |
+
out[i0] = src[i0]
|
| 168 |
+
|
| 169 |
+
for i in range(i0): # Forward-fill for any NaN before the seed
|
| 170 |
+
out[i] = out[i0]
|
| 171 |
+
|
| 172 |
+
for i in range(i0 + 1, len(src)):
|
| 173 |
+
v = src[i]
|
| 174 |
+
if math.isnan(v):
|
| 175 |
+
out[i] = out[i-1]
|
| 176 |
+
else:
|
| 177 |
+
# If out[i-1] is NaN (e.g. from a long series of NaNs in src not covered by forward fill), result is NaN
|
| 178 |
+
out[i] = alpha * v + (1.0 - alpha) * out[i-1]
|
| 179 |
+
return out
|
| 180 |
+
|
| 181 |
+
def ema(src: List[float], length: int) -> List[float]:
|
| 182 |
+
"""Calculates the Exponential Moving Average."""
|
| 183 |
+
if not src: return []
|
| 184 |
+
if length <= 0: return [math.nan] * len(src) # Or other handling for invalid length
|
| 185 |
+
|
| 186 |
+
k = 2.0 / (length + 1)
|
| 187 |
+
out = [math.nan] * len(src)
|
| 188 |
+
|
| 189 |
+
if not src: return [] # Should be caught already
|
| 190 |
+
|
| 191 |
+
out[0] = src[0] # First EMA is the first source value (propagates NaN if src[0] is NaN)
|
| 192 |
+
|
| 193 |
+
for i in range(1, len(src)):
|
| 194 |
+
# If src[i] is NaN, or out[i-1] is NaN, the result will be NaN.
|
| 195 |
+
out[i] = k * src[i] + (1.0 - k) * out[i-1]
|
| 196 |
+
return out
|
| 197 |
+
|
| 198 |
+
# /* ───────── Wilder ATR ───────── */
|
| 199 |
+
def wilder_atr(high: List[float], low: List[float], close: List[float], length: int = 14) -> List[float]:
|
| 200 |
+
"""Calculates Wilder's Average True Range."""
|
| 201 |
+
if not close or not high or not low: return []
|
| 202 |
+
if not (len(close) == len(high) == len(low)):
|
| 203 |
+
raise ValueError("Input lists must have the same length for ATR.")
|
| 204 |
+
|
| 205 |
+
tr = [math.nan] * len(close)
|
| 206 |
+
for i in range(len(close)):
|
| 207 |
+
prev_close = close[i-1] if i > 0 else close[i]
|
| 208 |
+
|
| 209 |
+
h_val, l_val, c_val = high[i], low[i], close[i] # Current values
|
| 210 |
+
pc_val = prev_close # Previous close
|
| 211 |
+
|
| 212 |
+
# If any component is NaN, the terms become NaN. max(NaN, num, num) is NaN.
|
| 213 |
+
term1 = h_val - l_val
|
| 214 |
+
term2 = abs(h_val - pc_val) if not math.isnan(h_val) and not math.isnan(pc_val) else math.nan
|
| 215 |
+
term3 = abs(l_val - pc_val) if not math.isnan(l_val) and not math.isnan(pc_val) else math.nan
|
| 216 |
+
|
| 217 |
+
if math.isnan(term1) or math.isnan(term2) or math.isnan(term3):
|
| 218 |
+
tr[i] = math.nan
|
| 219 |
+
else:
|
| 220 |
+
tr[i] = max(term1, term2, term3)
|
| 221 |
+
|
| 222 |
+
return rma(tr, length)
|
| 223 |
+
|
| 224 |
+
# /* ───────── Wilder RSI ───────── */
|
| 225 |
+
def wilder_rsi(close: List[float], length: int = 14) -> List[float]:
|
| 226 |
+
"""Calculates Wilder's Relative Strength Index."""
|
| 227 |
+
if not close: return []
|
| 228 |
+
if length <= 0: return [math.nan] * len(close)
|
| 229 |
+
|
| 230 |
+
diff = [0.0] * len(close)
|
| 231 |
+
for i in range(len(close)):
|
| 232 |
+
if i > 0:
|
| 233 |
+
# If close[i] or close[i-1] is NaN, diff[i] becomes NaN.
|
| 234 |
+
diff[i] = close[i] - close[i-1]
|
| 235 |
+
# else diff[i] is 0.0 (already initialized)
|
| 236 |
+
|
| 237 |
+
# up/dn will propagate NaN if diff[i] is NaN. Math.max(NaN, 0) is NaN in JS, but max(NaN,0) in Python is 0 or error.
|
| 238 |
+
# TS: Math.max(v, 0) -> if v is NaN, result is NaN.
|
| 239 |
+
up = [(_js_math_max(d, 0.0)) if not math.isnan(d) else math.nan for d in diff]
|
| 240 |
+
dn = [(_js_math_max(-d, 0.0)) if not math.isnan(d) else math.nan for d in diff]
|
| 241 |
+
|
| 242 |
+
# The TS logic for seedU/seedD and restU/restD is specific.
|
| 243 |
+
rm_up = rolling_mean(up, length)
|
| 244 |
+
rm_dn = rolling_mean(dn, length)
|
| 245 |
+
|
| 246 |
+
# .slice(0, len) in TS
|
| 247 |
+
seed_u = rm_up[:length]
|
| 248 |
+
seed_d = rm_dn[:length]
|
| 249 |
+
|
| 250 |
+
rest_u_input = up[length:]
|
| 251 |
+
rest_d_input = dn[length:]
|
| 252 |
+
|
| 253 |
+
rest_u = rma(rest_u_input, length)
|
| 254 |
+
rest_d = rma(rest_d_input, length)
|
| 255 |
+
|
| 256 |
+
u_rma_list = seed_u + rest_u
|
| 257 |
+
d_rma_list = seed_d + rest_d
|
| 258 |
+
|
| 259 |
+
# Ensure lengths match original close length due to concat
|
| 260 |
+
# If len(close) < length, seed_u/d might be shorter than length. rest_u/d will be from empty or short list.
|
| 261 |
+
# The resulting u_rma_list / d_rma_list should naturally align with len(close).
|
| 262 |
+
# Example: close len 5, length 10. up len 5. rm_up len 5 (all nan). seed_u = rm_up[:5] = 5 nans.
|
| 263 |
+
# rest_u_input = up[10:] = []. rma([], 10) = []. u_rma_list = 5 nans. Correct.
|
| 264 |
+
|
| 265 |
+
rsi_values = [math.nan] * len(close)
|
| 266 |
+
|
| 267 |
+
for i in range(len(u_rma_list)):
|
| 268 |
+
# Guard against d_rma_list being unexpectedly shorter if logic error, though it shouldn't be.
|
| 269 |
+
if i >= len(d_rma_list):
|
| 270 |
+
rsi_values[i] = math.nan
|
| 271 |
+
continue
|
| 272 |
+
|
| 273 |
+
val_u = u_rma_list[i]
|
| 274 |
+
val_d = d_rma_list[i]
|
| 275 |
+
|
| 276 |
+
if math.isnan(val_u) or math.isnan(val_d):
|
| 277 |
+
rsi_values[i] = math.nan
|
| 278 |
+
elif val_d == 0:
|
| 279 |
+
if val_u == 0: # Both avg_gain and avg_loss are 0
|
| 280 |
+
rsi_values[i] = math.nan # As per formula v/dRma[i] -> NaN/0 -> NaN. Some RSI define this as 50 or 100. Sticking to formula.
|
| 281 |
+
else: # val_u > 0 (non-negative due to max(v,0)) and val_d == 0
|
| 282 |
+
rsi_values[i] = 100.0
|
| 283 |
+
else: # val_d is not 0, and neither val_u nor val_d is NaN
|
| 284 |
+
rs = val_u / val_d
|
| 285 |
+
rsi_values[i] = 100.0 - (100.0 / (1.0 + rs))
|
| 286 |
+
|
| 287 |
+
return rsi_values
|
| 288 |
+
|
| 289 |
+
# /* ───────── WVF (FoxPro) – returns [last, upper, rangeHi] ───────── */
|
| 290 |
+
def foxpro_wvf(
|
| 291 |
+
close: List[float], low: List[float],
|
| 292 |
+
pd_: int = 22, bbl: int = 20, mult: float = 2.0,
|
| 293 |
+
lb: int = 50, ph: float = 0.85
|
| 294 |
+
) -> Tuple[float, float, float]:
|
| 295 |
+
"""Calculates Williams VIX Fix components."""
|
| 296 |
+
if not close or not low or not (len(close) == len(low)):
|
| 297 |
+
return (math.nan, math.nan, math.nan)
|
| 298 |
+
if len(close) == 0: return (math.nan, math.nan, math.nan)
|
| 299 |
+
|
| 300 |
+
|
| 301 |
+
hi_pd = rolling_max(close, pd_)
|
| 302 |
+
|
| 303 |
+
wvf = [math.nan] * len(close)
|
| 304 |
+
for i in range(len(close)):
|
| 305 |
+
# Ensure hi_pd[i] is not NaN and not zero before division
|
| 306 |
+
if not math.isnan(hi_pd[i]) and hi_pd[i] != 0 and \
|
| 307 |
+
not math.isnan(low[i]): # close[i] is not used in this specific formula line from TS
|
| 308 |
+
wvf[i] = ((hi_pd[i] - low[i]) / hi_pd[i]) * 100.0
|
| 309 |
+
else:
|
| 310 |
+
wvf[i] = math.nan
|
| 311 |
+
|
| 312 |
+
s_dev_raw = rolling_std(wvf, bbl)
|
| 313 |
+
s_dev = [s * mult if not math.isnan(s) else math.nan for s in s_dev_raw]
|
| 314 |
+
|
| 315 |
+
mid = rolling_mean(wvf, bbl)
|
| 316 |
+
|
| 317 |
+
upper = [(m + s_dev[i]) if not math.isnan(m) and i < len(s_dev) and not math.isnan(s_dev[i]) else math.nan
|
| 318 |
+
for i, m in enumerate(mid)]
|
| 319 |
+
|
| 320 |
+
rng_hi_raw = rolling_max(wvf, lb)
|
| 321 |
+
rng_hi = [v * ph if not math.isnan(v) else math.nan for v in rng_hi_raw]
|
| 322 |
+
|
| 323 |
+
n_idx = len(wvf) - 1
|
| 324 |
+
if n_idx < 0: # Empty wvf, should not happen if close is not empty
|
| 325 |
+
return (math.nan, math.nan, math.nan)
|
| 326 |
+
|
| 327 |
+
# Return last values of the calculated series
|
| 328 |
+
# Ensure lists are not empty before accessing last element
|
| 329 |
+
last_wvf = wvf[n_idx] if wvf else math.nan
|
| 330 |
+
last_upper = upper[n_idx] if upper else math.nan
|
| 331 |
+
last_rng_hi = rng_hi[n_idx] if rng_hi else math.nan
|
| 332 |
+
|
| 333 |
+
return (last_wvf, last_upper, last_rng_hi)
|
| 334 |
+
|
| 335 |
+
# /* ───────── MA labels ───────── */
|
| 336 |
+
def ma_labels(
|
| 337 |
+
row8: float, row13: float, row21: float,
|
| 338 |
+
prev8: float, prev13: float, prev21: float
|
| 339 |
+
) -> str:
|
| 340 |
+
"""Determines MA-based market label."""
|
| 341 |
+
# NaN comparisons (e.g. math.nan > 10) are False. This naturally handles NaNs in conditions.
|
| 342 |
+
if row8 > row13 and row13 > row21: return 'Bullish'
|
| 343 |
+
if row8 < row13 and row13 < row21: return 'Bearish'
|
| 344 |
+
if prev8 > prev13 and prev13 > prev21 and row13 > row8: return 'Spec. Bearish'
|
| 345 |
+
if prev8 < prev13 and prev13 < prev21 and row13 < row8: return 'Spec. Bullish'
|
| 346 |
+
return 'Neutral'
|
| 347 |
+
|
| 348 |
+
# /* ───────── RSI label (same wording) ───────── */
|
| 349 |
+
def rsi_label(rsi: float, trend_bull: bool) -> str:
|
| 350 |
+
"""Determines RSI-based market label."""
|
| 351 |
+
if math.isnan(rsi):
|
| 352 |
+
return f"Neutral (NaN)" # Or specific NaN label
|
| 353 |
+
|
| 354 |
+
rsi_str = f"{rsi:.1f}"
|
| 355 |
+
|
| 356 |
+
if rsi > 85: return f"Spec Sell ({rsi_str})"
|
| 357 |
+
if rsi > 80 and not trend_bull: return f"Spec Sell ({rsi_str})"
|
| 358 |
+
if rsi > 70: return f"Overbought ({rsi_str})"
|
| 359 |
+
if rsi < 20 and trend_bull: return f"Spec Buy ({rsi_str})"
|
| 360 |
+
if rsi < 26: return f"Oversold ({rsi_str})"
|
| 361 |
+
if trend_bull and rsi > 50: return f"Bullish ({rsi_str})"
|
| 362 |
+
if not trend_bull and rsi < 50: return f"Bearish ({rsi_str})"
|
| 363 |
+
return f"Neutral ({rsi_str})"
|
| 364 |
+
|
| 365 |
+
# /* ───────── ATR trailing stop ───────── */
|
| 366 |
+
def atr_trail(
|
| 367 |
+
close: List[float], high: List[float], low: List[float],
|
| 368 |
+
atr_p: int = 5, hhv_p: int = 10, mult: float = 2.5
|
| 369 |
+
) -> List[float]:
|
| 370 |
+
"""Calculates ATR Trailing Stop."""
|
| 371 |
+
if not close or not high or not low: return []
|
| 372 |
+
if not (len(close) == len(high) == len(low)):
|
| 373 |
+
raise ValueError("Input lists must have the same length for ATR Trail.")
|
| 374 |
+
|
| 375 |
+
atr_values = wilder_atr(high, low, close, atr_p)
|
| 376 |
+
|
| 377 |
+
prev_raw = [(h_val - mult * atr_val) if not math.isnan(h_val) and not math.isnan(atr_val) else math.nan
|
| 378 |
+
for h_val, atr_val in zip(high, atr_values)]
|
| 379 |
+
|
| 380 |
+
prev = rolling_max(prev_raw, hhv_p) # Max of (high - mult * atr) over hhvP
|
| 381 |
+
|
| 382 |
+
ts = [math.nan] * len(close)
|
| 383 |
+
|
| 384 |
+
for i in range(len(close)):
|
| 385 |
+
current_close = close[i]
|
| 386 |
+
prev_val_i = prev[i]
|
| 387 |
+
|
| 388 |
+
if i < 16:
|
| 389 |
+
ts[i] = current_close
|
| 390 |
+
else: # i >= 16
|
| 391 |
+
# Handle NaNs for comparison: nan > x is false. x > nan is false.
|
| 392 |
+
# So if prev_val_i is NaN, current_close > prev_val_i is false.
|
| 393 |
+
# If current_close is NaN, current_close > prev_val_i is false.
|
| 394 |
+
if not math.isnan(current_close) and not math.isnan(prev_val_i) and current_close > prev_val_i:
|
| 395 |
+
ts[i] = prev_val_i
|
| 396 |
+
else: # Covers current_close <= prev_val_i OR any involved value is NaN
|
| 397 |
+
# The original TS: `i ? ts[i-1] : close[i]`. Since i >= 16, `i` is true. So `ts[i-1]`.
|
| 398 |
+
if i > 0:
|
| 399 |
+
ts[i] = ts[i-1]
|
| 400 |
+
else: # This case (i=0 and i>=16) is impossible. Defensive.
|
| 401 |
+
ts[i] = current_close
|
| 402 |
+
return ts
|
| 403 |
+
|
| 404 |
+
# /* ───────── simple SuperTrend (returns [line, trendArr]) ───────── */
|
| 405 |
+
def super_trend(
|
| 406 |
+
close: List[float], high: List[float], low: List[float],
|
| 407 |
+
length: int = 10, mult: float = 3.0
|
| 408 |
+
) -> Tuple[List[float], List[int]]:
|
| 409 |
+
"""Calculates SuperTrend indicator."""
|
| 410 |
+
n = len(close)
|
| 411 |
+
if n == 0 or not (n == len(high) == len(low)):
|
| 412 |
+
return ([], [])
|
| 413 |
+
|
| 414 |
+
atr_values = wilder_atr(high, low, close, length)
|
| 415 |
+
|
| 416 |
+
hl2 = [(h_val + l_val) / 2.0 if not math.isnan(h_val) and not math.isnan(l_val) else math.nan
|
| 417 |
+
for h_val, l_val in zip(high, low)]
|
| 418 |
+
|
| 419 |
+
basic_up = [(val_hl2 - mult * val_atr) if not math.isnan(val_hl2) and not math.isnan(val_atr) else math.nan
|
| 420 |
+
for val_hl2, val_atr in zip(hl2, atr_values)]
|
| 421 |
+
basic_dn = [(val_hl2 + mult * val_atr) if not math.isnan(val_hl2) and not math.isnan(val_atr) else math.nan
|
| 422 |
+
for val_hl2, val_atr in zip(hl2, atr_values)]
|
| 423 |
+
|
| 424 |
+
f_up = [math.nan] * n
|
| 425 |
+
f_dn = [math.nan] * n
|
| 426 |
+
trend = [0] * n # 1 for uptrend, -1 for downtrend
|
| 427 |
+
|
| 428 |
+
if n == 0: return ([], []) # Should be caught
|
| 429 |
+
|
| 430 |
+
f_up[0] = basic_up[0]
|
| 431 |
+
f_dn[0] = basic_dn[0]
|
| 432 |
+
trend[0] = 1 # Seed with uptrend
|
| 433 |
+
|
| 434 |
+
for i in range(1, n):
|
| 435 |
+
prev_close_val = close[i-1]
|
| 436 |
+
prev_f_up_val = f_up[i-1]
|
| 437 |
+
prev_f_dn_val = f_dn[i-1]
|
| 438 |
+
|
| 439 |
+
# Final Upper Band
|
| 440 |
+
# TS: close[i-1] <= fUp[i-1] ? basicUp[i] : Math.max(basicUp[i], fUp[i-1])
|
| 441 |
+
# If prev_close_val or prev_f_up_val is NaN, condition `prev_close_val <= prev_f_up_val` is False.
|
| 442 |
+
if not math.isnan(prev_close_val) and not math.isnan(prev_f_up_val) and prev_close_val <= prev_f_up_val:
|
| 443 |
+
f_up[i] = basic_up[i]
|
| 444 |
+
else:
|
| 445 |
+
f_up[i] = _js_math_max(basic_up[i], prev_f_up_val) # Emulates JS Math.max
|
| 446 |
+
|
| 447 |
+
# Final Lower Band
|
| 448 |
+
# TS: close[i-1] >= fDn[i-1] ? basicDn[i] : Math.min(basicDn[i], fDn[i-1])
|
| 449 |
+
if not math.isnan(prev_close_val) and not math.isnan(prev_f_dn_val) and prev_close_val >= prev_f_dn_val:
|
| 450 |
+
f_dn[i] = basic_dn[i]
|
| 451 |
+
else:
|
| 452 |
+
f_dn[i] = _js_math_min(basic_dn[i], prev_f_dn_val) # Emulates JS Math.min
|
| 453 |
+
|
| 454 |
+
# Trend determination
|
| 455 |
+
current_close_val = close[i]
|
| 456 |
+
trend_changed = False
|
| 457 |
+
if trend[i-1] == -1:
|
| 458 |
+
# close[i] > fDn[i-1] (use prev_f_dn_val for fDn[i-1])
|
| 459 |
+
if not math.isnan(current_close_val) and not math.isnan(prev_f_dn_val) and current_close_val > prev_f_dn_val:
|
| 460 |
+
trend[i] = 1
|
| 461 |
+
trend_changed = True
|
| 462 |
+
elif trend[i-1] == 1:
|
| 463 |
+
# close[i] < fUp[i-1] (use prev_f_up_val for fUp[i-1])
|
| 464 |
+
if not math.isnan(current_close_val) and not math.isnan(prev_f_up_val) and current_close_val < prev_f_up_val:
|
| 465 |
+
trend[i] = -1
|
| 466 |
+
trend_changed = True
|
| 467 |
+
|
| 468 |
+
if not trend_changed:
|
| 469 |
+
trend[i] = trend[i-1]
|
| 470 |
+
|
| 471 |
+
st_line = [math.nan] * n
|
| 472 |
+
for i in range(n):
|
| 473 |
+
if trend[i] == 1:
|
| 474 |
+
st_line[i] = f_up[i]
|
| 475 |
+
elif trend[i] == -1:
|
| 476 |
+
st_line[i] = f_dn[i]
|
| 477 |
+
# else trend[i] == 0 (only for first element if n=1 and not updated), st_line[i] remains math.nan
|
| 478 |
+
|
| 479 |
+
return (st_line, trend)
|
| 480 |
+
|
| 481 |
+
# /* ───────── MACD (returns [line, signal, hist]) ───────── */
|
| 482 |
+
def macd_calc(src: List[float]) -> Tuple[List[float], List[float], List[float]]: # Renamed from macd to macd_calc
|
| 483 |
+
"""Calculates MACD, Signal Line, and Histogram."""
|
| 484 |
+
if not src: return ([], [], [])
|
| 485 |
+
|
| 486 |
+
fast_ema = ema(src, 12)
|
| 487 |
+
slow_ema = ema(src, 26)
|
| 488 |
+
|
| 489 |
+
macd_line = [(f - s) if not math.isnan(f) and not math.isnan(s) else math.nan
|
| 490 |
+
for f, s in zip(fast_ema, slow_ema)]
|
| 491 |
+
|
| 492 |
+
signal_line = ema(macd_line, 9)
|
| 493 |
+
|
| 494 |
+
histogram = [(m - s) if not math.isnan(m) and not math.isnan(s) else math.nan
|
| 495 |
+
for m, s in zip(macd_line, signal_line)]
|
| 496 |
+
|
| 497 |
+
return (macd_line, signal_line, histogram)
|
| 498 |
+
|
| 499 |
+
# /* ───────── Stochastic %K (fast) ───────── */
|
| 500 |
+
def _stoch_k(
|
| 501 |
+
close: List[float], high: List[float], low: List[float],
|
| 502 |
+
length: int = 14
|
| 503 |
+
) -> List[float]:
|
| 504 |
+
"""Helper to calculate Stochastic %K."""
|
| 505 |
+
n = len(close)
|
| 506 |
+
if n == 0 or length <= 0 or not (n == len(high) == len(low)):
|
| 507 |
+
return [math.nan] * n
|
| 508 |
+
|
| 509 |
+
k_values = [math.nan] * n
|
| 510 |
+
for i in range(n):
|
| 511 |
+
start_index = max(0, i - length + 1)
|
| 512 |
+
|
| 513 |
+
# Use _js_style_list_min/max for consistency with TS Math.min/max(...slice)
|
| 514 |
+
window_low = low[start_index : i + 1]
|
| 515 |
+
window_high = high[start_index : i + 1]
|
| 516 |
+
|
| 517 |
+
lo = _js_style_list_min(window_low)
|
| 518 |
+
hi = _js_style_list_max(window_high)
|
| 519 |
+
|
| 520 |
+
current_close = close[i]
|
| 521 |
+
|
| 522 |
+
if math.isnan(lo) or math.isnan(hi) or math.isnan(current_close):
|
| 523 |
+
k_values[i] = math.nan
|
| 524 |
+
elif hi == lo: # Both are same non-NaN value, implies hi-lo is 0
|
| 525 |
+
k_values[i] = 50.0 # As per TS logic
|
| 526 |
+
else:
|
| 527 |
+
# hi - lo cannot be zero here
|
| 528 |
+
k_values[i] = (100.0 * (current_close - lo)) / (hi - lo)
|
| 529 |
+
|
| 530 |
+
return k_values
|
| 531 |
+
|
| 532 |
+
# /* ───────── Stoch K/D (uses the helper above) ───────── */
|
| 533 |
+
def stoch_kd(
|
| 534 |
+
close: List[float], high: List[float], low: List[float],
|
| 535 |
+
length: int = 14 # This is %K period
|
| 536 |
+
) -> Tuple[List[float], List[float]]:
|
| 537 |
+
"""Calculates Stochastic %K and %D."""
|
| 538 |
+
# %D period is typically 3 for rollingMean of K
|
| 539 |
+
k = _stoch_k(close, high, low, length)
|
| 540 |
+
d = rolling_mean(k, 3) # %D is SMA of %K
|
| 541 |
+
return (k, d)
|
| 542 |
+
|
| 543 |
+
# /* ───────── DMI (only +DI, −DI, ADX) ───────── */
|
| 544 |
+
def dmi_calc( # Renamed from dmi to dmi_calc
|
| 545 |
+
high: List[float], low: List[float], close: List[float],
|
| 546 |
+
length: int = 14
|
| 547 |
+
) -> Tuple[List[float], List[float], List[float]]:
|
| 548 |
+
"""Calculates Directional Movement Index (+DI, -DI, ADX)."""
|
| 549 |
+
n = len(high)
|
| 550 |
+
if n == 0 or length <= 0 or not (n == len(low) == len(close)):
|
| 551 |
+
nan_list = [math.nan] * n
|
| 552 |
+
return (nan_list, nan_list, nan_list) if n > 0 else ([],[],[])
|
| 553 |
+
|
| 554 |
+
up_move = [math.nan] * n
|
| 555 |
+
dn_move = [math.nan] * n
|
| 556 |
+
|
| 557 |
+
for i in range(n):
|
| 558 |
+
if i > 0:
|
| 559 |
+
# NaN propagation: if high[i] or high[i-1] is NaN, up_move[i] is NaN.
|
| 560 |
+
up_move[i] = high[i] - high[i-1]
|
| 561 |
+
dn_move[i] = low[i-1] - low[i]
|
| 562 |
+
else: # TS: up/dn are 0 for i=0.
|
| 563 |
+
up_move[i] = 0.0
|
| 564 |
+
dn_move[i] = 0.0
|
| 565 |
+
|
| 566 |
+
plus_dm = [0.0] * n # Initialized to 0.0 as per TS fallback
|
| 567 |
+
minus_dm = [0.0] * n
|
| 568 |
+
|
| 569 |
+
for i in range(n):
|
| 570 |
+
u = up_move[i]
|
| 571 |
+
d = dn_move[i]
|
| 572 |
+
# Comparisons with NaN (e.g. NaN > 0) are False.
|
| 573 |
+
# So if u or d is NaN, conditions fail, and plus_dm/minus_dm remain 0 for that index.
|
| 574 |
+
if not math.isnan(u) and not math.isnan(d) and u > d and u > 0:
|
| 575 |
+
plus_dm[i] = u
|
| 576 |
+
# else: plus_dm[i] remains 0.0 (already initialized)
|
| 577 |
+
|
| 578 |
+
if not math.isnan(d) and not math.isnan(u) and d > u and d > 0:
|
| 579 |
+
minus_dm[i] = d
|
| 580 |
+
# else: minus_dm[i] remains 0.0
|
| 581 |
+
|
| 582 |
+
atr_arr = wilder_atr(high, low, close, length)
|
| 583 |
+
|
| 584 |
+
plus_dm_rma = rma(plus_dm, length)
|
| 585 |
+
minus_dm_rma = rma(minus_dm, length)
|
| 586 |
+
|
| 587 |
+
plus_di = [math.nan] * n
|
| 588 |
+
minus_di = [math.nan] * n
|
| 589 |
+
|
| 590 |
+
for i in range(n):
|
| 591 |
+
atr_val = atr_arr[i] # Can be NaN
|
| 592 |
+
# Division by zero or NaN atr_val
|
| 593 |
+
if not math.isnan(atr_val) and atr_val != 0:
|
| 594 |
+
# plus_dm_rma[i] can be NaN
|
| 595 |
+
if not math.isnan(plus_dm_rma[i]):
|
| 596 |
+
plus_di[i] = (100.0 * plus_dm_rma[i]) / atr_val
|
| 597 |
+
if not math.isnan(minus_dm_rma[i]):
|
| 598 |
+
minus_di[i] = (100.0 * minus_dm_rma[i]) / atr_val
|
| 599 |
+
# else DI remains NaN
|
| 600 |
+
|
| 601 |
+
dx = [math.nan] * n
|
| 602 |
+
for i in range(n):
|
| 603 |
+
pdi = plus_di[i]
|
| 604 |
+
mdi = minus_di[i]
|
| 605 |
+
if not math.isnan(pdi) and not math.isnan(mdi):
|
| 606 |
+
sum_di = pdi + mdi
|
| 607 |
+
if sum_di != 0: # Avoid division by zero
|
| 608 |
+
dx[i] = (100.0 * abs(pdi - mdi)) / sum_di
|
| 609 |
+
# else dx[i] remains NaN (covers pdi+mdi=0, leading to NaN in TS due to X/0 or 0/0)
|
| 610 |
+
|
| 611 |
+
adx = rma(dx, length)
|
| 612 |
+
|
| 613 |
+
return (plus_di, minus_di, adx)
|
| 614 |
+
|
| 615 |
+
# /* ───────── session VWAP (Resets each calendar day) ───────── */
|
| 616 |
+
def vwap_session(
|
| 617 |
+
close: List[float], volume: List[float], timestamp: List[int]
|
| 618 |
+
) -> List[float]:
|
| 619 |
+
"""Calculates session-based VWAP, resetting daily."""
|
| 620 |
+
n = len(close)
|
| 621 |
+
if n == 0 or not (n == len(volume) == len(timestamp)):
|
| 622 |
+
return [math.nan] * n if n > 0 else []
|
| 623 |
+
|
| 624 |
+
out = [math.nan] * n
|
| 625 |
+
|
| 626 |
+
def to_ms_ts(t: int) -> int: # Ensure timestamp is in milliseconds
|
| 627 |
+
return t * 1000 if t < 1_000_000_000_000 else t
|
| 628 |
+
|
| 629 |
+
sum_pv = 0.0
|
| 630 |
+
sum_v = 0.0
|
| 631 |
+
|
| 632 |
+
# JS toDateString() is locale-specific for its string format but represents a specific day.
|
| 633 |
+
# For Python, to match, use local timezone from timestamp for date boundary.
|
| 634 |
+
# A fixed format like YYYY-MM-DD is generally stabler.
|
| 635 |
+
# datetime.fromtimestamp(seconds_since_epoch) uses local timezone by default.
|
| 636 |
+
try:
|
| 637 |
+
# Initial day string based on local timezone interpretation of timestamp
|
| 638 |
+
first_ts_ms = to_ms_ts(timestamp[0])
|
| 639 |
+
cur_day_str = datetime.fromtimestamp(first_ts_ms / 1000.0).strftime('%Y-%m-%d')
|
| 640 |
+
except IndexError: # Should be caught by n==0
|
| 641 |
+
return []
|
| 642 |
+
|
| 643 |
+
for i in range(n):
|
| 644 |
+
current_close = close[i]
|
| 645 |
+
current_volume = volume[i]
|
| 646 |
+
ts_ms = to_ms_ts(timestamp[i])
|
| 647 |
+
|
| 648 |
+
# NaN propagation: if current_close or current_volume is NaN, sum_pv/sum_v become NaN
|
| 649 |
+
|
| 650 |
+
day_str_loop = datetime.fromtimestamp(ts_ms / 1000.0).strftime('%Y-%m-%d')
|
| 651 |
+
|
| 652 |
+
if day_str_loop != cur_day_str: # New day
|
| 653 |
+
sum_pv = 0.0
|
| 654 |
+
sum_v = 0.0
|
| 655 |
+
cur_day_str = day_str_loop
|
| 656 |
+
|
| 657 |
+
# If current_close or current_volume is NaN, product is NaN. sum_pv becomes NaN.
|
| 658 |
+
sum_pv += current_close * current_volume
|
| 659 |
+
# If current_volume is NaN, sum_v becomes NaN.
|
| 660 |
+
sum_v += current_volume
|
| 661 |
+
|
| 662 |
+
# Check for NaN in sums before division
|
| 663 |
+
if math.isnan(sum_pv) or math.isnan(sum_v):
|
| 664 |
+
out[i] = math.nan
|
| 665 |
+
elif sum_v != 0:
|
| 666 |
+
out[i] = sum_pv / sum_v
|
| 667 |
+
else: # sum_v is 0 (and not NaN)
|
| 668 |
+
out[i] = current_close # Fallback to current close price
|
| 669 |
+
|
| 670 |
+
return out
|
| 671 |
+
|
| 672 |
+
# /* ───────── bullish-probability ───────── */
|
| 673 |
+
def bullish_probability(
|
| 674 |
+
rsi: float, macd_hist: float, adx: float, st_k: float, st_d: float,
|
| 675 |
+
price: float, vwap_val: float,
|
| 676 |
+
lips: float, teeth: float, jaw: float
|
| 677 |
+
) -> float:
|
| 678 |
+
"""Calculates a bullish probability score."""
|
| 679 |
+
count = 0
|
| 680 |
+
# as_bool handles None/NaN correctly for conditions
|
| 681 |
+
count += 1 if as_bool(rsi > 50) else 0
|
| 682 |
+
count += 1 if as_bool(macd_hist > 0) else 0
|
| 683 |
+
count += 1 if as_bool(adx > 25) else 0
|
| 684 |
+
count += 1 if as_bool(st_k > st_d and st_k > 50) else 0
|
| 685 |
+
count += 1 if as_bool(price > vwap_val) else 0
|
| 686 |
+
count += 1 if as_bool(lips > teeth and teeth > jaw) else 0
|
| 687 |
+
|
| 688 |
+
probability = (count / 6.0) * 100.0
|
| 689 |
+
# Emulate Number(...toFixed(2)): convert to string with 2 decimal places, then to float
|
| 690 |
+
# This also handles rounding like toFixed (0.5 rounds away from zero).
|
| 691 |
+
# Python's f-string formatting with .2f rounds .5 to nearest even.
|
| 692 |
+
# For precise toFixed(2) behavior:
|
| 693 |
+
if math.isnan(probability): return math.nan
|
| 694 |
+
return float(f"{probability:.2f}") # Standard rounding often used in Python.
|
| 695 |
+
# For exact JS .toFixed() rounding:
|
| 696 |
+
# temp_str = format(Decimal(str(probability)), '.2f') # using Decimal for precise rounding
|
| 697 |
+
# return float(temp_str)
|
| 698 |
+
# Or simpler if precision needs are met by f-string:
|
| 699 |
+
# return round(probability * 100) / 100 # Not quite toFixed
|
| 700 |
+
# The provided TS likely relies on standard float to string formatting.
|
| 701 |
+
|
| 702 |
+
# /* ───────── probability label ───────── */
|
| 703 |
+
def _custom_round_js_style(val: float) -> int:
|
| 704 |
+
"""Emulates JavaScript's Math.round (0.5 rounds away from zero)."""
|
| 705 |
+
if math.isnan(val): return 0 # Or handle as error/NaN string
|
| 706 |
+
if val >= 0:
|
| 707 |
+
return math.floor(val + 0.5)
|
| 708 |
+
else:
|
| 709 |
+
return math.ceil(val - 0.5)
|
| 710 |
+
|
| 711 |
+
def probability_label(p: float) -> str:
|
| 712 |
+
"""Generates a descriptive label based on probability."""
|
| 713 |
+
desc = ""
|
| 714 |
+
if math.isnan(p):
|
| 715 |
+
desc = "Unknown"
|
| 716 |
+
elif p == 0:
|
| 717 |
+
desc = 'Sideways'
|
| 718 |
+
elif p <= 30:
|
| 719 |
+
desc = 'Bearish'
|
| 720 |
+
elif p <= 40:
|
| 721 |
+
desc = 'Koreksi Lanjutan'
|
| 722 |
+
elif p <= 50:
|
| 723 |
+
desc = 'Konsolidasi'
|
| 724 |
+
elif p <= 60:
|
| 725 |
+
desc = 'Teknikal Rebound'
|
| 726 |
+
else: # p > 60
|
| 727 |
+
desc = 'Probabilitas Bullish'
|
| 728 |
+
|
| 729 |
+
rounded_p_str = str(_custom_round_js_style(p)) if not math.isnan(p) else "N/A"
|
| 730 |
+
return f"{desc} ({rounded_p_str}%)"
|
| 731 |
+
|
| 732 |
+
# /* ───────── stage detector ───────── */
|
| 733 |
+
def stage_name(
|
| 734 |
+
close_val: float, macd_l_now: float, macd_l_prev: float,
|
| 735 |
+
macd_s_now: float, macd_s_prev: float,
|
| 736 |
+
rsi_val: float, ma50_val: float
|
| 737 |
+
) -> str:
|
| 738 |
+
"""Detects market stage based on indicators."""
|
| 739 |
+
# NaN comparisons evaluate to False, naturally leading to 'Netral' if critical values are NaN.
|
| 740 |
+
cond1 = (macd_l_prev < macd_s_prev and macd_l_now > macd_s_now and
|
| 741 |
+
rsi_val > 40 and rsi_val < 60 and
|
| 742 |
+
close_val < ma50_val)
|
| 743 |
+
if as_bool(cond1): return '1: Akumulasi' # Using as_bool for safety with potential None/NaN inputs
|
| 744 |
+
|
| 745 |
+
cond2 = (macd_l_now > macd_s_now and
|
| 746 |
+
rsi_val > 55 and
|
| 747 |
+
close_val > ma50_val)
|
| 748 |
+
if as_bool(cond2): return '2: Tren Naik'
|
| 749 |
+
|
| 750 |
+
cond3 = (macd_l_prev > macd_s_prev and macd_l_now < macd_s_now and
|
| 751 |
+
rsi_val > 60 and rsi_val < 70)
|
| 752 |
+
if as_bool(cond3): return '3: Distribusi'
|
| 753 |
+
|
| 754 |
+
cond4 = (macd_l_now < macd_s_now and
|
| 755 |
+
rsi_val < 45 and
|
| 756 |
+
close_val < ma50_val)
|
| 757 |
+
if as_bool(cond4): return '4: Tren Turun'
|
| 758 |
+
|
| 759 |
+
return 'Netral'
|
| 760 |
+
|
| 761 |
+
# Helper for arfoxScoreSeries: pandas-like shift
|
| 762 |
+
def _shift_series(series: List[float], periods: int) -> List[float]:
|
| 763 |
+
n = len(series)
|
| 764 |
+
if periods == 0:
|
| 765 |
+
return list(series) # Return a copy
|
| 766 |
+
|
| 767 |
+
shifted = [math.nan] * n
|
| 768 |
+
if periods > 0: # Positive shift, values from the past: shifted[i] = series[i-periods]
|
| 769 |
+
for i in range(periods, n):
|
| 770 |
+
shifted[i] = series[i - periods]
|
| 771 |
+
else: # Negative shift (not used in TS), values from the future
|
| 772 |
+
abs_periods = abs(periods)
|
| 773 |
+
for i in range(n - abs_periods):
|
| 774 |
+
shifted[i] = series[i + abs_periods]
|
| 775 |
+
return shifted
|
| 776 |
+
|
| 777 |
+
# /* ───────── full Arfox raw-score series ───────── */
|
| 778 |
+
def arfox_score_series(
|
| 779 |
+
price: List[float], volume: List[float], high: List[float], low: List[float], timestamp_ms: List[int]
|
| 780 |
+
) -> List[float]:
|
| 781 |
+
"""Calculates the Arfox raw score series."""
|
| 782 |
+
n_periods = len(price)
|
| 783 |
+
if n_periods == 0: return []
|
| 784 |
+
|
| 785 |
+
ma_local = rolling_mean # Use the globally defined rolling_mean
|
| 786 |
+
|
| 787 |
+
ma5 = ma_local(price, 5)
|
| 788 |
+
ma20 = ma_local(price, 20)
|
| 789 |
+
ma50 = ma_local(price, 50)
|
| 790 |
+
ma100 = ma_local(price, 100)
|
| 791 |
+
ma200 = ma_local(price, 200)
|
| 792 |
+
ma10v = ma_local(volume, 10)
|
| 793 |
+
|
| 794 |
+
prev_price = [math.nan] * n_periods
|
| 795 |
+
prev_vol = [math.nan] * n_periods
|
| 796 |
+
if n_periods > 0:
|
| 797 |
+
prev_price[0] = price[0] # TS: [price[0]].concat(price.slice(0,-1)) -> prevPrice[0] = price[0]
|
| 798 |
+
prev_vol[0] = volume[0] # Same for volume
|
| 799 |
+
for i in range(1, n_periods):
|
| 800 |
+
prev_price[i] = price[i-1]
|
| 801 |
+
prev_vol[i] = volume[i-1]
|
| 802 |
+
|
| 803 |
+
_macd_l, _macd_s, macd_hist = macd_calc(price)
|
| 804 |
+
_plus_di, _minus_di, adx_arr = dmi_calc(high, low, price)
|
| 805 |
+
st_k_arr, st_d_arr = stoch_kd(price, high, low)
|
| 806 |
+
|
| 807 |
+
high_roll_max10 = rolling_max(high, 10)
|
| 808 |
+
low_roll_min10 = rolling_min(low, 10)
|
| 809 |
+
rng10 = [(hr - lr) if not math.isnan(hr) and not math.isnan(lr) else math.nan
|
| 810 |
+
for hr, lr in zip(high_roll_max10, low_roll_min10)]
|
| 811 |
+
|
| 812 |
+
std20 = rolling_std(price, 20)
|
| 813 |
+
bbw = [(s * 2.0) if not math.isnan(s) else math.nan for s in std20]
|
| 814 |
+
bbw50 = ma_local(bbw, 50)
|
| 815 |
+
|
| 816 |
+
obv = [0.0] * n_periods
|
| 817 |
+
if n_periods > 0:
|
| 818 |
+
acc_obv = 0.0
|
| 819 |
+
# obv[0] = 0 as sign for i=0 is 0 in TS logic
|
| 820 |
+
for i in range(n_periods):
|
| 821 |
+
sign_val = 0.0
|
| 822 |
+
if i > 0:
|
| 823 |
+
price_diff = price[i] - price[i-1]
|
| 824 |
+
if math.isnan(price_diff): sign_val = math.nan # Match JS Math.sign(NaN) = NaN
|
| 825 |
+
elif price_diff > 0: sign_val = 1.0
|
| 826 |
+
elif price_diff < 0: sign_val = -1.0
|
| 827 |
+
# else sign_val is 0.0
|
| 828 |
+
|
| 829 |
+
term = sign_val * volume[i] # This can be NaN if sign_val or volume[i] is NaN
|
| 830 |
+
|
| 831 |
+
if math.isnan(acc_obv): pass # acc_obv remains NaN
|
| 832 |
+
elif math.isnan(term): acc_obv = math.nan
|
| 833 |
+
else: acc_obv += term
|
| 834 |
+
obv[i] = acc_obv
|
| 835 |
+
obv50 = ma_local(obv, 50)
|
| 836 |
+
|
| 837 |
+
vwap_arr = vwap_session(price, volume, timestamp_ms)
|
| 838 |
+
atr14 = wilder_atr(high, low, price, 14)
|
| 839 |
+
atr50 = ma_local(atr14, 50)
|
| 840 |
+
|
| 841 |
+
# Alligator lines using shifted MAs
|
| 842 |
+
lips = _shift_series(ma_local(price, 5), 3)
|
| 843 |
+
teeth = _shift_series(ma_local(price, 8), 5)
|
| 844 |
+
jaw = _shift_series(ma_local(price, 13), 8)
|
| 845 |
+
|
| 846 |
+
score = [10.0] * n_periods
|
| 847 |
+
|
| 848 |
+
# Use the globally defined wilder_rsi
|
| 849 |
+
rsi_arr_for_score = wilder_rsi(price, 14)
|
| 850 |
+
|
| 851 |
+
def add_score_item(idx: int, condition_val: bool, points_if_true: float, points_if_false: float):
|
| 852 |
+
# condition_val is already a resolved boolean from Python's NaN comparison behavior.
|
| 853 |
+
score[idx] += points_if_true if condition_val else points_if_false
|
| 854 |
+
|
| 855 |
+
for i in range(n_periods):
|
| 856 |
+
# Explicit NaN checks for conditions to ensure safety and clarity
|
| 857 |
+
p_i, ma5_i, pp_i = price[i], ma5[i], prev_price[i]
|
| 858 |
+
v_i, ma10v_i, pv_i = volume[i], ma10v[i], prev_vol[i]
|
| 859 |
+
ma20_i, ma50_i = ma20[i], ma50[i]
|
| 860 |
+
ma100_i, ma200_i = ma100[i], ma200[i]
|
| 861 |
+
rsi_i, macd_h_i, adx_i_sc = rsi_arr_for_score[i], macd_hist[i], adx_arr[i] # Renamed adx_i to adx_i_sc
|
| 862 |
+
rng10_i, stk_i, std_i = rng10[i], st_k_arr[i], st_d_arr[i]
|
| 863 |
+
bbw_i, bbw50_i_sc = bbw[i], bbw50[i] # Renamed bbw50_i to bbw50_i_sc
|
| 864 |
+
obv_i, obv50_i_sc = obv[i], obv50[i] # Renamed obv50_i to obv50_i_sc
|
| 865 |
+
vwap_i, atr14_i, atr50_i_sc = vwap_arr[i], atr14[i], atr50[i] # Renamed atr50_i to atr50_i_sc
|
| 866 |
+
lips_i, teeth_i, jaw_i = lips[i], teeth[i], jaw[i]
|
| 867 |
+
|
| 868 |
+
add_score_item(i, not math.isnan(p_i) and p_i >= 60, 10, -5)
|
| 869 |
+
add_score_item(i, not math.isnan(p_i) and not math.isnan(ma5_i) and p_i >= ma5_i, 10, -5)
|
| 870 |
+
add_score_item(i, not math.isnan(p_i) and not math.isnan(pp_i) and p_i > pp_i, 10, -5)
|
| 871 |
+
add_score_item(i, not math.isnan(pp_i) and pp_i >= 1, 5, -5)
|
| 872 |
+
|
| 873 |
+
change_cond = False
|
| 874 |
+
if not math.isnan(p_i) and not math.isnan(pp_i) and pp_i != 0:
|
| 875 |
+
change = ((p_i - pp_i) / pp_i) * 100.0
|
| 876 |
+
if not math.isnan(change) and change > 1: change_cond = True
|
| 877 |
+
add_score_item(i, change_cond, 10, -5)
|
| 878 |
+
|
| 879 |
+
vol_cond1 = False
|
| 880 |
+
if not math.isnan(v_i) and not math.isnan(ma10v_i) and ma10v_i != 0 : # Check ma10v_i != 0 if it could be
|
| 881 |
+
if v_i >= 2 * ma10v_i : vol_cond1 = True
|
| 882 |
+
elif not math.isnan(v_i) and not math.isnan(ma10v_i) and ma10v_i == 0 and v_i >=0 : # v_i >= 2*0
|
| 883 |
+
vol_cond1 = True
|
| 884 |
+
add_score_item(i, vol_cond1, 10, -5)
|
| 885 |
+
|
| 886 |
+
add_score_item(i, not math.isnan(v_i) and not math.isnan(pv_i) and v_i >= pv_i, 10, -5)
|
| 887 |
+
|
| 888 |
+
turnover_cond = False
|
| 889 |
+
if not math.isnan(v_i) and not math.isnan(p_i):
|
| 890 |
+
if (v_i * p_i) >= 5e10: turnover_cond = True
|
| 891 |
+
add_score_item(i, turnover_cond, 10, -10)
|
| 892 |
+
|
| 893 |
+
score[i] += 5 # bandar placeholder
|
| 894 |
+
|
| 895 |
+
cross_up, cross_dn = False, False
|
| 896 |
+
if i > 0: # Need previous values for MAs
|
| 897 |
+
ma20_prev, ma50_prev = ma20[i-1], ma50[i-1]
|
| 898 |
+
if not math.isnan(ma20_prev) and not math.isnan(ma50_prev) and \
|
| 899 |
+
not math.isnan(ma20_i) and not math.isnan(ma50_i):
|
| 900 |
+
if ma20_prev < ma50_prev and ma20_i > ma50_i: cross_up = True
|
| 901 |
+
if ma20_prev > ma50_prev and ma20_i < ma50_i: cross_dn = True
|
| 902 |
+
add_score_item(i, cross_up, 20, 0)
|
| 903 |
+
add_score_item(i, cross_dn, -20, 0) # if true, add -20, else add 0.
|
| 904 |
+
|
| 905 |
+
add_score_item(i, not math.isnan(ma20_i) and not math.isnan(ma50_i) and ma20_i > ma50_i, 15, -10)
|
| 906 |
+
add_score_item(i, not math.isnan(ma50_i) and not math.isnan(ma100_i) and ma50_i > ma100_i, 15, -10)
|
| 907 |
+
add_score_item(i, not math.isnan(ma100_i) and not math.isnan(ma200_i) and ma100_i > ma200_i, 15, -10)
|
| 908 |
+
|
| 909 |
+
add_score_item(i, not math.isnan(rsi_i) and rsi_i > 50, 5, -5)
|
| 910 |
+
add_score_item(i, not math.isnan(macd_h_i) and macd_h_i > 0, 5, -5)
|
| 911 |
+
add_score_item(i, not math.isnan(adx_i_sc) and adx_i_sc > 25, 10, -5)
|
| 912 |
+
|
| 913 |
+
rng_contr_cond = False
|
| 914 |
+
if not math.isnan(rng10_i) and not math.isnan(p_i) and p_i != 0:
|
| 915 |
+
if rng10_i < (p_i * 0.02): rng_contr_cond = True
|
| 916 |
+
elif not math.isnan(rng10_i) and not math.isnan(p_i) and p_i == 0 and rng10_i < 0: # rng10_i < 0 if p_i is 0
|
| 917 |
+
rng_contr_cond = True # If price is 0, 2% of price is 0. Range must be < 0 (e.g. negative range, not typical)
|
| 918 |
+
add_score_item(i, rng_contr_cond, -5, 0)
|
| 919 |
+
|
| 920 |
+
stoch_bull_cond = False
|
| 921 |
+
if not math.isnan(stk_i) and not math.isnan(std_i):
|
| 922 |
+
if stk_i > std_i and stk_i > 50: stoch_bull_cond = True
|
| 923 |
+
add_score_item(i, stoch_bull_cond, 5, -5)
|
| 924 |
+
|
| 925 |
+
add_score_item(i, not math.isnan(bbw_i) and not math.isnan(bbw50_i_sc) and bbw_i > bbw50_i_sc, 5, 0)
|
| 926 |
+
add_score_item(i, not math.isnan(obv_i) and not math.isnan(obv50_i_sc) and obv_i > obv50_i_sc, 5, 0)
|
| 927 |
+
add_score_item(i, not math.isnan(p_i) and not math.isnan(vwap_i) and p_i > vwap_i, 5, -5)
|
| 928 |
+
add_score_item(i, not math.isnan(atr14_i) and not math.isnan(atr50_i_sc) and atr14_i > atr50_i_sc, 5, 0)
|
| 929 |
+
|
| 930 |
+
alligator_bull_cond = False
|
| 931 |
+
if not math.isnan(lips_i) and not math.isnan(teeth_i) and not math.isnan(jaw_i):
|
| 932 |
+
if lips_i > teeth_i and teeth_i > jaw_i: alligator_bull_cond = True
|
| 933 |
+
add_score_item(i, alligator_bull_cond, 10, -10)
|
| 934 |
+
|
| 935 |
+
current_score_val = score[i]
|
| 936 |
+
if math.isnan(current_score_val): score[i] = 10.0 # Default to min if NaN
|
| 937 |
+
else: score[i] = max(10.0, min(100.0, current_score_val))
|
| 938 |
+
|
| 939 |
+
return score
|
| 940 |
+
|
| 941 |
+
# /* ───────── Conservative S/R ATR ───────── */
|
| 942 |
+
def sr_atr_conservative(
|
| 943 |
+
high: List[float], low: List[float], atr_arr: List[float],
|
| 944 |
+
sr_len: int = 20, atr_mult: float = 1.5
|
| 945 |
+
) -> Tuple[List[float], List[float], List[float], List[float]]:
|
| 946 |
+
"""Calculates conservative Support/Resistance levels using ATR."""
|
| 947 |
+
n = len(high)
|
| 948 |
+
if not (n == len(low) == len(atr_arr)):
|
| 949 |
+
if n > 0: # Base length on high if available
|
| 950 |
+
nan_list = [math.nan] * n
|
| 951 |
+
return (nan_list, nan_list, nan_list, nan_list)
|
| 952 |
+
return ([], [], [], []) # All inputs potentially empty
|
| 953 |
+
|
| 954 |
+
support = rolling_min(low, sr_len)
|
| 955 |
+
resistance = rolling_max(high, sr_len)
|
| 956 |
+
|
| 957 |
+
sl_con = [(s - atr_arr[i] * atr_mult) if not math.isnan(s) and i < len(atr_arr) and not math.isnan(atr_arr[i]) else math.nan
|
| 958 |
+
for i, s in enumerate(support)]
|
| 959 |
+
|
| 960 |
+
tp_con = [(r + atr_arr[i] * atr_mult) if not math.isnan(r) and i < len(atr_arr) and not math.isnan(atr_arr[i]) else math.nan
|
| 961 |
+
for i, r in enumerate(resistance)]
|
| 962 |
+
|
| 963 |
+
return (support, resistance, sl_con, tp_con)
|
| 964 |
+
|
| 965 |
+
# Define a type hint for the candle data for clarity
|
| 966 |
+
Candle = Dict[str, Any]
|
| 967 |
+
|
| 968 |
+
def fetch_yahoo(
|
| 969 |
+
symbol: str,
|
| 970 |
+
interval: str = '1h',
|
| 971 |
+
start_date: str = None,
|
| 972 |
+
end_date: str = None,
|
| 973 |
+
max_retry: int = 3,
|
| 974 |
+
timeout: int = 15
|
| 975 |
+
) -> List[Candle]:
|
| 976 |
+
"""
|
| 977 |
+
Fetches historical market data from Yahoo Finance with retry and timeout logic.
|
| 978 |
+
"""
|
| 979 |
+
start_ts = int(datetime.strptime(start_date, '%Y-%m-%d').timestamp())
|
| 980 |
+
end_ts = int(datetime.strptime(end_date, '%Y-%m-%d').timestamp())
|
| 981 |
+
|
| 982 |
+
api_url = (
|
| 983 |
+
f"https://query1.finance.yahoo.com/v8/finance/chart/{symbol}"
|
| 984 |
+
f"?period1={start_ts}&period2={end_ts}&interval={interval}"
|
| 985 |
+
f"&includePrePost=true&events=div|split"
|
| 986 |
+
)
|
| 987 |
+
print(api_url)
|
| 988 |
+
headers = {
|
| 989 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
|
| 990 |
+
}
|
| 991 |
+
|
| 992 |
+
res = None
|
| 993 |
+
for attempt in range(1, max_retry + 1):
|
| 994 |
+
try:
|
| 995 |
+
res = requests.get(api_url, headers=headers, timeout=timeout)
|
| 996 |
+
res.raise_for_status()
|
| 997 |
+
break
|
| 998 |
+
except (requests.exceptions.RequestException, requests.exceptions.HTTPError) as e:
|
| 999 |
+
# print(f"Attempt {attempt} for {symbol} failed: {e}")
|
| 1000 |
+
if attempt == max_retry:
|
| 1001 |
+
return [] # Return empty list on failure
|
| 1002 |
+
time.sleep(1 * attempt)
|
| 1003 |
+
|
| 1004 |
+
if not res:
|
| 1005 |
+
return []
|
| 1006 |
+
|
| 1007 |
+
js = res.json()
|
| 1008 |
+
chart_result = js.get('chart', {}).get('result')
|
| 1009 |
+
if not chart_result or not chart_result[0]:
|
| 1010 |
+
return []
|
| 1011 |
+
|
| 1012 |
+
res_data = chart_result[0]
|
| 1013 |
+
timestamps = res_data.get('timestamp', [])
|
| 1014 |
+
quote = res_data.get('indicators', {}).get('quote', [{}])[0]
|
| 1015 |
+
|
| 1016 |
+
candles: List[Candle] = []
|
| 1017 |
+
for i, t in enumerate(timestamps):
|
| 1018 |
+
candles.append({
|
| 1019 |
+
't': t * 1000,
|
| 1020 |
+
'o': quote.get('open', [])[i], 'h': quote.get('high', [])[i],
|
| 1021 |
+
'l': quote.get('low', [])[i], 'c': quote.get('close', [])[i],
|
| 1022 |
+
'v': quote.get('volume', [])[i],
|
| 1023 |
+
})
|
| 1024 |
+
|
| 1025 |
+
return [c for c in candles if c.get('c') is not None]
|
| 1026 |
+
|
| 1027 |
+
Row = Dict[str, Any]
|
| 1028 |
+
|
| 1029 |
+
# IDX tick size helpers
|
| 1030 |
+
def tick_step(p: float) -> int:
|
| 1031 |
+
if p < 200: return 1
|
| 1032 |
+
if p < 500: return 2
|
| 1033 |
+
if p < 2000: return 5
|
| 1034 |
+
if p < 5000: return 10
|
| 1035 |
+
return 25
|
| 1036 |
+
|
| 1037 |
+
def round_idx(p: float, direction: str = 'nearest') -> int:
|
| 1038 |
+
if math.isnan(p): return p
|
| 1039 |
+
s = tick_step(p)
|
| 1040 |
+
if direction == 'up': return math.ceil(p / s) * s
|
| 1041 |
+
if direction == 'down': return math.floor(p / s) * s
|
| 1042 |
+
return round(p / s) * s
|
| 1043 |
+
|
| 1044 |
+
# Price formatter
|
| 1045 |
+
def fmt(p: float, mkt: str, direction: str = 'nearest') -> str:
|
| 1046 |
+
if math.isnan(p): return 'N/A'
|
| 1047 |
+
if mkt == 'IDX':
|
| 1048 |
+
return str(round_idx(p, direction))
|
| 1049 |
+
|
| 1050 |
+
d = 2 if p >= 1 else 4 # For US Market
|
| 1051 |
+
if mkt == 'CRYPTO': d = 4
|
| 1052 |
+
return f"{p:.{d}f}"
|
| 1053 |
+
|
| 1054 |
+
# Trend flip helper
|
| 1055 |
+
def flip_since(trend: List[int], look: int = 60) -> Dict[str, int]:
|
| 1056 |
+
if not trend: return {'bars': 0}
|
| 1057 |
+
cur = last(trend)
|
| 1058 |
+
i = len(trend) - 1
|
| 1059 |
+
while i > 0 and trend[i] == cur and (len(trend) - 1 - i) < look:
|
| 1060 |
+
i -= 1
|
| 1061 |
+
idx = i + 1
|
| 1062 |
+
return {'bars': len(trend) - 1 - idx}
|
| 1063 |
+
|
| 1064 |
+
|
| 1065 |
+
def create_features_for_df(df: pd.DataFrame, timeframe_label: str) -> Dict[str, float]:
|
| 1066 |
+
"""
|
| 1067 |
+
Calculates a comprehensive and extensive set of features for a given dataframe
|
| 1068 |
+
and returns the last value of each.
|
| 1069 |
+
"""
|
| 1070 |
+
if df.empty or len(df) < 250:
|
| 1071 |
+
return {}
|
| 1072 |
+
|
| 1073 |
+
features = {}
|
| 1074 |
+
# Extract lists from the dataframe
|
| 1075 |
+
open_p = df['open'].tolist()
|
| 1076 |
+
close = df['close'].tolist()
|
| 1077 |
+
high = df['high'].tolist()
|
| 1078 |
+
low = df['low'].tolist()
|
| 1079 |
+
volume = df['volume'].tolist()
|
| 1080 |
+
timestamps_ms = (df.index.astype(np.int64) // 10**6).tolist()
|
| 1081 |
+
last_close = last(close)
|
| 1082 |
+
|
| 1083 |
+
# --- Foundational Indicators (used by other features) ---
|
| 1084 |
+
atr14 = wilder_atr(high, low, close, 14)
|
| 1085 |
+
last_atr14 = last(atr14)
|
| 1086 |
+
|
| 1087 |
+
## From build_row: ema50 is needed for trend_bull used in rsi_label ##
|
| 1088 |
+
ema50 = ema(close, 50)
|
| 1089 |
+
last_ema50 = last(ema50)
|
| 1090 |
+
trend_bull = last_close > last_ema50 if not math.isnan(last_close) and not math.isnan(last_ema50) else False
|
| 1091 |
+
|
| 1092 |
+
# --- 1. Price & Moving Average Features ---
|
| 1093 |
+
sma8 = rolling_mean(close, 8)
|
| 1094 |
+
sma20 = rolling_mean(close, 20)
|
| 1095 |
+
sma50 = rolling_mean(close, 50)
|
| 1096 |
+
sma200 = rolling_mean(close, 200)
|
| 1097 |
+
features['price_vs_sma20'] = (last_close / last(sma20)) - 1 if last(sma20) and not math.isnan(last(sma20)) else np.nan
|
| 1098 |
+
features['price_vs_sma50'] = (last_close / last(sma50)) - 1 if last(sma50) and not math.isnan(last(sma50)) else np.nan
|
| 1099 |
+
features['sma20_vs_sma50'] = (last(sma20) / last(sma50)) - 1 if last(sma50) and not math.isnan(last(sma50)) else np.nan
|
| 1100 |
+
features['sma50_vs_sma200'] = (last(sma50) / last(sma200)) - 1 if last(sma200) and not math.isnan(last(sma200)) else np.nan
|
| 1101 |
+
# Inspired by ma_labels: numerical representation of MA stack
|
| 1102 |
+
if last(sma8) > last(sma20) > last(sma50): features['ma_stack'] = 1
|
| 1103 |
+
elif last(sma8) < last(sma20) < last(sma50): features['ma_stack'] = -1
|
| 1104 |
+
else: features['ma_stack'] = 0
|
| 1105 |
+
|
| 1106 |
+
# --- 2. Momentum & Trend Features ---
|
| 1107 |
+
features['rsi_14'] = last(wilder_rsi(close, 14))
|
| 1108 |
+
macdL, macdS, macd_hist = macd_calc(close)
|
| 1109 |
+
features['macd_hist'] = last(macd_hist)
|
| 1110 |
+
stoch_k, stoch_d = stoch_kd(close, high, low, 14)
|
| 1111 |
+
features['stoch_k'] = last(stoch_k)
|
| 1112 |
+
features['stoch_d'] = last(stoch_d)
|
| 1113 |
+
plus_di, minus_di, adx = dmi_calc(high, low, close, 14)
|
| 1114 |
+
features['adx_14'] = last(adx)
|
| 1115 |
+
features['dmi_diff'] = last(plus_di) - last(minus_di)
|
| 1116 |
+
# Rate of Change (ROC) for 10 periods
|
| 1117 |
+
if len(close) > 10: features['roc_10'] = (last_close / close[-11] - 1) if close[-11] != 0 else np.nan
|
| 1118 |
+
|
| 1119 |
+
# Inspired by build_row: SuperTrend features
|
| 1120 |
+
st_line, st_trend = super_trend(close, high, low)
|
| 1121 |
+
flip_info = flip_since(st_trend)
|
| 1122 |
+
idx_start = len(st_trend) - 1 - flip_info['bars']
|
| 1123 |
+
entry_px = st_line[idx_start - 1] if idx_start > 0 else st_line[idx_start]
|
| 1124 |
+
features['supertrend_dir'] = last(st_trend)
|
| 1125 |
+
features['price_vs_supertrend'] = (last_close / last(st_line)) - 1 if last(st_line) else np.nan
|
| 1126 |
+
features['bars_since_st_flip'] = flip_info['bars']
|
| 1127 |
+
features['pl_since_st_flip'] = (last_close / entry_px - 1) if entry_px and not math.isnan(entry_px) else np.nan
|
| 1128 |
+
|
| 1129 |
+
# --- 3. Volatility Features ---
|
| 1130 |
+
features['atr_14_norm'] = (last_atr14 / last_close) if last_close and not math.isnan(last_close) else np.nan
|
| 1131 |
+
# Bollinger Bands
|
| 1132 |
+
std20 = rolling_std(close, 20)
|
| 1133 |
+
bb_mid = sma20
|
| 1134 |
+
bb_upper = [m + 2 * s for m, s in zip(bb_mid, std20)]
|
| 1135 |
+
bb_lower = [m - 2 * s for m, s in zip(bb_mid, std20)]
|
| 1136 |
+
bb_width = [(u - l) / m if m and not math.isnan(m) else np.nan for u, l, m in zip(bb_upper, bb_lower, bb_mid)]
|
| 1137 |
+
bb_percent_b = [(last_close - l) / (u - l) if (u-l) != 0 else np.nan for u,l in [(last(bb_upper), last(bb_lower))]]
|
| 1138 |
+
features['bb_width'] = last(bb_width)
|
| 1139 |
+
features['bb_percent_b'] = last(bb_percent_b)
|
| 1140 |
+
|
| 1141 |
+
# Inspired by build_row: Williams VIX Fix
|
| 1142 |
+
wvf, wvf_upper, _ = foxpro_wvf(close, low)
|
| 1143 |
+
features['wvf_raw'] = wvf
|
| 1144 |
+
features['wvf_vs_upper'] = (wvf / wvf_upper) - 1 if wvf_upper and not math.isnan(wvf_upper) else np.nan
|
| 1145 |
+
|
| 1146 |
+
# --- 4. Volume & High-Level Features ---
|
| 1147 |
+
vwap = vwap_session(close, volume, timestamps_ms)
|
| 1148 |
+
features['price_vs_vwap'] = (last_close / last(vwap)) - 1 if last(vwap) and not math.isnan(last(vwap)) else np.nan
|
| 1149 |
+
vol_sma20 = rolling_mean(volume, 20)
|
| 1150 |
+
features['volume_vs_sma20'] = (last(volume) / last(vol_sma20)) - 1 if last(vol_sma20) and not math.isnan(last(vol_sma20)) else np.nan
|
| 1151 |
+
# Inspired by build_row: Arfox Score
|
| 1152 |
+
score_series = arfox_score_series(close, volume, high, low, timestamps_ms)
|
| 1153 |
+
features['arfox_score'] = last(score_series)
|
| 1154 |
+
features['arfox_score_ma20'] = last(rolling_mean(score_series, 20))
|
| 1155 |
+
# Inspired by build_row: Stage Analysis (numerical)
|
| 1156 |
+
stage_str = stage_name(last_close, last(macdL), macdL[-2], last(macdS), macdS[-2], features['rsi_14'], last(sma50))
|
| 1157 |
+
stage_map = {'1: Akumulasi': 1, '2: Tren Naik': 2, '3: Distribusi': 3, '4: Tren Turun': 4}
|
| 1158 |
+
features['market_stage'] = stage_map.get(stage_str, 0) # 0 for Neutral
|
| 1159 |
+
|
| 1160 |
+
## From build_row: Bullish Probability ##
|
| 1161 |
+
lips, teeth, jaw = last(_shift_series(rolling_mean(close, 5), 3)), last(_shift_series(rolling_mean(close, 8), 5)), last(_shift_series(rolling_mean(close, 13), 8))
|
| 1162 |
+
features['bullish_prob_score'] = bullish_probability(features['rsi_14'], last(macd_hist), features['adx_14'], features['stoch_k'], features['stoch_d'], last_close, last(vwap), lips, teeth, jaw)
|
| 1163 |
+
|
| 1164 |
+
## From build_row: Conservative S/R ##
|
| 1165 |
+
sup, res, sl_con, tp_con = sr_atr_conservative(high, low, atr14)
|
| 1166 |
+
features['price_vs_support'] = (last_close / last(sup) - 1) if last(sup) else np.nan
|
| 1167 |
+
features['price_vs_resistance'] = (last_close / last(res) - 1) if last(res) else np.nan
|
| 1168 |
+
features['price_vs_sl_conserve'] = (last_close / last(sl_con) - 1) if last(sl_con) else np.nan
|
| 1169 |
+
|
| 1170 |
+
# --- 5. Price Action / Candlestick Features ---
|
| 1171 |
+
last_open = last(open_p)
|
| 1172 |
+
last_high = last(high)
|
| 1173 |
+
last_low = last(low)
|
| 1174 |
+
candle_range = last_high - last_low
|
| 1175 |
+
# Position of close within the full H-L range
|
| 1176 |
+
features['close_pos_in_range'] = (last_close - last_low) / candle_range if candle_range > 0 else 0.5
|
| 1177 |
+
# Normalized candle sizes
|
| 1178 |
+
if last_atr14 > 0:
|
| 1179 |
+
features['body_size_norm'] = abs(last_close - last_open) / last_atr14
|
| 1180 |
+
features['upper_wick_norm'] = (last_high - max(last_open, last_close)) / last_atr14
|
| 1181 |
+
features['lower_wick_norm'] = (min(last_open, last_close) - last_low) / last_atr14
|
| 1182 |
+
|
| 1183 |
+
# --- 6. NEW: Volume Profile Features (Optimized) ---
|
| 1184 |
+
vp_df = df.iloc[-100:].copy()
|
| 1185 |
+
# Initialize features to NaN to handle cases where calculation is skipped
|
| 1186 |
+
features['volume_profile_hvn_dist'] = np.nan
|
| 1187 |
+
features['volume_profile_lvn_dist'] = np.nan
|
| 1188 |
+
features['volume_profile_va_ratio'] = np.nan
|
| 1189 |
+
|
| 1190 |
+
if not vp_df.empty and vp_df['high'].max() > vp_df['low'].min():
|
| 1191 |
+
# Calculate Volume Profile
|
| 1192 |
+
price_range = vp_df['high'].max() - vp_df['low'].min()
|
| 1193 |
+
tick = tick_step(last_close)
|
| 1194 |
+
num_bins = int(price_range / tick) if tick > 0 else 20
|
| 1195 |
+
if num_bins < 2:
|
| 1196 |
+
num_bins = 2
|
| 1197 |
+
# Use observed=False to maintain old behavior and silence warning
|
| 1198 |
+
vp = vp_df.groupby(pd.cut(vp_df['close'], bins=num_bins, right=False), observed=False)['volume'].sum()
|
| 1199 |
+
|
| 1200 |
+
# Find Point of Control (POC), HVNs, and LVNs
|
| 1201 |
+
if not vp.empty:
|
| 1202 |
+
volume_threshold = vp.mean()
|
| 1203 |
+
hvns = vp[vp > volume_threshold]
|
| 1204 |
+
lvns = vp[vp < volume_threshold]
|
| 1205 |
+
|
| 1206 |
+
# Find nearest HVN and LVN
|
| 1207 |
+
if not hvns.empty:
|
| 1208 |
+
hvn_mids = pd.IntervalIndex(hvns.index).mid
|
| 1209 |
+
nearest_hvn = hvn_mids[np.abs(hvn_mids - last_close).argmin()]
|
| 1210 |
+
features['volume_profile_hvn_dist'] = (last_close / nearest_hvn - 1) if nearest_hvn != 0 else np.nan
|
| 1211 |
+
|
| 1212 |
+
if not lvns.empty:
|
| 1213 |
+
lvn_mids = pd.IntervalIndex(lvns.index).mid
|
| 1214 |
+
nearest_lvn = lvn_mids[np.abs(lvn_mids - last_close).argmin()]
|
| 1215 |
+
features['volume_profile_lvn_dist'] = (last_close / nearest_lvn - 1) if nearest_lvn != 0 else np.nan
|
| 1216 |
+
|
| 1217 |
+
# --- OPTIMIZED VALUE AREA CALCULATION ---
|
| 1218 |
+
total_volume = vp.sum()
|
| 1219 |
+
if total_volume > 0 and not vp.empty:
|
| 1220 |
+
# Sort bins by volume in descending order
|
| 1221 |
+
vp_sorted = vp.sort_values(ascending=False)
|
| 1222 |
+
|
| 1223 |
+
# Calculate cumulative share of volume
|
| 1224 |
+
vp_cumsum_share = vp_sorted.cumsum() / total_volume
|
| 1225 |
+
|
| 1226 |
+
# Filter to get the bins that make up the Value Area (70% of volume)
|
| 1227 |
+
value_area_bins = vp_sorted[vp_cumsum_share <= 0.70]
|
| 1228 |
+
|
| 1229 |
+
if not value_area_bins.empty:
|
| 1230 |
+
# Get the min and max price intervals from this group
|
| 1231 |
+
va_intervals = pd.IntervalIndex(value_area_bins.index)
|
| 1232 |
+
va_low = va_intervals.left.min()
|
| 1233 |
+
va_high = va_intervals.right.max()
|
| 1234 |
+
|
| 1235 |
+
# Calculate VA Ratio
|
| 1236 |
+
va_range = va_high - va_low
|
| 1237 |
+
if va_range > 0:
|
| 1238 |
+
if last_close > va_high:
|
| 1239 |
+
features['volume_profile_va_ratio'] = 1 + (last_close - va_high) / va_range
|
| 1240 |
+
elif last_close < va_low:
|
| 1241 |
+
features['volume_profile_va_ratio'] = 1 - (va_low - last_close) / va_range
|
| 1242 |
+
else:
|
| 1243 |
+
features['volume_profile_va_ratio'] = 1
|
| 1244 |
+
else: # Handle zero range case
|
| 1245 |
+
features['volume_profile_va_ratio'] = 1 if last_close == va_low else (2 if last_close > va_high else 0)
|
| 1246 |
+
return features
|
| 1247 |
+
|
| 1248 |
+
def generate_data_for_timeframe(timeframe: str, tickers: List[str], cfg: Dict) -> pd.DataFrame:
|
| 1249 |
+
"""
|
| 1250 |
+
Generates a complete training dataset for a single specified timeframe.
|
| 1251 |
+
It fetches data once per ticker, then samples and processes it.
|
| 1252 |
+
"""
|
| 1253 |
+
all_data_rows = []
|
| 1254 |
+
target_horizons = cfg["TARGET_HORIZONS"].get(timeframe, {})
|
| 1255 |
+
if not target_horizons:
|
| 1256 |
+
print(f"Warning: No target horizons defined for timeframe {timeframe}. Skipping.")
|
| 1257 |
+
return pd.DataFrame()
|
| 1258 |
+
|
| 1259 |
+
for ticker in tqdm(tickers, desc=f"Processing Tickers for {timeframe}"):
|
| 1260 |
+
# 1. Fetch one large chunk of data for the ticker for this timeframe
|
| 1261 |
+
fetch_start_dt = datetime.strptime(cfg["DATA_START_DATE"], '%Y-%m-%d') - timedelta(days=cfg["HISTORY_BUFFER_DAYS"])
|
| 1262 |
+
master_candles = fetch_yahoo(
|
| 1263 |
+
symbol=ticker,
|
| 1264 |
+
interval=timeframe,
|
| 1265 |
+
start_date=fetch_start_dt.strftime('%Y-%m-%d'),
|
| 1266 |
+
end_date=cfg["DATA_END_DATE"]
|
| 1267 |
+
)
|
| 1268 |
+
master_df = candles_to_dataframe(master_candles)
|
| 1269 |
+
if master_df.empty:
|
| 1270 |
+
print(f"DEBUG: fetch_yahoo returned no data for {ticker} on timeframe {timeframe}. Skipping.")
|
| 1271 |
+
continue
|
| 1272 |
+
|
| 1273 |
+
# 2. FIX: Identify a valid window for sampling that guarantees enough history for feature creation.
|
| 1274 |
+
min_history_required = 250 # As defined in create_features_for_df
|
| 1275 |
+
|
| 1276 |
+
# Find the first possible date we can sample from.
|
| 1277 |
+
first_valid_index_date = master_df.index[min_history_required] if len(master_df) > min_history_required else None
|
| 1278 |
+
|
| 1279 |
+
# If there's no valid date (not enough data overall), skip this ticker.
|
| 1280 |
+
if first_valid_index_date is None:
|
| 1281 |
+
print(f"DEBUG: {ticker} has fewer than {min_history_required} total data points. Skipping.")
|
| 1282 |
+
continue
|
| 1283 |
+
|
| 1284 |
+
# --- END BUFFER: Find the last possible date we can sample from ---
|
| 1285 |
+
max_horizon_candles = max(target_horizons.values()) if target_horizons else 0
|
| 1286 |
+
last_valid_index_date = master_df.index[-max_horizon_candles -1] if len(master_df) > max_horizon_candles else None
|
| 1287 |
+
|
| 1288 |
+
if last_valid_index_date is None:
|
| 1289 |
+
print(f"DEBUG: {ticker} does not have enough future data for the longest target horizon. Skipping.")
|
| 1290 |
+
continue
|
| 1291 |
+
|
| 1292 |
+
# --- Define the final sampling window with both buffers applied ---
|
| 1293 |
+
sampling_start_date = max(pd.to_datetime(cfg["DATA_START_DATE"]), first_valid_index_date)
|
| 1294 |
+
sampling_end_date = min(pd.to_datetime(cfg["DATA_END_DATE"]), last_valid_index_date)
|
| 1295 |
+
|
| 1296 |
+
sampling_window_df = master_df[
|
| 1297 |
+
(master_df.index >= sampling_start_date) &
|
| 1298 |
+
(master_df.index < sampling_end_date)
|
| 1299 |
+
]
|
| 1300 |
+
if sampling_window_df.empty:
|
| 1301 |
+
print(f"DEBUG: No data for {ticker} in the adjusted sampling window. Skipping.")
|
| 1302 |
+
continue
|
| 1303 |
+
|
| 1304 |
+
# 3. Get evenly spaced timestamps instead of random ones.
|
| 1305 |
+
n_samples = cfg["ROWS_PER_STOCK"]
|
| 1306 |
+
total_available_points = len(sampling_window_df)
|
| 1307 |
+
|
| 1308 |
+
if total_available_points < n_samples:
|
| 1309 |
+
# If we don't have enough data points for the desired sample size, use all available points.
|
| 1310 |
+
valid_timestamps = sampling_window_df.index.tolist()
|
| 1311 |
+
else:
|
| 1312 |
+
# Use np.linspace to get N evenly spaced indices from the start to the end of the dataframe.
|
| 1313 |
+
indices = np.linspace(0, total_available_points - 1, num=n_samples, dtype=int)
|
| 1314 |
+
print(total_available_points/n_samples)
|
| 1315 |
+
valid_timestamps = sampling_window_df.iloc[indices].index.tolist()
|
| 1316 |
+
|
| 1317 |
+
# 3. For each sampled timestamp, generate features and targets
|
| 1318 |
+
for ts in tqdm(valid_timestamps, desc=f"Sampling {ticker}", leave=False):
|
| 1319 |
+
# --- Feature Generation ---
|
| 1320 |
+
historical_df = master_df[master_df.index <= ts]
|
| 1321 |
+
feature_set = create_features_for_df(historical_df, timeframe)
|
| 1322 |
+
if not feature_set:
|
| 1323 |
+
print(f"DEBUG: Feature creation failed for {ticker} at {ts}. History length: {len(historical_df)}")
|
| 1324 |
+
continue
|
| 1325 |
+
|
| 1326 |
+
feature_set['ticker'] = ticker
|
| 1327 |
+
feature_set['timestamp'] = ts
|
| 1328 |
+
|
| 1329 |
+
# --- Target Calculation ---
|
| 1330 |
+
future_df = master_df[master_df.index > ts]
|
| 1331 |
+
current_price = historical_df.iloc[-1]['close']
|
| 1332 |
+
|
| 1333 |
+
if np.isnan(current_price) or current_price == 0:
|
| 1334 |
+
continue
|
| 1335 |
+
|
| 1336 |
+
for name, horizon_candles in target_horizons.items():
|
| 1337 |
+
if len(future_df) >= horizon_candles:
|
| 1338 |
+
future_candle = future_df.iloc[horizon_candles - 1]
|
| 1339 |
+
future_price = future_candle['close']
|
| 1340 |
+
pct_change = (future_price - current_price) / current_price
|
| 1341 |
+
|
| 1342 |
+
feature_set[f"{name}_pct_change"] = pct_change
|
| 1343 |
+
feature_set[f"{name}_end_time"] = future_candle.name
|
| 1344 |
+
else:
|
| 1345 |
+
feature_set[f"{name}_pct_change"] = np.nan
|
| 1346 |
+
feature_set[f"{name}_end_time"] = pd.NaT
|
| 1347 |
+
|
| 1348 |
+
# # --- NEW: Triple Barrier Label Calculation ---
|
| 1349 |
+
# label = 0 # Default to 0 (Hold/Timeout)
|
| 1350 |
+
# barrier_config = cfg.get("TRIPLE_BARRIER_CONFIG", {}).get(name)
|
| 1351 |
+
|
| 1352 |
+
# if barrier_config and len(future_df) >= horizon_candles:
|
| 1353 |
+
# upper_barrier = current_price * (1 + barrier_config["up"])
|
| 1354 |
+
# lower_barrier = current_price * (1 + barrier_config["down"])
|
| 1355 |
+
|
| 1356 |
+
# # Look at the price path over the defined horizon
|
| 1357 |
+
# path = future_df.iloc[:horizon_candles]
|
| 1358 |
+
|
| 1359 |
+
# for _, candle in path.iterrows():
|
| 1360 |
+
# if candle['high'] >= upper_barrier:
|
| 1361 |
+
# label = 1 # Price hit take-profit first
|
| 1362 |
+
# break
|
| 1363 |
+
# if candle['low'] <= lower_barrier:
|
| 1364 |
+
# label = -1 # Price hit stop-loss first
|
| 1365 |
+
# break
|
| 1366 |
+
# else:
|
| 1367 |
+
# label = np.nan # Not enough data to determine label
|
| 1368 |
+
|
| 1369 |
+
# feature_set[f"{name}_label"] = label
|
| 1370 |
+
|
| 1371 |
+
# --- NEW: Enhanced Triple Barrier (Level 1) ---
|
| 1372 |
+
# 2: Strong Buy, 1: Weak Buy (Fakeout), 0: Hold, -1: Weak Sell (Fakeout), -2: Strong Sell
|
| 1373 |
+
label = 0 # Default to Hold/Timeout
|
| 1374 |
+
barrier_config = cfg.get("TRIPLE_BARRIER_CONFIG", {}).get(name)
|
| 1375 |
+
|
| 1376 |
+
if barrier_config and len(future_df) >= horizon_candles:
|
| 1377 |
+
upper_barrier = current_price * (1 + barrier_config["up"])
|
| 1378 |
+
lower_barrier = current_price * (1 + barrier_config["down"])
|
| 1379 |
+
path = future_df.iloc[:horizon_candles]
|
| 1380 |
+
|
| 1381 |
+
for i, candle in enumerate(path.itertuples()):
|
| 1382 |
+
# Check for upper barrier touch
|
| 1383 |
+
if candle.high >= upper_barrier:
|
| 1384 |
+
label = 2 # Provisionally a Strong Buy
|
| 1385 |
+
# Check rest of path for a reversal to the lower barrier
|
| 1386 |
+
remaining_path = path.iloc[i+1:]
|
| 1387 |
+
if not remaining_path.empty and (remaining_path['low'] <= lower_barrier).any():
|
| 1388 |
+
label = 1 # It's a Weak Buy (bull trap)
|
| 1389 |
+
break # Outcome determined
|
| 1390 |
+
|
| 1391 |
+
# Check for lower barrier touch
|
| 1392 |
+
if candle.low <= lower_barrier:
|
| 1393 |
+
label = -2 # Provisionally a Strong Sell
|
| 1394 |
+
# Check rest of path for a reversal to the upper barrier
|
| 1395 |
+
remaining_path = path.iloc[i+1:]
|
| 1396 |
+
if not remaining_path.empty and (remaining_path['high'] >= upper_barrier).any():
|
| 1397 |
+
label = -1 # It's a Weak Sell (bear trap)
|
| 1398 |
+
break # Outcome determined
|
| 1399 |
+
else:
|
| 1400 |
+
label = np.nan # Not enough data to determine the label
|
| 1401 |
+
|
| 1402 |
+
feature_set[f"{name}_label"] = label
|
| 1403 |
+
|
| 1404 |
+
all_data_rows.append(feature_set)
|
| 1405 |
+
|
| 1406 |
+
if not all_data_rows:
|
| 1407 |
+
return pd.DataFrame()
|
| 1408 |
+
|
| 1409 |
+
# 4. Post-Processing: Convert to DataFrame and calculate final scores
|
| 1410 |
+
full_df = pd.DataFrame(all_data_rows)
|
| 1411 |
+
# DEBUG: Check the state of the DataFrame *before* dropping rows.
|
| 1412 |
+
# if full_df.empty:
|
| 1413 |
+
# print("DEBUG: No rows were generated after sampling. Check previous debug messages.")
|
| 1414 |
+
# return pd.DataFrame()
|
| 1415 |
+
# print(f"DEBUG: Generated {len(full_df)} rows before dropping NaNs. Checking rsi_14...")
|
| 1416 |
+
# print(full_df[['ticker', 'rsi_14']].to_string())
|
| 1417 |
+
# full_df.dropna(subset=['rsi_14'], inplace=True) # Ensure key features are present
|
| 1418 |
+
|
| 1419 |
+
print("\nCalculating benchmarks and final scores...")
|
| 1420 |
+
fixed_benchmarks = cfg.get("FIXED_BENCHMARKS", {})
|
| 1421 |
+
for name in tqdm(target_horizons.keys(), desc="Scoring Targets"):
|
| 1422 |
+
pct_change_col = f"{name}_pct_change"
|
| 1423 |
+
if pct_change_col not in full_df.columns:
|
| 1424 |
+
continue
|
| 1425 |
+
|
| 1426 |
+
# Calculate and store the benchmark (for debugging)
|
| 1427 |
+
benchmark = fixed_benchmarks.get(name)
|
| 1428 |
+
# If no fixed benchmark is defined for this target name, skip scoring it.
|
| 1429 |
+
if benchmark is None:
|
| 1430 |
+
print(f"Warning: No fixed benchmark found for '{name}'. Skipping scoring for this target.")
|
| 1431 |
+
continue
|
| 1432 |
+
|
| 1433 |
+
# full_df[f"{name}_avg_benchmark_change"] = benchmark
|
| 1434 |
+
|
| 1435 |
+
# Calculate the final score
|
| 1436 |
+
if benchmark == 0 or np.isnan(benchmark):
|
| 1437 |
+
full_df[name] = 0.5
|
| 1438 |
+
else:
|
| 1439 |
+
ratio = full_df[pct_change_col].fillna(0) / benchmark
|
| 1440 |
+
score = 0.5 + (ratio * cfg["SCORE_SCALING_FACTOR"])
|
| 1441 |
+
full_df[name] = score.clip(0.0, 1.0)
|
| 1442 |
+
|
| 1443 |
+
# 5. Final Formatting
|
| 1444 |
+
# Rename and format columns for final output
|
| 1445 |
+
jakarta_tz = 'Asia/Jakarta'
|
| 1446 |
+
full_df.rename(columns={'timestamp': 'start_time'}, inplace=True)
|
| 1447 |
+
full_df['start_time_gmt7'] = pd.to_datetime(full_df['start_time']).dt.tz_localize('UTC').dt.tz_convert(jakarta_tz).dt.strftime('%Y-%m-%d %H:%M:%S')
|
| 1448 |
+
|
| 1449 |
+
for name in target_horizons.keys():
|
| 1450 |
+
# Format percentage change
|
| 1451 |
+
pct_col = f"{name}_pct_change"
|
| 1452 |
+
if pct_col in full_df.columns:
|
| 1453 |
+
full_df[pct_col] = full_df[pct_col].apply(lambda x: f"{x:+.2%}" if pd.notna(x) else "N/A")
|
| 1454 |
+
|
| 1455 |
+
# Format end time
|
| 1456 |
+
end_time_col = f"{name}_end_time"
|
| 1457 |
+
if end_time_col in full_df.columns:
|
| 1458 |
+
new_end_time_col = f"{end_time_col}_gmt7"
|
| 1459 |
+
full_df[new_end_time_col] = pd.to_datetime(full_df[end_time_col]).dt.tz_localize('UTC').dt.tz_convert(jakarta_tz).dt.strftime('%Y-%m-%d %H:%M:%S')
|
| 1460 |
+
full_df.drop(columns=[end_time_col], inplace=True)
|
| 1461 |
+
|
| 1462 |
+
# Reorder columns for readability
|
| 1463 |
+
id_cols = ['ticker', 'start_time_gmt7']
|
| 1464 |
+
|
| 1465 |
+
# --- FIX: Identify feature columns by excluding known ID and target columns ---
|
| 1466 |
+
target_cols = sorted([c for c in full_df.columns if c.startswith('target')])
|
| 1467 |
+
known_non_feature_cols = set(id_cols + target_cols + ['start_time'])
|
| 1468 |
+
feature_cols = sorted([c for c in full_df.columns if c not in known_non_feature_cols])
|
| 1469 |
+
|
| 1470 |
+
# Construct the final list of columns in the desired order
|
| 1471 |
+
final_cols = id_cols + feature_cols + target_cols
|
| 1472 |
+
return full_df[final_cols]
|
| 1473 |
+
|
| 1474 |
+
|
| 1475 |
+
def candles_to_dataframe(candles: List[Dict[str, Any]]) -> pd.DataFrame:
|
| 1476 |
+
"""Converts the List[Candle] from fetch_yahoo into a pandas DataFrame."""
|
| 1477 |
+
if not candles:
|
| 1478 |
+
return pd.DataFrame()
|
| 1479 |
+
df = pd.DataFrame(candles)
|
| 1480 |
+
df['timestamp'] = pd.to_datetime(df['t'], unit='ms')
|
| 1481 |
+
df.set_index('timestamp', inplace=True)
|
| 1482 |
+
df.rename(columns={'o': 'open', 'h': 'high', 'l': 'low', 'c': 'close', 'v': 'volume'}, inplace=True)
|
| 1483 |
+
df.drop(columns=['t'], inplace=True)
|
| 1484 |
+
# Ensure data types are correct, handling potential None values
|
| 1485 |
+
for col in ['open', 'high', 'low', 'close', 'volume']:
|
| 1486 |
+
df[col] = pd.to_numeric(df[col], errors='coerce')
|
| 1487 |
+
return df
|
| 1488 |
+
|