0bps

Strategy vault / Other

Buy Hold Buy and Hold

Buy and hold strategy

JavaScriptOther
Author
ipqhjjybj
Complexity
Starter · 76 lines
Parameters
0

Buy and hold strategy

Source JavaScript

/*
Strategy Source: rqalpha
Strategy name: buy-hold
Strategy Author: ipqhjjybj
Strategy Description:
Brainless exponential type

*/

LoopInterval  		=   60 	   // Polling interval (seconds)
SlidePrice          =	0.3    // Sliding price (yuan)
minMoney			=   100;		// If the funds are less than this value, no purchase will be made

function adjustFloat(v) {
    return Math.floor(v*1000)/1000;
}

function CancelPendingOrders() {
    while (true) {
        var orders = null;
        while (!(orders = exchange.GetOrders())) {
            Sleep(Interval);
        }

        if (orders.length == 0) {
            return;
        }

        for (var j = 0; j < orders.length; j++) {
            exchange.CancelOrder(orders[j].Id, orders[j]);
            if (j < (orders.length-1)) {
                Sleep(Interval);
            }
        }
    }
}

function GetAccount() {
    var account;
    while (!(account = exchange.GetAccount())) {
        Sleep(Interval);
    }
    return account;
}

function GetTicker() {
    var ticker;
    while (!(ticker = exchange.GetTicker())) {
        Sleep(Interval);
    }
    return ticker;
}

function onTick(exchange) {
	var ticker = GetTicker();
    // Buy or Sell, Cancel pending orders first
    CancelPendingOrders();
    var account = GetAccount();
    var price = ticker.Last + SlidePrice;
    var amount = adjustFloat(account.Balance / price);
    if (account.Balance > minMoney && amount >= exchange.GetMinStock()) {
    	if (exchange.Buy(price, amount, "go long")) {
    		LastBuyPrice = LastHighPrice = price;
    	}
    } 
}

function main() {
    InitAccount = GetAccount();
    Log(exchange.GetName(), exchange.GetCurrency(), InitAccount);
    LoopInterval = Math.max(LoopInterval, 1);  
    while (true) {
        onTick(exchange);
        Sleep(LoopInterval*1000);
    }
}

Published by ipqhjjybj on the FMZ strategy square · last modified 2017-06-02. 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 production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/Buy-Hold-Buy-and-Hold.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