Instantly share code, notes, and snippets.

Embed
What would you like to do?
AirFXの注文割り込み用プログラム。現状1ポジのみ対応。上から3つ目までに設定したサイズ以上の注文があった場合割り込みます。
from zaifapi import ZaifLeverageTradeApi
from zaifapi import ZaifFuturesPublicApi
import time
import sys
###SETTINGS###
api_key = "YOUR_API_KEY"
api_secret = "YOUR_API_SECRET"
chase_size = 0.1 #このサイズ以上の注文に対し割り込みを行う
chase_spread = 10 #割り込みの際つける価格差
##############
def getid(apos):
ids = list(apos.keys())
id = ids[0]
return id
def main():
try:
zaif=ZaifLeverageTradeApi(api_key , api_secret)
apos = zaif.active_positions(type='futures',group_id='1')
if len(apos) == 0:
print("No position.")
sys.exit()
else:
id = getid(apos)
pos = apos.get(id)
action = pos.get('action')
amount = pos.get('amount')
price = pos.get('price')
print(action,round(price),amount)
except Exception as e:
print('error:',e)
sys.exit()
while True:
depth = ZaifFuturesPublicApi().depth(group_id='1',currency_pair='btc_jpy')
asks = depth.get('asks')
bids = depth.get('bids')
ask1 = asks[0]
ask2 = asks[1]
ask3 = asks[2]
bid1 = bids[0]
bid2 = bids[1]
bid3 = bids[3]
if action == 'bid':
if bid1[0] > price and bid1[1] > chase_size:
callback = zaif.change_position(type="futures",group_id ="1",leverage_id=int(id),price=round(bid1[0])+chase_spread)
print(callback)
elif bid2[0] > price and bid2[1] > chase_size:
callback = zaif.change_position(type="futures",group_id ="1",leverage_id=int(id),price=round(bid2[0])+chase_spread)
print(callback)
elif bid3[0] > price and bid3[1] > chase_size:
callback = zaif.change_position(type="futures",group_id ="1",leverage_id=int(id),price=round(bid3[0])+chase_spread)
print(callback)
else:
pass
elif action == 'ask':
if ask1[0] < price and ask1[1] > chase_size:
callback = zaif.change_position(type="futures",group_id ="1",leverage_id=int(id),price=round(ask1[0])-chase_spread)
print(callback)
elif ask2[0] < price and ask2[1] > chase_size:
callback = zaif.change_position(type="futures",group_id ="1",leverage_id=int(id),price=round(ask2[0])-chase_spread)
print(callback)
elif ask3[0] < price and ask3[1] > chase_size:
callback = zaif.change_position(type="futures",group_id ="1",leverage_id=int(id),price=round(ask3[0])-chase_spread)
print(callback)
else:
pass
if len(callback) == 3:
sys.exit()
else:
pass
time.sleep(1)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment