Hi
I would like to write a code where after a EMA crosses over another I buy a contract and sell when the Heiken Ashi (HA) turns from green to red.
The EMA element is fine but cannot get the HA bit.
I have the basics of the below but cannot work out how to write the code to get from green to red, a then vice a versa when going short from red to green
//heiken ashi
xClose = (Open+High+Low+Close)/4
if(barindex>2) then
xOpen= (xOpen[1] + xClose[1])/2
xHigh= Max(xOpen, xClose)
xLow= Min(xOpen, xClose)
endif
Can anyone help please?
There you go:
//heiken ashi
xClose = (Open+High+Low+Close)/4
if(barindex>2) then
xOpen= (xOpen[1] + xClose[1])/2
xHigh= Max(xOpen, xClose)
xLow= Min(xOpen, xClose)
endif
xBullish = xClose > xOpen
xBearish = xClose < xOpen
ColorChange = (xBullish AND xBearish[1]) OR (xBullish[1] AND xBearish)
COLORCHANGE will be true whenever two adjacent HA candlesticks have different colours.
Paulchal – I have tidied up your post. Please always use the ‘Insert PRT Code’ button when putting code in your posts as per the forum rules.
Hi
I applied the above code but cannot get it to work. I had to //REM xHigh and xLow as the system said it was not included in the code.
Same idea as before, When the MACD crosses over buy on a green HA. Close that order when the HA turns red (but do not automatically sell to open a new position).
Any pointers to where I am going wrong would be appreciated.
Sorry cant see where to “insert PRT code”.
//————————————————————————-
// Main code : MACD with HA End
//————————————————————————-
DEFPARAM CumulateOrders = False //MACD[12,26,9](close) Cumulating positions deactivated
// The system will cancel all pending orders and close all positions at 0:00. No new ones will be allowed until after the “FLATBEFORE” time.
DEFPARAM FLATBEFORE = 200000
// Cancel all pending orders and close all positions at the “FLATAFTER” time
DEFPARAM FLATAFTER = 205000
a = MACDline[12,26,9](close)
b = MACD[12,26,9](close)
//heiken ashi
xClose = (Open+High+Low+Close)/4
if(barindex>2) then
xOpen= (xOpen[1] + xClose[1])/2
//xHigh= Max(xOpen, xClose)
//xLow= Min(xOpen, xClose)
endif
xBullish = xClose > xOpen
xBearish = xClose < xOpen
ColorChange = (xBullish AND xBearish[1]) OR (xBullish[1] AND xBearish)
// Conditions to enter long positions
IF NOT LongOnMarket AND a crosses over b THEN
BUY 1 CONTRACTS AT MARKET
ENDIF
// Conditions to exit long positions
IF LongOnMarket AND Colorchange THEN
SELL AT MARKET
ENDIF
// Conditions to enter short positions
IF NOT ShortOnMarket AND a crosses under b THEN
SELLSHORT 1 CONTRACTS AT MARKET
ENDIF
// Conditions to exit short positions
IF ShortOnMarket AND Colorchange THEN
BUY AT MARKET
ENDIF
// Stops and targets : Enter your protection stops and profit targets here
SET TARGET PPROFIT 40
SET STOP Ptrailing 15
You posted in the wrong forum, this is a strategy and therefore the correct support forum is ProOrder.
To exit a Short trade you need EXITSHORT, since BUY will open a Long trade.
You do not need to start a new topic, please append your further questions here, if relatedto the same topic.
Sorry about any confusion I thought you wanted me to open a new post.
I tried the EXITSHORT and positions still did not open. There was a trade this afternoon that would have made 73 points. MACD crossed under and red HA candle present afterwards. Short position did not open.
Line 11 is correct.
Replace line 12 with:
b = ExponentialAverage[9](MACDline[12,26,9](close)) //Signal line
and use EXITSHORT.