自动交易
您的语言
您的规则
借助 GMI Edge API 将您的交易想法转化为行动 - 使用您选择的语言(包括 Python、C、C++、C# 等)编写代码并自动运行您的交易机器人。

交易机器人与手动交易相比的优势

全天候市场动态
机器人从不睡觉--即使在你休息的时候,也能不分昼夜地捕捉机会。

无情绪交易
消除恐惧和贪婪;机器人会毫不犹豫地执行你的计划。

对市场事件的即时反应
自动对价格飙升、新闻动态或市场波动做出反应——速度远超任何人。

可扩展性
以人工无法实现的规模管理复杂策略或高频交易。
如何拥有您的交易机器人

注册或登录 GMI 会员区

获取您的 GMI EDGE 交易账户凭证

阅读文档并开始编写您的交易策略

启动您的交易机器人,让它为您交易
功能特点

多种语言
可使用任何编程语言(Python、C++、C#、Java、JavaScript、Go 等)构建您的机器人。 没有任何限制。

无限自动化潜力
对机器人的数量或复杂性没有限制,从简单的 交易机器 到高级的多资产、多账户策略,均可扩展。

跨平台兼容性
在 Windows、Mac、Linux 甚至云服务器上均可使用API,随时随地运行机器人。

透明、现代的架构
没有隐藏层或专有脚本语言。 您的代码可直接与我们的 API 交互,实现完全的透明性和灵活性。
常见问题






交易机器人代码示例
import yaml
import httpx
import sys
import ssl
from datetime import datetime, timedelta
import time
def read_config(filename):
with open(filename, "r") as file:
return yaml.safe_load(file)
def check_response(response):
if response.status_code == httpx.codes.OK:
return
error_body = response.json()
error_code = error_body["Code"]
error_message = error_body["Error"]
print(f"[{error_code}] {error_message}")
sys.exit(-1)
def report_position(position):
order_id = position["OrderId"]
amount = position["Amount"]
symbol = position["Symbol"]
order_side = position["OrderSide"]
order_status = position["OrderStatus"]
print(f"#{order_id} {symbol} {amount} {order_side} {order_status}")
def time_to_seconds(time_text):
t = datetime.strptime(time_text, "%M:%S")
delta = timedelta(minutes=t.minute, seconds=t.second)
return delta.total_seconds()
def find_position_profit(orders_state, order_id):
pos_state = next(
order for order in orders_state if order["OrderId"] == order_id
)
return pos_state["Profit"]
if __name__ == "__main__":
print("Reading bot configuration...")
config = read_config("bot-config.yaml")
api_url = config["api-url"]
account = config["account"]
password = config["password"]
symbols = config["symbols"]
pause1 = config["pause1"]
pause1_sec = time_to_seconds(pause1)
pause2 = config["pause2"]
pause2_sec = time_to_seconds(pause2)
print("Done.")
print(f"Connecting to account {account} via {api_url}...")
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
ssl_context.load_default_certs()
http = httpx.Client(verify=ssl_context)
login_response = http.post(
api_url + "/login", json={"BotId": account, "Password": password}
)
check_response(login_response)
print("Connected.")
access_token = login_response.json()["AccessToken"]
auth_headers = {"Authorization": "Bearer " + access_token}
try:
while True:
print("Checking my open positions...")
open_pos_response = http.get(
api_url + "/positionlist", headers=auth_headers
)
check_response(open_pos_response)
all_positions = open_pos_response.json()["Orders"]
my_positions = [
p for p in all_positions if p["OrderSource"] == "TRADING_API"
]
if len(my_positions) == 0:
print("No open positions.")
else:
for position in my_positions:
report_position(position)
print("Closing my open positions...")
for position in my_positions:
order_id = position["OrderId"]
close_pos_response = http.post(
api_url + "/closeposition",
json={"OrderId": order_id},
headers=auth_headers,
)
check_response(close_pos_response)
print("Closed.")
for symbol in symbols:
print(f"Checking symbol {symbol}...")
syminfo_response = http.post(
api_url + "/symbolinfo",
json={"Symbol": symbol},
headers=auth_headers,
)
check_response(syminfo_response)
min_lots = syminfo_response.json()["MinTradeAmount"]
lot_size = syminfo_response.json()["ContractSize"]
min_amount = min_lots * lot_size
print(f"Minimum tradable amount for {symbol}: {min_amount}")
print("Opening positions")
pos_ids = []
for symbol in symbols:
buy_pos_response = http.post(
api_url + "/sendorder",
json={
"Symbol": symbol,
"OrderSide": "BUY",
"OrderType": "MARKET",
"Amount": min_amount,
},
headers=auth_headers,
)
check_response(buy_pos_response)
report_position(buy_pos_response.json())
buy_pos_id = buy_pos_response.json()["OrderId"]
sell_pos_response = http.post(
api_url + "/sendorder",
json={
"Symbol": symbol,
"OrderSide": "SELL",
"OrderType": "MARKET",
"Amount": min_amount,
},
headers=auth_headers,
)
check_response(sell_pos_response)
report_position(sell_pos_response.json())
sell_pos_id = sell_pos_response.json()["OrderId"]
pos_ids.append((buy_pos_id, sell_pos_id))
print(f"Wait {pause1}...")
time.sleep(pause1_sec)
print("Checking profit and loss...")
account_state_response = http.get(
api_url + "/accountstate", headers=auth_headers
)
check_response(account_state_response)
balance = account_state_response.json()["AccountState"]["Balance"]
print(f"Account balance: {balance}")
orders_state = account_state_response.json()["OrderStates"]
to_close = []
for buy_pos_id, sell_pos_id in pos_ids:
buy_pos_profit = find_position_profit(orders_state, buy_pos_id)
sell_pos_profit = find_position_profit(
orders_state, sell_pos_id
)
print(f"#{buy_pos_id} PROFIT {buy_pos_profit}")
print(f"#{sell_pos_id} PROFIT {sell_pos_profit}")
if buy_pos_profit <= sell_pos_profit:
to_close.append(buy_pos_id)
else:
to_close.append(sell_pos_id)
print("Close less profitable position in each pair...")
for order_id in to_close:
print(f"Closing #{order_id}...")
close_response = http.post(
api_url + "/closeposition",
json={"OrderId": order_id},
headers=auth_headers,
)
check_response(close_response)
print("Done.")
说明
所述代码定义了交易机器人的操作顺序:
- 1. 连接特定账户的交易 API
- 2. 选择目标交易品种
- 3. 识别这些交易品种由机器人开立的所有头寸
- 4. 平仓所选交易品种上所有由机器人发起的现有头寸
- 5. 为一个交易品种开立两个相反的头寸:
- 买入头寸
- 卖出头寸
- 6. 1 分钟后:平仓亏损头寸(根据当前盈亏情况
- 7. 1 分钟后:从步骤 1 开始重复整个循环
自动交易. 您的语言. 您的规则.
借助 GMI Edge API 将您的交易想法转化为行动 - 使用您选择的语言(包括 Python、C、C++、C# 等)编写代码并自动运行您的交易机器人。