Skip to content

ln-markets/api-python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LN Markets Python API

A simple way to connect your Python application to LN Markets!

Install

You can install this package with pip:

pip3 install ln-markets

Use

You can import the rest class from lnmarkets

from lnmarkets import rest

And the websocket class as well

from lnmarkets import websockets

Authentication

For authentication you need your api key secret and passphrase

Without you will not bet able to authenticate

⚠️ Never share your API Key, Secret or Passphrase

Websocket API

Configuration

Use the LNMarketsWebsocket and your key / passphrase to instanciate a new api connector:

options = {'key': 'your_api_key', 
           'secret': 'your_api_secret', 
           'passphrase': 'your_api_passphrase',
           'network': 'testnet'}

lnm = websockets.LNMarketsWebsocket(**options)
lnm.connect()

Check examples for more details as you'll need to extend this class most of the time.

Subscription

You can subscribe to LNM Markets public event such as futures last price ('futures:btc_usd:last-price') and index ('futures:btc_usd:index').

REST API Authentication

Using API key

Use the LNMarketsRest and your key / passphrase to instanciate a new api connector:

from lnmarkets import rest

options = {'key': 'your_api_key', 
           'secret': 'your_api_secret', 
           'passphrase': 'your_api_passphrase',
           'network': 'testnet'}

lnm = rest.LNMarketsRest(**options)

lnm.futures_get_ticker()

Using Mnemonic seed

Use the LNMarketsRest with BIP39 mnemonic phrase to instanciate a new api connector:

from lnmarkets import rest

mnemonic = 'struggle goddess action cheap endorse venue force tomato exercise cactus charge such'

lnm = rest.LNMarketsRest(mnemonic=mnemonic)

lnm.futures_get_ticker()

Mnemonic seed auth is a "hack" using the cookie used by LNM WebApp, so for avoid risk of stolen cookie, you should logout, when your program stop running:

lnm.logout()

The cookie have a lifetime (about 1 week), so if your program running for more than that time you might renew the cookie before expiry date (note that it can also be a safety measure tu renew your cookie often to avoid stolen cookies risk):

lnm.renew_cookie()

REST API

See the API documentation for more details.

futures_add_margin

Add more margin to an existing trade.

amount:
  type: Integer
  required: true
id:
  type: String
  required: true

Example:

lnm.futures_add_margin({
    'amount': 20000,
    'id': '249dc818-f8a5-4713-a3a3-8fe85f2e8969'
  })

POST /futures/add-margin documentation for more details.

futures_cancel_all

Cancel all opened (not running) trades for this user.

# No parameters

Example:

lnm.futures_cancel_all()

DELETE /futures/all/cancel documentation for more details.

futures_close_all

Close all running trades for this user.

# No parameters

Example:

lnm.futures_close_all()

DELETE /futures/all/close documentation for more details.

futures_cancel

Cancel a specific trade for this user.

id:
  type: String
  required: true

Example:

lnm.futures_cancel_position({
    'id': 'b87eef8a-52ab-2fea-1adc-c41fba870b0f'
  })

POST /futures/cancel documentation for more details.

futures_cashin

Cash in a part of the PL of a running trade.

amount:
  type: Integer
  required: true
id:
  type: String
  required: true

Example:

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

POST /futures/cash-in documentation for more details.

futures_carry_fees

Get carry-fees history.

from:
  type: Integer
  required: true

to:
  type: Integer
  required: tue

limit:
  type: Integer
  required: false
  default: 100

Example:

lnm.futures_carry_fees({
    'limit': 20
  })

GET /futures/carry-fees documentation for more details.

futures_close

Close a specific running trade for this user.

id:
  type: String
  required: true

Example:

lnm.futures_close({
    'id': 'a2ca6172-1078-463d-ae3f-8733f36a9b0e'
  })

DELETE /futures documentation for more details.

futures_get_trades

Retrieve all or a part of user's trades.

type:
  type: String
  required: true
  enum: ['open', 'running', 'closed']
  default: 'open'

from:
  type: Integer
  required: false

to:
  type: Integer
  required: false

limit:
  type: Integer
  required: false
  default: 100

Example:

lnm.futures_get_trades({
    'type': 'running'
  })

GET /futures documentation for more details.

futures_new_trade

Open a new trade. If type="l", the property price must be included in the request to know when the trade should be filled. You can choose to use the margin or the quantity as a parameter, the other will be calculated with the one you chose.

type:
  type: String
  required: true
  enum: ['l', 'm']

side:
  type: String
  required: true
  enum: ['b', 's']

margin:
  type: Integer
  required: true

leverage:
  type: Float
  required: true

quantity:
  type: Integer
  required: false

takeprofit:
  type: Integer
  required: false

stoploss:
  type: Integer
  required: false

price:
  type: Float
  required: false

Example:

  lnm.futures_new_trade({
    'type': 'm',
    'side': 's',
    'margin': 10000,
    'leverage': 25,
  })

POST /futures documentation for more details.

futures_update_trade

Modify stoploss or takeprofit parameter of an existing trade.

id:
  type: String
  required: true

type:
  type: String
  required: true
  enum: ['takeprofit', 'stoploss']

value:
  type: Float
  required: true

Example:

lnm.futures_update_trade({
    'id': 'b87eef8a-52ab-2fea-1adc-c41fba870b0f',
    'type': 'stoploss',
    'value': 13290.5
  })

PUT /futures documentation for more details.

futures_get_price

Get Futures price data over time.

from:
  type: Integer
  required: false

to:
  type: Integer
  required: false

limit: Integer
  required: false
  default: 100

Example:

lnm.futures_price({
    'limit': 20
  })

GET /futures/history/price documentation for more details.

futures_fixing

Get fixing data history.

from:
  type: Integer
  required: false

to:
  type: Integer
  required: false

limit:
  type: Integer
  required: false
  default: 100

Example:

lnm.futures_fixing({
    'limit': 20
  })

GET /futures/history/fixing documentation for more details.

futures_index

Get index history data.

from:
  type: Integer
  required: false

to:
  type: Integer
  required: false

limit:
  type: Integer
  required: false
  default: 100

Example:

lnm.futures_index({
    'limit': 20
  })

GET /futures/history/index documentation for more details.

futures_get_ticker

Get the futures ticker.

# No parameters

Example:

lnm.futures_get_ticker()

GET /futures/ticker documentation for more details.

futures_get_leaderboard

Queries the 10 users with the biggest positive PL on a daily, weekly, monthly and all-time basis.

# No parameters

Example:

lnm.futures_get_leaderboard()

GET /futures/leaderboard documentation for more details.

futures_get_market

Get the futures market details.

# No parameters

Example:

lnm.futures_get_market()

GET /futures/market documentation for more details.

options_get_instruments

Get the options instruments list.

# No parameters

Example:

lnm.options_get_instruments()

GET /options/instruments documentation for more details.

options_get_instrument

Get a specific option instrument detail.

instrument_name:
  type: String
  required: true

Example:

lnm.options_get_instrument({
    'instrument_name': 'BTC.2024-01-05.43000.C'
  })

GET /options/instrument documentation for more details.

options_close_all

Close all of the user option trades, the PL will be calculated against the current bid or offer depending on the type of the options.

# No parameters

Example:

lnm.options_close_all()

DELETE /options/close-all documentation for more details.

options_close

Close the user option trade, the PL will be calculated against the current bid or offer depending on the type of the option.

id:
  type: String
  required: true

Example:

lnm.options_close({
    'id': 'a61faebc-7cc9-47e4-a22d-9d3e95c98322'
  })

DELETE /options documentation for more details.

options_get_trades

Get user's options trades.

status:
  type: String
  enum: ['running', 'closed']
  default: running
  required: true

from:
  type: Integer
  required: false

to:
  type: Integer
  required: false

limit:
  type: Integer
  required: false

Example:

  lnm.options_get_trades({
    limit: 25,
    status: 'closed'
  })

GET /options documentation for more details.

options_new_trade

Create a new options trade

side:
  type: String
  enum: ['b']
  required: true

quantity:
  type: Integer
  required: true

settlement:
  type: String
  enum: ['physical', 'cash']
  required: true

instrument_name:
  type: String
  required: true

Example:

  lnm.options_new_trade({
    'side': 'b',
    'quantity': 10,
    'settlement': 'physical',
    'instrument_name': 'BTC.2024-01-05.43000.C'
  })

POST /options documentation for more details.

options_update_trade

Allows user to update settlement parameter in running option trade.

id:
  type: String
  required: true

settlement:
  type: String
  required: true
  enum: ['physical', 'cash']

Example:

lnm.options_update_trade({
    'id': 'a2ca6172-1078-463d-ae3f-8733f36a9b0e',
    'settlement': 'physical'
  })

PUT /options documentation for more details.

options_get_volatility

Return the volatility

instrument:
  type: String
  required: false

Example:

lnm.options_get_volatility({
    'instrument': 'BTC.2016-01-14.20000.C'
  })

GET /options/volatility documentation for more details.

options_get_market

Get the options market details.

# No parameters

Example:

lnm.options_get_market()

GET /options/market documentation for more details.

get_swaps

Get swap history

from:
  type: Integer
  required: false
  
to:
  type: Integer
  required: false
  
limit:
  type: Integer
  required: False

Example:

  lnm.get_swaps({
    'from': 1669980001000,
    'to': '1669990201000',
    'limit': 100
  })

GET /swap documentation for more details.

swap

Swap betweem sats and synthetic USD

in_asset:
  type: String
  required: true
  enum: ['USD', 'BTC']
  
out_asset:
  type: String
  required: true
  enum: ['USD', 'BTC']

in_amount:
  type: Integer
  required: False

out_amount:
  type: Integer
  required: false

Example:

  lnm.swap({
    'in_asset': 'BTC',
    'out_asset': 'USD',
    'out_amount': 100
  })

POST /swap documentation for more details.