Skip to content

Commit

Permalink
Improve sell logic
Browse files Browse the repository at this point in the history
  • Loading branch information
djkazic committed Nov 24, 2023
1 parent f9face2 commit 1cecbec
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 additions & 3 deletions main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,14 @@ async function loadModules() {
let sellPl = 0;
let buyPl = 0;
let changedPos = false;
let buyOrders = 0;
let sellOrders = 0;
let buyAvgPl = 0;
let sellAvgPl = 0;
runningPositions.forEach((position) => {
if (position.side === "s") {
sellOrders++;
sellPl += position.pl;
totalSellExposure += position.quantity;
if (position.pl > 30) {
logger(
Expand All @@ -278,6 +284,8 @@ async function loadModules() {
changedPos = true;
}
} else if (position.side === "b") {
buyOrders++;
buyPl += position.pl;
totalBuyExposure += position.quantity;
if (position.pl > 30) {
logger(
Expand All @@ -303,6 +311,13 @@ async function loadModules() {
}
}
});
if (buyOrders > 0) {
buyAvgPl = buyPl / buyOrders;
}
if (sellOrders > 0) {
sellAvgPl = sellPl / sellOrders;
}
logger("info", `BuyAvgPl: ${buyAvgPl}, SellAvgPl: ${sellAvgPl}`);
// Reread positions
// if (changedPos) {
totalSellExposure = 0;
Expand All @@ -314,19 +329,39 @@ async function loadModules() {
runningPositions.forEach((position) => {
if (position.side === "s") {
totalSellExposure += position.quantity;
logger("info", `Short position with pl ${position.pl}`);
sellPl += position.pl;
if (sellAvgPl > 0 && position.pl > sellAvgPl) {
logger(
"finance-profit",
`CLOSING SHORT POSITION ${JSON.stringify(position)}`
);
restClient.futuresCloseTrade(position.id);
sendTelegramMessage(
`Closed profitable short on LNM: fee ${position.opening_fee}, price ${position.price}, pl ${position.pl}`
);
} else {
logger("info", `Short position with pl ${position.pl}`);
}
} else if (position.side === "b") {
totalBuyExposure += position.quantity;
if (buyAvgPl > 0 && position.pl > buyAvgPl) {
logger(
"finance-profit",
`CLOSING LONG POSITION ${JSON.stringify(position)}`
);
restClient.futuresCloseTrade(position.id);
sendTelegramMessage(
`Closed profitable long on LNM: fee ${position.opening_fee}, price ${position.price}, pl ${position.pl}`
);
}
logger("info", `Long position with pl ${position.pl}`);
buyPl += position.pl;
}
});
// }
logger(
"info",
`Profitable sells: $${profitableSells} (exp $${totalSellExposure} pl ${sellPl} sats), profitable buys: $${profitableBuys} (exp $${totalBuyExposure} pl ${buyPl} sats)`
);

if (buyPl >= 70) {
logger("info", "[DRY-RUN] triggered synthetic exit");
//sellBypass = true;
Expand Down

0 comments on commit 1cecbec

Please sign in to comment.