Loading ...

三角套利策略体验版

Author: shoujing, Date: 2019-05-25 18:11:32
Tags:


/*backtest
start: 2019-04-24 00:00:00
end: 2019-05-24 00:00:00
period: 1h
exchanges: [{"eid":"OKEX","currency":"BTC_USDT","balance":10000,"stocks":3},{"eid":"OKEX","currency":"LTC_USDT","balance":10000,"stocks":3},{"eid":"OKEX","currency":"LTC_BTC","balance":10000,"stocks":3}]
*/

var initAccount={//初始账户
	BTC:0.009,
	LTC:0.7,
	USDT:55,
}

var doAccount={//操作账户初始化
	BTC:0.009,
	LTC:0.7,
	USDT:55,
}

var tax = 0.0015;//交易费率,0.15%

function getBaseData(){
	var P1,P2,P3;//基础价格数据,分别为btc_usdt,ltc_usdt,ltc_btc交易对
	P1 = exchanges[0].GetTicker();
	P2 = exchanges[1].GetTicker();
	P3 = exchanges[2].GetTicker();
	return [P1,P2,P3];
}

function main() {
	Log(initAccount);
	var xunhuan  = true;//程序是否继续执行,用于判断账户余额是否满足操作,这里不做考虑
	while(xunhuan){
		var baseData =  getBaseData();//获取基础数据
		if((baseData[1].Buy-baseData[0].Sell*baseData[2].Sell)*Q3>Q3*tax*(2*baseData[1].Buy+baseData[0].Sell*baseData[2].Sell)){//考虑了手续费的套利条件(P2-P1*P3)*Q3>Q3*tax*(2*P2+P1*P3),注意为使订单能全部成交,均使用对手价进行判断,实际操作时还需要考虑交易深度问题
			if(xunhuan){
				doAccount.LTC = doAccount.LTC + Q3*(1-tax);
				doAccount.BTC = doAccount.BTC - baseData[2].Sell*Q3;//买入LTC、卖出BTC
				doAccount.LTC = doAccount.LTC - Q3;
				doAccount.USDT = doAccount.USDT + baseData[1].Buy*Q3*(1-tax);//卖出LTC,获得USDT
				doAccount.BTC = doAccount.BTC + baseData[2].Sell*Q3*(1-tax);
				doAccount.USDT = doAccount.USDT - baseData[0].Sell*baseData[2].Sell*Q3;//买入BTC,获得USDT
				Log('Usdt收益:',doAccount.USDT-initAccount.USDT,'usdt','正循环套利','#FF0000');
				Log('真实收益:',doAccount.USDT-initAccount.USDT-(initAccount.LTC-doAccount.LTC)*baseData[1].Buy-(initAccount.BTC-doAccount.BTC)*baseData[2].Sell,'#0000FF')//扣除手续费后的真实收益
			    Log(doAccount.USDT-initAccount.USDT-(initAccount.LTC-doAccount.LTC)*baseData[1].Buy-(initAccount.BTC-doAccount.BTC)*baseData[2].Sell,'#0000FF')
            }else{
				xunhuan = false;
			}
		}else if(baseData[0].Buy*baseData[2].Buy-baseData[1].Sell>(2*baseData[0].Buy*baseData[2].Buy+baseData[1].Sell)*tax){//考虑手续费的逆循环套利
			if(xunhuan){
				doAccount.LTC = doAccount.LTC - Q3;
				doAccount.BTC = doAccount.BTC + baseData[2].Buy*Q3*(1-tax);
				doAccount.LTC = doAccount.LTC + Q3*(1-tax);
				doAccount.USDT = doAccount.USDT - baseData[1].Sell*Q3;
				doAccount.BTC = doAccount.BTC - baseData[2].Buy*Q3;
				doAccount.USDT = doAccount.USDT + baseData[0].Buy*baseData[2].Buy*Q3*(1-tax);
				Log('Usdt收益:',doAccount.USDT-initAccount.USDT,'usdt,逆循环套利','#FF0000');
				Log('真实收益:',doAccount.USDT-initAccount.USDT-(initAccount.LTC-doAccount.LTC)*baseData[1].Sell-(initAccount.BTC-doAccount.BTC)*baseData[2].Buy,'#0000FF')
			    Log(doAccount.USDT-initAccount.USDT-(initAccount.LTC-doAccount.LTC)*baseData[1].Buy-(initAccount.BTC-doAccount.BTC)*baseData[2].Sell,'#0000FF')
            }else{
				xunhuan = false;
			}
		}
		Sleep(1000);//程序休眠1秒
	}
}


More