ProRealCode - Trading & Coding with ProRealTime™
@Henrik, the entry parameters in your code looks interesting indeed, can you explain a bit more about them and perhaps post a pic of a chart showing entries? I tried to create a combined indicator of the entry parameters to get a better visual of valid entries but it is a bit tricky.
Regarding the carpet bombing method of doubling up on entries I am somewhat sceptical mostly because of stacking entries and doubling the position count gives a lower tolerance for retracements after entry and increase the risk of being stopped out.
Hi Cfta!
sorry for late response while im taking some time off. My codes are all inside bar instruction to make entry and exit insidebar with stop and limitorders for buy and profit secure exit. Then you can use bigger timeframe for signals. This is a way to get around prts limitations.
Happy New Year
Henrik
Hi all,
I have been observing the development of your system with great interest CTFA and I congratulate you with your work.
Your latest ‘long’ creation looks quite profitable on a 5 min timeframe in backtest and in walk forward analysis.
I took the liberty of fiddling around a bit with the HA and SQ cut-off value and also with the min max step. It is yet a little bit more profitable now.
The ideal situation would be to combine the long and the short version….
Congrats and Happy New Year,
Glen
Theoretically a better way to accumulate new orders would be to only add on pullbacks.
This would allow for better average entries and solve the problem of being stopped out on minor pullbacks.
Basically you still use the grid system to accumulate orders but only add after a pullback of x pips from the last high or low.
Since markets move in waves this should allow for better average entries.
Also instead of keeping the stop at the 1% risk level as you accumulate orders, rather use break-even after the initial 1% risk.
Taking risk of the table after the 1st ‘TopUp’ would give much better peace of mind.
Hi Henrik,
This sounds amazing to be able to circumvent the PRT limitations like this, at first I did not realize it was inside bar calculations. I have had some time off to and will go on a holiday tomorrow but I will look into it more once I’m back in a couple of weeks.
Juanj, only adding on pullbacks would end missing out on the fattest profit due to not adding on when price takes off fast in one direction and the pullback occurs first when the majority of the move is over. There is already a break even feature in the BB exit but it also let us keep a bit more profit.
@Henrik, finally I’m back with time for trading and developement, I have been looking into your code and also forward testing it and I think it looks very promising.
Do you think it will be possible to include entry based on HA Change and Squeeze? I have been trying to code it myself but can’t seem to figure out how to code it as an inside bar calculaction, I also had some issues with divided by zero errors. I think Squeeze in particular is very important to reduce entries during ranging and sideways movement.
It would be great if you could look into it since inside bar calculations create amazing opportunities for us 🙂
Hi Cfta!
The differens is that you set your buy and exit orders with stop and limit orders. The indicators are still limited to the timeframe you choose. I dont have any time right now but i can se if i can build in this stoporderway into your code.
Regards Henrik
Hello Henrik,
Okay that makes sense. No worries I look forward to your feedback when you have time to look into it.
I made some slight adjustments to your code by translating some of the phrases so it will be easier for our friends here to use and improve. I also tried to reverse the code to have a version for shorts but have not managed yet since it enters into 4 contracts right away.
Long which is working as intended;
//BREAKOUT////////////////////////////////////////////////////////////////////////
K=highest[1](close) //IC*0.25
//EXPECTED DAILY RANGE (PERCANTAGE) = ENTRY/STOP////////////////////////
DP=0.007
//GRID STEP DISTANCE/ STOP////////////////////////////////////////////////
IC=((CLOSE*DP)/4)
GRIDSTEP=IC
//-------------------------------------------------------------------------
//graph close as "close"
//GRAPH K AS "BREAKOUT"
//GRAPH START AS "START"
//GRAPH IC AS "AVERAGE RANGE"
//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+GRIDSTEP STOP
BUY 2 CONTRACT AT CLOSE+K+(GRIDSTEP*2) STOP
//BUY 4 CONTRACT AT START+(GRIDSTEP*3) STOP
ENDIF
IF c1 AND COUNTOFPOSITION=1 THEN
BUY 1 CONTRACT AT START+GRIDSTEP STOP
BUY 2 CONTRACT AT START+(GRIDSTEP*2) STOP
//BUY 4 CONTRACT AT START+(GRIDSTEP*3) STOP
ENDIF
IF c1 AND COUNTOFPOSITION=2 THEN
BUY 2 CONTRACT AT START+(GRIDSTEP*2) STOP
//BUY 4 CONTRACT AT START+(GRIDSTEP*3) STOP
ENDIF
//IF c1 AND COUNTOFPOSITION=4 THEN
//BUY 4 CONTRACT AT START+(GRIDSTEP*3) STOP
//ENDIF
////////////////////////////////////////////////////////////////////////////////////////
// SAFETY STOPS MATTA
////////////////////////////////////////////////////////////////////////////////////////
// Stops and targets
if countofposition=1 then
SELL AT START-GRIDSTEP STOP
ENDIF
if countofposition=2 then
SELL AT START STOP
ENDIF
if countofposition=4 then
SELL AT START+GRIDSTEP STOP
ENDIF
//if countofposition=8 then
//SELL AT START+(GRIDSTEP*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"
Short version which is not working properly yet;
//BREAKOUT////////////////////////////////////////////////////////////////////////
K=highest[1](close) //IC*0.25
//EXPECTED DAILY RANGE (PERCANTAGE) = ENTRY/STOP////////////////////////
DP=0.007
//GRID STEP DISTANCE/ STOP////////////////////////////////////////////////
IC=((CLOSE*DP)/4)
GRIDSTEP=IC
//-------------------------------------------------------------------------
//graph close as "close"
//GRAPH K AS "BREAKOUT"
//GRAPH START AS "START"
//GRAPH IC AS "AVERAGE RANGE"
//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
SELLSHORT 1 CONTRACT AT K STOP
SELLSHORT 1 CONTRACT AT CLOSE+K-GRIDSTEP STOP
SELLSHORT 2 CONTRACT AT CLOSE+K-(GRIDSTEP*2) STOP
//BUY 4 CONTRACT AT START+(GRIDSTEP*3) STOP
ENDIF
IF c1 AND COUNTOFPOSITION=1 THEN
SELLSHORT 1 CONTRACT AT START-GRIDSTEP STOP
SELLSHORT 2 CONTRACT AT START-(GRIDSTEP*2) STOP
//BUY 4 CONTRACT AT START+(GRIDSTEP*3) STOP
ENDIF
IF c1 AND COUNTOFPOSITION=2 THEN
SELLSHORT 2 CONTRACT AT START-(GRIDSTEP*2) STOP
//BUY 4 CONTRACT AT START+(GRIDSTEP*3) STOP
ENDIF
//IF c1 AND COUNTOFPOSITION=4 THEN
//BUY 4 CONTRACT AT START+(GRIDSTEP*3) STOP
//ENDIF
////////////////////////////////////////////////////////////////////////////////////////
// SAFETY STOPS MATTA
////////////////////////////////////////////////////////////////////////////////////////
// Stops and targets
if countofposition=1 then
EXITSHORT AT START+GRIDSTEP STOP
ENDIF
if countofposition=2 then
EXITSHORT AT START STOP
ENDIF
if countofposition=4 then
EXITSHORT AT START-GRIDSTEP STOP
ENDIF
//if countofposition=8 then
//SELL AT START+(GRIDSTEP*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
EXITSHORT AT FPP STOP
endif
ENDIF
GRAPH FPP AS "floatingprofit stop FPP"
//GRAPH floatingprofit AS "floatingprofit"
GRAPH CLOSE AS "Close"
@Henrik, I have been to trying to develope your code further and also run it live for testing but I keep getting the divided by zero error and has not managed to fix or work around it. Even if you are a little short on time do you think you could take a look to see if you can figure out the divided by zero problem so we can forward test the current code?
Everyone with decent coding skills is welcome to help, with real time stops and grid entries the system has huge potential 🙂
Hi Cfta!
Nice to here that you are looking in to it. I’ll try to clean up my stopporder version so that we get rid of the dbz error ! This one was only long and I havnt tried for shorts. But I’ll look into it.
Can you share the ha and squeeze indicators as itf:s so that we have the same material.
Regards
Henrik
The division by zero error is, I think, coming from the FPP variable calculation, if you are not on market, countofposition is null. You should only do this calculation if you are ONMARKET.
Hello fellows,
Thanks for the fast replies, here are the itf. files. The Squeeze is the more important one, the HA Change indicator is very useful but I think we can get rid of a lot of false signals if we replace it with Heiken Ashi Smoothed instead, if you think we can include it in the code. Maybe we can use this version Nicolas has made already (though we don’t need the bar painting function which I guess requires more calculations, it would be sufficient if it returns a 1 or -1 value but let’s use whatever is easier to code);
//PRC_HPT Heikin Ashi Smoothed | indicator
//25.04.2017
//Nicolas @ www.prorealcode.com
//Sharing ProRealTime knowledge
//translated from MT4 indicator code
//---settings
//MaPeriod=6
//MaPeriod2=2
//---end of settings
once maOpen=Open
once maClose=Close
once maLow=Low
once maHigh=High
if barindex>0 then
maOpen=(maOpen[1]*(MAperiod-1)+Open)/MAPeriod
maClose=(maClose[1]*(MAperiod-1)+Close)/MAPeriod
maLow=(maLow[1]*(MAperiod-1)+Low)/MAPeriod
maHigh=(maHigh[1]*(MAperiod-1)+High)/MAPeriod
haOpen=(ExtMapBuffer5[1]+ExtMapBuffer6[1])/2
haClose=(maOpen+maHigh+maLow+maClose)/4
haHigh=Max(maHigh, Max(haOpen, haClose))
haLow=Min(maLow, Min(haOpen, haClose))
if (haOpen<haClose) then
r=0
g=191
b=255
ExtMapBuffer7=haLow
ExtMapBuffer8=haHigh
else
r=255
g=10
b=0
ExtMapBuffer7=haHigh
ExtMapBuffer8=haLow
endif
ExtMapBuffer5=haOpen
ExtMapBuffer6=haClose
ExtMapBuffer1=weightedaverage[MAperiod2](ExtMapBuffer7)
ExtMapBuffer2=weightedaverage[MAperiod2](ExtMapBuffer8)
ExtMapBuffer3=weightedaverage[MAperiod2](ExtMapBuffer5)
ExtMapBuffer4=weightedaverage[MAperiod2](ExtMapBuffer6)
endif
DRAWCANDLE(ExtMapBuffer3,ExtMapBuffer2,ExtMapBuffer1,ExtMapBuffer4) coloured(r,g,b)
short = ExtMapBuffer7[1]>ExtMapBuffer8[1] and ExtMapBuffer7[2]<ExtMapBuffer8[2] and ExtMapBuffer7[0]>ExtMapBuffer8[0]
long = ExtMapBuffer7[1]<ExtMapBuffer8[1] and ExtMapBuffer7[2]>ExtMapBuffer8[2] and ExtMapBuffer7[0]<ExtMapBuffer8[0]
RETURN long as "long signal", short as "short signal"
Hi fellows,
I have been developing the strategy further and made some progress thanks to the new marvelous Heiken Ashi of higher timeframes indicator developed by Nicolas;
The below code is intended to be run M1 TF. The HA Change indicator has been posted here on the last couple of pages. Squeeze has been substituted for Bollinger Bandwidth which is a default indicator on PRT, it has a 20 period SMA on it.
Conditions to go long are; HA Change and HA of higher TF (1) to be above zero and for BBW to be above its SMA and for the current period to be above the previous period.
Conditions to go short are opposite for HA Change and HA of higher TF but the same for BBW and its SMA.
Also note that I have set new conditions for adding on to a trade, “countofposition<3” on line 41, of course this can be removed or set at each traders discretion. BBW to be above its SMA and for the current period to be above the previous period.
Exit is set to when HA of higher TF crosses zero. This seem to have nullified the BB Exit, I would like to have both in place but can’t figure out how to code it, advice is welcome.
If Nicolas or any other skilled coder find time and manages to adapt the HA of higher TF for minutes we can easily make this strategy even better, for instance running it on M1 and use HA of higher TF for H1 as filter and M15 for entries and exits.
PS. We are only one month from this thread’s 2 year anniversary 🙂
Long;
/// Definition of code parameters
defparam preloadbars =10000
DEFPARAM CumulateOrders = true // Cumulating positions deactivated
HAC= call "HA Change"
HAH= call "PRC_Heikin Ashi higher TF"[1]
BollBW= 3*std[20](close)/WilderAverage[20](close)
BollSMA= WilderAverage[20](3*std[20](close)/WilderAverage[20](close))
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.1 //standard deviation of MA floating profit
//dynamic step grid
minSTEP = 30 //minimal step of the grid
maxSTEP = 100 //maximal step of the grid
ATRcurrentPeriod = 300 //recent volatility 'instant' period
ATRhistoPeriod = 6000 //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. Add "AND STRATEGYPROFIT=0" on line 40 for one cycle only.
c1 = (HAC > 0)
c2 = (HAH > 0)
c3 = (BollBW > BollSMA)
c4 = (BollBW > BollBW[1])
IF NOT ONMARKET AND c1 AND c2 AND c3 AND c4 THEN
BUY 1 LOT AT MARKET
ENDIF
// case BUY - add orders on the same trend
if longonmarket and c3 and c4 and countofposition<3 and close-tradeprice(1)>=gridstep then
BUY amount LOT AT MARKET
endif
// Conditions to exit long positions
c5 = (HAH[1] CROSSES UNDER 0)
IF c5 THEN
SELL 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
//GRAPH floatingprofit as "float"
//GRAPH RRreached as "rr"
//GRAPH floatingprofit as "floating profit"
//GRAPH BBfloatingprofit coloured(255,0,0) as "BB Floating Profit"
//GRAPH positionprice-mindistancetoclose
//GRAPH moneyrisk
//GRAPH onepointvaluebasket
//GRAPH gridstep
Short;
/// Definition of code parameters
defparam preloadbars =10000
DEFPARAM CumulateOrders = true // Cumulating positions deactivated
HAC= call "HA Change"
HAH= call "PRC_Heikin Ashi higher TF"[1]
BollBW= 3*std[20](close)/WilderAverage[20](close)
BollSMA= WilderAverage[20](3*std[20](close)/WilderAverage[20](close))
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.1 //standard deviation of MA floating profit
//dynamic step grid
minSTEP = 25 //minimal step of the grid
maxSTEP = 100 //maximal step of the grid
ATRcurrentPeriod = 300 //recent volatility 'instant' period
ATRhistoPeriod = 6000 //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 short positions. Add "AND STRATEGYPROFIT=0" on line 40 for one cycle only.
c1 = (HAC < 0)
c2 = (HAH < 0)
c3 = (BollBW > BollSMA)
c4 = (BollBW > BollBW[1])
IF NOT ONMARKET AND c1 AND c2 AND c3 AND c4 THEN
SELLSHORT 1 LOT AT MARKET
ENDIF
// case sell add orders on the same trend
if shortonmarket and c3 and c4 and countofposition<3 and close-tradeprice(1)>=gridstep then
SELLSHORT amount LOT AT MARKET
endif
// Conditions to exit short positions
c5 = (HAH[1] CROSSES OVER 0)
IF c5 THEN
EXITSHORT 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
//GRAPH floatingprofit as "float"
//GRAPH RRreached as "rr"
GRAPH floatingprofit as "floating profit"
GRAPH BBfloatingprofit coloured(255,0,0) as "BB Floating Profit"
//GRAPH positionprice-mindistancetoclose
//GRAPH moneyrisk
//GRAPH onepointvaluebasket
//GRAPH gridstep
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.