Strategies
单商品小卖部策略V20_年化130
区班量化 · javascript · Volatility and Channels · 2019-10-20 15:52:19
Volatility and Channels
Risk and Position SizingPortfolio and Arbitrage
High Volatility · InferredLow Volatility · Inferred
No English write-up in the source (no author description).
Parameters
| Parameters | Default | Description |
|---|---|---|
| Interval | 10 | 轮询周期(秒) |
| mnum | 20 | 30分钟线周期 |
| initRatio | 0.5 | 初始仓位比例 |
Source code (97 lines)
/*backtest
start: 2019-01-01 00:00:00
end: 2019-10-10 00:00:00
period: 1d
exchanges: [{"eid":"OKEX","currency":"ETH_USDT","stocks":0}]
args: [["OpMode",1,10989],["MaxAmount",1,10989],["TradeFee",0.001,10989]]
*/
//注册币乎后https://m.bihu.com/signup?i=1ewtKO&s=4&c=4
//搜索 物联网区块链 可以联系到作者区班主
function main() {
var isInit = 1; //表示初始态
var allAmount;
var cashRatio;
var initAccount = _C(exchange.GetAccount);
var lastPrice;
var wantRatio;
var wantOper=0;//期待的操作,0不操作,1买入,-1卖出
Log(initAccount);
var mhigh;
var mlow;
while (true) {
var mrecords = exchange.GetRecords(PERIOD_M30);
//一定周期内的高低点
mhigh=TA.Highest(mrecords, mnum, 'High');
mlow=TA.Lowest(mrecords, mnum, 'Low');
var midLine = (mhigh+mlow)/2;
var ticker = _C(exchange.GetTicker);
var account = _C(exchange.GetAccount);
var nowPrice=ticker.Sell;
var obj;
if (isInit == 1) { //初始化状态为默认仓;
//账户现金乘以比例,除以当前价格,保留小数前3位
obj = $.Buy(_N(account.Balance * initRatio / ticker.Sell, 3));
if (obj) { //如果购买成功,就标志开仓
opAmount = obj.amount;
lastPrice = obj.price;
isInit=0; //初始化成功
account = _C(exchange.GetAccount);
Log("初始开仓:购买量", opAmount);
Log("目前持币数", account.Stocks);
}
}else{ //日常操作检测
if(nowPrice>midLine*1.03||nowPrice>lastPrice*1.07){
wantOper=-1;
}else if(nowPrice<midLine*0.97||nowPrice<lastPrice*0.93){
wantOper=1;
}else{
wantOper=0;
}
if (wantOper==-1) { //离市平仓
lastPrice=nowPrice; //不管买没买成功都修改了一下价格
allAmount=account.Balance+account.Stocks*ticker.Sell; //计算出总金额
cashRatio=parseFloat((account.Balance/allAmount).toFixed(3));
if(cashRatio>0.9){ //现金比例大于0.9,不做任何操作
wantRatio=0;
}else if(cashRatio>0.8){ //现金比例超过0.8,可以抛一成仓
wantRatio=0.1;
}else{ //其他情况都可以抛掉2成仓
wantRatio=0.2;
}
obj = $.Sell(_N(allAmount*wantRatio/ticker.Sell, 3));
if(obj){
opAmount = obj.amount;
Log("平仓:卖出量",opAmount);
nowAccount = _C(exchange.GetAccount);
Log("目前现金",nowAccount.Balance,"盈利",allAmount - initAccount.Balance);
}
}else if (wantOper==1) { //开仓买入
lastPrice=nowPrice; //不管买没买成功都修改了一下价格
allAmount=account.Balance+account.Stocks*ticker.Sell; //计算出总金额
cashRatio=parseFloat((account.Balance/allAmount).toFixed(3));
//Log("准备买入",cashRatio);
if(cashRatio<0.1){ //现金比例小于0.1,已没钱买了
wantRatio=0;
}else if(cashRatio<0.2){ //现金比例超过0.2,可以买一成仓
wantRatio=0.1;
}else{ //其他情况都可以买2成仓
wantRatio=0.2;
}
obj = $.Buy(_N(allAmount*wantRatio/ticker.Sell, 3));
if(obj){
opAmount = obj.amount;
Log("买入:买入量",opAmount);
nowAccount = _C(exchange.GetAccount);
Log("目前现金",nowAccount.Balance,"盈利",allAmount - initAccount.Balance);
}
}
}
Sleep(Interval*1000);
}
}