Skip to content

Instantly share code, notes, and snippets.

@normanlmfung
Last active October 10, 2021 00:59
Show Gist options
  • Save normanlmfung/cc06568019f07dc3d189a8da30bfa22e to your computer and use it in GitHub Desktop.
Save normanlmfung/cc06568019f07dc3d189a8da30bfa22e to your computer and use it in GitHub Desktop.
ftx positiona and pnl calc
import arrow
from datetime import datetime
from typing import Dict, List
from gizmo.datetime_gizmo import timer
from src.mds.ccxt.ftx import CasinoFtx
from gizmo import pnl_gizmo
apiKey = 'lalala'
secret = 'lalala'
subaccount = 'lalala'
param = {
'apiKey' : apiKey,
'secret' : secret,
'rateLimit' : 5000, # 5 seconds
'enableRateLimit' : True,
'headers': { 'FTX-SUBACCOUNT': subaccount }
}
exchange = CasinoFtx(param)
@timer
def _fetch_my_trades():
return exchange.fetch_my_trades()
@timer
def _fetch_ohlcv(exchange, ticker, param):
return exchange.fetch_ohlcv(ticker, '1d')
trades = _fetch_my_trades()
trades = [
{
"trade_date" : arrow.get(trade['datetime']).datetime,
"symbol" : trade['symbol'],
"side" : trade['side'],
"filled_price" : trade['price'],
"amount" : trade['amount'],
"contract_size" : 1, # FTX no inverses
"taker_or_maker" : trade['takerOrMaker'],
"cost" : trade['cost'],
"fee" : trade['fee']
} for trade in trades ]
tickers = set([trade['symbol'] for trade in trades])
prices : Dict[str, List[Dict[datetime, float]]]= {}
for ticker in tickers:
candles = _fetch_ohlcv(exchange, ticker, '1d')
high = candles[-1][1]
low = candles[-1][2]
now = datetime.utcnow()
prices[ticker] = [
{ 'date' : datetime(now.year, now.month, now.day), 'price' : (high+low)/2 }
]
(tickers_pnl, errors) = pnl_gizmo.calc_pos_pnl(
trades=trades,
prices=prices,
ticker_field_name='symbol',
side_field_name='side',
filled_price_field_name='filled_price',
amount_field_name='amount',
contract_size_field_name='contract_size',
trade_date_field_name='trade_date'
)
if tickers_pnl:
for ticker in tickers_pnl:
print("**************************************")
print(f"ticker: {ticker}")
print(f"{tickers_pnl[ticker]}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment