diff --git a/src/broker.ts b/src/broker.ts index 95051b3..8cc2dfb 100644 --- a/src/broker.ts +++ b/src/broker.ts @@ -61,9 +61,9 @@ export class Broker { } public newOrder(options: OrderOptions) { - const { size, stopPrice, limitPrice, slPrice, tpPrice, parentTrade } = options; + const { price, size, stopPrice, limitPrice, slPrice, tpPrice, parentTrade } = options; const isLong = size > 0; - const adjustedPrice = this.adjustPrice({ size }); + const adjustedPrice = this.adjustPrice({ price, size }); if (isLong) { if (!((limitPrice || stopPrice || adjustedPrice) > (slPrice || Number.NEGATIVE_INFINITY) && (limitPrice || stopPrice || adjustedPrice) < (tpPrice || Number.POSITIVE_INFINITY))) { @@ -124,7 +124,7 @@ export class Broker { const prevClose = first(data['close'].values.slice(-2)) as number; let reprocessOrders = false; - for (let i = 0; i < this.orders.length; i = 0) { + for (let i = 0; i < this.orders.length; i++) { const order = this.orders[i]; /* istanbul ignore if */ @@ -232,8 +232,8 @@ export class Broker { } /** - * Long/short `price`, adjusted for commisions. - * In long positions, the adjusted price is a fraction higher, and vice versa. + * Long/short `price`, adjusted for commissions or user-defined trade execution price. + * In long positions, the commission-adjusted price for is a fraction higher, and vice versa. */ private adjustPrice(options: { size: number, price?: number }) { const { size, price } = options; diff --git a/src/interfaces/order-options.interface.ts b/src/interfaces/order-options.interface.ts index 113918a..7c6c679 100644 --- a/src/interfaces/order-options.interface.ts +++ b/src/interfaces/order-options.interface.ts @@ -1,6 +1,7 @@ import { Trade } from '../trade'; export interface OrderOptions { + price?: number; size: number; limitPrice?: number; stopPrice?: number; diff --git a/test/sma-cross.strategy.ts b/test/sma-cross.strategy.ts index 73a2a13..bf973cb 100644 --- a/test/sma-cross.strategy.ts +++ b/test/sma-cross.strategy.ts @@ -33,7 +33,8 @@ export class SmaCross extends Strategy { next(ctx: Context) { const { index, signals } = ctx; if (index < this.params.n1 || index < this.params.n2) return; - if (signals.get('crossUp')) this.buy({ size: 1000 }); + const price = ctx.data['close']; + if (signals.get('crossUp')) this.buy({ size: 1000, tpPrice: price * 1.15, slPrice: price * 0.9 }); if (signals.get('crossDown')) this.sell({ size: 1000 }); } }