Skip to content

Commit

Permalink
wizard: add profit-based stop-loss
Browse files Browse the repository at this point in the history
  • Loading branch information
pmioduszewski committed Jun 20, 2023
1 parent 360aba8 commit 6909ab0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
9 changes: 8 additions & 1 deletion apps/arb-solana-bot/src/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ export const start = async () => {
autoReset?: {
enabled: boolean;
timeWindowMs: number;
};
},
expectedProfitBasedStopLoss?: {
enabled: boolean;
percent: number;
}
};
} = JSON.parse(fs.readFileSync("./config.json", "utf8"));

Expand Down Expand Up @@ -96,6 +100,9 @@ export const start = async () => {
enableAutoSlippage: config.strategy.slippage.enableAutoSlippage,
enableCompounding: config.strategy.enableCompounding ?? false,
autoReset: config.strategy.autoReset,
expectedProfitBasedStopLoss: config.strategy.expectedProfitBasedStopLoss,
onStopLossAction: "sell&reset"

});

const bot = extendBot(
Expand Down
15 changes: 8 additions & 7 deletions packages/core/src/strategies/ping-pong.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ export type PingPongStrategyConfig = {
executeAboveExpectedProfitPercent: number;
priorityFeeMicroLamports?: number;
lock?: boolean;
enableExpectedProfitBasedStopLoss?: boolean;
profitBasedStopLossPercent?: number;
expectedProfitBasedStopLoss?: {
enabled: boolean;
percent: number;
}
onStopLossAction?: "sell&reset" | "shutdown" | "sell&shutdown";
shouldReset?: boolean;
autoReset?: {
Expand All @@ -53,7 +55,6 @@ export const PingPongStrategy: Strategy<PingPongStrategyConfig> = {
lock: false,
enableAutoSlippage: false,
enableCompounding: false,
enableExpectedProfitBasedStopLoss: false,
},
uiHook: {},
// dependencies: {
Expand Down Expand Up @@ -373,16 +374,16 @@ export const PingPongStrategy: Strategy<PingPongStrategyConfig> = {
* then execute stop loss action
*/
if (
this.config.enableExpectedProfitBasedStopLoss &&
this.config.profitBasedStopLossPercent &&
this.config.expectedProfitBasedStopLoss?.enabled &&
this.config.expectedProfitBasedStopLoss.percent &&
expectedProfitPercent.toNumber() <
this.config.profitBasedStopLossPercent * -1 &&
Math.abs(this.config.expectedProfitBasedStopLoss.percent) * -1 &&
isSellSide
) {
bot.setStatus("strategy:stopLossExceeded");

if (this.config.onStopLossAction === "shutdown") {
const msg = `PingPongStrategy:run: profitBasedStopLossPercent ${this.config.profitBasedStopLossPercent} reached, shutting down bot`;
const msg = `PingPongStrategy:run: profitBasedStopLossPercent ${this.config.expectedProfitBasedStopLoss?.percent} reached, shutting down bot`;
bot.logger.info({ runtimeId }, msg);
console.log("\n\n" + msg + "\n\n");
bot.setStatus("!shutdown");
Expand Down
39 changes: 38 additions & 1 deletion packages/wizard/src/run-wizard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@ export const runWizard = async () => {
timeWindowMs: 60_000,
};

let expectedProfitBasedStopLoss = {
enabled: false,
percent: 0,
}

if (experienceLevel !== "beginner") {
features = await multiselect({
message: "What features do you want to setup? [spacebar] to select",
Expand All @@ -228,7 +233,8 @@ export const runWizard = async () => {
{ value: "executionRateLimiter", label: "Execution rate limiter" },
{ value: "iterationsRateLimiter", label: "Iterations rate" },
{ value: "aggregatorErrorsRateLimiter", label: "Aggregator errors rate limiter" },
{ value: "autoReset", label: "Auto reset" },
{ value: "autoReset", label: "Auto reset", hint: "experimental" },
{ value: "expectedProfitBasedStopLoss", label: "Stop Loss", hint: "experimental" },
],
required: false,
});
Expand Down Expand Up @@ -451,6 +457,32 @@ export const runWizard = async () => {
};
}
}

if (features.includes("expectedProfitBasedStopLoss")) {
const expectedProfitBasedStopLossPercent = await text({
message:
"What is the expected profit based stop loss percent?\n· If expected profit reaches this percent then swap back to the initial token & reset.\n· The provided number will always be negative (e.g. 0.5 = -0.5)",
initialValue: "0",
validate: (value) => {
if (value.length === 0) return "Please enter a expected profit based stop loss percent";
const expectedProfitBasedStopLossPercent = Math.abs(parseFloat(value));
if (isNaN(expectedProfitBasedStopLossPercent)) return "Please enter a valid number";
if (expectedProfitBasedStopLossPercent === 0) return "Please enter a non-zero number";
},
});

if (isCancel(expectedProfitBasedStopLossPercent)) {
cancel("Operation cancelled");
return process.exit(0);
}

if (typeof expectedProfitBasedStopLossPercent === "string") {
expectedProfitBasedStopLoss = {
enabled: true,
percent: Math.abs(parseFloat(expectedProfitBasedStopLossPercent)),
};
}
}
}

// Arb Protocol BuyBack
Expand Down Expand Up @@ -517,6 +549,10 @@ export const runWizard = async () => {
enabled: boolean;
timeWindowMs: number;
};
expectedProfitBasedStopLoss?: {
enabled: boolean;
percent: number;
}
};
};

Expand All @@ -537,6 +573,7 @@ export const runWizard = async () => {
? priorityFeeMicroLamports
: undefined,
autoReset,
expectedProfitBasedStopLoss
},
maxConcurrent: 1,
tui: {
Expand Down

0 comments on commit 6909ab0

Please sign in to comment.