Trigger already met - InputException

tkt2000
Hello Fellow Members,

I am new to Zerodha and trying to implement trailing stop loss with Zerodha APIs.

I can create GTT perfectly fine with below function.
`
function placeGTT(position) {
let stopLossTrigger = position.last_price * 0.91;
let stopLoss = position.last_price * 0.9;
let targetTrigger = position.last_price * 1.19;
let target = position.last_price * 1.2;
console.log("stopLossTrigger", stopLossTrigger, "stopLoss", stopLoss, "targetTrigger", targetTrigger, "target", target);
let params = {
trigger_type: kc.GTT_TYPE_OCO,
tradingsymbol: position.tradingsymbol,
exchange: position.exchange,
last_price: position.last_price,
trigger_values: [stopLossTrigger, targetTrigger],
orders: [
{
transaction_type: 'SELL',
quantity: position.quantity,
product: position.product,
order_type: 'SL',
price: stopLoss
},
{
transaction_type: 'SELL',
quantity: position.quantity,
product: position.product,
order_type: 'SL',
price: target
}
]

};
kc.placeGTT(params)
.then(function (response) {
console.log("placeGTT Success:", response);
position.trigger_id = response.trigger_id;
let redisData = JSON.stringify(position);
redis.set(position.instrument_token, redisData);
})
.catch(function (err) {
// Something went wrong.
console.log("placeGTT Error:", err);
});
}
`

However while trying to modify GTT, i am getting below error.
placeGTT Error: {
status: 'error',
message: 'Trigger already met.',
data: null,
error_type: 'InputException'
}

Here is the code to modify GTT.
`
redis.get(tick.instrument_token).then((result) => {
let redisData = JSON.parse(result);
//console.log(redisData);
tick.last_price = 150; //Price harcoded to check logic written below
if (tick.last_price > redisData.average_price) {

let percentageIncrease = ((tick.last_price - redisData.average_price) / redisData.average_price) * 100;
if (percentageIncrease > 10) {

console.log("Price increased more than than 10% hence updating GTT with increased stopLoss and target");

let stopLossTrigger = tick.last_price * 0.91;
let stopLoss = tick.last_price * 0.9;
let targetTrigger = tick.last_price * 1.19;
let target = tick.last_price * 1.2;

console.log("stopLossTrigger", stopLossTrigger, "stopLoss", stopLoss, "targetTrigger", targetTrigger, "target", target);

let params = {
trigger_type: kc.GTT_TYPE_OCO,
tradingsymbol: redisData.tradingsymbol,
exchange: redisData.exchange,
last_price: tick.last_price,
trigger_values: [stopLossTrigger, targetTrigger],
orders: [
{
transaction_type: 'SELL',
quantity: redisData.quantity,
product: redisData.product,
order_type: 'SL',
price: stopLoss
},
{
transaction_type: 'SELL',
quantity: redisData.quantity,
product: redisData.product,
order_type: 'SL',
price: target
}
]
};

console.log("Params", params);

kc.modifyGTT(redisData.trigger_id, params)
.then(function (response) {
console.log("GTT modified successfully: ", response);
//let redisData = JSON.stringify(position);
//redis.set(tick.instrument_token, redisData);
})
.catch(function (err) {
// Something went wrong.
console.log("placeGTT Error:", err);
});
} else {
console.log("Price increased but less than 10%");
}
} else {
console.log("Price is lower than my purchase");
}
});
`

Please help to resolve this.
Tagged:
This discussion has been closed.