bludnic / opentrader

Powerful open-source crypto trading bot - BETA
Apache License 2.0
163 stars 18 forks source link

Support DCA & Ladder orders #18

Open bludnic opened 6 months ago

bludnic commented 6 months ago

Currently we have two types of SmartTrades:

type SmartTradeType = "SmartTrade" | "DCA"

Entry and TakeProfit may be Order or Ladder.

type EntryType  = "Order" | "Ladder"
type TakeProfitType = "Order" | "Ladder"

Therefore there are 4 possible varieties of SmartTrade:

Buy Sell
Order Order
Order Ladder
Ladder Order
Ladder Ladder

Need to create a package for processing every type and variety of SmartTrade. The API may be something like this:

SmartTrade

const entity = prisma.smartTrade.findUnique({ where: { id: 1 } })
const smartTrade = new SmartTradeProcessor(entity)

// must cancel all open orders and mark SmartTrade as closed
await smartTrade.cancel() 

// place the entry order (entryType == "Order")
// or the first order of ladder (entryType == "Ladder")
await smartTrade.entry() 

// OR using hooks
// e.g. using WebSockets and receiving events from the exchange
await smartTrade.handleOrderFilled(orderId) // the processor will decide by itself what to do with that event
await smartTrade.handleOrderCanceled(orderId)

// @internal
// place the next entry buy order if needed (entryType === "Ladder")
// place TP sell order if entry buy order was filled (entryType === "Order")
await smartTrade.publishNext()

DCA

const entity = prisma.smartTrade.findUnique({ where: { id: 2 } })
const dca = new DCATradeProcessor(entity)

await dca.cancel() // same as for SmartTrade
await dca.entry() // same as for SmartTrade

// if TP was filled, then mark DCA as completed
// if SO (Safety Order) was filled, then it must update the TP order limit price
// if SL was filled, the mark DCA as completed
await dca.handleOrderFilled(orderId)

// @internal
await dca.updateTakeProfit()
bludnic commented 6 months ago

Introduced a new package @opentrader/processing. The processor can be added in that package.

bludnic commented 5 months ago

Partially done in https://github.com/bludnic/opentrader/commit/fa16fb4de3a56845417513b941f9f2668002774c

Added:

bludnic commented 5 months ago

https://www.notion.so/OpenTrader-Trading-Engine-509a80f1cf4d4beeae862561c5e11018