diff --git a/src/lib/backtest.ts b/src/lib/backtest.ts index b6cca7d..34ef21b 100644 --- a/src/lib/backtest.ts +++ b/src/lib/backtest.ts @@ -35,10 +35,13 @@ function updatePosition(position: IPosition, bar: IBar): void { * @param exitTime The timestamp for the bar when the position was exited. * @param exitPrice The price of the instrument when the position was exited. */ -function finalizePosition(position: IPosition, exitTime: Date, exitPrice: number, exitReason: string): ITrade { +function finalizePosition(position: IPosition, exitTime: Date, exitPrice: number, exitReason: string, fees: number): ITrade { const profit = position.direction === TradeDirection.Long ? exitPrice - position.entryPrice : position.entryPrice - exitPrice; + const growth = position.direction === TradeDirection.Long + ? exitPrice / position.entryPrice + : position.entryPrice / exitPrice; let rmultiple; if (position.initialUnitRisk !== undefined) { rmultiple = profit / position.initialUnitRisk; @@ -51,9 +54,7 @@ function finalizePosition(position: IPosition, exitTime: Date, exitPrice: number exitPrice: exitPrice, profit: profit, profitPct: (profit / position.entryPrice) * 100, - growth: position.direction === TradeDirection.Long - ? exitPrice / position.entryPrice - : position.entryPrice / exitPrice, + growth: growth - (growth * fees), riskPct: position.initialRiskPct, riskSeries: position.riskSeries, rmultiple: rmultiple, @@ -136,6 +137,11 @@ export function backtest; } + // + // Sum of maker fee and taker fee. + // + const fees = strategy.fees && strategy.fees() || 0; + // // Tracks trades that have been closed. // @@ -190,7 +196,7 @@ export function backtest extends I position: IPosition; } +/** + * Computes the fees. + * Return the sum of maker fee and taker fee. + */ +export type FeesFn = () => number; + /** * Arguments to a stop loss rule function. */ @@ -208,4 +214,10 @@ export interface IStrategy; + + /** + * Function that computes the fees + * Return the sum of maker fee and taker fee. + */ + fees?: FeesFn; } \ No newline at end of file