一、网格策略
网格策略,总结为四个字就是:高抛低吸。
例如:首次买入10万元,价格每下跌10,买入1万元,价格每上涨20,卖出5000元。
其最大的好处就是:行情越是震荡,获利空间越大。(反脆弱性)
二、生成模拟交易数据
随机生成交易数据的代码如下:
function generateSimulateData(base,num,lowest,maxDelta){
var ret = [base];
var price = base;
var deltas = [];
var total = base;
var min = base;
var max = base
for(var i=1; i<num; ++i){
var sign = Math.floor(Math.random()*10%2)>0.5?1:-1;
var delta = Math.floor(Math.random()*maxDelta)*sign;
price += delta;
if( price <= lowest){
price = lowest;
}
deltas.push(delta);
ret.push(price);
if( price > max){
max = price;
}
if( price < min){
min = price;
}
total += price;
}
var open = base;
var close = ret[num-1];
var avg = total/num;
var situation = (close/open-1)*100;
//console.log(`open=${open}, close=${close}, avg=${avg}, max=${max}, min=${min} situation=${situation.toFixed(2)}%`);
return {s:situation.toFixed(2),prices:ret};
}
假设我们要生成100个具有如下特征的数据:开盘价为1000,价格之间的波动幅度为0-4之间,最多跌到200(会不会跌到200,不一定)可以这样调用该函数:
let simulateData = generateSimulateData(1000,100,200,5);
console.log("{s:<涨跌幅>,prices:<具体数据>}")
三、网格变种策略 v1.0
前提:
- 资金无穷大
- 交易以当前价格瞬间完成,不影响后续市场价格
策略做法:
- 每秒交易一次。
- 当前价格与上次成功交易价格相同时,不交易。
- 当前价格大于持仓价格时,买入1。
- 当前价格小于持仓价格时,买入2。
- 不进行卖出操作,
策略目标:策略的涨幅大于BTC每日涨幅,跌幅小于每日跌幅。
那么策略核心代码如下:
for (let i=1; i<simulatePrices.length; ++i ){
let curPrice = simulatePrices[i];
if( myLastPrice==curPrice ){
continue;
}
if( curPrice>=myAvgPrice ){
myTotal += curPrice;
myBuyTimes += 1;
}
else {
myTotal += curPrice*2;
myBuyTimes += 2;
}
myAvgPrice = myTotal/myBuyTimes;
}
模拟数据下收益情况
以generateSimulateData(1000,86400,200,5);
构造365次数据,最终的平均涨跌幅如下:
originAvg,模拟数据的一天平均涨跌幅。
myAvg,使用本策略后一天的平均收益率。
originalSituation:-17.50%, mySituation:56.31%
originalSituation:42.40%, mySituation:-13.41%
originalSituation:4.60%, mySituation:-14.07%
originalSituation:-64.30%, mySituation:88.32%
originalSituation:-3.20%, mySituation:11.48%
originalSituation:-0.40%, mySituation:-2.58%
originalSituation:11.30%, mySituation:-10.25%
originalSituation:56.80%, mySituation:-23.02%
originalSituation:-52.50%, mySituation:31.71%
originalSituation:-37.50%, mySituation:50.15%
originalSituation:26.20%, mySituation:-26.08%
originalSituation:-55.50%, mySituation:143.93%
originalSituation:56.70%, mySituation:-26.36%
....
originAvg:6.55% , myAvg:9.33%
从上述的数据来看,很明显,这个策略并没有达到我们要涨多跌少的目标。
因此,需要对策略做相应优化,那么...
下文再见!
代码
function generateSimulateData(base,num,lowest,maxDelta){
let ret = [base];
let price = base;
let deltas = [];
let total = base;
let min = base;
let max = base
for(let i=1; i<num; ++i){
let sign = Math.floor(Math.random()*10%2)>0.5?1:-1;
let delta = Math.floor(Math.random()*maxDelta)*sign;
price += delta;
if( price <= lowest){
price = lowest;
}
deltas.push(delta);
ret.push(price);
if( price > max){
max = price;
}
if( price < min){
min = price;
}
total += price;
}
let open = base;
let close = ret[num-1];
let avg = total/num;
let situation = (close/open-1)*100;
//console.log(`open=${open}, close=${close}, avg=${avg}, max=${max}, min=${min} situation=${situation.toFixed(2)}%`);
return {s:situation.toFixed(2),prices:ret};
}
function myStrategy_1(simulatePrices){
let myTotal = simulatePrices[0];
let myLastPrice = simulatePrices[0];
let myAvgPrice = myTotal;
let myBuyTimes = 1;
for (let i=1; i<simulatePrices.length; ++i ){
let curPrice = simulatePrices[i];
// console.log(`curPrice:${curPrice}, myCount = ${myBuyTimes}, myTotal = ${myTotal}, myAvgPrice = ${myAvgPrice}`);
if( myLastPrice==curPrice ){
continue;
}
if( curPrice>=myAvgPrice ){
myTotal += curPrice;
myBuyTimes += 1;
}
//
else {
myTotal += curPrice*2;
myBuyTimes += 2;
}
myAvgPrice = myTotal/myBuyTimes;
}
// console.log(`myCount = ${myBuyTimes}, myTotal = ${myTotal}, myAvgPrice = ${myAvgPrice}`);
let s = (simulatePrices[simulatePrices.length-1]/myAvgPrice-1)*100
//console.log(`myCount = ${myBuyTimes}, myTotal = ${myTotal}, myAvgPrice = ${myAvgPrice}, situation = ${s.toFixed(2)}%`);
return s.toFixed(2);
}
function main(){
let originTotal = 0;
let myTotal = 0;
for( let i=0; i<365; ++i){
let simulateData = generateSimulateData(1000,86400,200,5);
let simulatePrices = simulateData["prices"];
let originalSituation = Number(simulateData["s"]);
let mySituation = Number(myStrategy_1(simulatePrices));
console.log(`originalSituation:${originalSituation.toFixed(2)}%, mySituation:${mySituation.toFixed(2)}%`);
originTotal += originalSituation;
myTotal += mySituation;
}
originTotal /= 365;
myTotal /= 365;
console.log(`originAvg:${originTotal.toFixed(2)}% , myAvg:${myTotal.toFixed(2)}%`);
}
main();
网友评论