On the rare occasion, my script crashes even though I feel the code below should prevent it. When I am about to change the stop or limit price, and the order appears to be working (Order.state equals Order.STATE_WORKING), I get the error "Order is not in valid state" and the script stops abruptly. The try clause does not seem to catch the error either.
Any suggestions on how to improve this code so that I can catch the error or, if nothing else, avoid the error altogether? Am I checking the order state correctly before changing the price? What am I missing?
if ( Order.state(oid) == Order.STATE_WORKING ) {
// try to capture error to avoid program stopping; report error to log file
try {
if ( Order.isBuy(oid) ) {
ret = Trade.buyStopLimit( { "stop": stp, "limit": lmt, "orderID": oid } ); // <<< ERROR HAPPENS HERE
} else {
ret = Trade.sellStopLimit( { "stop": stp, "limit": lmt, "orderID": oid } ); // <<< OR ERROR HAPPENS HERE
}
} catch(err) {
debug_output("error=" + err);
}
}
Any suggestions on how to improve this code so that I can catch the error or, if nothing else, avoid the error altogether? Am I checking the order state correctly before changing the price? What am I missing?
if ( Order.state(oid) == Order.STATE_WORKING ) {
// try to capture error to avoid program stopping; report error to log file
try {
if ( Order.isBuy(oid) ) {
ret = Trade.buyStopLimit( { "stop": stp, "limit": lmt, "orderID": oid } ); // <<< ERROR HAPPENS HERE
} else {
ret = Trade.sellStopLimit( { "stop": stp, "limit": lmt, "orderID": oid } ); // <<< OR ERROR HAPPENS HERE
}
} catch(err) {
debug_output("error=" + err);
}
}
Comment