LN Markets API python implementation
Project description
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
:warning: 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
futures_add_margin
futures_cancel_all
futures_close_all
futures_cancel
futures_cashin
futures_close
futures_get_trades
futures_new_trade
futures_update_trade
futures_carry_fees
futures_get_price
futures_fixing
futures_index
futures_get_leaderboard
futures_get_ticker
futures_get_market
options_get_instruments
options_get_instrument
options_close_all
options_close
options_get_trades
options_new_trade
options_update_trade
options_get_volatility
options_get_market
get_swaps
swap
- get_user
- update_user
- get_new_bitcoin_address
- get_bitcoin_addresses
- get_deposit
- get_deposits
- new_deposit
- new_deposit_susd
- get_withdrawal
- get_withdrawals
- new_withdraw
- new_withdraw_susd
- new_internal_transfer
- get_oracle_index
- get_oracle_last
- fetch_notifications
- mark_notifications_read
- app_configuration
- app_node
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file ln_markets-2.0.9.tar.gz
.
File metadata
- Download URL: ln_markets-2.0.9.tar.gz
- Upload date:
- Size: 8.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 48194e42bc763f2dcf8f4befb6ad615273ea6f9b47c4f117862cc0a328958e95 |
|
MD5 | 7d13e167c63b1465f6295583c7689dff |
|
BLAKE2b-256 | 1a5f3e76d004977bfd4129691c8cc7113f02c1a721cb9ebb9a7da92bfaf319de |
File details
Details for the file ln_markets-2.0.9-py2.py3-none-any.whl
.
File metadata
- Download URL: ln_markets-2.0.9-py2.py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e328d6eb7df02b6365cafd0222233039ddc5ed89441d8e02b26dc19284f1e93b |
|
MD5 | cca88e9f37ead0d52fd4dff9062c8fe2 |
|
BLAKE2b-256 | 017130d7ec995a31f35c4d768f1fc9171367cb9b6249ac84a02a538b696b799a |