Commit 6757a719 by zihan

添加防守策略

parent 92451abe
......@@ -14,8 +14,9 @@ class BiboxStrategy3 extends Strategy3 {
constructor(collector) {
super(collector, machine);
this.orderService = new Order();
this.lastPrice = -1;
this.samePriceCount = 0;
this.maxAllowLoss = 20;
// this.lastPrice = -1;
// this.samePriceCount = 0;
}
_doTrade(baseCurrency1, midCurrency, baseCurrency2, buyPrice, sellPrice, returnPrice, amount, returnAmount, doSaveOrder) {
......@@ -34,30 +35,30 @@ class BiboxStrategy3 extends Strategy3 {
console.log("sell@" + sellSymbol + " amount:" + amount + " price:" + sellPrice);
const sellStartTime = Date.now();
orderService.order(
sellSymbol, sellPrice, amount, constants.OrderSideSell
, (error, order) => {
if (error) {
if(error.code === "NO_BALANCE" || (error.code === 'ERROR' && error.msg === 'SYMBOL NOT FOUND') || error.statusCode == 502 || error.code == 'ECONNRESET'){
if(retryTime>0 && !createdSellOrder){
console.log("提示余额不足,再次尝试");
retryTime -- ;
setTimeout(()=>{
sellOrder();
},(totalRetryTime - retryTime)*50);
}else if(!createdSellOrder && createdBuyOrder){
console.log("已没有重试次数,但是买单已创建成功,继续重试");
setTimeout(()=>{
sellOrder();
},500);
sellSymbol, sellPrice, amount, constants.OrderSideSell
, (error, order) => {
if (error) {
if (error.code === "NO_BALANCE" || (error.code === 'ERROR' && error.msg === 'SYMBOL NOT FOUND') || error.statusCode == 502 || error.code == 'ECONNRESET') {
if (retryTime > 0 && !createdSellOrder) {
console.log("提示余额不足,再次尝试");
retryTime--;
setTimeout(() => {
sellOrder();
}, (totalRetryTime - retryTime) * 50);
} else if (!createdSellOrder && createdBuyOrder) {
console.log("已没有重试次数,但是买单已创建成功,继续重试");
setTimeout(() => {
sellOrder();
}, 500);
}
}
console.error("sell error:" + JSON.stringify(error) + ";sell price:" + sellPrice + " @amount:" + amount);
return;
}
}
console.error("sell error:" + JSON.stringify(error) + ";sell price:" + sellPrice + " @amount:" + amount);
return;
}
createdSellOrder = true;
doSaveOrder(order, midCurrency, baseCurrency2, constants.OrderTypeSell);
console.log("sell start@" + sellStartTime + " end@" + Date.now() + " symbol:" + sellSymbol);
})
createdSellOrder = true;
doSaveOrder(order, midCurrency, baseCurrency2, constants.OrderTypeSell);
console.log("sell start@" + sellStartTime + " end@" + Date.now() + " symbol:" + sellSymbol);
})
}
function returnOrder() {
......@@ -87,22 +88,22 @@ class BiboxStrategy3 extends Strategy3 {
if (order.executedQty === order.origQty) {
console.log("买入全部成交");
createdBuyOrder = true;
amount = collector.processAmount(midCurrency, baseCurrency2, parseFloat(order.executedQty*0.999));
setTimeout(()=>{
amount = collector.processAmount(midCurrency, baseCurrency2, parseFloat(order.executedQty * 0.999));
setTimeout(() => {
sellOrder();
},120);
}, 120);
returnOrder();
} else if (parseFloat(order.executedQty) > 0) {
returnAmount = parseFloat(returnAmount) * parseFloat(order.executedQty) / parseFloat(amount);
returnAmount = collector.processAmount(baseCurrency2, baseCurrency1, returnAmount);
amount = collector.processAmount(midCurrency, baseCurrency2, parseFloat(order.executedQty*0.999));
amount = collector.processAmount(midCurrency, baseCurrency2, parseFloat(order.executedQty * 0.999));
console.log(`买入部分成交${order.executedQty} 回归量调整为${returnAmount}`);
order.remark = "部分成交@amount " + order.executedQty + ";";
createdBuyOrder = true;
if (parseFloat(amount) > 0)
setTimeout(()=>{
setTimeout(() => {
sellOrder();
},120);
}, 120);
if (parseFloat(returnAmount) > 0)
returnOrder();
else {
......@@ -123,9 +124,11 @@ class BiboxStrategy3 extends Strategy3 {
// }
// }
}
_isFeeDeducedByOther(){
_isFeeDeducedByOther() {
return false;
}
_getTrades(order, callback) {
callback([]);
}
......@@ -134,33 +137,45 @@ class BiboxStrategy3 extends Strategy3 {
return STRATEGY3_RETURN_MIN_AMOUNT_MAP[currency] || 1;
}
_getMinTradeInterval(){
_getMinTradeInterval() {
return 20000;
}
_getMinMargin(){
_getMinMargin() {
return 0.08;
}
_giveUpOrder(baseCurrency1, midCurrency, baseCurrency2,totalMarginRate,buyDepth,sellDepth){
if(sellDepth ===1){
const sellPrices = this.collector.getDepth(midCurrency, baseCurrency2, 2);
const possibleLoss = (sellPrices[0][0] - sellPrices[1][0])/sellPrices[0][0];
if(sellPrices[0][0] === this.lastPrice){
this.samePriceCount ++;
}else{
this.lastPrice = sellPrices[0][0];
this.samePriceCount = 0;
}
if(possibleLoss*100-totalMarginRate > 3){
console.log(`此单风险过高,放弃。买一:${sellPrices[0][0]},买二:${sellPrices[1][0]},损失率${(possibleLoss * 100).toFixed(4)},利差:${totalMarginRate.toFixed(4)}`);
_giveUpOrder(baseCurrency1, midCurrency, baseCurrency2, totalMarginRate, buyDepth, sellDepth) {
if (sellDepth === 1) {
const sellPrices = this.collector.getDepth(midCurrency, baseCurrency2, 2);
const possibleLoss = (sellPrices[0][0] - sellPrices[1][0]) / sellPrices[0][0];
// if(sellPrices[0][0] === this.lastPrice){
// this.samePriceCount ++;
// }else{
// this.lastPrice = sellPrices[0][0];
// this.samePriceCount = 0;
// }
const possibleLossMoney = (possibleLoss - totalMarginRate / 100) * sellPrices[1][0];
if (possibleLossMoney > this.maxAllowLoss) {
console.log(`此单风险过高,放弃。可能损失:${possibleLossMoney},${baseCurrency1}=>${midCurrency}=>${baseCurrency2}`);
return true;
}
// if(possibleLoss*100-totalMarginRate > 3){
// console.log(`此单风险过高,放弃。买一:${sellPrices[0][0]},买二:${sellPrices[1][0]},损失率${(possibleLoss * 100).toFixed(4)},利差:${totalMarginRate.toFixed(4)}`);
// return true;
// }
}
return super._giveUpOrder(baseCurrency1,midCurrency,baseCurrency2,totalMarginRate,buyDepth,sellDepth);
return super._giveUpOrder(baseCurrency1, midCurrency, baseCurrency2, totalMarginRate, buyDepth, sellDepth);
}
_needConsiderDepthCount(){
return [[2,1],[3,2,1],[2,1]];
_logDelay() {
return true;
}
_needConsiderDepthCount() {
return [[2, 1], [3, 2, 1], [2, 1]];
}
// _giveUpOrder(baseCurrency1, midCurrency, baseCurrency2,totalMarginRate,buyDepth,sellDepth){
// return false
// }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment