If statement 带有全局变量和if条件的Pine脚本问题

If statement 带有全局变量和if条件的Pine脚本问题,if-statement,global-variables,return-value,pine-script,If Statement,Global Variables,Return Value,Pine Script,当我运行此代码时,我连续获得函数strategy.close的次数(qty\u percent=50,comment=“TP Pertial),尽管I是1而不是0 事实上,当我绘图时,你可以从中看到,我得到1,它是白色的值 我错过了一些东西,但我不知道是什么 有人能帮我吗 //@version=4 strategy("backtesting", initial_capital=20000, overlay = true) //set your strategy ema20

当我运行此代码时,我连续获得函数
strategy.close的次数(qty\u percent=50,comment=“TP Pertial)
,尽管
I
是1而不是0

事实上,当我绘图时,你可以从中看到,我得到1,它是白色的值

我错过了一些东西,但我不知道是什么

有人能帮我吗

//@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)
/@version=4
策略(“回溯测试”,初始资本=20000,叠加=true)
//制定你的战略
ema20=ema(关闭,20)
均线50=均线(收盘,50)
long=ema20>ema50
短路=ema20ema50且不短[11]
//预算是你想要分配给一个职位的钱
//desiredTakeProfitPercentage是您希望平仓的价格变动百分比
//tp_值和sl_值分别是您希望从某个职位赚取的钱和您希望损失的最大钱
整数预算=10000
所需浮动利润率=0.05
所需浮点数StopLossPercentage=0.03
浮动tp_值=500
浮动sl_值=250
浮动数量\关闭\百分比=0.5
Build=长条件
分录价格=价值(买入、未平仓、0)
浮点数存货=(预算/分录价格)
int intNumberStocks=ceil(numberStocks)
浮动位置值2=((未清-分录价格)*数量股票)+分录价格
浮动变动值=(未清-分录价格)*数量存货
浮动价格百分比=变更价值/预算
浮动价格百分比=变更价值/预算
浮动位置值=(变更值+(输入价格*数量库存))
int TP\ U数量\关闭\百分比=四舍五入(数量\关闭\百分比+整数库存)
//打开和关闭位置的条件
takeProfitValue=changeValue>tp_值
stopLossValue=changeValue<-sl_值
takeProfitPercentage=价格百分比>期望的takeProfitPercentage
stoplospercentage=价格百分比>价格百分比
longExitPrice=(输入价格*(1+0.1))
LongsTopLossSprice=(输入价格*(1-0.3))
//部分退出后的新变量和条件
exit_price=valuewhen(takeProfitPercentage,打开,0)
exit\u tp\u change=valuewhen(获取利润百分比、价格百分比、0)
浮动退出\值\位置=(退出\更改/数字存储)*(数字存储*0.5)
第二个\u获取利润百分比=退出\u值\u位置>所需的获取利润百分比
i=0
//在图表上绘出数值
绘图(输入价格,颜色=#aaa00b,线宽=2)
//绘图(位置值,颜色=#ffffff,线宽=1)
绘图(ema20,title=“20”,颜色=#aaaa00,线宽=1)
绘图(ema50,title=“50”,颜色=#550055,线宽=1)
//您要测试的时间段
开始=时间戳(2000,6,1,0,0)
结束=时间戳(2020,2,1,0,0)
//当进入

如果(时间>=开始和时间感谢您上传正确的脚本。
问题是
i
值不是您在问题中所述的
1
,而是在
01年5月25日
上的
0

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

plot(i, color=#ffffff, linewidth=4)
您应该像这样使用
plotchar()

plotchar(i, "i", "")
因为这不会扭曲你的图表。
该变量随后在数据窗口中可见

编辑:
i
值为零,因为它在每个条上重新初始化为
0

而不是使用

i=0
试用

var int i = 0

当使用
var
关键字时,变量仅在第一个栏初始化一次,并在所有后续栏中保持其值(当然,除非您为其指定另一个值).

无法将此脚本添加到图表中。它引发了一个
内部服务器研究错误
。请提供一个有效的脚本。非常抱歉,我上载了一个旧版本的脚本。我上载了新版本的脚本。感谢您抽出时间。我已更新了我的答案,以解释
I=0
问题。