Hello All,
I’m starting to learn how to use ProRealCode to create an automated trading programme and have come across a high volume of trades when backtesting that look to have instantly closed out but I’m at a loss as to why they are occurring. I started off by using the Simplified Creation option and picked my combination of indicators – this has worked to an extent but only less than 50% are successful – the ‘instant kill’ trades are what is throwing me off which I cannot work out what the issue is (programme per below):
Has anyone ever encountered this before and if so, any ideas as to what is wrong? thanks in advance!
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
// Prevents the system from creating new orders to enter the market or increase position size before the specified time
noEntryBeforeTime = 063000
timeEnterBefore = time >= noEntryBeforeTime
// Prevents the system from placing new orders to enter the market or increase position size after the specified time
noEntryAfterTime = 205800
timeEnterAfter = time < noEntryAfterTime
// Prevents the system from placing new orders on specified days of the week
daysForbiddenEntry = OpenDayOfWeek = 6 OR OpenDayOfWeek = 0
// Conditions to enter long positions
indicator1 = Volume
c1 = (indicator1 > 500)
indicator2 = ExponentialAverage[8](close)
indicator3 = ExponentialAverage[21](close)
c2 = (indicator2 > indicator3)
IF (c1 AND c2) AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry THEN
BUY 3 CONTRACT AT MARKET
ENDIF
// Conditions to exit long positions
indicator4 = MACDline[12,26,9](close)
c3 = (indicator4 >= 9.5)
IF c3 THEN
SELL AT MARKET
ENDIF
// Conditions to enter short positions
indicator5 = Volume
c4 = (indicator5 > 500)
indicator6 = ExponentialAverage[8](close)
indicator7 = ExponentialAverage[21](close)
c5 = (indicator6 < indicator7)
IF (c4 AND c5) AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry THEN
SELLSHORT 3 CONTRACT AT MARKET
ENDIF
// Conditions to exit short positions
indicator8 = MACDline[12,26,9](close)
c6 = (indicator8 <= -6)
IF c6 THEN
EXITSHORT AT MARKET
ENDIF
// Stops and targets
SET STOP pTRAILING 4
SET TARGET pPROFIT 20
It’s because your conditions are met every candlestick, so many trades are entered.
Try using more strict settings.
Moreover, exiting when MACD is in your own direction is a bit awkward! You exit LONG trades when MACD >= 9.5, I’d say you would exit LONG trades when it’s below zero, wouldn’t you? (the opposite for SHORT trades)
Besides this, maybe setting a crossover of the two averages, instead of simply checking that one is greater than the other (which leads to so many trades because checking it’s > occurs almost always, while a crossover occurs every N bars).
It’s a start, don’t be discoureged! Read many topics, study many pieces of code, use part of them to build your own strategy spending time and efforts to achieve better results.
Usually a strategy crossover, simply put, is not likely to yield good results, you can add a filter, maybe enter when, after a crossover, price retraces and touches again the average(s).
Learning from (& copying) other strategies will be of great help!
Appreciate it Roberto!
I’ve been looking at creating the automated programme over the weekend so I’m still getting to grips with it. I’ve not got a programming background hence trying to pick this up is proving difficult but I’m not gonna give up – I see the promise in this. It’s just getting the darn thing to work and work consistently. Whatever combination of indicators used will generate either lower volume, greater accuracy but lower return or the opposite! granted, I expect losing trades – I guess it’s just striking a good balance.
Ideally, I just want it to do what’s worked relatively well for me manually – execute and hold through the momentum of trend until it turns. Trying to get it done as a programme is my challenge :/
Yes, what I meant with MACD is that (see attached pic) in an uptrend exiting when it’s ABOVE a certain small value means it will be closed soon, much earlier than the end of the uptrend! (the opposite for downtrends).
Appreciate the advice! I’ve been trialling different setup to reduce the frequency of those trades which instantly open and close with more validation. I am however, perplexed as to how I can add more validation to prevent weird trades from happening.
Example; I have setup BUY criteria to trigger on price being higher than 2 periods earlier, my shorter term MA is trending above my longer term MA and there is volume. The criteria was met however it opened a buy when the momentum was fading and it came off of highs. Any suggestions on how I can avoid that? I keep thinking I need validation which effectively says ‘only trigger if there is consecutively trending close prices’ but no idea how to include this.
Would you mind posting your latest code?
Sure. See below. This kind of approach works quite well manually but replicating with conditions, etc..into an automated programme for me as a complete novice programmer 🙁
// Conditions to enter long positions
indicator1 = ExponentialAverage[100](close)
c1 = (close > indicator1[2])
indicator2 = ExponentialAverage[8](close)
indicator3 = ExponentialAverage[100](close)
c2 = (indicator2 > indicator3[1])
c3 = (close >= close[2])
IF (c1 AND c2 AND c3) AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry THEN
BUY 3 CONTRACT AT MARKET
ENDIF
// Conditions to exit long positions
indicator4 = RSI[14](close)
c4 = (indicator4 > indicator4[10])
IF c4 THEN
SELL AT MARKET
ENDIF
// Conditions to enter short positions
indicator5 = ExponentialAverage[100](close)
c5 = (close < indicator5[2])
indicator6 = ExponentialAverage[8](close)
indicator7 = ExponentialAverage[100](close)
c6 = (indicator6 < indicator7[1])
c7 = (close <= close[2])
IF (c5 AND c6 AND c7) AND timeEnterBefore AND timeEnterAfter AND not daysForbiddenEntry THEN
SELLSHORT 3 CONTRACT AT MARKET
ENDIF
// Conditions to exit short positions
indicator8 = RSI[14](close)
c8 = (indicator8 < indicator8[10])
IF c8 THEN
EXITSHORT AT MARKET
ENDIF
You can avoid accumulating positions by adding:
AND Not OnMarket
to your conditions at lines 9 and 29.