Placing order when HullMA changes from red to green
11/07/2019 at 7:56 PM #112368
I am testing a few simple strategies, one of them being to buy when the Hull moving average changes from red to green. This has been addressed before and is easy to implement. However, when I run this strategy, ProRealTime only places orders at certain times and not others (see attachment). I have posted here the simplified code which is run on the US 500 Cash 1hour. What am I missing?Simple Hull red to green1234567891011121314151617181920212223242526272829//Market: US 500 Cash//Timeframe: 1hDEFPARAM CumulateOrders=FalsePeriod1=20Period3=28PositionSize = 1timeframe(default)HullAverage1=WeightedAverage[round(sqrt(Period1))](2*WeightedAverage[round(Period1/2)](close)-WeightedAverage[Period1](close))Bull1 = HullAverage1 >= HullAverage1Bear1 = HullAverage1 =< HullAverage1If (Bull1 and Bear1) thenbuy PositionSize contract at market//SET STOP pLOSS slendifHullAverage3=WeightedAverage[round(sqrt(Period3))](2*WeightedAverage[round(Period3/2)](close)-WeightedAverage[Period3](close))//Bull3=HullAverage3>HullAverage3Bear3 = HullAverage3 < HullAverage3If Bear3 thensell at marketendifgraphonprice HullAverage1graphonprice HullAverage3
You must be logged in to access attached files.11/07/2019 at 8:15 PM #112372
Due to the “=” operator in lines 11 and 12, should this be true, line 12 would always override the result from line 11. You should place “=” only in one of them. In your case it’s the same as line 11 without the “=” operator.
Line 9 is useless when not using multiple time frames.
Set your price chart to japanese candlesticks, not heikin-ashi, or use HA in your code. They don’t match, obviously.11/07/2019 at 8:30 PM #112373
Thank you for the quick reply. The full strategy is mutliple time frame, and I left in line 9 by kistake when simplifying the code to try and resolve the specific problem.
If I take out the ‘=’ in lines 11 and 12, or in only one of the lines, the result is the same. Also, I am trading off the Hull moving average which is not affected by having candlesticks or Heikin Ashi – I have changed to candlesticks now but it makes no difference. The turning points still do not get noticed in some instances. It is really strange and I do not understand it.11/08/2019 at 1:18 AM #112378
This is the correct formula, the same one used by PRT:1234x1 = WeightedAverage[Period](close)x2 = (2 * WeightedAverage[round(Period/2)](close)) - x1HullMA = WeightedAverage[round(sqrt(Period))](x2)RETURN HullMA11/08/2019 at 7:41 AM #11238011/08/2019 at 9:05 AM #11238511/08/2019 at 9:05 AM #112386
You are using a 1-line formula that can be misinterpreted by the text parser.
”Divide and Conquer” is always a better approach. Keeping the code clearer and easier to read is the best choice not just to help other readers, but mainly yourself when maintaining code after months or years from when it was first written.
Adding comments when needed is also beneficial for future use.11/08/2019 at 10:57 AM #112399
I have changed the code to reflect suggestions. I have also removed all reference to other time frames – this code runs on one time frame only. I have also attached a new picture. Note that the green/red Hull MA is created by the platform indicators. The thin black line on top is the graphed Hull MA from the code. They overlap. I am still puzzled. This is from today’s data so should be able to be easily replicated by anyone pasting the code into their own platform.Hull MA Red to Green1234567891011121314151617181920212223242526272829303132333435//Market: GBPUSD//Timeframe: 1hDEFPARAM CumulateOrders=FalsePeriod1=20Period3=28PositionSize = 1//Calculate Hull Moving Average for Entry signalx1 = WeightedAverage[Period1](close)x2 = (2 * WeightedAverage[round(Period1/2)](close)) - x1HullMA1 = WeightedAverage[round(sqrt(Period1))](x2)Bull1 = HullMA1 >= HullMA1Bear1 = HullMA1 < HullMA1If (Bull1 and Bear1) thenbuy PositionSize contract at market//SET STOP pLOSS slendif//Calculate Hull Moving Average for Exit signaly1 = WeightedAverage[Period3](close)y2 = (2 * WeightedAverage[round(Period3/2)](close)) - y1HullMA3 = WeightedAverage[round(sqrt(Period3))](y2)//Bull3=HullAverage3>HullAverage3Bear3 = HullMA3 < HullMA3If Bear3 thensell at marketendifgraphonprice HullMA1graphonprice HullMA3
You must be logged in to access attached files.11/08/2019 at 2:40 PM #112420
I can’t see any thin black line on top on the image you posted … is it my eyes again!? 🙂 I have zoomed in also.
(Btw … much better equity curve / profit on 2 hour TF. but I know that is not the point of the Thread … just saying.)
PS I can see the black line now … I read on top as ‘above’! Doh me! 🙂11/08/2019 at 2:49 PM #112421
They overlap. I am still puzzled
Why are you puzzled?
What would you expect HullMA1 and HullMA3 to do (but overlap) as they are the same code (thanks to Roberto) which replicates the Platform code for Hull MA??11/08/2019 at 3:02 PM #11242211/08/2019 at 3:05 PM #112423
They overlap. I am still puzzled
Why are you puzzled?
What would you expect HullMA1 and HullMA3 to do (but overlap) as they are the same code (thanks to Roberto) which replicates the Platform code for Hull MA??
Sorry, a misunderstanding. I am not puzzled by the overlap (expected) but by the problem being discussed in the thread. It is very strange to me that a buy signal is sometimes produced but not always, and if one studies the chart, it should be there. It is based on one criteria, that being that the indicator changes direction.11/08/2019 at 3:12 PM #112425
buy signal is sometimes produced but not always
Right gotcha … and the red circles are where a BUY doesn’t happen (when expected to).
I’ll think on it now.
PS have you tried using Cursor Details and looked at the exact values of HullMA1 on that bar … it can be deceiving looking at the overall curves and when you look at exact values for that bar it often shows that code works as it should11/08/2019 at 4:28 PM #112426
Change your exit conditions to the following and all will work as you think it should:1If onmarket and Bear3 then
The problem is that your entry and exit conditions can sometimes both be true at the same time and so the sell order cancels the buy order.11/08/2019 at 4:43 PM #112429
My test works perfectly, please attach the same pic as before (not with HA candlesticks) AND let the mouse sit on a candle so that data about onscreen MA’s are shown on the pic.
Vonasi is right, try that before.