Hello – I hope this finds everybody well.
As I’ve just made my first foray into coding, after hanging around on the site for sometime over a year now, I thought I’d share and of course would welcome any feedback. By way on context, I’ve been fiddling around with trading (via a spread betting account with IG) for a couple of years. I’ve been up a bit, down a bit and a couple of months ago found myself pretty much back where I started, and struggling to find the time to concentrate properly what with a full time job and a young family. I’ve had several false starts with ProRealCode and never got as far as putting a system live – until about 3 weeks ago.
The very simple system runs on DAX 5 minute chart and uses a 200 period EMA and SuperTrend as filters, and an ADX/ADXR crossover as entry trigger. Stops and profit target is fixed (about a 1 to 1.6 R/R). Back-testing showed it to be profitable based on a 100K bars test (starting in February 2019), but with a horrible curve – a fairly consistent losing streak early on, but consistently profitable since about the end of last July. Now, I know this is far from satisfactory for the purist, but recent performance was enough to persuade me to make it live, with low position size and guaranteed stops.
I absolutely don’t expect this to run for ever – it’s going to stop working based on back test, but it’s given me the confidence that there’s something to build on for the future. Happily after 3 and a bit weeks of running, account is up about 18% and win rate is just the right side of 50%.
// Definition of code parameters
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
// Common indicators
indicator1 = ExponentialAverage[200](close)
indicator2 = SuperTrend[3,10]
indicator3 = ADX[11]
indicator4 = ADXR[11]
// Common variable values
adxval = 22
profitpoints = 83
losspoints = 50
// Conditions to enter long positions
c1 = (close > indicator1)
c2 = (close > indicator2)
c3 = (indicator3 CROSSES OVER indicator4)
c4 = indicator3 > adxval
IF c1 AND c2 AND c3 AND c4 THEN
BUY 0.5 PERPOINT AT MARKET
ENDIF
// Conditions to enter short positions
c5 = (close < indicator1)
c6 = (close < indicator2)
c7 = (indicator3 CROSSES OVER indicator4)
c8 = indicator3 > adxval
IF c5 AND c6 AND c7 AND c8 THEN
SELLSHORT 0.5 PERPOINT AT MARKET
ENDIF
// Stops and targets
SET STOP pLOSS losspoints
SET TARGET pPROFIT profitpoints
//EXIT for Weekend
IF opendayofweek = 5 and openhour >= 21 and openminute >= 50 then
sig=1
else
sig=0
endif
IF sig=1 THEN
SELL AT MARKET
EXITSHORT AT MARKET
ENDIF
Back test view attached along with account growth chart based on the 46 trades placed to date.
Comments and advice welcome. I’m very new to this, and not even scratched the surface.
Cheers
WT
Thanks for sharing your code.
I think it will be useful to many and a source for new ideas 🙂
Any news from the recent results of your strategy? Thanks for sharing! 🙂
So, I ran this live for 2 months – 11th May to around 11th July. As per my original post, it started very well then began to tail off as market conditions changed. I lucked out really starting it at a good time. Win rate for the period was about 42% overall and it was a couple of hundred quid in profit when I stopped it. My intention has always been to go back and look at how I can refine it, probably initially looking at some multi-timeframe filters. Also keen to explore alternative exit (e.g. trailing stop) rather than fixed points target.
Results from running in back-test from 11th July to current are very bad, but the market’s basically been going sideways. So it definitely needs some better filtering to look for trending periods.
Hi Welshtrader, thank you for sharing your code. I’m a beginner, so please bear with me. I’ve added a few conditions (VWAP, KST and VWMA) to increase the WinRate to around 64% (Sept. 16th 2019 to date) with a R/R of x1.9 (Capital of 1500€, TF 5 minutes and Size = 1 DAX 1€ Contract). I’ve also copied the ML code from juanj (https://www.prorealcode.com/topic/machine-learning-in-proorder/) and the MFE from Nicolas (https://www.prorealcode.com/blog/learning/trailing-stop-max-favorable-excursion-mfe/).
Walk Forward shows a Win Rate of 62.5% and R/R of x1.6.
Not sure of the Tick by Tick mode in the backtest.
Please test and let me know…
A big thank you to robertogozzi and Nicolas who guided me through my learning process.
DEFPARAM CumulateOrders = False // Cumulating positions deactivated
N = 1
Timeframe(5 minutes)
EMA200 = ExponentialAverage[200](close)
SupT = SuperTrend[3,10]
iADX = ADX[13]
iADXR = ADXR[13]
VWAP = CALL "VWAP Intrady"
OscKST, SignalKST = CALL "KST8"
VWMA3 = CALL "VWMA Intraday"
EMA3 = ExponentialAverage[3](close)
// Common variable values
adxval = StartingValue
profitpoints = 500
losspoints = 84 //(Best in WF 94)
trailingstop = 50
///////////////////////////////////////////////////////////////////////
// Conditions to enter long positions
c1 = (close > EMA200) AND (close > VWAP) AND (OscKST > SignalKST) AND (VWMA3 > EMA3)
c2 = (close > SupT)
c3 = (iADX CROSSES OVER iADXR)
c4 = iADX > adxval
// Conditions to enter short positions
c5 = (close < EMA200)
c6 = (close < SupT)
c7 = (iADX CROSSES OVER iADXR)
c8 = iADX > adxval
/////////////////////////////////////////////////////////////////////////
// Heuristics Algorithm Start
If onmarket[1] = 1 and onmarket = 0 Then
optimize = optimize + 1
EnDif
StartingValue = 24 //(Best in WF 24)
ResetPeriod = 8 //Specify no of months after which to reset optimization
Increment = 1
MaxIncrement = 4 //Limit of no of increments either up or down
Reps = 5 //Number of trades to use for analysis
MaxValue = 29 //Maximum allowed value
MinValue = increment //Minimum allowed value
once monthinit = month
once yearinit = year
If (year = yearinit and month = (monthinit + ResetPeriod)) or (year = (yearinit + 1) and ((12 - monthinit) + month = ResetPeriod)) Then
ValueX = StartingValue
WinCountB = 0
StratAvgB = 0
BestA = 0
BestB = 0
monthinit = month
yearinit = year
EndIf
once ValueX = StartingValue
once PIncPos = 1 //Positive Increment Position
once NIncPos = 1 //Neative Increment Position
once Optimize = 0 ////Initialize Heuristicks Engine Counter (Must be Incremented at Position Start or Exit)
once Mode = 1 //Switches between negative and positive increments
If Optimize = Reps Then
WinCountA = 0 //Initialize current Win Count
StratAvgA = 0 //Initialize current Avg Strategy Profit
For i = 1 to Reps Do
If positionperf(i) > 0 Then
WinCountA = WinCountA + 1 //Increment Current WinCount
EndIf
StratAvgA = StratAvgA + (((PositionPerf(i)*countofposition[i]*100000)*-1)*-1)
Next
StratAvgA = StratAvgA/Reps //Calculate Current Avg Strategy Profit
If StratAvgA >= StratAvgB Then
StratAvgB = StratAvgA //Update Best Strategy Profit
BestA = ValueX
EndIf
If WinCountA >= WinCountB Then
WinCountB = WinCountA //Update Best Win Count
BestB = ValueX
EndIf
If WinCountA > WinCountB and StratAvgA > StratAvgB Then
Mode = 0
ElsIf WinCountA < WinCountB and StratAvgA < StratAvgB and Mode = 1 Then
ValueX = ValueX - (Increment*NIncPos)
NIncPos = NIncPos + 1
Mode = 2
ElsIf WinCountA >= WinCountB or StratAvgA >= StratAvgB and Mode = 1 Then
ValueX = ValueX + (Increment*PIncPos)
PIncPos = PIncPos + 1
Mode = 1
ElsIf WinCountA < WinCountB and StratAvgA < StratAvgB and Mode = 2 Then
ValueX = ValueX + (Increment*PIncPos)
PIncPos = PIncPos + 1
Mode = 1
ElsIf WinCountA >= WinCountB or StratAvgA >= StratAvgB and Mode = 2 Then
ValueX = ValueX - (Increment*NIncPos)
NIncPos = NIncPos + 1
Mode = 2
EndIf
If NIncPos > MaxIncrement or PIncPos > MaxIncrement Then
If BestA = BestB Then
ValueX = BestA
Else
If reps >= 10 Then
WeightedScore = 10
Else
WeightedScore = round((reps/100)*100)
EndIf
ValueX = round(((BestA*(20-WeightedScore)) + (BestB*WeightedScore))/20) //Lower Reps = Less weight assigned to Win%
EndIf
NIncPos = 1
PIncPos = 1
ElsIf ValueX > MaxValue Then
ValueX = MaxValue
ElsIf ValueX < MinValue Then
ValueX = MinValue
EndIF
Optimize = 0
EndIf
// Heuristics Algorithm End
/////////////////////////////////////////////////////////////////////////
IF c1 AND c2 AND c3 AND c4 THEN
BUY N CONTRACT AT MARKET
ENDIF
IF c5 AND c6 AND c7 AND c8 THEN
SELLSHORT N CONTRACT AT MARKET
ENDIF
//trailing stop
//resetting variables when no trades are on market
if not onmarket then
MAXPRICE = 0
MINPRICE = close
priceexit = 0
endif
//case SHORT order
if shortonmarket then
MINPRICE = MIN(MINPRICE,close) //saving the MFE of the current trade
if tradeprice(1)-MINPRICE>=trailingstop*pointsize then //if the MFE is higher than the trailingstop then
priceexit = MINPRICE+trailingstop*pointsize //set the exit price at the MFE + trailing stop price level
endif
endif
//case LONG order
if longonmarket then
MAXPRICE = MAX(MAXPRICE,close) //saving the MFE of the current trade
if MAXPRICE-tradeprice(1)>=trailingstop*pointsize then //if the MFE is higher than the trailingstop then
priceexit = MAXPRICE-trailingstop*pointsize //set the exit price at the MFE - trailing stop price level
endif
endif
//exit on trailing stop price levels
if onmarket and priceexit>0 then
EXITSHORT AT priceexit STOP
SELL AT priceexit STOP
endif
// Stops and targets
SET STOP pLOSS losspoints
SET TARGET pPROFIT profitpoints
i get this error using the code…
Below the code I’ve used for VWAP courtesy of one of the members here. Let me know if you spot an error in the code.
// VWAP@Time intraday
// 10.04.2020
// Daniele Maddaluno
DEFPARAM CalculateOnLastBars = 2500
startTime=080000
endTime= 235959
if opentime < startTime or opentime > endTime then
n = 0
dwapT1 = 0
//dwapT2 = 0
priced = 0
shared = 0
summ = 0
vwap = close
//vwapstd = 0
else
n = n + 1
// This if has been added just for plot reasons
if n <= 1 then
dwapT1 = 0
//dwapT2 = 0
else
dwapT1 = 190
//dwapT2 = 128
endif
priced = priced + (totalprice*volume)
shared = shared + volume
if shared>0 then
vwap = priced/shared
summ = summ + square(totalprice - vwap)
//vwapstd = sqrt(summ / n)
endif
endif
// Manage the coloring of vwap mid line
if close > vwap then
dwapR = 0
dwapG = 128
dwapB = 192
else
dwapR = 255
dwapG = 0
dwapB = 0
endif
return vwap coloured(dwapR, dwapG, dwapB, dwapT1) as "vwap"
V WAP … hmm, brings to mind a certain song title. And everything else. And now you’re all going to think of that every time you see that indicator, hahaha.
Cardi. B? Can you please post the lycrics here? ))))))
No … you will be banned! 🙂
I’d never heard of Cardi B or the song, but being an inquisitive chap I had to check it out! 🙂
Just checked, it’s Megan T. S. Anyway, everyone here undertands what VWAP stands for 🙂
What is Heuristics used for in your program.
don’t use valueX
fifi743
Thank you for spotting this gross mistake. My bad!
I’ve “tried” to correct it and run again backtest and WF.
So, the initial version (ver 1 ) I posted (working witout ML) did generate in backtest 6574€ WinRate 64.23% and Profit Rate 1.9x. When running the WF, the results are 5509€ WinRate 60.96% and Profit Rate 1.5x. (files below)
The version with ML embedded (ver 3) did generate in backtest 8073€ WinRate 60.43% and Profit Rate 2.04x. When running the WF, the results are 6249€ WinRate 55.79% and Profit Rate 1.56x. (files below + ITF)
So, the results with ML appear to be better in normal backtest and in WF.
Please review and let me know how we can further improve the result.