【发布时间】: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