ProBacktest – Exit doesn't work
- This topic has 1 reply, 2 voices, and was last updated 6 years ago by .
Viewing 2 posts - 1 through 2 (of 2 total)
Viewing 2 posts - 1 through 2 (of 2 total)
Forums › ProRealTime English forum › ProOrder support › ProBacktest – Exit doesn't work
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:
Why is the position open for only one bar even if I define nrbars = 10?
Thanks and best regards
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
////////// //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.