50 Line Grid Strategy Tutorial
JavaScript strategy by 发明者量化-小小梦.
JavaScriptGrid
The original spec has no written description. Read the source below.
Parameters
| Name | Default | What it controls |
|---|---|---|
| direction | true | Grid direction |
| _GridNum | 10 | Number of grid nodes |
| _GridPointAmount | 0.1 | Order quantity per grid node |
| _GridPointDis | 20 | Grid node spacing |
| _GridCovDis | 50 | Grid node closing spread |
Source JavaScript
var _StopLoss = 0
var _StopWin = 0
var _Grid = []
function UpdateGrid(nowBidsPrice, nowAsksPrice, direction){ // up 1, down -1
if(_Grid.length == 0 || (direction == 1 && nowBidsPrice - _Grid[_Grid.length - 1].price > _GridPointDis) ||
(direction == -1 && _Grid[_Grid.length - 1].price - nowAsksPrice > _GridPointDis)){
var nowPrice = direction == 1 ? nowBidsPrice : nowAsksPrice
_Grid.push({
price: _Grid.length == 0 ? nowPrice : _Grid[_Grid.length - 1].price + _GridPointDis * direction,
hold : {price: 0, amount: 0},
coverPrice : _Grid.length == 0 ? nowPrice - direction * _GridCovDis : _Grid[_Grid.length - 1].price + _GridPointDis * direction - direction * _GridCovDis
})
var tradeInfo = direction == 1 ? $.Sell(_GridPointAmount) : $.Buy(_GridPointAmount)
_Grid[_Grid.length - 1].hold.price = tradeInfo.price
_Grid[_Grid.length - 1].hold.amount = tradeInfo.amount
$.PlotFlag(new Date().getTime(), JSON.stringify(tradeInfo), "O")
}
if(_Grid.length > 0 &&
((direction == 1 && nowAsksPrice < _Grid[_Grid.length - 1].coverPrice) || (direction == -1 && nowBidsPrice > _Grid[_Grid.length - 1].coverPrice))){
var coverInfo = direction == 1 ? $.Buy(_Grid[_Grid.length - 1].hold.amount) : $.Sell(_Grid[_Grid.length - 1].hold.amount)
_Grid.pop()
$.PlotFlag(new Date().getTime(), JSON.stringify(coverInfo), "C")
_StopWin++
} else if(_Grid.length > _GridNum){
var coverfirstInfo = direction == 1 ? $.Buy(_Grid[0].hold.amount) : $.Sell(_Grid[0].hold.amount)
_Grid.shift()
$.PlotFlag(new Date().getTime(), JSON.stringify(coverfirstInfo), "C")
_StopLoss++
}
}
function main(){
while(1){
var ticker = _C(exchange.GetTicker)
var records = _C(exchange.GetRecords)
$.PlotRecords(records, "kline")
UpdateGrid(ticker.Buy, ticker.Sell, direction)
var msg = ""
for(var i = 0; i < _Grid.length; i++){
msg += JSON.stringify(_Grid[i]) + "\n"
}
LogStatus(_D(), "_StopWin:", _StopWin, "_StopLoss:", _StopLoss, _C(exchange.GetAccount), "\n", "_Grid.length:", _Grid.length, "_GridNum:", _GridNum, "\n", msg)
Sleep(500)
}
}Published by 发明者量化-小小梦 on the FMZ strategy square · last modified 2018-08-25. 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/50-Line-Grid-Strategy-Tutorial.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.
Related specs
- Bybit Inverse Contract Dynamic Grid Special GridJavaScript · Grid
- Grid Pending Order Plus Martin Multiple IncreaseJavaScript · Grid · 1d
- Single Commodity Small Shop Strategy V20 Annualized 130JavaScript · Grid · 1d
- Two Way Contract Grid Trading V102JavaScript · Grid · 1m
- Mesh Deformation Strategy for Single Sided MeshJavaScript · Grid
- Mesh Deformation Strategy for Single Sided Mesh NotesJavaScript · Grid · 1d