Skip to content
Merged
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
10 changes: 5 additions & 5 deletions src/broker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))) {
Expand Down Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/order-options.interface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Trade } from '../trade';

export interface OrderOptions {
price?: number;
size: number;
limitPrice?: number;
stopPrice?: number;
Expand Down
3 changes: 2 additions & 1 deletion test/sma-cross.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
}
Loading