Skip to main content

用于 3commas 的 Python api 包装器,在 api、模型、错误处理中具有扩展功能

项目描述

标题

这个库提供了帮助开发访问 3commas api的代码的工具,非常快速和容易。该库建立在 py3cw 库之上。主要特点是api的预建函数,更容易访问返回数据的模型,返回数据的自动属性解析,内置错误解析。

WIP:这个图书馆还在建设中。缺少一些端点实现。如果您需要特定的端点实现,请随时在 github 中创建 PR https://github.com/badass-blockchain/python-three-commas

安装

pip install three-commas

用法

该包是根据 api 路径的名称构建的。例如:

from three_commas import api

api.ver1.bots # bots endpoint
api.ver1.accounts # account endpoint
api.ver1.deals # deals endpoint
api.ver1.users # users endpoint
api.v2.smart_trades # v2 smart_trades endpoint

端点也从路径镜像。

# GET /ver1/bots/pairs_black_list
error, black_list_pairs = api.ver1.bots.get_pairs_black_list()

# GET /v2/smart_trades/{id}
error, smart_trades_list = api.v2.smart_trades.get_by_id(id=<your_smart_trade_id>)

您可以通过以下方式获取所有机器人:

error, bots_list = api.ver1.bots.get()

或单个机器人:

error, bot = api.ver1.bots.get_show_by_id(bot_id=<your_bot_id>)

或者聪明的交易

error, smart_trade = api.v2.smart_trades.get_by_id(id=9993000)

端点返回具有附加功能的 dict 对象。您可以像普通字典一样使用该对象(与您从 py3cw 接收的方式完全相同),或使用添加的功能。例如,如果您想获得机器人 max_active_deals,您可以同时执行以下操作:

error, bot = api.ver1.bots.get_show_by_id(bot_id=9999999)
if not error:
    max_active_deals = bot['max_active_deals']
    max_active_deals = bot.max_active_deals

Websocket 流

您可以轻松连接到 websockets 您可以使用注释。

import three_commas
from three_commas.model import DealEntity, SmartTradeV2Entity

@three_commas.streams.smart_trades
def handle_smart_trades(smart_trade: SmartTradeV2Entity):
    # Do here something with the smart trade
    # Every new smart trade is passed to this function
    print(smart_trade)

@three_commas.streams.deals
def handle_deals(deal: DealEntity):
    # do your awesome stuff with the deal
    print(deal)  #  {'id': 1311811868, 'type': 'Deal', 'bot_id': 6313165, 'max_safety_orders': 6, 'deal_has_error': False ....
    print(deal.account_id)  #  99648312
    print(deal.created_at)  #  string object '2022-02-18T05:26:06.803Z'
    print(deal.parsed(True).created_at)  #  datetime.datetime object 

为了使用 websocket 流,您需要在您的环境中设置 api 密钥和秘密。[后面的文档你可以找到如何设置环境变量](#设置api密钥和秘密)

或者您可以将密钥传递给装饰器:

@three_commas.streams.deals(api_key='<your_api_key>', secret='<your_secret>')
def handle_deals(deal):
    ...

对于调试,您可以打开调试级别

import logging

logging.getLogger('three_commas.streams').setLevel(level=logging.DEBUG)

你会看到很多 websocket 消息,包括 ping:

> DEBUG:three_commas.streams.streams: {"type": "welcome"}
> DEBUG:three_commas.streams.streams: {"type": "ping", "message": 1645286932}
> DEBUG:three_commas.streams.streams: {"type": "ping", "message": 1645286935}
> DEBUG:three_commas.streams.streams: {"type": "ping", "message": 1645286938}

您还可以将级别设置为 info 以减少详细的日志记录 logging.getLogger('three_commas.streams').setLevel(level=logging.INFO)

解析

该库的功能之一是自动解析返回的数据。从 api 获取的一些数字数据作为字符串返回。例如在机器人对象中:

...
"base_order_volume": "0.003",
"safety_order_volume": "0.003",
"safety_order_step_percentage": "1.0",
...

现在您无需费心检查字段的类型并将其解析为所需的类型。这个库自动解析这些字段:

error, bot = api.ver1.bots.get_show(9999999)
# base_order_volume is a float
base_order_volume = bot.get_base_order_volume() 

默认情况下会进行解析(日期时间字段除外)。如果您不希望解析该字段,并且希望返回原始字符串,请使用 parsed=False

error, bot = api.ver1.bots.get_show(9999999)
# base_order_volume is a str
base_order_volume = bot.parsed(False).base_order_volume 

像“created_at”这样的一些字段是时间戳。您可以将这些字段解析为 python 日期时间对象。默认情况下不解析时间戳字段,仅在需要时解析:

error, account = api.ver1.accounts.get_by_id(8888888)

# the original string returned by the api
created_at_str = account.created_at

# parsed into a datetime.datetime object
created_at_datetime = account.parsed(True).created_at

API 键

为了使用 api,您需要设置 api 密钥和秘密。这可以在全局范围内或按请求完成。要全局设置它,您需要设置环境变量 THREE_COMMAS_API_KEY 和 THREE_COMMAS_API_SECRET。要根据请求执行此操作,只需将它们传递给函数:

error, account = api.ver1.accounts.get_by_id(8888888, api_key='my_key', api_secret='my_secret')

请求密钥具有优先权。如果同时设置了全局和请求键,则将使用请求键。

强制模式

您可以全局设置强制模式,也可以按请求设置。要全局设置它,请将环境变量 THREE_COMMAS_FORCED_MODE 设置为 paper 或 real。要对每个请求使用强制模式,请将其作为参数传递:

error, paper_deals = api.ver1.deals.get(forced_mode='paper')
error, real_deals = api.ver1.deals.get(forced_mode='real')

枚举

一些枚举字段具有功能。

error, accounts_list = api_v1.accounts.get_accounts()
if not error:
    for account in accounts_list:
        if account.get_market_code().is_binance():
            # do stuff with the binance account.
else:
    # deal with error here

您可以检查 three_commas.model.generated_enums 包中可用的枚举

设置api key和secret

始终确保您的 api 密钥安全存储并且没有被推送到任何公共存储库中

dotenv 文件和 dotenv 加载器(首选,更安全)

您可以将密钥和秘密设置为环境变量。首选更安全的方法是使用 .env 文件:

创建一个名为“.env”的文件,其内容如下:

THREE_COMMAS_API_KEY=<your_api_key>
THREE_COMMAS_API_SECRET=<your_api_secret>

使用 .env 加载器作为 python-dotenv。使用 pip 在您的机器上安装它:“pip install python-dotenv”。如果您的项目是 git 项目,请确保您 gitignore .env 文件。然后在你的代码中

from dotenv import load_dotenv
load_dotenv()

现在您的变量已加载。享受使用图书馆。

使用 os 模块设置环境变量(不太安全)

也可以直接在代码中设置api key和secret。这种方法不太安全,不推荐。

import os

os.environ["THREE_COMMAS_API_KEY"] = <your_api_key>
os.environ["THREE_COMMAS_API_SECRET"] = <your_api_secret>

例子

发布新的智能交易

smart_trade = {
    'account_id': 99999999,
    'pair': 'USDT_FUN',
    'position': {
        'type': 'buy',
        'order_type': 'market',
        'units': {
            'value': "30526.0",
        },
        "total": {
            "value": "600.61907506"
        }
    },
    'take_profit': {
        'enabled': False,
    },
    'stop_loss': {
        'enabled': False,
    }

}

error, smart_trade_response = api.v2.smart_trades.post(smart_trade)

检索智能交易

error, smart_trade = api.v2.smart_trades.get_by_id(13819196)
if not error:
    # Do your awesome stuff with the smart trade
    print(smart_trade.profit)

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

三逗号-0.2.9.tar.gz (39.0 kB 查看哈希

已上传 source