ProRealCode - Trading & Coding with ProRealTime™
//Heikin-Ashi
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]
Greencandle=xclose>xopen
redcandle=xclose<xopen
If changeToGreencandle then
Indicator1=1
elsif greencandle then
indicator1=0.5
Else
Indicator1=0
Endif
If ChangeToRedcandle then
Indicator22=-1
elsif redcandle then
indicator22=-0.5
Else
Indicator22=0
Endif
Return indicator1 COLOURED(0,255,0) as "Buy", indicator22 COLOURED(255,0,0) as "Sell"
🙂
Grid orders with one combined stop loss and limit, can it be done?I don’t recommend this system for beginners but everyone is free to use it but make sure you do your homework on it and pay attention to if cumulative orders are allowed or not. For all other followers I’m currently working on an alternate entry method and possibly another CTFTA indicator to facilitate setting alerts for setups.
//Heikin-Ashi
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]
Greencandle=xclose>xopen
redcandle=xclose<xopen
If changeToGreencandle then
Indicator1=1
elsif greencandle then
indicator1=0.5
elsif Changetoredcandle then
indicator1=-1
elsif redcandle then
indicator1=-0.5
Else
Indicator1=0
Endif
Return indicator1
Long;
/// Definition of code parameters
defparam preloadbars =10000
DEFPARAM CumulateOrders = true // Cumulating positions deactivated
AAA= ADX [25]
DIM= DI[25]
HAC= call "HA Change"
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 = 1 //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 = (AAA > 10)
c2 = (DIM > 0)
c3 = (HAC = 1)
//first trade whatever condition, for one cycle only add "AND STRATEGYPROFIT=0" on line 39.
if NOT ONMARKET AND (c1)=1 AND (c2)=1 AND (c3)=1 AND close>close[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[20](floatingprofit)
BBfloatingprofit = MAfloatingprofit - std[20](MAfloatingprofit)*sd
//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
c4 = (HAC = -1)
if onmarket and (c4)=1 then
SELL AT MARKET
endif
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
AAA= ADX [25]
DIM= DI[25]
HAC= call "HA Change"
once RRreached = 0
accountbalance = 100000 //account balance in money at strategy start
riskpercent = 1 //whole account risk in percent%
amount = 1 //lot amount to open each trade
rr = 1 //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 = (AAA > 10)
c12 = (DIM < 0)
c13 = (HAC = -1)
//first trade whatever condition, for only one cycle add "AND STRATEGYPROFIT=0" on line 39.
if NOT ONMARKET AND (c11)=1 AND (c12)=1 AND (c13)=1 AND close<close[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[200](floatingprofit)
BBfloatingprofit = MAfloatingprofit - std[200](MAfloatingprofit)//*sd
//floating profit risk reward check
if rr>0 and floatingprofit>moneyrisk*rr then
RRreached=1
endif
c14 = (HAC > 0)
if c14 then
EXITSHORT AT MARKET
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
I hope you will enjoy it and find room for improvements 🙂
Hey traders,
Long time no speak, I have been waiting for MTF to become available but it seem to take longer and longer.
However here is an early christmas gift. The below update is fairly useful on low-medium timframes such as M15 but when using aggregating orders (of course we can stick with single orders too) we need to run it on low TF such as M1 or lower to be able to rely on our SL or BB exit since it kicks in at close of candle and too much can happen during a M15 candle and cause much larger losses during the 15 minutes it takes for the candle too close hence I recommend setting a low maximum number of orders and monitor the system manually while it is up and running.
The update is based on Squeeze (from BB Squeeze but only the squeeze function and not the momentum) and Heiken Ashi. Squeeze need to be off (blue bar) and Heiken Ashi decide the direction of the trade either by change of color or by the current color when Squeeze changes to off (for change of color “HAC crosses over 0” and for current direction “HAC >0” for long) on line 31.
First the required indicators, HA Change;
//Heikin-Ashi
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]
Greencandle=xclose>xopen
redcandle=xclose<xopen
If changeToGreencandle then
Indicator1=1
elsif greencandle then
indicator1=0.5
elsif Changetoredcandle then
indicator1=-1
elsif redcandle then
indicator1=-0.5
Else
Indicator1=0
Endif
Return indicator1
And Squeeze;
// Bollinger bands
source = close
bbMa = Average[bbLength](source)
dev = bbMult * STD[bbLength](source)
bbUpper = bbMa + dev
bbLower = bbMa - dev
// Keltner channels
kcMa = Average[kcLength](source)
kcRange = TR(source)
kcMaRange = Average[kcLength](kcRange)
kcUpper = kcMa + kcMaRange * kcMult
kcLower = kcMa - kcMaRange * kcMult
// Squeeze indicator
if bbLower > kcLower and bbUpper < kcUpper then
// Squeeze on
indicator1=-1
else
// Squeeze off
indicator1=1
endif
return indicator1
The strategy for longs;
/// Definition of code parameters
defparam preloadbars =10000
DEFPARAM CumulateOrders = true // Cumulating positions deactivated
SQ= call "Squeeze"[12, 1.5, 12, 1]
HAC= call "HA Change"
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 = 1 //risk reward ratio (set to 0 disable this function)
sd = 0.25 //standard deviation of MA floating profit
//dynamic step grid
minSTEP = 20 //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 = (SQ > 0)
c2 = (HAC CROSSES OVER 0)
//first trade whatever condition, for one cycle only add "AND STRATEGYPROFIT=0" on lne 34
if NOT ONMARKET AND (c1)=1 AND (c2)=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[20](floatingprofit)
BBfloatingprofit = MAfloatingprofit - std[20](MAfloatingprofit)*sd
//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
And shorts;
/// Definition of code parameters
defparam preloadbars =10000
DEFPARAM CumulateOrders = true // Cumulating positions deactivated
SQ= call "Squeeze"[12, 1.5, 12, 1]
HAC= call "HA Change"
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 = 20 //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 short positions
c11 = (SQ > 0)
c12 = (HAC CROSSES UNDER 0)
//first trade whatever condition, for only one cycle add "AND STRATEGYPROFIT=0" on line 34
if NOT ONMARKET AND (c11)=1 AND (c12)=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[20](floatingprofit)
BBfloatingprofit = MAfloatingprofit - std[20](MAfloatingprofit)*sd
//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
Remember to be mindful of line 34 to set or avoid recurring cycles, recurring is great for testing but risky for live trading, also note to increase most parameters if using on very low TF.
I hope you will find it useful 🙂
//BREAKOUT////////////////////////////////////////////////////////////////////////
K=highest[X](close) //IC*0.25
//FÖRVÄNTAD DAGLIG RÖRELSE (PROCENT) = INKÖP/STOP////////////////////////
DP=0.007
//AVSTÅND INKÖP MATTA / STOP////////////////////////////////////////////////
IC=((CLOSE*DP)/4)
BOMHAST=IC
//-------------------------------------------------------------------------
//graph close as "close"
//GRAPH K AS "BREAKOUT"
//GRAPH START AS "START"
//GRAPH IC AS "NORMALSPANN"
//GRAPH floatingprofit as "floating profit"
//GRAPH BBfloatingprofit as "BB Floating Profit"
//-------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////////////
// Conditions to enter long positions
////////////////////////////////////////////////////////////////////////////////////////
//TREND INDI////////////////////////////////////////////////////////////////////////
TFC=1
indicator1 = dema[18*TFC](ROC[13*TFC](ExponentialAverage[1*TFC]))
indicator2 = dema[18*TFC](ROC[13*TFC](ExponentialAverage[1*TFC]))
c1 = (indicator1 > indicator2[1])
//SET STOP ORDER INDI////////////////////////////////////////////////////////////////////////
TFC2=15
indicator10 = dema[20*TFC2](ROC[25*TFC2](ExponentialAverage[1*TFC2]))
indicator20 = dema[20*TFC2](ROC[25*TFC2](ExponentialAverage[1*TFC2]))
c10 = (indicator10 > indicator20[1])
//c20 = (indicator10[1] < indicator20[2]) AND C20
IF c1 AND C10 AND NOT ONMARKET THEN
START=K
BUY 1 CONTRACT AT K STOP
BUY 1 CONTRACT AT CLOSE+K+BOMHAST STOP
BUY 2 CONTRACT AT CLOSE+K+(BOMHAST*2) STOP
//BUY 4 CONTRACT AT START+(BOMHAST*3) STOP
ENDIF
IF c1 AND COUNTOFPOSITION=1 THEN
BUY 1 CONTRACT AT START+BOMHAST STOP
BUY 2 CONTRACT AT START+(BOMHAST*2) STOP
//BUY 4 CONTRACT AT START+(BOMHAST*3) STOP
ENDIF
IF c1 AND COUNTOFPOSITION=2 THEN
BUY 2 CONTRACT AT START+(BOMHAST*2) STOP
//BUY 4 CONTRACT AT START+(BOMHAST*3) STOP
ENDIF
//IF c1 AND COUNTOFPOSITION=4 THEN
//BUY 4 CONTRACT AT START+(BOMHAST*3) STOP
//ENDIF
////////////////////////////////////////////////////////////////////////////////////////
// SAFETY STOPS MATTA
////////////////////////////////////////////////////////////////////////////////////////
// Stops and targets
if countofposition=1 then
SELL AT START-BOMHAST STOP
ENDIF
if countofposition=2 then
SELL AT START STOP
ENDIF
if countofposition=4 then
SELL AT START+BOMHAST STOP
ENDIF
//if countofposition=8 then
//SELL AT START+(BOMHAST*2) STOP
//ENDIF
floatingprofit = (((close-positionprice)*pointvalue)*countofposition)/pipsize
//actual trade gains
MAfloatingprofit = average[20](floatingprofit) //[ONMARKET]
BBfloatingprofit = MAfloatingprofit - std[20](MAfloatingprofit)*1.8//0.25
FPP= (BBfloatingprofit)/((pointvalue*countofposition/pipsize))+positionprice
if onmarket then
if FPP<CLOSE then
SELL AT FPP STOP
endif
ENDIF
GRAPH FPP AS "floatingprofit stop FPP"
//GRAPH floatingprofit AS "floatingprofit"
GRAPH CLOSE AS "Close"
Describe what this topic is trying to build, in plain English, and ProRealAI writes the ProRealTime™ indicator, screener or system for you.
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
2 years, 6 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.