Hello everyone,
I would like to share with you my first code. What I’m pretending is to know when enter into the market for now only with Long positions.
I took the MACD crossing lines to know when enter over next 20 minutes when this occurs but taking into account the Volume and the RSI.
It is good, but I know that could one of the best 😉 It is why I’m sharing this code, to make it better using maybe another indicators or strategies to know exactly when exit.
The system enter very well….but doesn’t exit properly.
Can you help me to improve it?
Thank you!!!
P.D: It also works over SP500
DEFPARAM CumulateOrders = false // Posizioni cumulate disattivate
DEFPARAM flatbefore = 153000
DEFPARAM flatafter = 224500
ncontracts = 50
currentRSI = RSI[20](close)
volumen = Volume
BlueLineMACD = MACDLINE(close)
RedLineMACD = ExponentialAverage[9](BlueLineMACD)
//GRAPH BlueLineMACD Coloured(0,0,255) as "Blue Line MACD"
//GRAPH RedLineMACD Coloured(255,0,0) as "Red Line MACD"
//GRAPH amplitude Coloured(125,125,0) as "Amplitude MACD"
// Condiciones para entrada de posiciones largas
IF NOT LongOnMarket AND currentRSI[0] > currentRSI[1] AND currentRSI[0] > 50 AND BlueLineMACD[0] >= RedLineMACD[0] AND volumen > 5000 AND close[10] < close[1] AND close[2] - close[1] < 5 THEN
BUY ncontracts CONTRACTS AT MARKET
longentryrsi = currentRSI
longvolumn = volumen
volumndecresess = 0
ENDIF
IF volumen < longvolumn THEN
volumndecresess = volumndecresess + 1
ELSE
volumndecresess = 0
ENDIF
rsidiff = currentRSI[0] - longentryrsi
// Condiciones de salida de posiciones largas
If LongOnMarket AND (rsidiff < 6 AND BlueLineMACD[0] <= RedLineMACD[0]) THEN
SELL AT MARKET
ENDIF
// Stops y objetivos: introduzca aquí sus stops de protección y objetivos de beneficios
SET STOP $LOSS 2000
SET TARGET $PROFIT 2000
Looks like its got good potential, I got below on the Dow over 10,000 bars at £1 per point spreadbet on 5 Min Timeframe.
I changed only the settings below to as shown
<pre class=”lang:probuilder decode:true”>SET STOP $LOSS 160
SET TARGET $PROFIT 235
GraHal
Hi frovira
Might you be considering doing a Short version of this Algo at any time please?
I love the short duration in and out! To my way of thinking this shows the Algo entered at the right time, made the money and then exit … no need to wait around down and up, up and down over many periods until a profit shows up!?
Well Done, Good Work!
GraHal
PS Wow … even trying an optimised time constraint doesn’t improve results! It could be that trades are intrinsically not taken overnight due to conditions not met etc.
Lots of the winning Trades have a £0 MAE and loads have a very low MAE, only 8 eventual winning trades have a MAE of any significance (6 at -£20s, 1 at -£33 and 1 at -£65.
Yeah we need that Short version frovira.
I’ll have a try at a better exit strategy, others please feel free to contribute as my coding skills are nowhere near lots on here.
Cheers
GraHal
Hello,
thx for the code! Two swift remarks:
- the standard 14 period for RSI works slightly better, so no added value to take 20.
- the variable “volumndecresess” is not used in your code
I guess Volume > 5000 will limit overnight trades, but I’ve put the time constraint below on it anyway with no reduction in gain on DAX @ 15 min over 100,000 bars.
PS / Edit I’ve just remembered … there is no volume on DAX anyway after 16:30 GMT??
MyTime = Time > 070000 and Time < 210000
I’ve also added Nicolas Trailing Stop … full code below. About £100 improvement and with additional safety. also.
//https://www.prorealcode.com/topic/eeuu-tech-100-20minutes-macd-strategy-long/
DEFPARAM CumulateOrders = false // Posizioni cumulate disattivate
//DEFPARAM flatbefore = 153000
//DEFPARAM flatafter = 224500
MyTime = Time > 070000 and Time < 210000
ncontracts = 1
currentRSI = RSI[14](close)
volumen = Volume
BlueLineMACD = MACDLINE(close)
RedLineMACD = ExponentialAverage[9](BlueLineMACD)
//GRAPH BlueLineMACD Coloured(0,0,255) as "Blue Line MACD"
//GRAPH RedLineMACD Coloured(255,0,0) as "Red Line MACD"
//GRAPH amplitude Coloured(125,125,0) as "Amplitude MACD"
// Condiciones para entrada de posiciones largas
IF NOT LongOnMarket AND currentRSI[0] > currentRSI[1] AND currentRSI[0] > 50 AND BlueLineMACD[0] >= RedLineMACD[0] AND volumen > A AND close[10] < close[1] AND close[2] - close[1] < 5 AND MyTime THEN
BUY ncontracts CONTRACTS AT MARKET
longentryrsi = currentRSI
longvolumn = volumen
volumndecresess = 0
ENDIF
IF volumen < longvolumn THEN
volumndecresess = volumndecresess + 1
ELSE
volumndecresess = 0
ENDIF
rsidiff = currentRSI[0] - longentryrsi
// Condiciones de salida de posiciones largas
If LongOnMarket AND (rsidiff < 6 AND BlueLineMACD[0] <= RedLineMACD[0]) THEN
SELL AT MARKET
ENDIF
// Stops y objetivos: introduzca aquí sus stops de protección y objetivos de beneficios
SET STOP $LOSS 110
SET TARGET $PROFIT 110
//trailing stop function
trailingstart = 70 //trailing will start @trailinstart points profit
trailingstep = 6 //trailing step to move the "stoploss"
//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THEN
newSL = tradeprice(1)+trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
newSL = newSL+trailingstep*pipsize
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THEN
newSL = tradeprice(1)-trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND newSL-close>=trailingstep*pipsize THEN
newSL = newSL-trailingstep*pipsize
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF
//************************************************************************
//GRAPH newSL as "trailing"
Below Long and Short version (although very few shorts!) for UKX @ 15 Min
//https://www.prorealcode.com/topic/eeuu-tech-100-20minutes-macd-strategy-long/
DEFPARAM CumulateOrders = false // Posizioni cumulate disattivate
MyTime = Time > 070000 and Time < 210000
MHist = MACD[12,26,9](close)
BlueMACD = MACDline[12,26,9](close)
RedMACD = BlueMACD - MHist
//GOLONG
IF RSI[14](close) > 65 AND BlueMACD crosses over RedMACD AND volume > 2500 AND ADXR[14] > 15 AND Average[7](close) > Average[20](close) AND MyTime THEN
BUY 1 CONTRACTS AT MARKET
ENDIF
//EXITLONG
If RSI[14](close) < 30 AND BlueMACD crosses under RedMACD THEN
SELL AT MARKET
ENDIF
//GOSHORT
IF Chandle[20](close) < -80 AND BlueMACD < RedMACD AND MyTime THEN
SellShort 1 CONTRACTS AT MARKET
ShortChandle = Chandle[20](close)
ENDIF
ChanddiffShort = abs(Chandle[20](close)- ShortChandle)
//EXITSHORT
If ChanddiffShort > 24 AND BlueMACD > RedMACD THEN
ExitShort AT MARKET
ENDIF
SET STOP PLOSS 50
SET TARGET PPROFIT 100
//trailing stop function
trailingstart = 160 //trailing will start @trailinstart points profit
trailingstep = 6 //trailing step to move the "stoploss"
//reset the stoploss value
IF NOT ONMARKET THEN
newSL=0
ENDIF
//manage long positions
IF LONGONMARKET THEN
//first move (breakeven)
IF newSL=0 AND close-tradeprice(1)>=trailingstart*pipsize THEN
newSL = tradeprice(1)+trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND close-newSL>=trailingstep*pipsize THEN
newSL = newSL+trailingstep*pipsize
ENDIF
ENDIF
//manage short positions
IF SHORTONMARKET THEN
//first move (breakeven)
IF newSL=0 AND tradeprice(1)-close>=trailingstart*pipsize THEN
newSL = tradeprice(1)-trailingstep*pipsize
ENDIF
//next moves
IF newSL>0 AND newSL-close>=trailingstep*pipsize THEN
newSL = newSL-trailingstep*pipsize
ENDIF
ENDIF
//stop order to exit the positions
IF newSL>0 THEN
SELL AT newSL STOP
EXITSHORT AT newSL STOP
ENDIF