的官方SDK柠檬市场交易和市场数据API
lemon-markets-sdk的Python项目详细描述
欢迎
这是官员柠檬市场SDK。有了这个SDK,您可以在柠檬市场以更简单的方式实现API,例如
- 创建、列出和检索订单或交易记录
- 列出你的投资组合
- 检索帐户详细信息、当前现金等
- 获取历史市场数据和实时订单记录数据
您可以在这里找到原始API文档:documentation.lemon.markets
开始吧
要使用我们的API,您必须注册lemon.markets。 柠檬市场完全免费,我们不收取市场数据或其他费用。如果你参加我们的beta交易比赛,你甚至有机会赢钱!在
安装这个SDK非常简单,只需运行
pip install lemon-markets-sdk
你可以走了。在
代币
对于每个API请求,都需要有一个API访问令牌。您可以在创建策略时在仪表板上生成一个。在
你的令牌看起来像这个:3c1c7343b6c8c241ebeedcb2a92095ba20dc692d
要使用token,导入token类,实例化它,然后由SDK完成剩下的工作:
fromlemon_markets.tokenimportTokenmy_token=Token("3c1c7343b6c8c241ebeedcb2a92095ba20dc692d")
您的帐户已链接到您的令牌,因此如果您想获取帐户详细信息,请执行以下操作:
^{pr2}$账户
如果您想list所有帐户(此时您应该只有一个帐户-演示帐户):
fromlemon_markets.accountimportAccountall_accounts=Account.list(authorization_token=my_token)my_account=all_accounts.results[0]# this one should be the demo account
如果您想检索单个帐户,请按其uuid获取:
fromlemon_markets.accountimportAccountmy_account=Account(uuid='76a42cea-a144-488f-afc2-a6fb967c3ab2',authorization_token=my_token)
您可以对帐户执行以下几项非常有用的操作:
my_account.cash_in_invest# gets the cash you have at hand (total balance minus open order volume)my_account.total_balance# gets your total cashmy_account.fetch_account_state()# updates your cash_to_invest and total_balance
订单
您想用我们的API做的一件重要的事情是创建订单。所以我们开始:
fromlemon_markets.orderimportOrderimportdatetimenew_order=Order(instrument="DE0007100000",# ISIN for Daimler, you can pass an instrument instance in here as wellquantity=10,side="buy",limit_price=50.00,valid_until=datetime.datetime.now()+datetime.timedelta(days=1),account=my_account)new_order.create()
您需要将上一步中的account实例传递给Order类。当您代表帐户创建订单时,需要account实例。您不需要传递令牌实例,因为帐户已链接到令牌。在
创建订单后,您可以通过访问is_executed属性来检查执行状态,例如new_order.is_executed
。
您可以通过执行new_order.check_if_executed()
来刷新状态。在
创建后,订单具有以下属性: |属性值| | ------ | ------ | |uuid |每个订单的自定义uuid(str)| |date_created |创建日期的datetime对象| |仪器|仪器对象| |数量|数量(国际)| |边|买入/卖出(str)| |类型|市场/停止|市场/限制/停止|限制(str)| |最近处理时间的已处理日期时间对象| |由于部分执行,已处理的数量可能低于预期数量。(内景)| |平均价格|执行价格(浮动)| |valid until |有效截止时间的datetime对象| |状态|打开/正在处理/已过期/已删除/已执行(str)
List所有订单:
fromlemon_markets.orderimportOrderall_orders=Order.list(account=my_account)all_orders.results
如果您的令牌链接到策略,则只显示该策略的顺序。在
Retrieve单个订单:
fromlemon_markets.orderimportOrderorder=Order(account=my_account,uuid="76a42cea-a144-488f-afc2-a6fb967c3ab2")order.retrieve()
投资组合
List您的所有资产组合项
fromlemon_markets.portfolioimportPortfolioportfolio=Portfolio.list(account=my_account)
只会显示您的代币链接到的策略中列出的投资组合项。在
市场数据
您可以通过柠檬市场restapi或通过websockets实时数据。在
仪器
但首先,你需要知道哪些工具(股票、衍生工具、ETF、债券)可供选择柠檬市场公司名称:
fromlemon_markets.instrumentimportInstrumentinstruments=Instrument.list(authorization_token=my_token)
历史数据:OHLC
我们希望以M1为基础(每分钟)检索仪器的OHLC(开盘-高开-低-收盘)数据:
fromlemon_markets.data.ohlcimportM1m1_data=M1.list(instrument="DE0007100000")# you can pass an Instrument instance in here as well
实时数据:Websocket
- 在接收数据之前,我们需要声明这种情况下会发生什么。在
- 其次,我们需要告诉流,它应该订阅一个工具。在
要获取ticks数据:
fromlemon_markets.data.streamsimportTickStreamdefon_message(socket,tick):print(tick)tick_stream=TickStream(on_message=on_message)defon_open(socket):tick_stream.subscribe("DE0007100000")# when the socket is open, we would like to subscribe to the Daimler tick streamtick_stream.on_open=on_opentick_stream.run()# this runs the socket forever/ until the connection interrups.
以及获取L1订单数据(或所谓的“报价”):
fromlemon_markets.data.streamsimportQuoteStreamdefon_message(socket,quote):print(quote)quote_stream=QuoteStream(on_message=on_message)defon_open(socket):quote_stream.subscribe("DE0007100000")# when the socket is open, we would like to subscribe to the Daimler quote streamquote_stream.on_open=on_openquote_stream.run()
待办事项
这个SDK还没有完成,但这是一个好的开始。请随意贡献!在
- 带readthedocs的文档
- 连接中断时重新连接websocket
- 测试
- 代码文档
- 添加列表终结点分页器
如有任何反馈、问题等,请随时联系info@lemon.markets或加入我们的Slack community
- 项目
标签: