Home > Blockchain >  Small white level API quantitative strategy instance - cross-platform move brick (with the code)
Small white level API quantitative strategy instance - cross-platform move brick (with the code)

Time:09-22

# - * - coding: utf-8 - * -

# spot move brick strategy implementation
# strategy logic: monitor trading for dish spreads, the unity between different platforms to sell high buy low

The from quant import const
The from quant. Utils import logger
The from quant. Config the import config
The from quant. Market import market
The from quant. Trade import trade
The from quant. Const import BINANCE, COINSUPER
The from quant. Order the import order
The from quant. Market import Orderbook
The from quant. Utils. Decorators import async_method_locker
The from quant. Order import ORDER_ACTION_BUY ORDER_ACTION_SELL, ORDER_STATUS_FILLED, \
ORDER_STATUS_PARTIAL_FILLED, ORDER_STATUS_CANCELED


The class Test1:

Def __init__ (self) :
"" "initialization
"" "
# maker platform
Self. Maker_platform=COINSUPER
Self. Maker_account=config. Accounts [0] (" account ")
Self. Maker_access_key=config. Accounts [0] [" access_key "]
Self. Maker_secret_key=config. Accounts [0] [" secret_key "]
Self. Maker_symbol=config. Maker_symbol

# taker platform
Self. Taker_platform=BINANCE
Self. Taker_account=config. Accounts [1] (" account ")
Self. Taker_access_key=config. Accounts [1] [" access_key "]
Self. Taker_secret_key=config. Accounts [1] [" secret_key "]
Self. Taker_symbol=config. Taker_symbol

The self. The strategy=config. Strategy # policy name

Self. Maker_buy_order_no=None # maker to create buy order id of the
Self. Maker_sell_order_no=None # maker to create a sell order id
Self. Maker_buy_price=1 # buy orders prices
Self. Maker_sell_price=1 # sell order prices
Self. Maker_buy_cumqty=0 # buy order cumulative volume
Self. Maker_sell_cumqty=0 # sell order cumulative volume

Self. Taker_bid_price=1 # taker dish the bid price is the reference price
Self. Taker_ask_price=1 # taker dish ask prices refer to prices

# are cancelled spreads
Self. Spread=config. Spread

# maker trading module
Cc={
"The strategy" : the self. The strategy,
"Platform" : the self maker_platform,
"Symbol" : the self maker_symbol,
"Account" : the self maker_account,
"Access_key:" self maker_access_key,
"Secret_key" : the self maker_secret_key,
"Asset_update_callback" : the self on_event_asset_update_callback,
"Order_update_callback" : the self on_event_maker_order_update,
"Init_success_callback" : the self on_event_maker_init_success_callback
}
# spot transactions without the position correction
Self. Maker_trader=Trade (cc) * *

# taker trading module
Cc={
"The strategy" : the self. The strategy,
"Platform" : the self taker_platform,
"Symbol" : the self taker_symbol,
"Account" : the self taker_account,
"Access_key:" self taker_access_key,
"Secret_key" : the self taker_secret_key,
"Asset_update_callback" : the self on_event_asset_update_callback,
"Order_update_callback" : the self on_event_taker_order_update,
"Init_success_callback" : the self on_event_taker_init_success_callback
}
# spot transactions without the position correction
Self. Taker_trader=Trade (cc) * *

# subscription prices, here only subscribe to the taker, according to the taker market on maker platform for dynamic hang from single
Market (const MARKET_TYPE_ORDERBOOK, self taker_platform, self. Taker_symbol, self. On_event_orderbook_update)

# init success
Self. Maker_init_success=False
Self. Taker_init_success=False

Async def on_event_maker_init_success_callback (self, success, error) :
# normal login, and gets the current outstanding orders
Logger. The info (" maker coinsuper initialize success: "the success," error: "error, caller=self)
Self. Maker_init_success=success

Async def on_event_taker_init_success_callback (self, success, error) :
# normal login, and gets the current outstanding orders
Logger. The info (" taker binance initialize success: "the success," error: "error, caller=self)
Self. Taker_init_success=success

Async def on_event_asset_update_callback (self, asset) :
Logger. The info (" current asset detail: ", asset)

Async def on_event_taker_order_update (self, order: the order) :
"" "taker order status update
"" "
Logger. The info (taker "order update", the order, the caller=self)

@ async_method_locker (" core ")
Async def on_event_maker_order_update (self, order: the order) :
"" "maker order status update
"" "
Logger. The info (maker "order update", the order, the caller=self)

# only for processing state, such as completely clinch a deal, part of the deal or withdraw single
If the order status!=ORDER_STATUS_PARTIAL_FILLED and order status!=ORDER_STATUS_FILLED and \
The order status!=ORDER_STATUS_CANCELED:
Return

# pay
If the order. The action==ORDER_ACTION_BUY and order. Order_no==self. Maker_buy_order_no:
Cur_cumqty=float (order quantity) - float (# order. Remain) the cumulative volume
Pre_cumqty=self. Maker_buy_cumqty # last time cumulative volume
Cur_filledqty=round (cur_cumqty - pre_cumqty, 3) # current volume

If cur_filledqty & gt; 0:
Action=ORDER_ACTION_SELL
Price=round (self. Taker_bid_price * (1-0.05), and 6) # below the taker the bid price 5% after single
Order_no, error=await self. Taker_trader. Create_order (action, price, cur_filledqty)

If the error:
Logger. The error (" taker create sell order error! Error: "error, caller=self)
The else:
# record the current total volume
Self. Maker_buy_cumqty=cur_cumqty

# if the transaction completed, or cancellations, reset
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related