├── Backtest.py ├── GraphBacktest.py ├── README.md ├── charts.py └── liquidity.py /Backtest.py: -------------------------------------------------------------------------------- 1 | import pandas as pd 2 | import matplotlib.pyplot as plt 3 | import matplotlib.cbook as cbook 4 | import matplotlib.dates as mdates 5 | import numpy as np 6 | import liquidity 7 | import GraphBacktest 8 | import charts 9 | 10 | # network 1 ETH, 2 ARB, 3 OPT 11 | 12 | Adress= "0x93f267fd92b432bebf4da4e13b8615bb8eb2095c"#snx eth 13 | Adress= "0xcb0c5d9d92f4f2f80cce7aa271a1e148c226e19d" # Rai Dai 14 | Adress= "0x8ad599c3a0ff1de082011efddc58f1908eb6e6d8" # ETH USDC Ethereum 15 | Adress= "0x2e9c575206288f2219409289035facac0b670c2f" # ETH DAI Optimism 16 | #Adress= "0x68f180fcce6836688e9084f035309e29bf0a2095" # WBTC DAI 17 | startfrom= 1632081600 18 | network = 3 19 | 20 | dpd=GraphBacktest.graph(network,Adress,startfrom) 21 | 22 | 23 | decimal0=dpd.iloc[0]['pool.token0.decimals'] 24 | decimal1=dpd.iloc[0]['pool.token1.decimals'] 25 | decimal=decimal1-decimal0 26 | dpd['fg0']=((dpd['feeGrowthGlobal0X128'])/(2**128))/(10**decimal0) 27 | dpd['fg1']=((dpd['feeGrowthGlobal1X128'])/(2**128))/(10**decimal1) 28 | 29 | mini = 3273.4 30 | maxi = 4038.3 31 | target = 2711.53 #1 / dpd['close'].iloc[-1] 32 | base = 1 33 | 34 | 35 | # mini = 1 / 3215 #optimism 36 | # maxi = 1 / 2903.3 37 | # target = 1 #in base 0 38 | # base = 0 39 | 40 | # mini = 2892.9 #ethereum 41 | # maxi = 3235 42 | # target = 5910 #in base 0 43 | # base = 0 44 | 45 | # mini = 1/4048 #ethereum 46 | # maxi = 1/ 2892 47 | # target = 5910 / dpd['close'].iloc[0] #in base 0 48 | # base = 1 49 | 50 | 51 | 52 | #Calculate F0G and F1G (fee earned by an unbounded unit of liquidity in one period) 53 | 54 | dpd['fg0shift']=dpd['fg0'].shift(-1) 55 | dpd['fg1shift']=dpd['fg1'].shift(-1) 56 | dpd['fee0token']=dpd['fg0']-dpd['fg0shift'] 57 | dpd['fee1token']=dpd['fg1']-dpd['fg1shift'] 58 | 59 | # calculate my liquidity 60 | 61 | SMIN=np.sqrt(mini* 10 ** (decimal)) 62 | SMAX=np.sqrt(maxi* 10 ** (decimal)) 63 | 64 | if base == 0: 65 | 66 | sqrt0 = np.sqrt(dpd['close'].iloc[-1]* 10 ** (decimal)) 67 | dpd['price0'] = dpd['close'] 68 | 69 | else: 70 | 71 | sqrt0= np.sqrt(1/dpd['close'].iloc[-1]* 10 ** (decimal)) 72 | dpd['price0']= 1/dpd['close'] 73 | 74 | if sqrt0>SMIN and sqrt0mini and dpd['low'].iloc[i]mini and (1/dpd['high'].iloc[i]) sqrtB): 7 | (sqrtA,sqrtB)=(sqrtB,sqrtA) 8 | 9 | amount0=((liquidity*2**96*(sqrtB-sqrtA)/sqrtB/sqrtA)/10**decimals) 10 | 11 | return amount0 12 | 13 | def get_amount1(sqrtA,sqrtB,liquidity,decimals): 14 | 15 | if (sqrtA > sqrtB): 16 | (sqrtA,sqrtB)=(sqrtB,sqrtA) 17 | 18 | amount1=liquidity*(sqrtB-sqrtA)/2**96/10**decimals 19 | 20 | return amount1 21 | 22 | def get_amounts(asqrt,asqrtA,asqrtB,liquidity,decimal0,decimal1): 23 | 24 | sqrt=(np.sqrt(asqrt*10**(decimal1-decimal0)))*(2**96) 25 | sqrtA=np.sqrt(asqrtA*10**(decimal1-decimal0))*(2**96) 26 | sqrtB=np.sqrt(asqrtB*10**(decimal1-decimal0))*(2**96) 27 | 28 | if (sqrtA > sqrtB): 29 | (sqrtA,sqrtB)=(sqrtB,sqrtA) 30 | 31 | if sqrt<=sqrtA: 32 | 33 | amount0=get_amount0(sqrtA,sqrtB,liquidity,decimal0) 34 | return amount0,0 35 | 36 | elif sqrtsqrtA: 37 | amount0=get_amount0(sqrt,sqrtB,liquidity,decimal0) 38 | 39 | amount1=get_amount1(sqrtA,sqrt,liquidity,decimal1) 40 | 41 | return amount0,amount1 42 | 43 | else: 44 | amount1=get_amount1(sqrtA,sqrtB,liquidity,decimal1) 45 | return 0,amount1 46 | 47 | 48 | 49 | '''get_liquidity function''' 50 | #Use 'get_liquidity' function to calculate liquidity as a function of amounts and price range 51 | def get_liquidity0(sqrtA,sqrtB,amount0,decimals): 52 | if (sqrtA > sqrtB): 53 | (sqrtA,sqrtB)=(sqrtB,sqrtA) 54 | 55 | liquidity=amount0/((2**96*(sqrtB-sqrtA)/sqrtB/sqrtA)/10**decimals) 56 | return liquidity 57 | 58 | def get_liquidity1(sqrtA,sqrtB,amount1,decimals): 59 | 60 | if (sqrtA > sqrtB): 61 | (sqrtA,sqrtB)=(sqrtB,sqrtA) 62 | 63 | liquidity=amount1/((sqrtB-sqrtA)/2**96/10**decimals) 64 | return liquidity 65 | 66 | def get_liquidity(asqrt,asqrtA,asqrtB,amount0,amount1,decimal0,decimal1): 67 | 68 | sqrt=(np.sqrt(asqrt*10**(decimal1-decimal0)))*(2**96) 69 | sqrtA=np.sqrt(asqrtA*10**(decimal1-decimal0))*(2**96) 70 | sqrtB=np.sqrt(asqrtB*10**(decimal1-decimal0))*(2**96) 71 | 72 | 73 | if (sqrtA > sqrtB): 74 | (sqrtA,sqrtB)=(sqrtB,sqrtA) 75 | 76 | if sqrt<=sqrtA: 77 | 78 | liquidity0=get_liquidity0(sqrtA,sqrtB,amount0,decimal0) 79 | return liquidity0 80 | elif sqrtsqrtA: 81 | 82 | liquidity0=get_liquidity0(sqrt,sqrtB,amount0,decimal0) 83 | liquidity1=get_liquidity1(sqrtA,sqrt,amount1,decimal1) 84 | liquidity=liquidity0 if liquidity0