Strategy for Heiken-Ashi close crossing Triple Exponential Moving Average


Category:
0
0

Hi All,

Need help in the thinkscript code below. I’m trying to code a backtesting strategy on /ES mini, where when the Heikin Ashi candle closes above TEMA line, it should trigger buy order and once the Heikin Ashi candle closes below the TEMA, it should exit buy. Something like the attached image. The chart settings are /ES 1000 Tick chart with Heinkin Ashi candles and TEMA 30

The below code gives me the Entry and Exit as attached image but doesn’t give any values in the report, also attached image.

Any help appreciated. Thanks.

def HAclose = ohlc4;
def TEMA10 = TEMA(“length” = 30);

def crossover= if HAclose > TEMA30 and HAclose [1] < TEMA30 [1] then 1 else 0;

def trigger=if crossover then 1 else 0;

AddOrder(OrderType.BUY_TO_OPEN, trigger, 1
,tickColor = GetColor(2), arrowColor = GetColor(3), name = “Buy”);

AddOrder(OrderType.SELL_TO_CLOSE, HAclose < TEMA30 and HAclose[1] > TEMA30[1], 1, tickColor = GetColor(2), arrowColor = GetColor(3), name = “Exit_Buy”);

Attachments:
RESOLVED
Marked as spam
Posted by (Questions: 6, Answers: 6)
Asked on November 11, 2019 1:59 pm
737 views
0
Private answer

I had to update the title of your question so that everyone else would know the context of your question and be able to find it in the search results. The original question title was "Thinkscript Heinken Ashi candle" and it told us absolutely nothing about the topic of your question.

Now on to the solution. I think it will help if you read up on how to apply the AddOrder statement. We have several videos on this topic which would have helped you avoid the mistake you made in your code. But here are the full details straight from the developers at Thinkorswim:

https://toslc.thinkorswim.com/center/reference/thinkScript/Functions/Others/AddOrder.html

Reading from that page, we find the third parameter of this function contains the price at which the theoretical trades are executed. In your original code the third parameter contains a value of 1. Which is why your chart does not show the dashed lines connecting each buy and sell order on the chart. Because the buy and sells are all taking place at $1.00. This is also the reason why your report is showing zero profit. Because you are executing every order at one dollar.

Here is your corrected code that will record the buys and sells at the open of the bar that follows the bar that crosses.

plot haClose = ohlc4;
plot tema = TEMA(“length” = 30);
def crossover= haClose > tema and haClose[1] < tema[1];
AddOrder(OrderType.BUY_TO_OPEN, crossover, open[-1], tickColor = GetColor(2), arrowColor = GetColor(3), name = “Buy”);
AddOrder(OrderType.SELL_TO_CLOSE, haClose < tema and haClose[1] > tema[1], open[-1], tickColor = GetColor(2), arrowColor = GetColor(3), name = “Exit_Buy”);

In closing. I will mention that there is very little reason for using a triple exponential moving average. Especially with a length of 30. Why? I wrote an entire article on the topic so you can find the answers here:

https://www.hahn-tech.com/moving-averages-exponential-problems/

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on November 12, 2019 1:29 pm
0
Hi Pete, Thanks for your reply. Got it working. Also thanks for pointing out the Tema30 and sharing the Tema link. I'm back testing a few different strategies to check which one will work for me the best. Now i'm trying to create advanced order for this as below. Also watching your youtube videos "Thinkorswim Auto Trade Almost". 1. Manual Entry 2. Exit on Stop loss 3. Exit on target 4. Exit when crossover (haClose < tema and haClose[1] > tema[1]) The first 3 points i can handle, any thoughts on the 4th point. If the 4th point (exit on crossover) can be implemented in TOS advanced orders? Thanks again.
( at November 12, 2019 3:04 pm)
0
Regarding the 4th point? You use it just as it is. plot signal = haClose < tema and haClose[1] > tema[1]);
( at November 12, 2019 6:56 pm)
0
I'm trying to setup advanced order, getting help from your youtube video. For the above plot signal = haClose < tema and haClose[1] > tema[1]), will I be able to do it if it's a tick 500 chart (aggregation). All I see in the advanced order tab is time based aggregation.
( at November 13, 2019 2:57 pm)
0
No. Tick charts are not supported for Conditional Orders, Study Alerts, Scans and Watchlist Columns. Which means tick charts are only supported on Charts. Nothing else.
( at November 13, 2019 4:47 pm)
0
Thanks for the clarification.
( at November 15, 2019 7:47 am)