如何在PineScript中获取default_qty_value?

Edu*_*rdo 5 trading pine-script

我正在编写一个策略,我想根据策略设置中设置的 default_qty_value 执行计算。是的,我知道我可以使用strategy.position_avg_price来获取头寸大小,但这些计算必须在策略打开之前完成。

\n

有没有办法检索这个值,或者用户是否需要进行输入?

\n

这是代码。

\n
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/\n// \xc2\xa9 EduardoMattje\n//@version=4\n\nstrategy("Reversal closing price", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=10, initial_capital=10000)\n\n// Settings\n\norder_direction = input("Both", "Order direction", options=["Both", "Long", "Short"])\nreward_risk_ratio = input(2.0, "Reward to risk ratio", minval=1.0, step=0.1)\nstop_lookback = input(3, "Stoploss candle lookback", minval=1)\nallow_retracing = input(true, "Allow price retracing")\ndisregard_trend = input(false, "Allow trades to take place regardless of the trend")\nma_cross_stop = input(false, "Close if MA crosses in oposite direction")\nsrc = input(hl2, "Price source")\n\n// MA calculation and plot\n\nma_type = input("EMA", "Moving average type", options=["EMA", "HMA", "SMA", "WMA"])\nma_long_period = input(80, "MA long period")\nma_short_period = input(8, "MA short period")\nma_long = ma_type == "EMA" ? ema(src, ma_long_period) : ma_type == "HMA" ? hma(src, ma_long_period) : ma_type == "SMA" ? sma(src, ma_long_period) : wma(src, ma_long_period)\nma_short = ma_type == "EMA" ? ema(src, ma_short_period) : ma_type == "HMA" ? hma(src, ma_short_period) : ma_type == "SMA" ? sma(src, ma_short_period) : wma(src, ma_short_period)\nma_bull = disregard_trend == true? true : ma_short > ma_long\nma_bear = disregard_trend == true? true : ma_short < ma_long\nplot(ma_long, "MA long", disregard_trend == true ? color.gray : ma_bull ? color.green : color.red, 3)\nplot(ma_short, "MA short", disregard_trend == true ? color.gray : ma_bull ? color.green : color.red, 3)\n\n// RCP calculation\n\nrcp_bull = low[0] < low[1] and low[0] < low[2] and close[0] > close[1]\nrcp_bear = high[0] > high[1] and high[0] > high[2] and close[0] < close[1]\n\n// Order placement\n\nin_market = strategy.position_size != 0\n\nbought = strategy.position_size[0] > strategy.position_size[1] and strategy.position_size[1] == 0\nsold = strategy.position_size[0] < strategy.position_size[1] and strategy.position_size[1] == 0\nclosed = not in_market and in_market[1]\nlong_position = strategy.position_size > 0\nshort_position = strategy.position_size < 0\n\nlong_condition = rcp_bull and not in_market and order_direction != "Short" and ma_bull\nshort_condition = rcp_bear and not in_market and order_direction != "Long" and ma_bear \n\nbuy_price = high + syminfo.mintick\nsell_price = low - syminfo.mintick\n\n// Stop loss orders\n\nstop_price = long_position ? valuewhen(bought, lowest(stop_lookback)[1] - syminfo.mintick, 0) : short_position ? valuewhen(sold, highest(3)[1] + syminfo.mintick, 0) : na\nstop_comment = "Stop loss triggered"\nstrategy.close("Long", low <= stop_price, stop_comment)\nstrategy.close("Short", high >= stop_price, stop_comment)\nplot(stop_price, "Stop price", color.red, 2, plot.style_linebr)\n\n// MA cross close orders\n\nif ma_cross_stop\n    if long_position and ma_bear\n        strategy.close("Long", comment=stop_comment)\n    if short_position and ma_bull\n        strategy.close("Short", comment=stop_comment)\n\n// Take profit orders\n\nstop_ticks = abs(strategy.position_avg_price - stop_price)\ntarget_ticks = stop_ticks * reward_risk_ratio\ntake_profit_price = long_position ? valuewhen(bought, strategy.position_avg_price + target_ticks, 0) : short_position ? valuewhen(sold, strategy.position_avg_price - target_ticks, 0) : na\ntarget_comment = "Take profit"\nstrategy.close("Long", high >= take_profit_price, target_comment)\nstrategy.close("Short", low <= take_profit_price, target_comment)\nplot(take_profit_price, "Target price", color.green, 2, plot.style_linebr)\n\n//\n\nif long_condition\n    strategy.entry("Long", true, qty= order_size, stop=buy_price)\nif short_condition\n    strategy.entry("Short", false, stop=sell_price)\n\n// Price retracing orders\n\nif allow_retracing\n    better_price_long = barssince(closed) > barssince(long_condition) and barssince(long_condition) >= 1 and not in_market and ma_bull and buy_price < valuewhen(long_condition, buy_price, 0) and buy_price[0] < buy_price[1]\n    if better_price_long\n        strategy.cancel("Long")\n        strategy.entry("Long", true, stop=buy_price)\n    \n    better_price_short = barssince(closed) > barssince(short_condition) and barssince(short_condition) >= 1 and not in_market and ma_bear and sell_price > valuewhen(short_condition, sell_price, 0) and sell_price[0] > sell_price[1]\n    if better_price_short\n        strategy.cancel("Short")\n        strategy.entry("Short", false, stop=sell_price)\n
Run Code Online (Sandbox Code Playgroud)\n

Edu*_*rdo 2

经过几次尝试,我成功地做到了我们巴西人所说的“gambiarra”。

在第一根柱中,我开仓并将仓位大小保存在名为risk_size 的变量中。这是放入第一个订单的金额,因此这就是下一个订单将用作风险的金额。

你可以将其更改为合同、金钱或其他任何形式。在交易视图实现内置变量之前,必须这样做。

// Get risk size

var start_price = 0.0
var risk_size = 0.0

if barstate.isfirst
    strategy.entry("Get order size", true)
    start_price := close

if barssince(barstate.isfirst) >= 2 and strategy.position_entry_name == "Get order size"
    risk_size := strategy.position_size * start_price
    strategy.close("Get order size")
Run Code Online (Sandbox Code Playgroud)