• API
• FAQ
• Tools
• Archive
SHARE
TWEET # Untitled a guest Mar 26th, 2020 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #!/usr/bin/python3
2.
3. from sympy.solvers import solve
4. from sympy import Symbol
5. import math
6. import sys
7.
8. # This is a simple script to compute the FTX Leveraged Token return for a given day
10. # Every day at 00:02:00 UTC the leveraged token rebalances its leverage to 3x based on any gain (or loss) throughout the day
11. # There is also an intra-day rebalance for when the token's underlying has a negative return that brings its effective leverage to 4x
12. # For BULL tokens this is a 11% drop in the underlying, for BEAR tokens this is a 6.5% rise in the underlying
13. # Note that there is no longer any rebalance when the underlying has large moves in its favour (up for BULL, down for BEAR)
14.
15. # This script requires 5 arguments:
16. # 1) bull or bear to indicate it is a bull or bear token (3x long, 3x short)
17. # 2) start underlying price -- the price of underlying contract at 00:02:00 UTC for the day 0
18. # 3) peak / trough intraday price -- this is the lowest price (trough) for BULL tokens or the highest price (peak) for BEAR tokens
19. # 4) end underlying price -- the price of underlying contract at 00:02:00 UTC the next day 1
20. # 5) token start price -- this is the price of the token at 00:02:00 for day 0
21.
22. # Example: BULL token for ETH/USD with start underlying price \$130, trough price \$81.564, end underlying price \$84, and start token price \$20
23. # python3 leveragedtoken.py bull 130 81.564 84 20
24. # Start price of underlying: \$130
25. # Number of rebalances: 4
26. # Price of trough: \$81.564
27. # End price of underlying: \$84
28. # Return on underlying: -35.38%
29. # Token start price: \$20
30. # Token end price: \$4.150591349124615
31.
32.
33. def solvebull(troughreturn):
34.     x = Symbol('x')
35.     solution=solve(1 - 0.89**(x-1) - troughreturn, x)
36.     numrebalances=math.floor(solution)-1
37.     return numrebalances
38.
39. def solvebear(peakreturn):
40.     x = Symbol('x')
41.     solution=solve(1.065**(x-1) - 1 - peakreturn, x)
42.     numrebalances=math.floor(solution)-1
43.     return numrebalances
44.
45.
46. if len(sys.argv) < 5:
47.     print("Must enter at least five arguments: bull/bear, start underlying price, peak price (bear) or trough price (bull), end of day underlying price, and token start price")
48.     exit()
49.
50. type=sys.argv
51. underlyingstart=sys.argv
52. underlyingopp=sys.argv
53. underlyingend=sys.argv
54. tokenstartprice=sys.argv
55. tokenendprice=float(tokenstartprice)
56. if sys.argv=="bull":
57.     boom=(float(sys.argv)-float(sys.argv))/float(sys.argv)
58.     if boom>=0.11:
59.         numrebalances=solvebull(boom)
60.         oppmsg="Number of rebalances: " + str(numrebalances) + "\nPrice of trough: \$" + str(underlyingopp)
61.         loopnum=numrebalances
62.         x=0
63.         while x<loopnum:
64.             tokenendprice=tokenendprice*0.67
65.             x+=1
66.         finalreturn=(float(underlyingend)-float(underlyingopp))/float(underlyingopp)
67.         tokenendprice=tokenendprice*(1+finalreturn)
68.         totalreturn=(float(underlyingend)-float(underlyingstart))/float(underlyingstart)
69.     else:
70.         numrebalances=0
71.         oppmsg="No drop triggering rebalance for peak \$" + str(underlyingopp)
72.         totalreturn=(underlyingstart-underlyingend)/underlyingstart
73.         tokenendprice=tokenendprice*(1+totalreturn)
74. elif sys.argv=="bear":
75.     boom=(float(sys.argv)-float(sys.argv))/float(sys.argv)
76.     if boom>=0.065:
77.         numrebalances=solvebear(boom)
78.         oppmsg="Number of rebalances: " + str(numrebalances) + "\nPrice of peak: \$" + str(underlyingopp)
79.         loopnum=numrebalances
80.         tokenendprice=float(tokenstartprice)
81.         x=0
82.         while x<loopnum:
83.             tokenendprice=tokenendprice*0.805
84.             x+=1
85.         finalreturn=(float(underlyingopp)-float(underlyingend))/float(underlyingopp)
86.         tokenendprice=tokenendprice*(1+finalreturn)
87.         totalreturn=(float(underlyingstart)-float(underlyingend))/float(underlyingstart)
88.     else:
89.         numrebalances=0
90.         oppmsg="No rise triggering rebalance for peak \$" + str(underlyingopp)
91.         totalreturn=(float(underlyingstart)-float(underlyingend))/float(underlyingstart)
92.         tokenendprice=tokenendprice*(1+totalreturn)
93. else:
94.     print("Must specify bull or bear token")
95.     exit()
96.
97.
98. print("Start price of underlying: \$" + str(underlyingstart))
99. print(oppmsg)
100. print("End price of underlying: \$" + str(underlyingend))
101. print("Return on underlying: " + str(round(100*totalreturn,2)) + "%")
102. print("Token start price: \$" + str(tokenstartprice))
103. print("Token end price: \$" + str(tokenendprice))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Top