Loading ...

网格策略

Author: alinwo, Date: 2019-07-11 21:52:57
Tags:

什么是网格交易策略? 这是一种仓位策略,用于动态调仓。

所谓网格交易法(grid trading method),也称鱼网交易网,指以某点为基点,每上涨或下跌一定点数挂一定数量空单或多单,设定盈利目标,但不设止损,当价格朝期望方向进展时获利平仓,并在原点位挂同样的买单或卖单。

基本概念 1、底仓价:价格的标准线,建仓和调仓的重要依据。 2、低吸高抛:仓位控制贯彻低吸高抛,绝不追涨杀跌。

下面举个例子: 操作:在底仓价的附近,我们根据网格的大小,比如每跌3%按仓位买入(第一档:买40%,第二档:买30%,第三档:买20%,第四档:买10%)。

要注意的是,这里买卖不是绝对的定量,而是调仓到对应仓位。如果第一次跌破3%,而后上涨到5%时,是不操作的,因为下跌时只建了40%的仓,而上涨5%的仓位是60%,不够抛出。

数字货币中应用网格策略 借助发明者平台,我们可以通过JS代码完整实现网格交易策略,策略实现选中币种价格上涨时分批次建仓以及币价下跌时分批次减仓。

策略参数详情 https://www.pcclean.io/数字货币量化策略之网格策略/


var KaiCangPercent=0.5; //开仓百分比
var PriceChangeStep=0.005; //网格交易中的价格改变量(百分比)
var StocksChangeStep=0.2; //网格交易中的股票改变量(百分比)

var minBalance=80;
var minStocks=0.001;
var IsJianCang=false; //是否建仓
var LastOperationPrice=null; //网格交易中最近处理的股票价格
var PriceChangeStep_Real=null; //网格交易中的价格改变量(实际值)
var StocksChangeStep_Real=null;//网格交易中的股票改变量(实际值)

function getAccount(){
	var curAccount = exchange.GetAccount();
	while(!curAccount){
		Log("GetAccount() Retrying...");
		Sleep(2000);
		nowAccount = exchange.GetAccount();
	}
	return curAccount;
}

function getTicker(){
	var curTicker=exchange.GetTicker();
	while(!curTicker){
		Log("GetTicker() Retrying...");
		Sleep(2000);
		curTicker=exchange.GetTicker();
	}
	return curTicker;
}

//type: PERIOD_M5 etc...
function getRecords(type){
	var records = exchange.GetRecords(type);
	while(!records || records.length < 100)
	{
		Log("GetRecords() retrying...");
		Sleep(2000);
		records = exchange.GetRecords(type);		
	}
	return records;
}

function printProfit(){
	try {
		var curAccount = getAccount();
		var currrentPrice = getTicker().Last; //当前价格
		LogProfit(curAccount.Balance + curAccount.FrozenBalance + (curAccount.Stocks + curAccount.FrozenStocks) * currrentPrice);
	} catch (e) {
		Log("LogProfit error:" + e);
	}
}

function EMAChecker(){
	//获得records
	var records = getRecords(PERIOD_H1);	
	var len = records.length;
	//获得ema
	var ret1=TA.EMA(records,5);
	var ret2=TA.EMA(records,15);
	var emaFast=ret1[len - 1];
	var emaSlow=ret2[len - 1];
	if (emaFast-emaSlow>=5){
		return 1;//牛市
	}
	else if(emaFast-emaSlow<=-5){
		return -1;//熊市
	}
	return 0;
}

function MACDChecker(){
	//获得records
	var records = getRecords(PERIOD_H1);	
	var len = records.length;
	
	//获得当前MACD指标
	var ret = TA.MACD(records, 12, 26, 9);
	var dif = ret[0];
	var dea = ret[1];
	var macd = ret[2];

	//获得KDJ
	var ret2 = TA.KDJ(records, 9, 3, 3);
	var vark = ret2[0];
	var vard = ret2[1];
	var varj = ret2[2];
	
	if (macd[len - 1]>=0.2){
		return 1;//牛市
	}
	else if (macd[len - 1]<=-0.2){
		return -1;//熊市
	}
	return 0;
}

//amount: CNY amount
function BuyAndPrintProfit(amount){
	var nowAccount = getAccount();
	if (nowAccount.Balance > minBalance) {
		exchange.Buy(amount);		
		printProfit();
	}
}

function SellAndPrintProfit(stocksAmount){
	var nowAccount = getAccount();
	if (nowAccount.Stocks > minStocks) {
		exchange.Sell(stocksAmount);		
		printProfit();
	}
}

function BuyPercent(percent){
	var nowAccount = getAccount();
	var nowPrice = getTicker().Last;
	var totalAssets=nowAccount.Balance + nowAccount.FrozenBalance + (nowAccount.Stocks + nowAccount.FrozenStocks) * nowPrice;
	var canOperateAssets=parseFloat((totalAssets*percent).toFixed(2));
	var realBuyAmount=(canOperateAssets<nowAccount.Balance?canOperateAssets:nowAccount.Balance);
	BuyAndPrintProfit(realBuyAmount);
	return (realBuyAmount/nowPrice);
}

function ObjToString(object){
	var output = '';
	for (var property in object) {
	  output += property + ': ' + object[property]+'; ';
	}
	return output;
}

function startTable(){
	//开仓
	var stocksAmount=BuyPercent(KaiCangPercent);
	IsJianCang=true;
	var nowPrice = getTicker().Last;
	var nowAccount = getAccount();
	PriceChangeStep_Real=nowPrice*PriceChangeStep;		
	StocksChangeStep_Real=stocksAmount*StocksChangeStep;
	LastOperationPrice=nowPrice;
	
	var logItem={
		"开仓百分比":KaiCangPercent*100+"%",
		"价格步长":PriceChangeStep_Real,
		"币量步长":StocksChangeStep_Real,
		"开仓价":nowPrice
		};
	Log("布网完成!"+ObjToString(logItem));
}

function NiuShi_Do(){
	if (!IsJianCang){
		startTable();
	}else{
		//按网格买卖
		var nowPrice = getTicker().Last;
		if (nowPrice-LastOperationPrice>0){//涨
			var n=Math.floor((nowPrice-LastOperationPrice)/PriceChangeStep_Real);
			if (n>0){
				LastOperationPrice=LastOperationPrice+n*PriceChangeStep_Real;
				SellAndPrintProfit(n*StocksChangeStep_Real);
				
				//如果没有库存了
				var nowAccount = getAccount();
				if ((nowAccount.Stocks + nowAccount.FrozenStocks) <= minStocks){
					Log("涨幅过大?已经没有库存了。");
					startTable();
				}		
			}			
		}else if (nowPrice-LastOperationPrice<0){//跌
			var n=Math.floor((LastOperationPrice-nowPrice)/PriceChangeStep_Real);
			if (n>0){
				LastOperationPrice=LastOperationPrice-n*PriceChangeStep_Real;
				BuyAndPrintProfit(n*StocksChangeStep_Real*nowPrice);
			}
		}
	}
}

function XiongShi_Do(){
	//清仓
	var nowAccount = getAccount();
	SellAndPrintProfit(nowAccount.Stocks);	
	IsJianCang=false;
}

function main() {
    Log(exchange.GetAccount());

    while (true) {
		
		//确定是否在MACD牛市范围
		var result1=MACDChecker();
		var result2=EMAChecker();
		if (result1===1 && result2===1){
			NiuShi_Do();
		}else if (result1===-1 || result2===-1){
			XiongShi_Do();
		}       

        //等待下次查询交易所
        Sleep(15*1000);
    }
}

More