-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Trade): implement Stop Loss (#89)
- Loading branch information
Showing
32 changed files
with
483 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { z } from "zod"; | ||
import { | ||
cancelSmartTrade, | ||
IBotConfiguration, | ||
type SmartTradeService, | ||
type TBotContext, | ||
useSmartTrade, | ||
} from "@opentrader/bot-processor"; | ||
import { logger } from "@opentrader/logger"; | ||
|
||
export function* testStopLoss(ctx: TBotContext<TestStopLossSchema>) { | ||
logger.info("[TestStopLoss] Executing strategy template"); | ||
const { entry, tp, sl } = ctx.config.settings; | ||
|
||
if (ctx.onStop) { | ||
logger.info("[TestStopLoss] Stopping strategy"); | ||
yield cancelSmartTrade(); | ||
return; | ||
} | ||
|
||
if (ctx.onStart) { | ||
const smartTrade: SmartTradeService = yield useSmartTrade({ | ||
buy: { | ||
type: entry.type, | ||
}, | ||
sell: { | ||
type: tp.type, | ||
price: tp.price, | ||
}, | ||
sl: { | ||
type: sl.type, | ||
stopPrice: sl.stopPrice, | ||
price: sl.price, | ||
}, | ||
quantity: 0.0001, | ||
}); | ||
logger.info(smartTrade, "[TestStopLoss] Trade created"); | ||
} | ||
} | ||
|
||
testStopLoss.schema = z.object({ | ||
entry: z.object({ | ||
type: z.enum(["Market", "Limit"]).default("Market").describe("Type of the entry order."), | ||
price: z.number().optional().describe("Limit price of the entry order."), | ||
}), | ||
tp: z.object({ | ||
type: z.enum(["Limit", "Market"]).default("Limit").describe("Type of the take profit order."), | ||
price: z.number().default(105000).describe("Limit price of the take profit order."), | ||
}), | ||
sl: z.object({ | ||
type: z.enum(["Limit", "Market"]).default("Limit").describe("Type of the stop loss order."), | ||
stopPrice: z.number().default(95000).describe("Stop price of the stop loss order."), | ||
price: z.number().default(90000).describe("Limit price of the stop loss order."), | ||
}), | ||
}); | ||
testStopLoss.hidden = true; | ||
testStopLoss.watchers = { | ||
watchCandles: ({ symbol }: IBotConfiguration) => symbol, | ||
}; | ||
testStopLoss.runPolicy = { | ||
onCandleClosed: true, | ||
}; | ||
|
||
export type TestStopLossSchema = IBotConfiguration<z.infer<typeof testStopLoss.schema>>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.