PineScript止损止盈策略优化问询:添加独立开关按钮与切换入场K线价格计算基准
Solution to Your PineScript Stop Loss/Take Profit Issues
Let's tackle both of your problems step by step with modified code and clear explanations:
1. Adding Independent Toggle Buttons for SL/TP Display
The key here is to separate the boolean toggle (enable/disable) from the percentage input—mixing them was causing the type error. We'll create separate input.bool controls for enabling SL/TP, while keeping the percentage inputs as input.float for calculations.
2. Using Entry Candle Price Instead of Position Average Price
strategy.position_avg_price updates with every add/remove to your position, which causes the SL/TP levels to shift. Instead, we'll capture the price of the candle that triggered your entry and use that as the fixed base for calculations. We'll also handle both long and short positions correctly.
Modified Full Code
//@version=5 strategy("SL/TP with Toggle & Entry Price Base", overlay=true, default_qty_type=strategy.cash, default_qty_value=10000) // Stop Loss & Take Profit Settings Group var stp_tp_group = "Stop Loss & Take Profit Controls" // Independent Toggle Buttons (Enable/Disable Display) sl_enabled = input.bool(title="Enable Stop Loss Display", group=stp_tp_group, defval=true, tooltip="Toggle to show/hide Stop Loss line") tp_enabled = input.bool(title="Enable Take Profit Display", group=stp_tp_group, defval=true, tooltip="Toggle to show/hide Take Profit line") // Percentage Inputs (Keep Calculation Logic Intact) sl_pct = input.float(title="Stop Loss %", group=stp_tp_group, defval=2.0, minval=0.1)/100 tp_pct = input.float(title="Take Profit %", group=stp_tp_group, defval=4.0, minval=0.1)/100 // Capture Entry Candle Price (Fixed Base for SL/TP) var float entry_candle_price = na // Detect when a new position is opened (from flat to long/short) position_changed = strategy.position_size[1] == 0 and strategy.position_size != 0 if position_changed // Use entry candle's close price (you can replace with open/high/low if needed) entry_candle_price := close // Alternative: Use the exact entry price from the strategy order // entry_candle_price := strategy.opentrades.entry_price(strategy.opentrades - 1) // Calculate SL/TP Levels Based on Entry Price & Position Direction var float stop_level = na var float take_level = na if strategy.position_size > 0 // Long Position stop_level := entry_candle_price * (1 - sl_pct) take_level := entry_candle_price * (1 + tp_pct) elif strategy.position_size < 0 // Short Position stop_level := entry_candle_price * (1 + sl_pct) take_level := entry_candle_price * (1 - tp_pct) else // No Position stop_level := na take_level := na // Plot SL/TP Only If Toggles Are Enabled plot(sl_enabled ? stop_level : na, color=color.red, title="Stop Loss", linewidth=2) plot(tp_enabled ? take_level : na, color=color.green, title="Take Profit", linewidth=2) // Example Entry Signal (Replace with your own strategy logic) long_condition = ta.crossover(ta.sma(close, 50), ta.sma(close, 200)) if long_condition strategy.entry("Long", strategy.long) short_condition = ta.crossunder(ta.sma(close, 50), ta.sma(close, 200)) if short_condition strategy.entry("Short", strategy.short)
Key Explanations:
- Toggle Buttons: The
sl_enabledandtp_enabledboolean inputs control whether the SL/TP lines are plotted. This keeps the percentage inputs purely for calculation, avoiding type conflicts. - Fixed Entry Price: We detect when a new position is opened (
position_changedcondition) and capture the entry candle's price. This value stays fixed even if you add to your position later. - Multi-Direction Support: The code adjusts SL/TP calculations based on whether you're in a long or short position (critical for accurate levels in short trades).
- Flexible Entry Price: You can swap
closewithopen,high,low, or evenstrategy.opentrades.entry_price()if you want to use the exact fill price of your order instead of the candle's price.
内容的提问来源于stack exchange,提问作者Trader_M
相关产品推荐
相关产品推荐

