-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathengine-main.mojo
164 lines (134 loc) · 4.17 KB
/
engine-main.mojo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import time
from memory import UnsafePointer
from collections import Dict
from monoio_connect import (
create_monoio_runtime,
logt,
logd,
logi,
logw,
loge,
Fixed,
parse_bool,
init_logger,
destroy_logger,
LogLevel,
)
from ccxt.base import (
TradingContext,
ExchangeId,
Any,
Ticker,
OrderBook,
Trade,
Balance,
Order,
Exchangeable,
OrderType,
OrderSide,
Strings,
)
from ccxt.foundation.bybit import Bybit
from ccxt.foundation.gate import Gate
from ccxt.foundation.binance import Binance
from ccxt.foundation.bitmex import BitMEX
from ccxt.foundation import (
create_exchange_instance,
)
from ccxt.pro.gate import Gate as GatePro
from ccxt.pro.binance import Binance as BinancePro
from ccxt.pro.bybit import Bybit as BybitPro
from ccxt import Strategizable
from ccxt.executor import Executable, Executor
from ccxt.engine import (
Engine,
run,
)
from mojoenv import load_mojo_env
struct MyStrategy[E: Exchangeable](Strategizable):
var ex: UnsafePointer[Executor[E]]
var signal: Bool
fn __init__[E_: Exchangeable](out self, ex: UnsafePointer[Executor[E_]]):
self.ex = ex.bitcast[Executor[E]]()
self.signal = False
fn __moveinit__(out self, owned existing: Self):
self.ex = existing.ex
existing.ex = UnsafePointer[Executor[E]]()
self.signal = existing.signal
fn __del__(owned self):
pass
fn on_init(mut self) raises:
logd("on_init")
# 订阅ticker
self.ex[].subscribe_ticker("XRP_USDT", Dict[String, Any]())
# 订阅tickers
var symbols = List[String]()
symbols.append("XRP_USDT")
symbols.append("BTC_USDT")
self.ex[].subscribe_tickers(symbols, Dict[String, Any]())
# 订阅order_book
self.ex[].subscribe_order_book("XRP_USDT", Dict[String, Any]())
# 订阅trade
self.ex[].subscribe_trade("XRP_USDT", Dict[String, Any]())
# 订阅balance
self.ex[].subscribe_balance(Dict[String, Any]())
# 订阅order
self.ex[].subscribe_order("XRP_USDT", Dict[String, Any]())
# 订阅my_trades
self.ex[].subscribe_my_trades("XRP_USDT", Dict[String, Any]())
# 获取ticker
var ticker = self.ex[].fetch_ticker("XRP_USDT")
logd("ticker: " + str(ticker))
fn on_deinit(mut self) raises:
logd("on_deinit")
fn on_ticker(mut self, ticker: Ticker) raises:
logd("on_ticker")
if not self.signal:
# 异步下单
var params = Dict[String, Any]()
self.ex[].create_order_async(
"BTC_USDT",
OrderType.Limit,
OrderSide.Buy,
Fixed(1.0),
Fixed(93000),
params,
)
self.signal = True
fn on_order_book(mut self, order_book: OrderBook) raises:
logd("on_order_book")
fn on_trade(mut self, trade: Trade) raises:
logd("on_order_book")
fn on_balance(mut self, balance: Balance) raises:
logd("on_balance")
fn on_order(mut self, order: Order) raises:
logd("on_order")
fn on_my_trade(mut self, trade: Trade) raises:
logd("on_my_trade")
fn main() raises:
var logger = init_logger(LogLevel.Debug, "", "")
var env_vars = load_mojo_env(".env")
var api_key = env_vars["GATEIO_API_KEY"]
var api_secret = env_vars["GATEIO_API_SECRET"]
var testnet = parse_bool(env_vars["GATEIO_TESTNET"])
var config = Dict[String, Any]()
config["api_key"] = api_key
config["api_secret"] = api_secret
config["testnet"] = testnet
config["verbose"] = True
var engine = Engine[Gate, GatePro, MyStrategy[Gate]](
config, ExchangeId.gateio, "1", "1"
)
engine.start()
# var engine1 = Engine[Binance, BinancePro, MyStrategy[Binance]](
# config, ExchangeId.binance, "1", "1"
# )
# engine1.start()
# var engine2 = Engine[Bybit, BybitPro, MyStrategy[Bybit]](
# config, ExchangeId.bybit, "1", "1"
# )
# engine2.start()
# run[Gate, GatePro, MyStrategy[Gate]](
# config, ExchangeId.gateio, "1", "1"
# )
destroy_logger(logger)