gpt4 book ai didi

pine-script - 如何获取 PineScript 中的 default_qty_value?

转载 作者:行者123 更新时间:2023-12-05 04:58:22 25 4
gpt4 key购买 nike

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

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

这是代码。

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © EduardoMattje
//@version=4

strategy("Reversal closing price", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=10, initial_capital=10000)

// Settings

order_direction = input("Both", "Order direction", options=["Both", "Long", "Short"])
reward_risk_ratio = input(2.0, "Reward to risk ratio", minval=1.0, step=0.1)
stop_lookback = input(3, "Stoploss candle lookback", minval=1)
allow_retracing = input(true, "Allow price retracing")
disregard_trend = input(false, "Allow trades to take place regardless of the trend")
ma_cross_stop = input(false, "Close if MA crosses in oposite direction")
src = input(hl2, "Price source")

// MA calculation and plot

ma_type = input("EMA", "Moving average type", options=["EMA", "HMA", "SMA", "WMA"])
ma_long_period = input(80, "MA long period")
ma_short_period = input(8, "MA short period")
ma_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)
ma_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)
ma_bull = disregard_trend == true? true : ma_short > ma_long
ma_bear = disregard_trend == true? true : ma_short < ma_long
plot(ma_long, "MA long", disregard_trend == true ? color.gray : ma_bull ? color.green : color.red, 3)
plot(ma_short, "MA short", disregard_trend == true ? color.gray : ma_bull ? color.green : color.red, 3)

// RCP calculation

rcp_bull = low[0] < low[1] and low[0] < low[2] and close[0] > close[1]
rcp_bear = high[0] > high[1] and high[0] > high[2] and close[0] < close[1]

// Order placement

in_market = strategy.position_size != 0

bought = strategy.position_size[0] > strategy.position_size[1] and strategy.position_size[1] == 0
sold = strategy.position_size[0] < strategy.position_size[1] and strategy.position_size[1] == 0
closed = not in_market and in_market[1]
long_position = strategy.position_size > 0
short_position = strategy.position_size < 0

long_condition = rcp_bull and not in_market and order_direction != "Short" and ma_bull
short_condition = rcp_bear and not in_market and order_direction != "Long" and ma_bear

buy_price = high + syminfo.mintick
sell_price = low - syminfo.mintick

// Stop loss orders

stop_price = long_position ? valuewhen(bought, lowest(stop_lookback)[1] - syminfo.mintick, 0) : short_position ? valuewhen(sold, highest(3)[1] + syminfo.mintick, 0) : na
stop_comment = "Stop loss triggered"
strategy.close("Long", low <= stop_price, stop_comment)
strategy.close("Short", high >= stop_price, stop_comment)
plot(stop_price, "Stop price", color.red, 2, plot.style_linebr)

// MA cross close orders

if ma_cross_stop
if long_position and ma_bear
strategy.close("Long", comment=stop_comment)
if short_position and ma_bull
strategy.close("Short", comment=stop_comment)

// Take profit orders

stop_ticks = abs(strategy.position_avg_price - stop_price)
target_ticks = stop_ticks * reward_risk_ratio
take_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
target_comment = "Take profit"
strategy.close("Long", high >= take_profit_price, target_comment)
strategy.close("Short", low <= take_profit_price, target_comment)
plot(take_profit_price, "Target price", color.green, 2, plot.style_linebr)

//

if long_condition
strategy.entry("Long", true, qty= order_size, stop=buy_price)
if short_condition
strategy.entry("Short", false, stop=sell_price)

// Price retracing orders

if allow_retracing
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]
if better_price_long
strategy.cancel("Long")
strategy.entry("Long", true, stop=buy_price)

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]
if better_price_short
strategy.cancel("Short")
strategy.entry("Short", false, stop=sell_price)

最佳答案

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

在第一个柱中,我打开了一个头寸,并将头寸的大小保存在一个名为 risk_size 的变量中。这是第一个订单中投入的金额,因此这是下一个订单将用作风险的金额。

您可以将其更改为契约(Contract)、金钱或其他任何内容。在 Trading View 实现内置变量之前,必须这样做。

// 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")

关于pine-script - 如何获取 PineScript 中的 default_qty_value?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64036082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com