Skip to content

modifyPosition

ctx.order.modifyPosition(params)

At a glance — adjust the stop-loss and/or take-profit of the open position without closing it. The go-to for moving a stop to break-even or trailing it. Returns an OrderTicket.

Signature

modifyPosition(params: ModifyParams): OrderTicket

ModifyParams lets you set either or both of stopLoss / takeProfit as a PriceLevel.

Example

order/limit-and-cancel
import { defineStrategy, type OrderTicket } from '@nexpips/sdk-trading';
/**
* Ordres en attente : poser un `limitBuy`, l'annuler (`cancel`) s'il n'est
* pas rempli en quelques barres, et resserrer le stop d'une position ouverte
* (`modifyPosition`). Le `limitBuy` renvoie un `OrderTicket` opaque à conserver.
*/
export default defineStrategy({
symbol: 'EURUSD',
timeframe: 'M15',
risk: { maxRiskPercentPerTrade: 1, maxOpenPositions: 1, maxDailyLossPercent: 5 },
setup: () => {
let pending: OrderTicket | null = null;
let barsWaiting = 0;
return {
onBar(ctx) {
if (ctx.position.hasPendingOrder && pending) {
barsWaiting += 1;
if (barsWaiting > 3) {
ctx.order.cancel(pending);
pending = null;
barsWaiting = 0;
}
return;
}
if (ctx.position.isLong) {
ctx.order.modifyPosition({ stopLoss: { type: 'pips', value: 10 } });
return;
}
if (ctx.position.isFlat) {
const limitPrice = ctx.bar.close - 0.001;
pending = ctx.order.limitBuy(limitPrice, {
riskPercent: 1,
stopLoss: { type: 'pips', value: 20 },
takeProfit: { type: 'rr', value: 2 },
});
barsWaiting = 0;
}
},
};
},
});

Tighten the stop once the position is open.

To know

See also