0bps

Strategy vault / Grid

Unilateral Grid

When testing, fill in the starting price to test whether it is normal. If starting price is not filled in, buy at current price + price range WeChat173970984

JavaScriptGrid
Author
6821281
Complexity
Intermediate · 147 lines
Parameters
8

When testing, fill in the starting price to test whether it is normal. If starting price is not filled in, buy at current price + price range WeChat173970984

Parameters

NameDefaultWhat it controls
buyjingdu2Order price accuracy
buymount3Order quantity precision
buypersenttrueOrder Percentage
buylimit0.01Minimum transaction volume
reffertime3000Refresh interval
moneybt100Price spacing of the grid
startpricexfalseStarting Price
liruncha0.1Profit Spread

Source JavaScript

//Get account balance
var  startPrice= 0;
function getBalancex(){
    var balance = exchange.GetAccount();
    Log("Balance:", balance.Balance,"Coin balance:", balance.Stocks);
     return balance;
}    


//Get current coin price
function getPrice(typex){
    var NowPrice =exchange.GetTicker();
    if(typex=='buy'){
        
     Log("Current buy price:"+(NowPrice.Buy))
     return NowPrice.Buy;
    }
    if(typex=='sell'){
     Log("Current sell price:"+(NowPrice.Sell))
     return NowPrice.Sell;
    }
}

//Cancel all buy orders and place them again
function cancelOrders(){
    //Get all unfilled orders
    var orders = _C(exchange.GetOrders);
    for(var z in orders){
       //# Log("Current order type:",orders[z].Type,"OrderId:",orders[z].Id);
        if(orders[z].Type==0){
            exchange.CancelOrder(orders[z].Id)
            Log("Cancel previous pending order successfully,id:",orders[z].Id);
        }
    }
    /*
    for(var i=0;i<orders.length;i++){
        //Only cancel buy orders, do not cancel sell orders
        if(orders.Type==0){
            exchange.CancelOrder(orders.Id)
            Log("Cancel previous pending order successfully")
        }
    }*/
}



var saveBuyId= 0;
var doNowPrice = 0;
//Order operation Buy operation
function doTradingBuy(){
    cancelOrders()
    var price = getPrice("buy");
    if(startpricex>0){
        price=startpricex-moneybt;
    }else{
        price=price-moneybt;  //The actual price is the current price minus the price within the range
    }
    startPrice=price;
    var balancex = getBalancex();
    balanceAmount = balancex.Balance;
    
    //Quantity to be purchased
    doAmount = (balanceAmount*(buypersent/100))/price
    
    
    
    var id = 0; 
    if(doAmount>buylimit){
        saveBuyId=id= exchange.Buy(price, doAmount);
        Log("Order Successfulid:", id,"  Price:", price,"  Quantity:", doAmount);
    }else{
        Log("The available quantity is lower than the minimum transaction volume:", buylimit,"  Quantity:", doAmount);
    }
}


//Based on Last TimeIDto check the order status and then sell
function doSellTrading2(){
    //Get order status
    Log("Previous OrderID:", saveBuyId);
    if(saveBuyId!=0){
        var order = exchange.GetOrder(saveBuyId);
        if(order){
            if(order.Status==ORDER_STATE_CLOSED){
                leftStocks=leftStocks-orderList[i].Amount
                var sellPrice = order.Price+liruncha;
                var sellAmount =  order.Amount;
                var id=exchange.Sell(sellPrice, sellAmount);
                Log("Sale Successfulid:", id,"  Price:", sellPrice,"  Quantity:", sellAmount);
            }
        }
    }

}









//Periodically scan for fulfilled buy orders and sell the corresponding quantity
function doSellTrading(){
    //If the balance is greater than zero  
    balan = getBalancex()
    leftStocks=balan.Stocks;
    if(balan.Stocks>0){
        var orderList = exchange.GetTrades()
        //Before Theoretical Testing20Just Need This Data
        if(orderList){
            for(i=0;i<=100;i++){
                //Log("View order information:",orderList);
                if(orderList[i].Type==0 && leftStocks>orderList[i].Amount){
                    leftStocks=leftStocks-orderList[i].Amount
                    var sellPrice = orderList[i].Price+liruncha;
                    var sellAmount =  orderList[i].Amount;
                    var id=exchange.Sell(sellPrice, sellAmount);
                    Log("Sale Successfulid:", id,"  Price:", sellPrice,"  Quantity:", sellAmount);
                }
            }
        }
    }
}


function onTick(){
    //Write the strategy logic here, which will be called continuouslyexchange
    doSellTrading2()
    doTradingBuy()
}
/*
function main(){
    var id = exchange.Sell(99999, 1);
    var order = exchange.GetOrder(id);//ParametersidThis is the order number; you need to enter the number of the order you want to query
    Log("Id", order.Id, "Price:", order.Price, "Amount:", order.Amount, "DealAmount:",
        order.DealAmount, "Status:", order.Status, "Type:", order.Type);
}
*/
function main(){
    exchange.SetPrecision(buyjingdu, buymount);
    while(true){
        onTick();
        Sleep(reffertime);
    }
}

Published by 6821281 on the FMZ strategy square · last modified 2018-04-26. 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/Unilateral-Grid.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