Simple Grid Test
JavaScript strategy by cnxzcxy.
JavaScriptGrid
The original spec has no written description. Read the source below.
Source JavaScript
/*backtest
start: 2018-08-01 00:00:00
end: 2018-09-01 15:00:00
period: 15m
exchanges: [{"eid":"Huobi","currency":"LTC_BTC","stocks":0}]
*/
var runBuyOrder = [];
var runSellOrder = [];
var basePrice = 0;
var percent = 0.05;
function onTick(){
var date = _D();
var depth = exchange.GetDepth();
account = exchange.GetAccount();
//Initialize account
if((account.Balance > 0) && (basePrice == 0)){
buyMoney = account.Balance * 0.09;
basePrice = depth.Asks[0].Price;
buyPrice = basePrice * (1-percent * 0);
runBuyOrder.push(exchange.Buy(buyPrice, buyMoney / buyPrice));
buyPrice = basePrice * (1-percent * 1);
runBuyOrder.push(exchange.Buy(buyPrice, buyMoney / buyPrice));
buyPrice = basePrice * (1-percent * 2);
runBuyOrder.push(exchange.Buy(buyPrice, buyMoney / buyPrice));
buyPrice = basePrice * (1-percent * 3);
runBuyOrder.push(exchange.Buy(buyPrice, buyMoney / buyPrice));
buyPrice = basePrice * (1-percent * 4);
runBuyOrder.push(exchange.Buy(buyPrice, buyMoney / buyPrice));
buyPrice = basePrice * (1-percent * 5);
runBuyOrder.push(exchange.Buy(buyPrice, buyMoney / buyPrice));
buyPrice = basePrice * (1-percent * 6);
runBuyOrder.push(exchange.Buy(buyPrice, buyMoney / buyPrice));
buyPrice = basePrice * (1-percent * 7);
runBuyOrder.push(exchange.Buy(buyPrice, buyMoney / buyPrice));
buyPrice = basePrice * (1-percent * 8);
runBuyOrder.push(exchange.Buy(buyPrice, buyMoney / buyPrice));
buyPrice = basePrice * (1-percent * 9);
runBuyOrder.push(exchange.Buy(buyPrice, buyMoney / buyPrice));
buyPrice = basePrice * (1-percent * 10);
runBuyOrder.push(exchange.Buy(buyPrice, buyMoney / buyPrice));
Log(date);
Log('exchange:', exchange.GetName(), ' Account:', exchange.GetAccount());
Log('Sell Order:', runSellOrder, ' Buy Order:', runBuyOrder);
}
//Check order polling
allOrders = runBuyOrder.concat(runSellOrder);
allOrders.forEach(function(orderId){
order = exchange.GetOrder(orderId);
//Check sell order
if(runSellOrder.indexOf(order.Id) > -1){
if((order.Status == ORDER_STATE_CLOSED) && (order.Type == ORDER_TYPE_SELL)){
Log(date);
Log('Sell order successful price:', order.Price, ' Quantity:', order.DealAmount);
Log('exchange:', exchange.GetName(), ' Account:', exchange.GetAccount());
//Remove from local database and place a new buy order
index = runSellOrder.indexOf(order.Id);
runSellOrder.splice(index, 1);
buyPrice = order.Price * (1 - percent);
runBuyOrder.push(exchange.Buy(buyPrice, order.DealAmount * order.Price / buyPrice));
}
}
//Check the purchase order
if(runBuyOrder.indexOf(order.Id) > -1){
if((order.Status == ORDER_STATE_CLOSED) && (order.Type == ORDER_TYPE_BUY)){
Log(date);
Log('Buy order successful price:', order.Price, ' Quantity:', order.DealAmount);
Log('exchange:', exchange.GetName(), ' Account:', exchange.GetAccount());
//Remove from local database and place a new sell order
index = runBuyOrder.indexOf(order.Id);
runBuyOrder.splice(index, 1);
sellPrice = order.Price * (1 + percent);
runSellOrder.push(exchange.Sell(sellPrice, order.DealAmount));
}
}
});
}
function main(){
while(true){
onTick();
}
}
Published by cnxzcxy on the FMZ strategy square · last modified 2018-09-14. 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/Simple-Grid-Test.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