1
1

LNMarketsの基本メソッド備忘録

Posted at

趣旨

LNMarketsを使って自分でロジックを組む時に使えそうなメソッドの仕様を確認したので、その備忘録として記録に残します。非常に簡単ですし、ほぼ公式Docsから持ってきただけなんですが、僕が確かめたい項目だけまとめてあります。全部見たい方は公式Docsを見る方がよいです。

from lnmarkets import rest
import json

# Creating an instance
options = {'key': 'your_key',
           'secret': 'your_secret',
           'passphrase': 'your_pass',
           'network': 'mainnet'}
lnm = rest.LNMarketsRest(**options)

# Fetching market data
market_data = lnm.futures_get_market()

# Getting future trades
print(lnm.futures_get_trades({
    'type': 'running' # enum: ['open', 'running', 'closed']
  }))

# Fetching last price
ticker_json_string = lnm.futures_get_ticker()
ticker = json.loads(ticker_json_string)
last_price = int(ticker['lastPrice'])
print(last_price)

# Setting stop loss and take profit / they should be integers
def round_to_nearest_integer(number):
    return round(number)
takeprofit = round_to_nearest_integer(last_price * 0.91)
stoploss = round_to_nearest_integer(last_price * 1.02)
print(takeprofit)
print(stoploss)

# Setting leverage / it should be float
leverage = float(25)

# Cancelling a position
for trade in trades:
    if trade['pl'] <= -100:
        print(lnm.futures_cancel_position({'id': trade['id']})) # diff from .futures_close? and what about futures_cancel?
    else:
        data_for_print = trade['id']
        print(f'skipping cancelling trade with id of {data_for_print} because the pl didnt reach threshold yet')

# Updating a trade or modifying stop loss
lnm.futures_update_trade({
    'id': 'trade_id',
    'type': 'stoploss',
    'value': 13290.5
  })

# Closing all positions
print(lnm.futures_close_all())

# Placing a future order
print(lnm.futures_new_trade({
    'type': 'm', # m or l
    'side': 's', # b or s
    'margin': 100, # in sats
    'leverage': 25.0, # float
    # 'quantity': 1,  # in usd
    'takeprofit': takeprofit, # integer
    'stoploss': stoploss # integer
  }))

# Trailing stop modification
trail_distance_long = 1200.0  # Distance to trail behind the market price for long positions
trail_distance_short = 1200.0  # Distance to trail above the market price for short positions
print('starting trailing stop modification')
for trade in trades:  # Assuming 'trades' is a list of your trades
    if trade['side'] == 'b':  # 'b' for buy/long position
        new_stoploss_value = last_price - trail_distance_long
        if 'stoploss' in trade and new_stoploss_value <= trade['stoploss']:
            continue
    elif trade['side'] == 's':  # 's' for sell/short position
        new_stoploss_value = last_price + trail_distance_short
        if 'stoploss' in trade and new_stoploss_value >= trade['stoploss']:
            continue
    else:
        continue
        
    lnm.futures_update_trade({
        'id': trade['id'],
        'type': 'stoploss',
        'value': new_stoploss_value
    })

# Cashing in
lnm.futures_cashin({
    'amount': 1000,
    'id': "99c470e1-2e03-4486-a37f-1255e08178b1"
  })

# Fetching carry fees
print(lnm.futures_carry_fees({
    'from': 0,
    'to': 5,
    #'limit': 20
  }))

# Placing an order for an option
instruments = lnm.options_get_instruments()
market = json.loads(instruments)
target = market[-1]
print(lnm.options_new_trade({
    'side': 'b',
    'quantity': 1,
    'settlement': 'physical',
    'instrument_name': target
  }))

# Fetching running option trade data
print(lnm.options_get_trades({
    # limit: 25,
    # status: 'running'
  }))

こんなに簡単にデルタニュートラル・先物・オプションが使えてそれが全てライトニング上にあるというのは本当に感激です。サービス提供者に感謝です。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1