Hallo,
in my script below the exit doesn’t work correct and I don’t know exactly the reason. The main problem is that a position closes everytime at the next bar, except for the last open position. The script should be some kind of template, in which I can easy change conditions like longentry, stop etc..
Important:
- Test parameters should be defined in the input section
- Only one position at the same time
- longentry and shortentry are just an example – It should be possible to change these inputs easily
- In this script longexit and shortexit is fixed depending on bars- If longentry and shortentry triggers, I want to keep the position oben for several bars.
- The other inputs like stop, take profit, direction, position size and date should remain and work in each case.
Why is the position open for only one bar even if I define nrbars = 10?
Thanks and best regards
//////////
//Input
//////////
//Timeframe defined by Chart-Timeframe
//Cumulation and Chart History
defparam cumulateorders = false
defparam preloadbars = 1000 //Start and End Date below must fit in Chart History - Choose large enough Chart History
//Direction
Dir = 1 //Long = 1; Short = 2; Both = 3
//Positionsize
positionsize = 1 //Size for Contracts/Shares/Lots(Standard Lot)
//Amount of Bars for Test
nrbars = 10
//Long/Short Condition
longentry = close > close[1]
longexit = tradeindex + barindex[nrbars]
shortentry = close < close[1]
shortexit = tradeindex + barindex[nrbars]
//Start and End Date
startdate = 20190201 //YYYYMMDD
enddate = 20190330
//StopLoss and ProfitTarget
stopuse = 1 //Stoploss = 1; Trailingstop = 2; No Stop = 3
profituse = 2 //Take profit = 1; No Take profit = 2
stoploss = 10
trailingstop = 10
takeprofit = 25
//////////
//Calculation
//////////
//Direction
if dir=1 then
lentry = longentry
lexit = longexit
elsif dir = 2 then
sentry = shortentry
sexit = shortexit
elsif dir = 3 then
lentry = longentry
lexit = longexit
sentry = shortentry
sexit = shortexit
endif
//Stop Type
if stopuse = 1 then
stoptype = stoploss
elsif stopuse = 2 then
stoptype = trailingstop
elsif stopuse = 3 then
stoptype = undefined
endif
//Take Profit
if profituse = 1 then
profittype = undefined
elsif profituse = 2 then
profittype = takeprofit
endif
//Trade Execution dependig on Date
if date >= startdate and date <= enddate then
if not onmarket and lentry then
buy positionsize contract at market
endif
if not onmarket and sentry then
sellshort positionsize contract at market
endif
//Exit Position
if longonmarket and lexit then
sell at market
endif
if shortonmarket and sexit then
exitshort at market
endif
//Set Stop and ProfitTarget
set stop loss stoptype
set target profit profittype
endif
your “lexit” and “sexit” variables are always true, that’s why you are exiting directly the orders at next bar.
If you want to exit orders depending of how many bars have elapsed since their inception, you should subtract the actual barindex to tradeindex. There are many examples on forums, “exit after n bars …”, etc.