Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/fees #17

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/lib/backtest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -136,6 +137,11 @@ export function backtest<InputBarT extends IBar, IndicatorBarT extends InputBarT
indicatorsSeries = inputSeries as IDataFrame<IndexT, IndicatorBarT>;
}

//
// Sum of maker fee and taker fee.
//
const fees = strategy.fees && strategy.fees() || 0;

//
// Tracks trades that have been closed.
//
Expand Down Expand Up @@ -190,7 +196,7 @@ export function backtest<InputBarT extends IBar, IndicatorBarT extends InputBarT
// Close the current open position.
//
function closePosition(bar: InputBarT, exitPrice: number, exitReason: string) {
const trade = finalizePosition(openPosition!, bar.time, exitPrice, exitReason);
const trade = finalizePosition(openPosition!, bar.time, exitPrice, exitReason, fees);
completedTrades.push(trade!);
// Reset to no open position;
openPosition = null;
Expand Down Expand Up @@ -427,7 +433,7 @@ export function backtest<InputBarT extends IBar, IndicatorBarT extends InputBarT
if (openPosition) {
// Finalize open position.
const lastBar = indicatorsSeries.last();
const lastTrade = finalizePosition(openPosition, lastBar.time, lastBar.close, "finalize");
const lastTrade = finalizePosition(openPosition, lastBar.time, lastBar.close, "finalize", fees);
completedTrades.push(lastTrade);
}

Expand Down
12 changes: 12 additions & 0 deletions src/lib/strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ export interface IOpenPositionRuleArgs<BarT extends IBar, ParametersT> 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.
*/
Expand Down Expand Up @@ -208,4 +214,10 @@ export interface IStrategy<InputBarT extends IBar = IBar, IndicatorsBarT extends
* Return the amount of profit to trigger an exit.
*/
profitTarget?: ProfitTargetFn<InputBarT, ParametersT>;

/**
* Function that computes the fees
* Return the sum of maker fee and taker fee.
*/
fees?: FeesFn;
}