saving
This commit is contained in:
@@ -14,6 +14,7 @@ import valkey
|
||||
from dotenv import load_dotenv
|
||||
import modules.manual_leverage as leverage
|
||||
import modules.aster_auth as aster_auth
|
||||
import modules.utils as utils
|
||||
|
||||
### MANUAL LEVERAGE DATA ###
|
||||
df_leverage_by_exch = pd.DataFrame(data=leverage.LEVERAGE_BY_EXCH)
|
||||
@@ -117,6 +118,48 @@ def load_extend_current_fr(df_mkt_stats: pd.DataFrame) -> pd.DataFrame:
|
||||
|
||||
return df
|
||||
|
||||
async def get_candles(symbol: str) -> pd.DataFrame:
|
||||
### Candles for Midpoint Dispersion ###
|
||||
# Aster
|
||||
symbol_ast = utils.symbol_to_aster_fmt(symbol)
|
||||
aster_candles = {
|
||||
"url": "/fapi/v3/klines",
|
||||
"method": "GET",
|
||||
"params": {
|
||||
'symbol': symbol_ast,
|
||||
'interval': '1m',
|
||||
'limit':'1440'
|
||||
}
|
||||
}
|
||||
j = await aster_auth.post_authenticated_url(aster_candles)
|
||||
df_candles_aster = pd.DataFrame(j, columns=['open_ts','open_px','high_px','low_px','close_px','volume','close_ts','quote_asset_volume','count_trades','taker_buy_base_asset_volume','taker_buy_quote_asset_volume','_drop'])
|
||||
df_candles_aster = df_candles_aster[['open_px', 'low_px', 'high_px', 'close_px', 'volume', 'open_ts']]
|
||||
df_candles_aster[['open_px', 'low_px', 'high_px', 'close_px', 'volume']] = df_candles_aster[['open_px', 'low_px', 'high_px', 'close_px', 'volume']].astype(float)
|
||||
|
||||
df_candles_aster['med_px'] = ( df_candles_aster['high_px'] + df_candles_aster['low_px'] ) / 2
|
||||
df_candles_aster['typical_px'] = ( df_candles_aster['open_px'] + df_candles_aster['high_px'] + df_candles_aster['low_px'] + df_candles_aster['close_px'] ) / 4
|
||||
|
||||
# Extend
|
||||
symbol_ext = utils.symbol_to_extend_fmt(symbol)
|
||||
ext_params = {
|
||||
'interval':'1m',
|
||||
'limit':1440,
|
||||
}
|
||||
r = json.loads(requests.get(f'https://api.starknet.extended.exchange/api/v1/info/candles/{symbol_ext}/trades', params=ext_params).text)
|
||||
df_candles_extended = pd.DataFrame(r['data'])
|
||||
df_candles_extended = df_candles_extended.rename({'o':'open_px','l':'low_px','h':'high_px','c':'close_px','v':'volume','T':'open_ts'}, axis=1)
|
||||
df_candles_extended[['open_px', 'low_px', 'high_px', 'close_px', 'volume']] = df_candles_extended[['open_px', 'low_px', 'high_px', 'close_px', 'volume']].astype(float)
|
||||
df_candles_extended['med_px'] = ( df_candles_extended['high_px'] + df_candles_extended['low_px'] ) / 2
|
||||
df_candles_extended['typical_px'] = ( df_candles_extended['open_px'] + df_candles_extended['high_px'] + df_candles_extended['low_px'] + df_candles_extended['close_px'] ) / 4
|
||||
|
||||
df_candles_comb = df_candles_aster.merge(df_candles_extended, on='open_ts', how='inner', suffixes=('_ast','_ext'))
|
||||
df_candles_comb['open_dt'] = pd.to_datetime(df_candles_comb['open_ts'], unit='ms')
|
||||
df_candles_comb['med_ratio_aster_over_extend'] = ( df_candles_comb['med_px_ast'] / df_candles_comb['med_px_ext'] ) - 1
|
||||
|
||||
return df_candles_comb
|
||||
|
||||
|
||||
|
||||
async def loop() -> None:
|
||||
global Mkt_Info_Last_Refresh_TS_ms
|
||||
try:
|
||||
@@ -163,6 +206,15 @@ async def loop() -> None:
|
||||
# print(df_best_fr_rate.columns)
|
||||
# print(df_best_fr_rate.iloc[0])
|
||||
|
||||
candles_ratios = []
|
||||
|
||||
for index, row in df_best_fr_rate.iterrows():
|
||||
df = await get_candles(symbol=row['symbol_ext'])
|
||||
buy_ratio_ext = float(df['med_ratio_aster_over_extend'].median())
|
||||
candles_ratios.append({'symbol_ext':row['symbol_ext'], 'buy_ratio_ext':buy_ratio_ext,'buy_ratio_ast':buy_ratio_ext*-1})
|
||||
|
||||
df_best_fr_rate = df_best_fr_rate.merge(pd.DataFrame(candles_ratios), on='symbol_ext', how='left')
|
||||
|
||||
if len(df_best_fr_rate) < 1:
|
||||
raise ValueError(f'NO BFR RATE: {df_best_fr_rate}')
|
||||
|
||||
@@ -177,6 +229,7 @@ async def loop() -> None:
|
||||
min_order_size=float(df_best_fr_rate['min_order_size_ast'].iloc[0]),
|
||||
min_lot_size=float(df_best_fr_rate['min_lot_size_ast'].iloc[0]),
|
||||
min_notional=float(df_best_fr_rate['min_notional_ast'].iloc[0]),
|
||||
buy_ratio=float(df_best_fr_rate['buy_ratio_ast'].iloc[0]),
|
||||
)
|
||||
EXTEND = structs.Perpetual_Exchange(
|
||||
mult = int(df_best_fr_rate['max_leverage_ext'].iloc[0]),
|
||||
@@ -188,6 +241,7 @@ async def loop() -> None:
|
||||
min_order_size=float(df_best_fr_rate['min_order_size_ext'].iloc[0]),
|
||||
min_lot_size=float(df_best_fr_rate['min_lot_size_ext'].iloc[0]),
|
||||
min_notional=float(df_best_fr_rate['min_notional_ext'].iloc[0]),
|
||||
buy_ratio=float(df_best_fr_rate['buy_ratio_ext'].iloc[0]),
|
||||
)
|
||||
except Exception as e:
|
||||
logging.critical(f'Failed to build ASTER/EXTEND objs err: {e}; df cols: {df_best_fr_rate.columns}')
|
||||
@@ -196,13 +250,13 @@ async def loop() -> None:
|
||||
best_next_funding_pair: dict[str, dict] = {'ASTER': asdict(obj=ASTER), 'EXTEND': asdict(obj=EXTEND)}
|
||||
VAL_KEY.set(name='fr_engine_best_fund_rate_output', value=json.dumps(obj=best_next_funding_pair))
|
||||
|
||||
master_data = df_comb_fr[
|
||||
['symbol_ast','max_leverage_ast','lh_asset_ast','rh_asset_ast','funding_rate_ast','min_price_ast','min_order_size_ast','min_lot_size_ast','min_notional_ast',
|
||||
'symbol_ext','max_leverage_ext','lh_asset_ext','rh_asset_ext','funding_rate_ext','min_price_ext','min_order_size_ext','min_lot_size_ext','min_notional_ext']
|
||||
master_data = df_best_fr_rate[
|
||||
['symbol_ast','max_leverage_ast','lh_asset_ast','rh_asset_ast','funding_rate_ast','min_price_ast','min_order_size_ast','min_lot_size_ast','min_notional_ast','buy_ratio_ast',
|
||||
'symbol_ext','max_leverage_ext','lh_asset_ext','rh_asset_ext','funding_rate_ext','min_price_ext','min_order_size_ext','min_lot_size_ext','min_notional_ext','buy_ratio_ext']
|
||||
].to_json(orient='records')
|
||||
|
||||
|
||||
VAL_KEY.set(name='fr_engine_best_fund_rate_master', value=str(master_data))
|
||||
print(df_best_fr_rate[['symbol_ext','max_leverage_ext','funding_rate_ast','funding_rate_ext','net_funding_rate','daily_volume_ast']].head(10))
|
||||
print(df_best_fr_rate[['symbol_ext','max_leverage_ext','buy_ratio_ext','net_funding_rate','daily_volume_ast']].head(10))
|
||||
logging.info(f'BFR REFRESHED @ {datetime.now()}')
|
||||
time.sleep(LOOP_SLEEP_SEC)
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user