SpotGridStra
Perform grid management on spot trading
PythonGrid
Perform grid management on spot trading
Source Python
# Spot grid management
'''
# Example:
def main():
spot_g_stra = ext.SpotGridStra({'exchange': exchanges[1], #Spot exchange
'ZPrecision': 2,
'XPrecision': 3,
'MinStock': 0.001,
'GridSize': 0.03,
'Symbol': 'BNB',
'BalancePoint': 0.5
})
while True:
spot_g_stra.run()
Sleep(60*10000)
'''
class SpotGridStra():
def __init__(self, cfg):
self.cfg = cfg
self.exchange = cfg['exchange'] #exchange
self.ZPrecision = cfg['ZPrecision'] #Coin price precision, integer
self.XPrecision = cfg['XPrecision'] #Minimum quantile of coin trading volume, e.g., for BNB is2
self.Symbol = cfg['Symbol'] #Coin symbol
self.GridSize = cfg['GridSize'] #Adjust According to the Difference
self.MinStock = cfg['MinStock'] #Minimum Trading Volume of Coin
self.BalancePoint = cfg.get('BalancePoint', 0.5) #The balance point of a specific currency's position
def CancelPendingOrders(self):
ret = False
while True:
orders = _C(self.exchange.GetOrders)
if len(orders) == 0 :
return ret
for j in range(len(orders)):
self.exchange.CancelOrder(orders[j].Id)
ret = True
if j < len(orders) - 1:
Sleep(1000)
return ret
def onTick(self):
self.exchange.SetCurrency(self.Symbol + '_USDT')
acc = _C(self.exchange.GetAccount)
#Log(acc)
ticker = _C(self.exchange.GetTicker)
spread = ticker.Sell - ticker.Buy
account_info = acc['Info']
bals = account_info['balances']
sel_asset = None
usdt_val = 0.0
for asset in bals:
if asset['asset'] == self.Symbol:
sel_asset = asset
if asset['asset'] == 'USDT':
usdt_val = float(asset['free'])
assert sel_asset is not None, 'Corresponding Asset Not Found":' + self.Symbol
asset_val = ((float(sel_asset['free']) + float(sel_asset['locked'])) * ticker.Sell)
total_val = usdt_val + asset_val
Log(self.Symbol, total_val, '=', acc.Balance, '+', asset_val)
diffAsset = (total_val*self.BalancePoint - asset_val)
ratio = diffAsset / total_val
#Log("ratio:", ratio, _D())
if abs(ratio) < self.GridSize:
return False
if ratio > 0 :
buyPrice = _N(ticker.Sell + spread, self.ZPrecision)
buyAmount = _N(diffAsset / buyPrice, self.XPrecision)
if buyAmount < self.MinStock:
return False
self.exchange.Buy(buyPrice, buyAmount, diffAsset, ratio)
else :
sellPrice = _N(ticker.Buy - spread, self.ZPrecision)
sellAmount = _N(-diffAsset / sellPrice, self.XPrecision)
if sellAmount < self.MinStock:
return False
self.exchange.Sell(sellPrice, sellAmount, diffAsset, ratio)
return True
def run(self):
if self.onTick():
Sleep(1000)
self.CancelPendingOrders()
ext.SpotGridStra = SpotGridStraPublished by ktrader on the FMZ strategy square · last modified 2021-04-29. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.
Build this with AgenKit
A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.
/agenkit build a grid bot with inventory caps and order deduplication from strategies/SpotGridStra.md- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- Get tests, backtest, risk controls and a paper-trading switch
Understand it first
Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.