-
-
Save pinkumohikan/90ef25490f8d38722b2c2f3b59a31587 to your computer and use it in GitHub Desktop.
いざ、python入門
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ccxt | |
import os | |
from time import sleep | |
from functools import reduce | |
LOT = 1 | |
BTC_QUANTITY_PER_LOT = 0.01 | |
MAX_OPEN_ORDERS = 2 | |
SYMBOL = "FX_BTC_JPY" | |
SLEEP_INTERVAL = 15 | |
ORDER_TIMEOUT_MINUTES = 3 | |
def detect_middle_price(bf): | |
book = bf.fetch_order_book(symbol=SYMBOL) | |
highest_buy_price = book['bids'][0][0] | |
lowest_sell_price = book['asks'][0][0] | |
middle_price = round((highest_buy_price + lowest_sell_price) / 2, 0) | |
return middle_price | |
def order_long(bf, btc_quantity, price): | |
open_orders = bf.fetch_open_orders(symbol=SYMBOL) | |
if len(open_orders) >= MAX_OPEN_ORDERS: | |
# TODO: 例外投げるのがpythonぽいらしい | |
print("open order limit reached.") | |
return | |
bf.create_limit_buy_order(SYMBOL, btc_quantity, price, { | |
'minute_to_expire': ORDER_TIMEOUT_MINUTES, | |
}) | |
print("long ordered.") | |
def order_short(bf, btc_quantity, price): | |
open_orders = bf.fetch_open_orders(symbol=SYMBOL) | |
if len(open_orders) >= MAX_OPEN_ORDERS: | |
# TODO: 例外投げるのがpythonぽいらしい | |
print("open order limit reached.") | |
return | |
bf.create_limit_sell_order(SYMBOL, btc_quantity, price, { | |
'minute_to_expire': ORDER_TIMEOUT_MINUTES, | |
}) | |
print("short ordered.") | |
def fetch_position(bf): | |
positions = bf.private_get_getpositions(params={'product_code': SYMBOL}) | |
if not positions: | |
return None, 0 | |
side = positions[0]['side'] | |
sizes = map(lambda p: p['size'], positions) | |
quantity = reduce(lambda carry, size: carry + size, sizes) | |
return side, quantity | |
def cancel_open_orders(bf): | |
orders = bf.fetch_open_orders(symbol=SYMBOL) | |
for o in orders: | |
bf.cancel_order(symbol=SYMBOL, id=o['id']) | |
def main(): | |
bf = ccxt.bitflyer({ | |
'apiKey': os.environ.get("BF_API_KEY"), | |
'secret': os.environ.get("BF_API_SECRET"), | |
}) | |
while True: | |
middle_price = detect_middle_price(bf) | |
print("middle_price:", middle_price) | |
target_offset_jpy = 200 | |
buy_price = middle_price - target_offset_jpy | |
sell_price = middle_price + target_offset_jpy | |
side, quantity = fetch_position(bf) | |
if not side or quantity < BTC_QUANTITY_PER_LOT: | |
print("no current position.") | |
cancel_open_orders(bf) | |
order_long(bf, BTC_QUANTITY_PER_LOT * LOT, buy_price) | |
order_short(bf, BTC_QUANTITY_PER_LOT * LOT, sell_price) | |
sleep(SLEEP_INTERVAL) | |
continue | |
if side == "BUY": | |
print("current position is long.") | |
order_short(bf, BTC_QUANTITY_PER_LOT * LOT, sell_price) | |
else: | |
print("current position is short.") | |
order_long(bf, BTC_QUANTITY_PER_LOT * LOT, buy_price) | |
sleep(SLEEP_INTERVAL) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment