Title: Help with Correctly Coding SL and TP in thinkScript - Positions Not Closing Reliably
Hello everyone,
I'm working on a thinkScript strategy for trading /MNQ futures, and I'm having issues with the stop-loss logic. Sometimes, the stop-loss order does not trigger as expected, leaving positions open when the price hits or crosses the stop-loss level. I suspect the issue might be related to how I'm defining the conditions or the order execution in thinkScript, but I could use some expert eyes to pinpoint the problem and suggest a fix.
Below is the relevant code snippet for position management, including the stop-loss and take-profit logic:
The issue occurs intermittently: when the price reaches or exceeds the stop-loss level (e.g., low <= EntryPrice() - stopLossPoints for longs), the SELL_TO_CLOSE or BUY_TO_CLOSE order doesn't always execute, and the position remains open.
Are there best practices for coding stop-losses in thinkScript to avoid missed executions, especially for futures like /MNQ?
Any suggestions for improving the code or debugging this issue would be greatly appreciated. If you need more context (e.g., the full strategy code or how finalBuySignal/finalSellSignal are defined), let me know, and I can provide additional details.
Thanks in advance for your help!
Hello everyone,
I'm working on a thinkScript strategy for trading /MNQ futures, and I'm having issues with the stop-loss logic. Sometimes, the stop-loss order does not trigger as expected, leaving positions open when the price hits or crosses the stop-loss level. I suspect the issue might be related to how I'm defining the conditions or the order execution in thinkScript, but I could use some expert eyes to pinpoint the problem and suggest a fix.
Below is the relevant code snippet for position management, including the stop-loss and take-profit logic:
Code:
# Position Management
# Calculate fixed stop loss based on risk percentage
def accountSize = 10000; # Example account size
def maxRiskPercent = 1; # 1% risk per trade
def maxRiskDollars = accountSize * (maxRiskPercent / 100);
def contractSize = 1; # 1 contract for /MNQ
def pointValue = 2; # $2 per point for /MNQ
def stopLossPoints = Max(1, maxRiskDollars / (contractSize * pointValue)); # SL in points, minimum 1 point
def takeProfitPoints = stopLossPoints * 2; # Example: 2:1 reward-to-risk ratio
def isLong = GetQuantity() > 0;
def isShort = GetQuantity() < 0;
# Entry orders with commissions
AddOrder(OrderType.BUY_AUTO, finalBuySignal, open[-1], 1, Color.GREEN, Color.GREEN);
AddOrder(OrderType.SELL_AUTO, finalSellSignal, open[-1], 1, Color.RED, Color.RED);
# Stop loss and take profit for LONG positions
AddOrder(OrderType.SELL_TO_CLOSE, isLong and low <= EntryPrice() - stopLossPoints, EntryPrice() - stopLossPoints, 1, Color.RED, Color.RED);
AddOrder(OrderType.SELL_TO_CLOSE, isLong and high >= EntryPrice() + takeProfitPoints, EntryPrice() + takeProfitPoints, 1, Color.GREEN, Color.GREEN);
# Stop loss and take profit for SHORT positions
AddOrder(OrderType.BUY_TO_CLOSE, isShort and high >= EntryPrice() + stopLossPoints, EntryPrice() + stopLossPoints, 1, Color.RED, Color.RED);
AddOrder(OrderType.BUY_TO_CLOSE, isShort and low <= EntryPrice() - takeProfitPoints, EntryPrice() - takeProfitPoints, 1, Color.GREEN, Color.GREEN);
The issue occurs intermittently: when the price reaches or exceeds the stop-loss level (e.g., low <= EntryPrice() - stopLossPoints for longs), the SELL_TO_CLOSE or BUY_TO_CLOSE order doesn't always execute, and the position remains open.
Are there best practices for coding stop-losses in thinkScript to avoid missed executions, especially for futures like /MNQ?
Any suggestions for improving the code or debugging this issue would be greatly appreciated. If you need more context (e.g., the full strategy code or how finalBuySignal/finalSellSignal are defined), let me know, and I can provide additional details.
Thanks in advance for your help!