Select 条件使用下拉输入和回测百分比止盈和止损

Select condition using drop-down input and Take profit and Stop loss by percentage for backtesting

我还是个学习pine脚本的新手。现在有点迷路了。我有 2 个条件,分别用于 Long Entry 、 Short Entry 、 Long Exit 和 Short Exit。

可以对 select 使用下拉输入,这样我就可以根据该条件进行多种组合?

下面的百分比计算止盈和止损也是正确的吗?

//@version=5
strategy(title="Belajar - Tanya", overlay=true,
     max_bars_back = 600, max_lines_count = 140, max_labels_count = 35,
     process_orders_on_close = true,
     default_qty_type = strategy.cash, default_qty_value = 100, currency=currency.USD,
     backtest_fill_limits_assumption = 0, slippage = 6, commission_type = strategy.commission.percent, commission_value = 0.1,
     initial_capital = 100, calc_on_every_tick = true, calc_on_order_fills = true, pyramiding = 0)


// EMA Calculation
ema1 = ta.ema(close, 24)
ema2 = ta.sma(close, 24)
ema3 = ta.ema(close, 200)

// Entry & Exit Conditions
LongEntry1 = ema1 > ema2
LongEntry2 = ema2 > ema3
LongExit1 = ema2 < ema2
LongExit2 = close < ema3
ShortEntry1 = ema1 < ema2
ShortEntry2 = ema2 > ema3
ShortExit1 = ema2 > ema2
ShortExit2 = close > ema3

// User select conditions to use using dropd-own menu
input_trigger_LongEntry = input.string(title="Select Long Entry Trigger", defval="LongEntry1", options=["LongEntry1", "LongEntry2"])
input_trigger_ShortEntry = input.string(title="Select Short Entry Trigger", defval="ShortEntry1", options=["ShortEntry1", "ShortEntry2"]) 
input_trigger_LongExit = input.string(title="Select Long Exit Trigger", defval="LongExit1", options=["LongExit1", "LongExit2"])
input_trigger_ShortExit = input.string(title="Select Short Exit Trigger", defval="ShortExit1", options=["ShortExit1", "ShortExit2"]) 

//input_trigger_LongEntry = input.string(title="Select Long Entry Trigger", defval=LongEntry1, options=[LongEntry1, LongEntry2])
//input_trigger_ShortEntry = input.string(title="Select Short Entry Trigger", defval=ShortEntry1, options=[ShortEntry1, ShortEntry2]) 
//input_trigger_LongExit = input.string(title="Select Long Exit Trigger", defval=LongExit1, options=[LongExit1, LongExit2])
//input_trigger_ShortExit = input.string(title="Select Short Exit Trigger", defval=ShortExit1, options=[ShortExit1, ShortExit2]) 


// Select Start and End Date
varip strat_start_date  = timestamp("01 Jan 2021 00:00 +0000")
varip str_start_date    = "Start Date"
varip str_end_date      = "End Date"

i_strat_startTime       = input.time(strat_start_date, str_start_date, group = "Strategy Tester", inline = "date")
i_strat_endTime         = input.time(timestamp ("31 Dec 2029 00:00 +0000"), str_end_date, group = "Strategy Tester", inline = "date")
i_strat_enable_L        = input.bool(false, "Long Trades", group = "Strategy Tester")
i_strat_enable_S        = input.bool(true, "Short Trades", group = "Strategy Tester")

f_inDateRange (start, end) => time >= start and time <= end
inDateRange = f_inDateRange (i_strat_startTime, i_strat_endTime)

// User input TP & SL
i_strat_longTPpercent = input.float(title="Long Take Profit (%)", minval=0.0, step=0.1, defval=2, group="TP & SL") 
i_strat_longSLpercent = input.float(title="Long Stop Loss (%)", minval=0.0, step=0.1, defval=2, group="TP & SL")
i_strat_shortTPpercent = input.float(title="Short Take Profit (%)", minval=0.0, step=0.1, defval=2, group="TP & SL") 
i_strat_shortSLpercent = input.float(title="Short Stop Loss (%)", minval=0.0, step=0.1, defval=2, group="TP & SL")

// Calculations TP & SL Percentage

strat_percentAsPoints(pcnt) =>
    strategy.position_size != 0 ? math.round(pcnt / 100.0 * strategy.position_avg_price / syminfo.mintick) : float(na)

strat_percentAsPrice(pcnt) =>
    strategy.position_size != 0 ? ((pcnt / 100.0) + 1.0) * strategy.position_avg_price : float(na)
 
strat_current_position_size = math.abs(strategy.position_size)
strat_initial_position_size = math.abs(ta.valuewhen(strategy.position_size[1] == 0.0, strategy.position_size, 0))   
    
strat_TP_long = strategy.position_avg_price + strat_percentAsPoints(i_strat_longTPpercent) * syminfo.mintick * strategy.position_size / math.abs(strategy.position_size)
strat_SL_long = strategy.position_avg_price - strat_percentAsPoints(i_strat_longSLpercent) * syminfo.mintick * strategy.position_size / math.abs(strategy.position_size)
strat_TP_short = strategy.position_avg_price - strat_percentAsPoints(i_strat_shortTPpercent) * syminfo.mintick * strategy.position_size / math.abs(strategy.position_size)
strat_SL_short = strategy.position_avg_price + strat_percentAsPoints(i_strat_shortSLpercent) * syminfo.mintick * strategy.position_size / math.abs(strategy.position_size)


// Strategy
var trade_cond = 0
var strat_entry_price = float(na)

inTrade = strategy.position_size > 0
notInTrade = strategy.position_size <= 0

enter_long = input_trigger_LongEntry and inDateRange and notInTrade
enter_short = input_trigger_ShortEntry and inDateRange and notInTrade
exit_long = (input_trigger_LongExit or strat_TP_short) and (inDateRange or inTrade)
exit_short = (input_trigger_ShortExit or strat_TP_short) and (inDateRange or inTrade)

strat_entry_price := enter_long or enter_short ? close : strat_entry_price

strategy.entry("Entry Long", strategy.long, when=i_strat_enable_L and enter_long)
strategy.entry("Entry Short", strategy.short, when=i_strat_enable_S and enter_short)
strategy.close("Close Long", when=exit_long or enter_short, qty_percent=100)
strategy.close("Close Short", when=exit_short or enter_long, qty_percent=100)


// Plot
plot(ema1, color=color.blue)
plot(ema2, color=color.purple)
plot(ema3, color=color.yellow)

您可以使用 input.string() 为用户提供一些选项并让他们使用。然后在你的脚本中你需要检查选择的选项并采取相应的行动。

像这样:

long_cond = input.string("Condition 1", "Condition for Long", ["Condition1", "Condition 2"])

_condition() =>
    switch long_cond
        "Condition 1" => // Do something
        "Condition 2" => // Do something

您可以使用以下模板根据百分比计算您的止盈和止损水平。

takeProfit = input.float(2.0, "Take Profit (%)", minval=0.0, step=0.1) / 100.0
stopLoss = input.float(1.0, "Stop Loss (%)", minval=0.0, step=0.1) / 100.0

tp  = strategy.position_avg_price * (1 + takeProfit)
sl = strategy.position_avg_price * (1 - stopLoss)

Source