Skip to content

Instantly share code, notes, and snippets.

@normanlmfung
normanlmfung / py
Last active October 16, 2024 05:02
aggregated_orderbook.py
from enum import Enum
import time
from datetime import datetime
from typing import Dict, List
import logging
from tabulate import tabulate
import asyncio
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
@normanlmfung
normanlmfung / py
Last active June 30, 2024 02:49
earnings_provider.py
import os
import sys
import traceback
import re
import json
import uuid
from typing import List, Dict
import time
import arrow
from datetime import datetime, timedelta, timezone
import time
import datetime
from datetime import datetime, timedelta
import enum
from typing import List, Dict
import os.path
import pandas as pd
from tabulate import tabulate
import asyncio
import random
async def generate_random_int():
await asyncio.sleep(1) # Simulating some asynchronous task
return random.randint(1, 100)
async def main():
results = await asyncio.gather(generate_random_int(), generate_random_int())
print("Results:", results)
@normanlmfung
normanlmfung / py
Last active May 9, 2024 02:50
ccxt_ws_ob_provider.py
'''
This is a simply python util that fetches order book using CCXT Pro
https://docs.ccxt.com/#/ccxt.pro.manual?id=instantiation
Usage:
python ccxt_ws_ob_provider.py --ticker BTC/USDT:USDT --ts_delta_observation_ms_threshold 150 --ts_delta_consecutive_ms_threshold 150
Keep track of latency issues:
a) ts_delta_observation_ms (Default 150 ms): Keep track of server clock vs timestamp from exchange
b) ts_delta_consecutive_ms (Default 150 ms): Keep track of gap between consecutive updates
'''
Example 3, What if market just follow Fibonacci? After ten itterations, it'd converge to usd61.4k, as price_range narrows!
0: 54237.944
1: 65883.57060800001
2: 58686.57336425601
3: 63134.3176608898
4: 60385.61168557012
5: 62084.31197831768
6: 61034.51519739968
# Example 2, Big chop on 1 May 2024
swing_low = 56500 # 20240501
swing_high = 64000 # 20240425
retracement_level = estimate_fib_retracement(swing_low, 2, swing_high, 1)
'''
Fibonacci predicted bounce back to 61135.0 (vs actual 64.5k on 4 May 2024).
What happenned?
a) weaker than expected employment data
Unemployment act 3.9 est 3.8 https://tradingeconomics.com/united-states/unemployment-rate
Non farm payroll act 175k est 243k https://tradingeconomics.com/united-states/non-farm-payrolls
MAGIC_FIB_LEVELS = [0, 0.236, 0.382, 0.5, 0.618, 0.786, 1.00, 1.618, 2.618, 3.618, 4.236]
# https://blog.quantinsti.com/fibonacci-retracement-trading-strategy-python/
def estimate_fib_retracement(
swing_low: float,
swing_low_idx: int,
swing_high: float,
swing_high_idx: int,
target_fib_level: float = 0.618 # Magic 618
):
@normanlmfung
normanlmfung / py
Last active May 9, 2024 03:05
crypto_pairs_correlation.py
from datetime import datetime
from typing import Dict, List, Union
import pandas as pd
from ccxt.binance import binance
from ccxt.base.exchange import Exchange
num_candles_limit : int = 100 # Depends on exchange but generally 100 ok!
param : Dict = {
'apiKey' : None,
'secret' : None,
@normanlmfung
normanlmfung / csv
Created April 25, 2024 05:05
BTCUSDT_candles_2024-03-01-00-00-00_2024-04-24-00-00-00_1h.csv
,exchange,symbol,timestamp_ms,open,high,low,close,volume,is_dummy,datetime_utc,datetime,year,month,day,datetime_diff_sec,hour,minute,candle_height,pct_change_close,ema_short_periods,ema_long_periods,ema_close,std,h_l,h_pc,l_pc,tr,atr,hurst_exp,boillenger_upper,boillenger_lower,close_delta,up,down,rsi,macd,signal,macd_minus_signal,ema_short_slope,ema_long_slope,gap_close_vs_ema,close_above_or_below_ema,close_vs_ema_inflection
0,okx_linear,BTC/USDT:USDT,1709222400000.0,62787.3,62920.1,61501.4,62208.9,1572168.0,False,2024-02-29 16:00:00,2024-03-01 00:00:00,2024,3,1,,0,0,1418.699999999997,,62208.9,62208.9,62208.9,,1418.699999999997,,,1418.699999999997,,,,,,,,,0.0,0.0,0.0,,,0.0,,0.0
1,okx_linear,BTC/USDT:USDT,1709226000000.0,62208.9,62433.0,60550.0,61038.5,1855491.0,False,2024-02-29 17:00:00,2024-03-01 01:00:00,2024,3,1,3600.0,1,0,1883.0,-1.8814028217827405,62125.3,62180.35365853659,62180.35365853659,,1883.0,224.09999999999854,1658.9000000000015,1883.0,,,,,-1170.4000000000015,0.0,1170.4000000000015,,-55.0536585365