Commit 6757a719 by zihan

添加防守策略

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