This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from ib_async import IB, Stock, LimitOrder | |
| import asyncio | |
| # ================== CONFIG ================== | |
| HOST = '127.0.0.1' | |
| PORT = 4001 # ← Change to your Gateway port (4002 paper / 4001 live) | |
| CLIENT_ID = 125 # unique per script | |
| # ================== CONNECT WITH ERROR HANDLING ================== | |
| ib = IB() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import yfinance as yf | |
| import requests | |
| import csv | |
| import io | |
| import sys | |
| import statistics | |
| import random | |
| def get_sp500_tickers() -> list[str]: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import requests | |
| import csv | |
| import io | |
| def get_russell_2000_tickers() -> list[str]: | |
| """ | |
| Fetches current Russell 2000 constituents from iShares IWM ETF holdings CSV. | |
| Pure Python + built-in csv module. No pandas. Handles the messy header + huge disclaimer. | |
| Returns clean tickers ready for yfinance (BRK.B → BRK-B). | |
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| >>> statistics.mean([0.02, 0.01, -0.06, 0.01]) | |
| -0.004999999999999999 | |
| >>> statistics.stdev([0.02, 0.01, -0.06, 0.01]) | |
| 0.03696845502136473 | |
| >>> statistics.stdev([0.02, 0.01, -0.02, 0.01]) | |
| 0.017320508075688773 | |
| >>> statistics.mean([0.02, 0.01, -0.01, 0.01]) | |
| 0.0075 | |
| >>> statistics.stdev([0.02, 0.01, -0.00, 0.01]) | |
| 0.008164965809277261 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Calculates the current RSI using your exact percentage-based logic (no smoothing) | |
| * | |
| * @param {A1:A100} priceRange Range containing historical closing prices (oldest to newest) | |
| * @param {number} period RSI period (default 14) | |
| * @return {number} Current RSI value, rounded to 2 decimals | |
| * @customfunction | |
| */ | |
| function CURRENT_RSI(priceRange, period = 14) { | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * FIXED VERSION - Calculates the next closing price needed to reach a target RSI | |
| * Matches your exact percentage-based RSI (no smoothing) perfectly. | |
| * | |
| * @param {A1:A100} priceRange Range containing closing prices (oldest to newest) | |
| * @param {number} period RSI period (default 14) | |
| * @param {number} threshold Target RSI (e.g. 30 or 20) | |
| * @param {string} direction "above" or "below" | |
| * @return {number} Next closing price needed | |
| * @customfunction |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import csv | |
| from typing import Dict, List, Optional | |
| import math # only for sqrt in correlation (pure basics) | |
| def is_float(num): | |
| try: | |
| float(num) | |
| return True |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /**this works on already flat array */ | |
| function RSIRNG(prices, start, end) { | |
| if (!Array.isArray(prices)) { | |
| throw "Second argument must be a range (e.g. B2:B100)"; | |
| } | |
| // Calculate percentage changes | |
| let diffPcnts = []; | |
| for (let i = start + 1; i < end; i++) { | |
| let change = prices[i] - prices[i - 1]; | |
| diffPcnts.push(change / prices[i - 1]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Calculates RSI using the last N closing prices up to the current row. | |
| * @param {number} period The RSI period (e.g. 7, 14) | |
| * @param {range} priceRange The full range of closing prices (e.g. C2:C) | |
| * @return The RSI value for the current row. | |
| * @customfunction | |
| */ | |
| function RSI(period, priceRange) { | |
| if (!priceRange || !Array.isArray(priceRange)) { | |
| return "Invalid price range"; |
NewerOlder