Skip to content

Latest commit

 

History

History
178 lines (154 loc) · 4.34 KB

ahr999定投策略.md

File metadata and controls

178 lines (154 loc) · 4.34 KB

Name

ahr999定投策略

Author

ChaoZhang

Strategy Arguments

Argument Default Description
InitMoney 1000000 初始金额
Interval 24 定投间隔
Money 1000 定投金额
Top 1.2 (?定投设置)定投线
Bottom 0.45 抄底线
Multiple 10 抄底部数
TakeProfit true (?逃顶设置)是否逃顶
TakeProfitLine 0.45 逃顶线
TakeProfitRate 0.2 逃顶比例

Source (javascript)

/*backtest
start: 2018-04-14 00:00:00
end: 2021-07-18 00:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Binance","currency":"BTC_USDT","balance":1000000,"stocks":0}]
*/

/*
 * @Project:
 * @Version:
 * @Author: RedSword <coo@fmz.com>
 * @Description:
 * @Date: 2021-07-19 11:02:43
 * @LastEditors: RedSword
 * @LastEditTime: 2021-07-19 15:55:16
 * @Copyright:: Copyright © 2020 FMZ Quant
 * 感谢ahr999大神,后续完善可以加入一些理财,币和钱都闲着,收益会低一点,也可以放到bitfinex放借贷,年化也有十几的收益,还可以放大资金,买了币以后,就去交割合约做多,暂时想到这么多,希望大家多多交流
 * 参考:
 * https://github.com/who3m1/ahr999-mixin
 * https://btctom.com/ahr999
 * https://btctom.com/ahr999x
 */

function harmonicMean(x) {
	if (x.length === 0) {
		return undefined;
	}
	var reciprocalSum = 0;
	for (var i = 0; i < x.length; i++) {
		if (x[i] <= 0) {
			return undefined;
		}
		reciprocalSum += 1 / x[i];
	}
	return x.length / reciprocalSum;
}
function GetNowPrice() {
	var ticker = exchange.GetTicker();
	return ticker.Last;
}
function GetPrices() {
	var records = exchange.GetRecords(PERIOD_D1);
	var prices = [];
	for (let i = 0; i < records.length; i++) {
		const record = records[i];
		// Log(record);
		prices.push(record.Close);
	}
	return prices;
}

function CalcAHR999() {
	var now = parseInt(Unix());
	var prices = GetPrices();
	var avgPrice = harmonicMean(prices);
	var nowPrice = GetNowPrice();
	var birthday = (now - 1230940800) / (24 * 60 * 60);
    var logPrice = Math.pow(10, 5.84 * Math.log10(parseInt(birthday)) - 17.01);
	//2020-4-16 13:16 以后使用新的指标
	//https://weibo.com/5034063086/IDzPWyN8Z?from=page_1005055034063086_profile&wvr=6&mod=weibotime
	if (now > 1587014160) {
		logPrice = Math.pow(10, 5.8 * Math.log10(parseInt(birthday)) - 16.88);
	}
	var ahr999 = Math.round((nowPrice / avgPrice) * (nowPrice / logPrice) * 1000) / 1000;
	var ahr999x = Math.round((avgPrice / nowPrice) * (logPrice / nowPrice) * 3 * 1000) / 1000;
	return {
		ahr999: ahr999,
		ahr999x: ahr999x,
	};
}

function init() {
	exchange.SetMaxBarLen(200);
	if (exchange.GetCurrency().indexOf("BTC_USD") == -1) {
		throw "只支持BTC交易对";
	}
}
function UpdateStatus(account, nowPrice) {
	var table = {
		type: "table",
		title: "持仓信息",
		cols: ["定投币种", "初始净值", "当前净值", "定投次数", "持仓数量", "持仓均价", "当前价格", "累计定投", "可用资金", "盈利率%"],
		rows: [],
	};

	var netValue = account.Balance + account.Stocks * nowPrice;
	table.rows.push([
		exchange.GetCurrency(),
		InitMoney,
		_N(netValue, 2),
		Global.number,
		_N(account.Stocks, 6),
		_N((InitMoney - account.Balance) / account.Stocks, 2),
		nowPrice,
		_N(Global.number * Money, 2),
		_N(account.Balance, 2),
		_N((netValue / InitMoney) * 100),
	]);
	LogStatus("`" + JSON.stringify(table) + "`");
}
var Global = {
	upTime: 0, //循环间隔
	number: 0, //定投次数
	multipleNumber: 0, //抄底次数
};

function main() {
	while (true) {
		var now = parseInt(Unix());
		if (now > Global.upTime) {
			var price = GetNowPrice();
			var account = exchange.GetAccount();
			var ahr999 = CalcAHR999();
			Global.upTime = now + 3600 * Interval;
			if (ahr999.ahr999 >= Bottom && ahr999.ahr999 < Top) {
				if (Money > account.Balance) continue;
				Log("开始定投");
				exchange.Buy(-1, Money);
				Global.number++;
			} else if (ahr999.ahr999 < Bottom) {
				if (Money * Multiple > account.Balance) continue;
				Log("开始抄底");
				exchange.Buy(-1, Money * Multiple);
				Global.number += Multiple;
			}
			if (TakeProfit & (ahr999.ahr999x < TakeProfitLine)) {
				Log("开始顶逃");
				var sell = Global.number * TakeProfitRate * Money;
				var coinNumber = sell / price;
				exchange.Sell(-1, coinNumber);
			}
			UpdateStatus(account, price);
		}
		Sleep(1000);
	}
}

Detail

https://www.fmz.com/strategy/299799

Last Modified

2021-07-26 14:54:26