【问题标题】:Pine Script issue with global variable and if condition全局变量和 if 条件的 Pine 脚本问题
【发布时间】:2020-11-12 17:20:42
【问题描述】:

当我运行此代码时,我连续获得函数 strategy.close(qty_percent=50, comment="TP-Pertial) 尽管 i 是 1 而不是 0。

事实上,当我绘制 i 时,我得到 1,正如您从 picture 中看到的那样,它是白色值。

我想念一些东西,但我不知道是什么。

谁能帮帮我?

//@version=4
strategy("backtesting", initial_capital=20000, overlay = true)

//set your strategy
ema20 = ema(close, 20)
ema50 = ema(close, 50)

long = ema20 > ema50
short = ema20 < ema50

longCondition = long and long[2] and not long[3]
shortCondition = short and short[10] and not short[11] 

closeLong = ema20 < ema50 and not long[3]
closeShort = ema20 > ema50 and not short[11]

//budget is the money that you want allocate to open a position
//desiredTakeProfitPercentage is the percentage of price change you want to close your position
//tp_Value and sl_Value are the money that you want to earn from a position and max money you want to lose respectively
int budget = 10000
float desiredTakeProfitPercentage = 0.05
float desiredStopLossPercentage = 0.03
float tp_Value = 500
float sl_Value = 250
float qty_close_percentage = 0.5

bought = longCondition
entry_price = valuewhen(bought, open, 0)

float numberStocks = (budget/entry_price)
int intNumberStocks = ceil(numberStocks)
float positionValue2 = ((open - entry_price)*numberStocks) + entry_price
float changeValue = (open - entry_price) * numberStocks
float price_TPPercentage = changeValue/budget
float price_SLPercentage = changeValue/budget
float positionValue = (changeValue + (entry_price * numberStocks))
int TP_qty_close_percentage = round(qty_close_percentage + intNumberStocks)



//condition to open and close position
takeProfitValue = changeValue > tp_Value
stopLossValue = changeValue < - sl_Value
takeProfitPercentage = price_TPPercentage > desiredTakeProfitPercentage
stopLossPercentage = price_SLPercentage > price_SLPercentage

longExitPrice = (entry_price * (1 + 0.1))
longStopLossPrice = (entry_price * (1 - 0.3))

//new variables and condition after partial exit
exit_price = valuewhen(takeProfitPercentage, open, 0)
exit_tp_change = valuewhen(takeProfitPercentage, price_TPPercentage, 0)

float exit_value_position = (exit_tp_change/numberStocks)*(numberStocks*0.5)

second_takeProfitPercentage = exit_value_position > desiredTakeProfitPercentage

i=0

//plot the value on chart 
plot(entry_price, color=#aaa00b, linewidth=2)
//plot(positionValue, color=#ffffff, linewidth=1)
plot(ema20, title="20", color=#aaaa00, linewidth=1)
plot(ema50, title="50", color=#550055, linewidth=1)

//period of time you want to test
start = timestamp(2000,6,1,0,0)
end = timestamp(2020,2,1,0,0)

//when enter
if (time >=start and time <= end and strategy.position_size < 1)
    strategy.entry("Long", strategy.long, qty=intNumberStocks, when=longCondition)

//when close
strategy.close("Long", when = closeLong, comment="closeLong")
if(i==0 and takeProfitPercentage)
    i:=1
    strategy.close("Long", qty_percent=50, comment="TP-Partial")

if(stopLossValue or stopLossPercentage)
    strategy.close("Long", comment="SL")
    i:=0
if(second_takeProfitPercentage and i==1)
    strategy.close("Long", comment= "TP-close")
    i:=0

plot(i, color=#ffffff, linewidth=4)

【问题讨论】:

  • 此脚本无法添加到图表中。它抛出一个internal server study error。请提供一个有效的脚本。
  • 很抱歉,我上传了一个旧版本的脚本。我上传了新版本。感谢您的宝贵时间。

标签: if-statement global-variables return-value pine-script


【解决方案1】:

感谢您上传正确的脚本。
问题是i 值不是您在问题中所说的1,而是25 May 01 上的0

另外,不要使用plot() 进行调试

plot(i, color=#ffffff, linewidth=4)

你应该像这样使用plotchar()

plotchar(i, "i", "")

因为这不会扭曲您的图表。
然后该变量在数据窗口中可见。

编辑:i 的值为零,因为它在每个柱上都被重新初始化为 0
而不是使用

i=0

尝试使用

var int i = 0

当使用var 关键字时,该变量仅在第一个柱上初始化一次,并在随后的所有柱中保持其值(当然,除非您为其分配另一个值)。

【讨论】:

  • 我已经更新了我的答案来解释 i=0 问题。
猜你喜欢
  • 1970-01-01
  • 2020-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多