Skip to content

Instantly share code, notes, and snippets.

@kangchihlun
Created November 2, 2021 04:02
Show Gist options
  • Save kangchihlun/1dfa483704374007097aa454891b8933 to your computer and use it in GitHub Desktop.
Save kangchihlun/1dfa483704374007097aa454891b8933 to your computer and use it in GitHub Desktop.
omg 基差清算模擬
# coding=utf-8
import os,sys
import time
liqperc = 0.05 # 清算線,抓安全一點 5%
initialCapital = 10000
omg_1231_entry_price = 13.91 # omg 1231 開倉價
omg_perp_entry_price = 15.378 # omg perp 開倉價
basis = abs(omg_perp_entry_price - omg_1231_entry_price) / omg_perp_entry_price
basis_increment = 0.005
for lvg in range(1,10):
size_omg1231 = initialCapital * lvg / omg_1231_entry_price
size_omgPerp = initialCapital * lvg / omg_perp_entry_price
# 績差遞增,只要假設一邊拉開就行,另一腳價格可以無視
ibasis = basis
for i in range(50):
ibasis += basis_increment * i
fake_perp_price = (1 + ibasis) * omg_perp_entry_price
_pnl = (size_omgPerp + size_omg1231) * (omg_perp_entry_price - fake_perp_price)
margin_ratio = (initialCapital + _pnl)/initialCapital
print('cur lvg :'+str(lvg)+ ' cur basis '+str(ibasis*100)+ '% curr capital=' + str(initialCapital + _pnl) + ' margin_ratio '+str(margin_ratio) )
if(margin_ratio < liqperc):
print('got liquidated at ' + str( ibasis * 100 ) + '%')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment