When I run this code I get consecutive times the function strategy.close(qty_percent=50, comment="TP-Pertial) though the i is 1 and not 0.
Infact, When I plot i I get 1 as you can see from pictures, it is the white value.
I miss something but I don't know what.
Anyone can help me?
//@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")
plot(i, color=#ffffff, linewidth=4)
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
if(takeProfitPercentage and i==0)
strategy.close("Long", qty_percent=0.5, comment="TP-Partial")
i:=1
Aucun commentaire:
Enregistrer un commentaire