ProRealCode - Trading & Coding with ProRealTime™
Thanks for the support guys, losing trades are an inevitable part of doing business so let’s just make sure we learn from them 🙂
Good idea Cosmic, the code you posted is practically the same as older versions before Mike introduced the entry parameters and it is often better to use when market conditions are unclear, I use it myself sometimes too.
It has proven quite difficult to automate entries which doesn’t require certain market conditions, I’m currently testing reduced periods and eventually removing a a condition or two in order to get entry conditions which are less sensitive and start the system but still reduce the aspect of waiting and watching to get a good entry.
Kasper, I’m no expert on beartraps but that certainly looked like one with a resolute break of the monthly support. BTW have you made any further progress experimenting with the entry conditions?
Yesterday I found a nice piece of code in the library uploaded by Doctrading;
It has been bothering me that by default PRT does not show DI+ and DI- on the ADX and I havn’t figured out how to display it as an indicator or how to code it as a parameter but looks like Doc has the answer;
Defparam Cumulateorders = false
n = 5
// ACHAT
ca1 = ADX[50] > 10
ca2 = DIplus[50](close) > DIminus[50](close)
ca3 = low < BollingerDown[20](close) and close > BollingerDown[20](close)
IF ca1 AND ca2 AND ca3 THEN
BUY n SHARES AT MARKET
ENDIF
// VAD
cv1 = ADX[50] > 10
cv2 = DIplus[50](close) < DIminus[50](close)
cv3 = high > BollingerUp[20](close) and close < BollingerUp[20](close)
IF cv1 AND cv2 AND cv3 THEN
SELLSHORT n SHARES AT MARKET
ENDIF
// Stops et objectifs
SET STOP pLOSS 60
SET TARGET pPROFIT 10<br><br>
This way we make the ADX condition also take into account the direction of the price movement and not just it’s strenght, I will try to integrate it to the CFTA indicator but without the Bollinger band parameter.
Hi Cfta. I must say that I kind of got stuck and didn’t develop it further. We agree on the many false breakout’s, and I think the majority if them is because trades goes against the main trend. I not sure, but anyway in all the textbook examples:”trend is your friend” and also “It pays to trade with the professionals”
We need the multi timeframe support to get this to work. But anyway the gridorder with dynamic gridstep code is here, and It will actually work on any strategy. In my mind there could be some interesting ways to develop new entries on together with a change in the code:
I don’t know how or if there is a way to use the build in support/resistance lines in the code, Anyone know of this?
Also I been reading that the re-test + false breakouts are the way professionals trades and stops out the “non professionals” because everyone has their SL right below the support line – exactly as we saw yesterday.
Cheers Kasper
Hey Kasper, indeed MTF support would make all the difference in the world 🙂 Entries on S/R sounds good and for a skilled coder like Nicolas it’s probably possible to code but I’m not sure if we can make it work in in real trading because too often the price don’t respect S/R. SL to BE at certain number of entries or profit should be fairly easy to code I might be able to do it myself even if it will take time since I’m still a beginner. Anyway don’t forget about the BB exit feature but make sure the RR setting is not too high, I think the BB exit is the better option most of the time since it can also save profits and not just exit on BE.
I stripped a few things from the indicator to simplify the code and get more entries with less conditions, I think it looks pretty promising and might be good enough to make a screener out of it even though we should try a few different period values and setings since I coded it this afternoon. Hope you guys like it!
//==================CFTA-v3.0
//Heikin-Ash
xClose = (Open+High+Low+Close)/4
if(barindex>2) then
xOpen = (xOpen[1] + xClose[1])/2
endif
greencandle = xClose>xOpen AND xClose[1]<xOpen[1]
redcandle = xClose<xOpen AND xClose[1]>xOpen[1]
//***************Long
c1 = MACD > 0
c2 = ADX[10] > 20
c3 = CCI[10] > 0
DIP = DIplus[10]
DIM = DIminus[10]
c4 = (DIP > DIM[1])
If c1 and c2 and c3 and c4 and greencandle then
Indicator1=1
Else
Indicator1=0
Endif
//*************Short
c11 = MACD < 0
c12 = ADX[10] > 20
c13 = CCI[10] < 0
DIP = DIplus[10]
DIM = DIminus[10]
c14 = (DIP < DIM[1])
If c11 and c12 and c13 and c14 and redcandle then
Indicator22=-1
Else
Indicator22=0
Endif
Return indicator1 COLOURED(0,255,0) as "Buy", indicator22 COLOURED(255,0,0) as "Sell"
Hi CFTA. I think I missed out of the BB exit? what is that? 🙂 I finally got the breakeven and one Trailingstop to work. I thought it was easy, but it took a few evenings and a weekend to complete and test. I made some re-arrangement of the code to include both manual LONG and SHORT and an auto feature with some switches in the code. Very convenient when testing. I did not find a decent auto code so I left it out- but made room for it. I have not optimized the Trailing stop in any way just assumed the following
I hope you like it. Cheers Kasper
NOTE! If you use the manual code it supposed to only make 1 trade cycle, however I just discovered that if it breaks even with exactly 0% it will run a 2nd round!
// =============================================CFTA 2 sec LOW v3.11======================================================
//=====================================GRID ORDER and Dynamic GridSTEP Money MGT=============================================
//================parameters==============
defparam preloadbars=2000
//defparam flatbefore=090000
//defparam flatafter=170000
DEFPARAM CumulateOrders = true
once RRreached = 0
//=============manuel or autotrades=======
ManuelLong=1
Manuelshort=0
Auto=0
//===============Tradetime================
IF (Time >= 090000 AND Time <= 170000)then
Tradetime=1
else
Tradetime=0
endif
//==========Account and Gridstep Variables==========
//
//NOTE! Dynamic Gridstep if more correct on larger TF>10min
accountbalance = 10000 //account balance in money at strategy start
riskpercent = 1 //whole account risk in percent%
RR = 2//RR- risk reward ratio (set to 0 disable this function)
Breakevenfactor=25//When to breakeven in % of RR and Calc of RRreached1
Trailingfactor1=66//in % of RR Used for calc of RRreached2
Trailingfactor2=45//in % of RR Used for EXIT STOP ORDER @RRreached2
minSTEP = 5 //minimal step of the grid
maxSTEP = 20 //maximal step of the grid
ATRcurrentPeriod = 5 //recent volatility 'instant' period
ATRhistoPeriod = 100 //historical volatility period
//========================================
ATR = averagetruerange[ATRcurrentPeriod]
histoATR= highest[ATRhistoPeriod](ATR)
resultMAX = MAX(minSTEP*pipsize,histoATR - ATR)
resultMIN = MIN(resultMAX,maxSTEP*pipsize)
gridstep = (resultMIN)
BEF=Breakevenfactor*RR/100//BreakevenFactor
TF1=Trailingfactor1*RR/100
TF2=Trailingfactor2*RR/100
//==============Reinvestment or not=======
reinvest=1
if reinvest then
if liveaccountbalance<>10000 then
amount=1
if liveaccountbalance >20000 then
amount=2
if liveaccountbalance >30000 then
amount=3
if liveaccountbalance >40000 then
amount=4
if liveaccountbalance >50000 then
amount=5
if liveaccountbalance >60000 then
amount=6
if liveaccountbalance >70000 then
amount=7
if liveaccountbalance >80000 then
amount=8
if liveaccountbalance >90000 then
amount=9
if liveaccountbalance >100000 then
amount=10
endif
endif
endif
endif
endif
endif
endif
endif
endif
endif
else
amount=1
Endif
//===============money risk management=============================
liveaccountbalance = accountbalance+strategyprofit
moneyrisk = (liveaccountbalance*(riskpercent/100))
if onmarket then
onepointvaluebasket = pointvalue*countofposition
mindistancetoclose =(moneyrisk/onepointvaluebasket)*pipsize
endif
floatingprofit = (((close-positionprice)*pointvalue)*countofposition)/pipsize //actual trade gains
MAfloatingprofit = average[20](floatingprofit)
BBfloatingprofit = MAfloatingprofit - std[20](MAfloatingprofit)*2
//==========floating profit risk reward Trailing check======================
if RR>0 and floatingprofit>moneyrisk*BEF then // Breakeven RRreached 1 at BreakevenFactor% of rr
RRreached=1
if RRreached[1]>RRreached then
RRReached=RRreached[1]
endif
endif
if RR>0 and floatingprofit>moneyrisk*TF1 then//RRreached 2 at Trailingfactor1% of rr
RRreached=2
if RRreached[1]>RRreached then
RRReached=RRreached[1]
endif
endif
if RR>0 and floatingprofit>moneyrisk*RR then//RR Reached
RRreached=3
if RRreached[1]>RRreached then
RRReached=RRreached[1]
endif
endif
//===============LONG CONDITIONS and INDICATOR SECTION======================
longcondition=1
//===========================================================================
//=========================ENTER LONG TRADE==================================
IF Not ONMARKET and auto and tradetime and longcondition then
BUY amount CONTRACT AT MARKET
ENDIF
if NOT ONMARKET and ManuelLong AND close<close[1] AND STRATEGYPROFIT=0 then //for only one trade
BUY amount CONTRACT AT MARKET
ENDIF
// =================case BUY - add Gridorders on LONG trend==============
if longonmarket and close-tradeprice(1)>=gridstep*pipsize then
BUY amount LOT AT MARKET
Endif
//===========================EXIT LONG POSITIONS=============================
if onmarket and RRreached=0 then//-1%
SELL AT positionprice-mindistancetoclose STOP
endif
if onmarket and RRreached=1 then//break even
SELL AT positionprice STOP
endif
if onmarket and RRreached=2 then//Profit RR*0,66
//if floatingprofit <=(positionprice+mindistancetoclose*Tralingfactor1*rr) then
SELL AT positionprice+mindistancetoclose*TF2 STOP
//exit2=positionprice+mindistancetoclose*Trailingfactor1*rr
endif
//endif
if onmarket and RRreached=3 or RRreached=3 and floatingprofit <= BBfloatingprofit then//RR Reached
//if onmarket and RRreached=3 or RRreached=3 and floatingprofit Crosses under BBfloatingprofit then//RR Reached
SELL AT MARKET
//endif
endif
if Time >= 170000 then
SELL AT MARKET
endif
//resetting the risk reward reached variable
if not onmarket then
RRreached = 0
endif
//===============SHORT CONDITIONS and INDICATOR SECTION======================
Shortcondition=1
//===========================================================================
//==========================ENTER SHORT TRADE================================
IF Not ONMARKET and auto and tradetime and Shortcondition then
SELLSHORT amount CONTRACT AT MARKET
ENDIF
if NOT ONMARKET and Manuelshort AND close>close[1] AND STRATEGYPROFIT=0 then //for only one trade
SELLSHORT amount CONTRACT AT MARKET
ENDIF
//===============case SELL- add Gridorders on SHORT trend====================
if shortonmarket and tradeprice(1)-close>=gridstep*pipsize then
SELLSHORT amount LOT AT MARKET
endif
//==================================EXIT SHORT POSITIONS========================================
if onmarket and RRreached=0 then
EXITSHORT AT positionprice-mindistancetoclose STOP
endif
if onmarket and RRreached=1 then//break even
EXITSHORT AT positionprice STOP
endif
if onmarket and RRreached=2 then//Profit RR*0,66
//if floatingprofit <= positionprice+mindistancetoclose*Tralingfactor1*rr then
EXITSHORT AT positionprice+mindistancetoclose*TF2 STOP
//exit3=positionprice+mindistancetoclose*TF2
endif
//endif
if onmarket and RRreached=3 or RRreached=3 and floatingprofit <= BBfloatingprofit then//RR Reached
//if onmarket and RRreached=3 or RRreached=3 and floatingprofit crosses under BBfloatingprofit then//RR Reached
EXITSHORT AT MARKET
endif
//endif
if Time >= 170000 then
EXITSHORT AT MARKET
endif
//resetting the risk reward reached variable
if not onmarket then
RRreached = 0
endif
//=======================================================================================
//Graph gridstep COLOURED(0,0,0) AS "gridstep"
//Graph floatingprofit COLOURED(255,0,0) AS "floatingprofit"
//Graph BBfloatingprofit COLOURED(0,125,0) AS "BBfloatingprofit"
//Graph rrreached COLOURED(0,0,0) AS "RRreached"
//Graph exit3 COLOURED(0,0,0) AS "ExitLong"
//Graph positionprice COLOURED(0,255,0) AS "ExitLong2"
//Graph mindistancetoclose COLOURED(255,255,0) AS "mindistancetoclose"
//==================CFTA-v3.1
//***************Long
c1 = MACD > 0
c2 = ADX[10] > 20
c3 = CCI[10] > 0
DIP = DIplus[10]
DIM = DIminus[10]
c4 = (DIP > DIM[1])
If c1 and c2 and c3 and c4 then
Indicator1=1
Else
Indicator1=0
Endif
//*************Short
c11 = MACD < 0
c12 = ADX[10] > 20
c13 = CCI[10] < 0
DIP = DIplus[10]
DIM = DIminus[10]
c14 = (DIP < DIM[1])
If c11 and c12 and c13 and c14 then
Indicator22=-1
Else
Indicator22=0
Endif
Return indicator1 COLOURED(0,255,0) as "Buy", indicator22 COLOURED(255,0,0) as "Sell"
/// Definition of code parameters
defparam preloadbars =10000
DEFPARAM CumulateOrders = true // Cumulating positions deactivated
MAC= MACD[60,130,45]
CCC= CCI [100]
AAA= ADX [10]
DIP= DIplus[50]
DIM= DIminus[50]
once RRreached = 0
accountbalance = 10000 //account balance in money at strategy start
riskpercent = 1 //whole account risk in percent%
amount = 1 //lot amount to open each trade
rr = 2 //risk reward ratio (set to 0 disable this function)
sd = 0.25 //standard deviation of MA floating profit
//dynamic step grid
minSTEP = 5 //minimal step of the grid
maxSTEP = 30 //maximal step of the grid
ATRcurrentPeriod = 150 //recent volatility 'instant' period
ATRhistoPeriod = 3000 //historical volatility period
ATR = averagetruerange[ATRcurrentPeriod]
histoATR= highest[ATRhistoPeriod](ATR)
resultMAX = MAX(minSTEP*pipsize,histoATR - ATR)
resultMIN = MIN(resultMAX,maxSTEP*pipsize)
gridstep = (resultMIN)
// Conditions to enter long positions
c1 = (MAC > 0)
c2 = (CCC > 0)
c3 = (AAA > 20)
c4 = (DIP > DIM[1])
//first trade whatever condition, for one cycle only add "AND STRATEGYPROFIT=0" on lne 39
if NOT ONMARKET AND HIGHEST[1](c1)=1 AND HIGHEST[1](c2)=1 AND HIGHEST[1](c3)=1 AND HIGHEST[1](c4)=1 then
BUY amount LOT AT MARKET
endif
// case BUY - add orders on the same trend
if longonmarket and close-tradeprice(1)>=gridstep then
BUY amount LOT AT MARKET
endif
//money management
liveaccountbalance = accountbalance+strategyprofit
moneyrisk = (liveaccountbalance*(riskpercent/100))
if onmarket then
onepointvaluebasket = pointvalue*countofposition
mindistancetoclose =(moneyrisk/onepointvaluebasket)*pipsize
endif
//floating profit
floatingprofit = (((close-positionprice)*pointvalue)*countofposition)/pipsize
//actual trade gains
MAfloatingprofit = average[40](floatingprofit)
BBfloatingprofit = MAfloatingprofit - std[40](MAfloatingprofit)*sd
//CFTA cross exit
if LONGONMARKET AND c1=0 or c2=0 or c3=0 or c4=0 then
SELL AT MARKET
endif
//floating profit risk reward check
if rr>0 and floatingprofit>moneyrisk*rr then
RRreached=1
endif
//stoploss trigger when risk reward ratio is not met already
if onmarket and RRreached=0 then
SELL AT positionprice-mindistancetoclose STOP
endif
//stoploss trigger when risk reward ratio has been reached
if onmarket and RRreached=1 then
if floatingprofit crosses under BBfloatingprofit then
SELL AT MARKET
endif
endif
//resetting the risk reward reached variable
if not onmarket then
RRreached = 0
endif
Short;
/// Definition of code parameters
defparam preloadbars =10000
DEFPARAM CumulateOrders = true // Cumulating positions deactivated
MAC= MACD[60,130,45]
CCC= CCI [100]
AAA= ADX [10]
DIP= DIplus[50]
DIM= DIminus[50]
once RRreached = 0
accountbalance = 10000 //account balance in money at strategy start
riskpercent = 2 //whole account risk in percent%
amount = 1 //lot amount to open each trade
rr = 2 //risk reward ratio (set to 0 disable this function)
sd = 0.25 //standard deviation of MA floating profit
//dynamic step grid
minSTEP = 5 //minimal step of the grid
maxSTEP = 30 //maximal step of the grid
ATRcurrentPeriod = 150 //recent volatility 'instant' period
ATRhistoPeriod = 3000 //historical volatility period
ATR = averagetruerange[ATRcurrentPeriod]
histoATR= highest[ATRhistoPeriod](ATR)
resultMAX = MAX(minSTEP*pipsize,histoATR - ATR)
resultMIN = MIN(resultMAX,maxSTEP*pipsize)
gridstep = (resultMIN)
// Conditions to enter short positions
c11 = (MAC < 0)
c12 = (CCC < 0)
c13 = (AAA > 20)
c14= (DIP < DIM[1])
//first trade whatever condition, for only one cycle add "AND STRATEGYPROFIT=0" on line 39
if NOT ONMARKET AND HIGHEST[1](c11)=1 AND HIGHEST[1](c12)=1 AND HIGHEST[1](c13)=1 AND HIGHEST[1](c14)=1 then
SELLSHORT amount LOT AT MARKET
endif
// case SELL - add orders on the same trend
if shortonmarket and tradeprice(1)-close>=gridstep then
SELLSHORT amount LOT AT MARKET
endif
//money management
liveaccountbalance = accountbalance+strategyprofit
moneyrisk = (liveaccountbalance*(riskpercent/100))
if onmarket then
onepointvaluebasket = pointvalue*countofposition
mindistancetoclose =(moneyrisk/onepointvaluebasket)*pipsize
endif
//floating profit
floatingprofit = (((close-positionprice)*pointvalue)*countofposition)/pipsize
//actual trade gains
MAfloatingprofit = average[40](floatingprofit)
BBfloatingprofit = MAfloatingprofit - std[40](MAfloatingprofit)*sd
//CFTA exit
if c11=0 or c12=0 or c13=0 or c14=0 then
EXITSHORT AT MARKET
endif
//floating profit risk reward check
if rr>0 and floatingprofit>moneyrisk*rr then
RRreached=1
endif
//stoploss trigger when risk reward ratio is not met already
if onmarket and RRreached=0 then
EXITSHORT AT positionprice-mindistancetoclose STOP
endif
//stoploss trigger when risk reward ratio has been reached
if onmarket and RRreached=1 then
if floatingprofit crosses under BBfloatingprofit then
EXITSHORT AT MARKET
endif
endif
//resetting the risk reward reached variable
if not onmarket then
RRreached = 0
endif
Long & Short
/// Definition of code parameters
defparam preloadbars =10000
DEFPARAM CumulateOrders = true // Cumulating positions deactivated
MAC= MACD[60,130,45]
CCC= CCI [100]
AAA= ADX [10]
DIP= DIplus[50]
DIM= DIminus[50]
once RRreached = 0
accountbalance = 10000 //account balance in money at strategy start
riskpercent = 1 //whole account risk in percent%
amount = 1 //lot amount to open each trade
rr = 2 //risk reward ratio (set to 0 disable this function)
sd = 0.25 //standard deviation of MA floating profit
//dynamic step grid
minSTEP = 5 //minimal step of the grid
maxSTEP = 30 //maximal step of the grid
ATRcurrentPeriod = 150 //recent volatility 'instant' period
ATRhistoPeriod = 3000 //historical volatility period
ATR = averagetruerange[ATRcurrentPeriod]
histoATR= highest[ATRhistoPeriod](ATR)
resultMAX = MAX(minSTEP*pipsize,histoATR - ATR)
resultMIN = MIN(resultMAX,maxSTEP*pipsize)
gridstep = (resultMIN)
// Conditions to enter long positions
c1 = (MAC > 0)
c2 = (CCC > 0)
c3 = (AAA > 20)
c4 = (DIP > DIM[1])
//first trade whatever condition, for one cycle only add "AND STRATEGYPROFIT=0" on line 39
if NOT ONMARKET AND HIGHEST[1](c1)=1 AND HIGHEST[1](c2)=1 AND HIGHEST[1](c3)=1 AND HIGHEST[1](c4)=1 then
BUY amount LOT AT MARKET
endif
// case BUY - add orders on the same trend
if longonmarket and close-tradeprice(1)>=gridstep then
BUY amount LOT AT MARKET
endif
// Conditions to enter short positions
c11 = (MAC < 0)
c12 = (CCC < 0)
c13 = (AAA > 20)
c14= (DIP < DIM[1])
//first trade whatever condition, for only one cycle add "AND STRATEGYPROFIT=0" on line 55
if NOT ONMARKET AND HIGHEST[1](c11)=1 AND HIGHEST[1](c12)=1 AND HIGHEST[1](c13)=1 AND HIGHEST[1](c14)=1 then
SELLSHORT amount LOT AT MARKET
endif
// case SELL - add orders on the same trend
if shortonmarket and tradeprice(1)-close>=gridstep then
SELLSHORT amount LOT AT MARKET
endif
//money management
liveaccountbalance = accountbalance+strategyprofit
moneyrisk = (liveaccountbalance*(riskpercent/100))
if onmarket then
onepointvaluebasket = pointvalue*countofposition
mindistancetoclose =(moneyrisk/onepointvaluebasket)*pipsize
endif
//floating profit
floatingprofit = (((close-positionprice)*pointvalue)*countofposition)/pipsize
//actual trade gains
MAfloatingprofit = average[40](floatingprofit)
BBfloatingprofit = MAfloatingprofit - std[40](MAfloatingprofit)*sd
//CFTA cross exit
if LONGONMARKET AND c1=0 or c2=0 or c3=0 or c4=0 then
SELL AT MARKET
endif
//floating profit risk reward check
if rr>0 and floatingprofit>moneyrisk*rr then
RRreached=1
endif
//stoploss trigger when risk reward ratio is not met already
if onmarket and RRreached=0 then
SELL AT positionprice-mindistancetoclose STOP
endif
//stoploss trigger when risk reward ratio has been reached
if onmarket and RRreached=1 then
if floatingprofit crosses under BBfloatingprofit then
SELL AT MARKET
endif
endif
//CFTA exit
if c11=0 or c12=0 or c13=0 or c14=0 then
EXITSHORT AT MARKET
endif
//floating profit risk reward check
if rr>0 and floatingprofit>moneyrisk*rr then
RRreached=1
endif
//stoploss trigger when risk reward ratio is not met already
if onmarket and RRreached=0 then
EXITSHORT AT positionprice-mindistancetoclose STOP
endif
//stoploss trigger when risk reward ratio has been reached
if onmarket and RRreached=1 then
if floatingprofit crosses under BBfloatingprofit then
EXITSHORT AT MARKET
endif
endif
//resetting the risk reward reached variable
if not onmarket then
RRreached = 0
endif
The latest indicator version without multiplier;
/// Definition of code parameters
defparam preloadbars =10000
DEFPARAM CumulateOrders = true // Cumulating positions deactivated
MAC= MACD[60,130,45]
CCC= CCI [100]
AAA= ADX [10]
DIP= DIplus[50]
DIM= DIminus[50]
once RRreached = 0
accountbalance = 10000 //account balance in money at strategy start
riskpercent = 1 //whole account risk in percent%
amount = 1 //lot amount to open each trade
rr = 2 //risk reward ratio (set to 0 disable this function)
sd = 0.25 //standard deviation of MA floating profit
//dynamic step grid
minSTEP = 10 //minimal step of the grid
maxSTEP = 50 //maximal step of the grid
ATRcurrentPeriod = 150 //recent volatility 'instant' period
ATRhistoPeriod = 3000 //historical volatility period
ATR = averagetruerange[ATRcurrentPeriod]
histoATR= highest[ATRhistoPeriod](ATR)
resultMAX = MAX(minSTEP*pipsize,histoATR - ATR)
resultMIN = MIN(resultMAX,maxSTEP*pipsize)
gridstep = (resultMIN)
// Conditions to enter long positions
c1 = (MAC > 0)
c2 = (CCC > 0)
c3 = (AAA > 20)
c4 = (DIP > DIM[1])
//first trade whatever condition, for one cycle only add "AND STRATEGYPROFIT=0" on line 39
if NOT ONMARKET AND HIGHEST[1](c1)=1 AND HIGHEST[1](c2)=1 AND HIGHEST[1](c3)=1 AND HIGHEST[1](c4)=1 then
BUY amount LOT AT MARKET
endif
// case BUY - add orders on the same trend
if longonmarket and close-tradeprice(1)>=gridstep then
BUY amount LOT AT MARKET
endif
// Conditions to enter short positions
c11 = (MAC < 0)
c12 = (CCC < 0)
c13 = (AAA > 20)
c14= (DIP < DIM[1])
//first trade whatever condition, for only one cycle add "AND STRATEGYPROFIT=0" on line 55
if NOT ONMARKET AND HIGHEST[1](c11)=1 AND HIGHEST[1](c12)=1 AND HIGHEST[1](c13)=1 AND HIGHEST[1](c14)=1 then
SELLSHORT amount LOT AT MARKET
endif
// case SELL - add orders on the same trend
if shortonmarket and tradeprice(1)-close>=gridstep then
SELLSHORT amount LOT AT MARKET
endif
//money management
liveaccountbalance = accountbalance+strategyprofit
moneyrisk = (liveaccountbalance*(riskpercent/100))
if onmarket then
onepointvaluebasket = pointvalue*countofposition
mindistancetoclose =(moneyrisk/onepointvaluebasket)*pipsize
endif
//floating profit
floatingprofit = (((close-positionprice)*pointvalue)*countofposition)/pipsize
//actual trade gains
MAfloatingprofit = average[40](floatingprofit)
BBfloatingprofit = MAfloatingprofit - std[40](MAfloatingprofit)*sd
//CFTA cross exit
if LONGONMARKET AND c1=0 or c2=0 or c3=0 or c4=0 then
SELL AT MARKET
endif
//floating profit risk reward check
if rr>0 and floatingprofit>moneyrisk*rr then
RRreached=1
endif
//stoploss trigger when risk reward ratio is not met already
if onmarket and RRreached=0 then
SELL AT positionprice-mindistancetoclose STOP
endif
//stoploss trigger when risk reward ratio has been reached
if onmarket and RRreached=1 then
if floatingprofit crosses under BBfloatingprofit then
SELL AT MARKET
endif
endif
//CFTA exit
if c11=0 or c12=0 or c13=0 or c14=0 then
EXITSHORT AT MARKET
endif
//floating profit risk reward check
if rr>0 and floatingprofit>moneyrisk*rr then
RRreached=1
endif
//stoploss trigger when risk reward ratio is not met already
if onmarket and RRreached=0 then
EXITSHORT AT positionprice-mindistancetoclose STOP
endif
//stoploss trigger when risk reward ratio has been reached
if onmarket and RRreached=1 then
if floatingprofit crosses under BBfloatingprofit then
EXITSHORT AT MARKET
endif
endif
//resetting the risk reward reached variable
if not onmarket then
RRreached = 0
endif
And with multiplier;
//==================CFTA-v3.1
Indicator1=0
//***************Long
c1 = MACD[60,130,45] > 0
c2 = ADX[50] > 10
c3 = CCI[100] > 0
DIP = DIplus[50]
DIM = DIminus[50]
c4 = (DIP > DIM[1])
//*************Short
c11 = MACD[60,130,45] < 0
c12 = ADX[50] > 10
c13 = CCI[100] < 0
DIP = DIplus[50]
DIM = DIminus[50]
c14 = (DIP < DIM[1])
If c1 and c2 and c3 and c4 then
Indicator1=1
Endif
If c11 and c12 and c13 and c14 then
Indicator1=-1
Endif
Return indicator1
Remember the grid step and ATR settings are at the discretion of each trader and may be adapted to different markets depending on volatility.
I hope you will enjoy it and give some feedback on possible improvements 🙂
//Heikin-Ash
xClose = (Open+High+Low+Close)/4
if(barindex>2) then
xOpen = (xOpen[1] + xClose[1])/2
endif
changeToGreencandle = xClose>xOpen AND xClose[1]<xOpen[1]
ChangeToRedcandle = xClose<xOpen AND xClose[1]>xOpen[1]
If changeToGreencandle then
Indicator1=1
Else
Indicator1=0
Endif
If ChangeToRedcandle then
Indicator22=-1
Else
Indicator22=0
Endif
Return indicator1 COLOURED(0,255,0) as "changeToGreencandle", indicator22 COLOURED(255,0,0) as "ChangeToRedcandle"
//www.prorealcode.com/topic/grid-orders-with-one-combined-stop-loss-and-limit-can-it-be-done/page/9/
//-------------------------------------------------------------------------
// Main code : Grid Long BB w/Ep
//-------------------------------------------------------------------------
//Open a single grid trade upon new candle
defparam preloadbars = 100
once RRreached = 0
//parameters
accountbalance = 10000 //account balance in USD
riskpercent = 2 //whole account risk in percent%
gridstep = 10 //grid step in point
MaxPosition = 5 //max position to open
amount = 1 //lot amount to open each trade
rr = 2.5 //risk reward ratio (0 - disable)
ep = 100000 //open position if close < ep
//first trade
//if NOT ONMARKET AND close>close[1] AND STRATEGYPROFIT=0 then
if NOT ONMARKET AND close<ep AND STRATEGYPROFIT=0 then
BUY amount LOT AT MARKET
endif
// case BUY - add orders on the same trend
if longonmarket and close-tradeprice(1)>=gridstep*pipsize And CountOfPosition < MaxPosition then
BUY amount LOT AT MARKET
endif
//money management
liveaccountbalance = accountbalance+strategyprofit
moneyrisk = (liveaccountbalance*(riskpercent/100))
if onmarket then
onepointvaluebasket = pointvalue*countofposition
mindistancetoclose =(moneyrisk/onepointvaluebasket)*pipsize
endif
//floating profit
floatingprofit = (((close-positionprice)*pointvalue)*countofposition)/pipsize //actual trade gains
MAfloatingprofit = average[20](floatingprofit)
BBfloatingprofit = MAfloatingprofit - std[20](MAfloatingprofit)*2
//floating profit risk reward check
if rr>0 and floatingprofit>moneyrisk*rr then
RRreached=1
endif
//stoploss trigger when risk reward ratio is not met already
if onmarket and RRreached=0 then
SELL AT positionprice-mindistancetoclose STOP
endif
//stoploss trigger when risk reward ratio has been reached
if onmarket and RRreached=1 then
if floatingprofit crosses under BBfloatingprofit then
SELL AT MARKET
endif
SELL AT positionprice-mindistancetoclose STOP
endif
//resetting the risk reward reached variable
if not onmarket then
RRreached = 0
endif
Grid orders with one combined stop loss and limit, can it be done?
This topic contains 307 replies,
has 1 voice, and was last updated by OtherAttorney
1 year, 9 months ago.
| Forum: | ProOrder: Automated Strategies & Backtesting |
| Language: | English |
| Started: | 04/14/2016 |
| Status: | Active |
| Attachments: | 106 files |
The information collected on this form is stored in a computer file by ProRealCode to create and access your ProRealCode profile. This data is kept in a secure database for the duration of the member's membership. They will be kept as long as you use our services and will be automatically deleted after 3 years of inactivity. Your personal data is used to create your private profile on ProRealCode. This data is maintained by SAS ProRealCode, 407 rue Freycinet, 59151 Arleux, France. If you subscribe to our newsletters, your email address is provided to our service provider "MailChimp" located in the United States, with whom we have signed a confidentiality agreement. This company is also compliant with the EU/Swiss Privacy Shield, and the GDPR. For any request for correction or deletion concerning your data, you can directly contact the ProRealCode team by email at privacy@prorealcode.com If you would like to lodge a complaint regarding the use of your personal data, you can contact your data protection supervisory authority.