Pine script 如何在脚本中获取默认的数量值?

Pine script 如何在脚本中获取默认的数量值?,pine-script,trading,Pine Script,Trading,我正在编写一个策略,我想根据策略设置中设置的默认数量值进行计算。是的,我知道我可以使用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

我正在编写一个策略,我想根据策略设置中设置的默认数量值进行计算。是的,我知道我可以使用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)
//此源代码受Mozilla公共许可证2.0的条款约束,位于https://mozilla.org/MPL/2.0/
//©EduardoMattje
//@版本=4
策略(“反转收盘价”,叠加=真,默认数量类型=策略。股权百分比,默认数量值=10,初始资本=10000)
//背景
订单方向=输入(“两者”、“订单方向”,选项=[“两者”、“长”、“短”])
报酬与风险之比=输入(2.0,“报酬与风险之比”,最小值=1.0,步骤=0.1)
停止\回望=输入(3,“停止损失蜡烛回望”,最小值=1)
允许追溯=输入(真,“允许价格追溯”)
忽略趋势=输入(false,“允许交易在不考虑趋势的情况下进行”)
ma_交叉_停止=输入(错误,“如果ma在oposite方向交叉,则关闭”)
src=输入(hl2,“价格来源”)
//MA计算和绘图
均线类型=输入(“均线”、“移动平均线类型”,选项=[“均线”、“HMA”、“SMA”、“WMA”])
毫安长周期=输入(80,“毫安长周期”)
毫安短周期=输入(8,“毫安短周期”)
ma_long=ma_type==“EMA”?均线(src,ma_long_period):ma_type==“HMA”?hma(src,ma_long_period):ma_type==“SMA”?sma(src,马龙时期):wma(src,马龙时期)
MAU short=MAU type==“均线”?均线(src,短期均线):均线类型==“HMA”?hma(src,MAU短周期):MAU类型=“SMA”?sma(src,MAU短周期):wma(src,MAU短周期)
马牛=无视趋势==真?正确:马乌短>马乌长
马熊=无视趋势==正确?正确:马乌短<马乌长
绘图(马龙,“马龙”,不考虑趋势==真彩色。灰色:马牛色。绿色:红色,3)
曲线图(马乌短,“马乌短”,忽略趋势==真彩色。灰色:马乌牛彩色。绿色:彩色。红色,3)
//RCP计算
rcp_bull=low[0]close[1]
rcp_bear=高位[0]>高位[1]和高位[0]>高位[2]和收盘[0]<收盘[1]
//下单
在市场=策略。定位\u大小!=0
购买=策略。位置大小[0]>策略。位置大小[1]和策略。位置大小[1]==0
销售=策略。位置大小[0]<策略。位置大小[1]和策略。位置大小[1]==0
关闭=不在_市场和在_市场[1]
多头位置=策略。位置大小>0
空头位置=策略。位置大小<0
多头条件=rcp多头且不在市场和订单方向!=“短”和马牛
空头条件=rcp空头且不在市场和订单方向!=“龙”和马熊
购买价格=高+符号信息.mintick
售价=低-syminfo.mintick
//止损单
止损价格=多头仓位?valuewhen(买入,最低(停止回望)[1]-syminfo.mintick,0):空头位置?valuewhen(售出,最高(3)[1]+syminfo.mintick,0):不适用
停止注释=“已触发停止损失”
策略。结束(“长”,低=停止价格,停止评论)
打印(停止价格,“停止价格”,颜色为.red,2,打印样式为\u linebr)
//交叉关闭指令
如果马è过è停
如果多头和马熊
策略关闭(“长”,注释=停止注释)
如果短线仓位和多头仓位
策略关闭(“短”,注释=停止注释)
//接受利润订单
止损指数=abs(策略.位置平均价格-止损价格)
目标成本=停止成本*奖励成本比率
获取利润价格=多头仓位?valuewhen(买入,策略。仓位\平均价格+目标\刻度,0):空头\仓位?valuewhen(售出,策略.位置平均价格-目标刻度,0):不适用
target_comment=“获利回吐”
策略.收盘(“多头”,高>=获利回吐,目标回吐)
策略.收盘(“空头”、低barssince(long_条件)和barssince(long_条件)>=1,不在_市场,ma_牛市和买入价格卖空(卖空条件)和卖空(卖空条件)>=1且不在市场中,当(卖空条件,卖空价格,0)和卖空价格[0]>卖空价格[1]
如果更好的话,价格会更低
策略.取消(“空头”)
策略.输入(“做空”,错误,止损=卖出价)

经过几次试验,我成功地做到了我们巴西人所说的“冈比亚”

在第一个栏中,我打开一个头寸,我将头寸的大小保存在一个称为risk_size的var中。这是第一个订单中投入的资金量,这是下一个订单将用作风险的资金量

您可以将其更改为合同、货币或其他任何形式。在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")

谢谢,这就是我要找的!