0bps

Strategy vault / Grid

Simple Grid Test

JavaScript strategy by cnxzcxy.

JavaScriptGrid
Author
cnxzcxy
Complexity
Intermediate · 104 lines
Parameters
0
Backtest bar
15m · intraday
Tested on
LTC/BTC · spot

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.

in Claude Code, Cursor, Codex & 4 more
/agenkit build a grid bot with inventory caps and order deduplication from strategies/Simple-Grid-Test.md
  1. npx agenkit install engineering-kit in your project
  2. Paste the command into your agent and approve each phase
  3. 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

esc

Try , , or .

↑↓ move↵ open