DEMA Crossover with Heikin-Ashi Candle Confirmation for ThinkorSwim

B

blackbox

New member
I ported this over to Easylanguage for Tradestation - I didn't know that TOS didn't have autotrading. Either way... strategy still holds up in Tradestation going forward testing for futures. Equities might be harder since there can be a lot of overnight movement. Weekends are the mostly the only issue with Futures.


Anyway you could share the tradestation code for this strategy? Thank you
 
G

gamestop

New member
Does anyone have access to the tradestation code for this strategy? @DaGekko @blackbox
 
H

hexis777

New member
Please use the DEMA_HA code with caution - the way it is built gives an advantage in the back-test that is very difficult to repeat in live trading. The three lines of code that cause an issue in my view are:

def demaCrossingOverGoingUp = demaSlow < demaFast and demaSlow[-1] > demaFast[-1];

AddOrder(OrderType.SELL_AUTO, sell equals 1, open, 100, Color.RED, Color.RED, "Sell @" + open);
AddOrder(OrderType.BUY_AUTO, buy equals 1, open, 100, Color.GREEN, Color.GREEN, "Buy @" + open);

I tested with 8 tick Range Bars with over 5000 trades and then did an Out of Sample test. With this code I could not repeat the back-test results because many of the entries could not be made as signaled with this code.

Made a few changes in the code.

def demaSlow = DEMA(close, 8);#8
def demaFast = DEMA(close, 21);#21
plot demaFastlt = DEMA(close, 50);#50
demaFastlt.SetLineWeight(2);
plot demaFastvlt = DEMA(close, 200);#200
demaFastvlt.SetLineWeight(4);

def HAclose = ohlc4;
def HAopen = CompoundValue(1,(HAopen[1] + HAclose[1]) / 2,
(open[1] + close[1]) / 2);
def HAhigh = Max(Max(high, HAopen), HAclose);
def HAlow = Min(Min(low, HAopen), HAclose);

def demaCrossingOverGoingDown = demaSlow[1] > demaFast[1] and demaSlow < demaFast;
def demaCrossingOverGoingUp = demaSlow < demaFast and demaSlow[-1] > demaFast[-1];

def HARed = HAOpen > HAclose;
def HAGreen = !HARed;
def HADecidedRed = HARed and HAHigh == HAopen;
def HADecidedGreen = HAGreen and HAlow == HAOpen;

######################################################################################
######################################################################################

#def sellsignal = demaCrossingOverGoingDown and (HADecidedRed or HADecidedRed[1] or HADecidedRed[2]);
def sellsignal = demaCrossingOverGoingDown and close < demaFastlt and (HADecidedRed or HADecidedRed[1] or HADecidedRed[2]);

#def buysignal = demaCrossingOverGoingUp and (HADecidedGreen or HADecidedGreen[1] or HADecidedGreen[2]);
def buysignal = demaCrossingOverGoingUp and close > demaFastlt and (HADecidedGreen or HADecidedGreen[1] or HADecidedGreen[2]);

#When testing need to turn the comment "" off
AddOrder(OrderType.SELL_AUTO, sellsignal equals 1, close, 1, Color.RED, Color.RED, "DEMA Sell @" + close);
AddOrder(OrderType.BUY_AUTO, buysignal equals 1, close[-1], 1, Color.GREEN, Color.GREEN, "DEMA Buy @" + close);

#AddVerticalLine(close crosses demaFastvlt, close, Color.yellow, Curve.SHORT_DASH);
#AddVerticalLine(buysignal, close, Color.GREEN, Curve.SHORT_DASH);

AddChartBubble(demaCrossingOverGoingUp, low,"DEMA HA",Color.dark_green);
AddChartBubble(demaCrossingOverGoingDown, high,"DEMA HA",Color.dark_red);

#Alert
def alerttrigger1 = buysignal;
def alerttrigger2 = sellsignal;

input alerttext1 = " ++++++++++ DEMA + HA +++++++++++ ";
input alerttext2 = " ---------- DEMA - HA ---------- ";
input UseAlerts = {false, default true};
input AlertType = {default "BAR", "ONCE", "TICK"};

def at = AlertType;

input AlertSound = {"Chimes", "Bell", default "Ring", "NoSound", "Ding"};

Alert (alerttrigger1 and UseAlerts, alerttext1, if at == 1 then Alert.ONCE else if at == 2 then Alert.TICK else Alert.BAR, AlertSound);

Alert (alerttrigger2 and UseAlerts, alerttext2, if at == 1 then Alert.ONCE else if at == 2 then Alert.TICK else Alert.BAR, AlertSound);
---
Hi my friend. Please, Where can I find this script to use it in the TOS Scanner tool to search for Stocks that have the indicator (DEMA HA) shown in the study to perform Call operations?
Thanks
 
H

harber09

New member
VIP
How do we scan for buy and sell signals on the daily?
 
BenTen

BenTen

Administrative
Staff
VIP
@harber09 The script provided in this thread is a backtesting strategy; here is the modified version of it to be used as an indicator. I added the buy and sell arrows to help you scan for signals.

Code:
def demaSlow = DEMA(close, 8);
def demaFast = DEMA(close, 21);

def HAclose = ohlc4;
def HAopen = CompoundValue(1,(HAopen[1] + HAclose[1]) / 2,
(open[1] + close[1]) / 2);
def HAhigh = Max(Max(high, HAopen), HAclose);
def HAlow = Min(Min(low, HAopen), HAclose);

def demaCrossingOverGoingDown = demaSlow[1] > demaFast[1] and demaSlow < demaFast;
def demaCrossingOverGoingUp = demaSlow < demaFast and demaSlow[-1] > demaFast[-1];

def HARed = HAOpen > HAclose;
def HAGreen = !HARed;
def HADecidedRed = HARed and HAHigh == HAopen;
def HADecidedGreen = HAGreen and HAlow == HAOpen;

def sell = demaCrossingOverGoingDown and (HADecidedRed or HADecidedRed[1] or HADecidedRed[2]);

def buy = demaCrossingOverGoingUp and (HADecidedGreen or HADecidedGreen[1] or HADecidedGreen[2]);

# Plot Confirmed Signals
plot bullish = buy;
bullish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
bullish.SetDefaultColor(Color.CYAN);
bullish.SetLineWeight(1);
plot bearish = sell;
bearish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
bearish.SetDefaultColor(Color.MAGENTA);
bearish.SetLineWeight(1);
 
E

Echeveste

New member
Built up this DEMA Crossover strategy with Heikin-Ashi candle confirmation. Works best in Daily setup.



ThinkScript Code

Rich (BB code):
def demaSlow = DEMA(close, 8);
def demaFast = DEMA(close, 21);

def HAclose = ohlc4;
def HAopen = CompoundValue(1,(HAopen[1] + HAclose[1]) / 2,
(open[1] + close[1]) / 2);
def HAhigh = Max(Max(high, HAopen), HAclose);
def HAlow = Min(Min(low, HAopen), HAclose);

def demaCrossingOverGoingDown = demaSlow[1] > demaFast[1] and demaSlow < demaFast;
def demaCrossingOverGoingUp = demaSlow < demaFast and demaSlow[-1] > demaFast[-1];

def HARed = HAOpen > HAclose;
def HAGreen = !HARed;
def HADecidedRed = HARed and HAHigh == HAopen;
def HADecidedGreen = HAGreen and HAlow == HAOpen;

def sell = demaCrossingOverGoingDown and (HADecidedRed or HADecidedRed[1] or HADecidedRed[2]);

def buy = demaCrossingOverGoingUp and (HADecidedGreen or HADecidedGreen[1] or HADecidedGreen[2]);

AddOrder(OrderType.SELL_AUTO, sell equals 1, open, 100, Color.RED, Color.RED, "Sell @" + open);
AddOrder(OrderType.BUY_AUTO, buy equals 1, open, 100, Color.GREEN, Color.GREEN, "Buy @" + open);


Hello,
How could I apply the code to work in my whatchlist and/or make a sound when a buy or sell happens?
Thank you!
 
E

Echeveste

New member
@harber09 The script provided in this thread is a backtesting strategy; here is the modified version of it to be used as an indicator. I added the buy and sell arrows to help you scan for signals.

Code:
def demaSlow = DEMA(close, 8);
def demaFast = DEMA(close, 21);

def HAclose = ohlc4;
def HAopen = CompoundValue(1,(HAopen[1] + HAclose[1]) / 2,
(open[1] + close[1]) / 2);
def HAhigh = Max(Max(high, HAopen), HAclose);
def HAlow = Min(Min(low, HAopen), HAclose);

def demaCrossingOverGoingDown = demaSlow[1] > demaFast[1] and demaSlow < demaFast;
def demaCrossingOverGoingUp = demaSlow < demaFast and demaSlow[-1] > demaFast[-1];

def HARed = HAOpen > HAclose;
def HAGreen = !HARed;
def HADecidedRed = HARed and HAHigh == HAopen;
def HADecidedGreen = HAGreen and HAlow == HAOpen;

def sell = demaCrossingOverGoingDown and (HADecidedRed or HADecidedRed[1] or HADecidedRed[2]);

def buy = demaCrossingOverGoingUp and (HADecidedGreen or HADecidedGreen[1] or HADecidedGreen[2]);

# Plot Confirmed Signals
plot bullish = buy;
bullish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
bullish.SetDefaultColor(Color.CYAN);
bullish.SetLineWeight(1);
plot bearish = sell;
bearish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
bearish.SetDefaultColor(Color.MAGENTA);
bearish.SetLineWeight(1);

Can I save it as a study and use auto buy condition to execute automatically?
Thanks
 
BenTen

BenTen

Administrative
Staff
VIP
@Echeveste ThinkorSwim does not support auto trading at the moment. That script is only for backtesting.
 
H

hexis777

New member
@mcdon030 Please., Could you create a script to scan the variable "DEMA HA" in the TOS Scan tool? Thank You
 
M

Moose

New member
Please use the DEMA_HA code with caution - the way it is built gives an advantage in the back-test that is very difficult to repeat in live trading. The three lines of code that cause an issue in my view are:

def demaCrossingOverGoingUp = demaSlow < demaFast and demaSlow[-1] > demaFast[-1];

AddOrder(OrderType.SELL_AUTO, sell equals 1, open, 100, Color.RED, Color.RED, "Sell @" + open);
AddOrder(OrderType.BUY_AUTO, buy equals 1, open, 100, Color.GREEN, Color.GREEN, "Buy @" + open);

I tested with 8 tick Range Bars with over 5000 trades and then did an Out of Sample test. With this code I could not repeat the back-test results because many of the entries could not be made as signaled with this code.

Made a few changes in the code.

def demaSlow = DEMA(close, 8);#8
def demaFast = DEMA(close, 21);#21
plot demaFastlt = DEMA(close, 50);#50
demaFastlt.SetLineWeight(2);
plot demaFastvlt = DEMA(close, 200);#200
demaFastvlt.SetLineWeight(4);

def HAclose = ohlc4;
def HAopen = CompoundValue(1,(HAopen[1] + HAclose[1]) / 2,
(open[1] + close[1]) / 2);
def HAhigh = Max(Max(high, HAopen), HAclose);
def HAlow = Min(Min(low, HAopen), HAclose);

def demaCrossingOverGoingDown = demaSlow[1] > demaFast[1] and demaSlow < demaFast;
def demaCrossingOverGoingUp = demaSlow < demaFast and demaSlow[-1] > demaFast[-1];

def HARed = HAOpen > HAclose;
def HAGreen = !HARed;
def HADecidedRed = HARed and HAHigh == HAopen;
def HADecidedGreen = HAGreen and HAlow == HAOpen;

######################################################################################
######################################################################################

#def sellsignal = demaCrossingOverGoingDown and (HADecidedRed or HADecidedRed[1] or HADecidedRed[2]);
def sellsignal = demaCrossingOverGoingDown and close < demaFastlt and (HADecidedRed or HADecidedRed[1] or HADecidedRed[2]);

#def buysignal = demaCrossingOverGoingUp and (HADecidedGreen or HADecidedGreen[1] or HADecidedGreen[2]);
def buysignal = demaCrossingOverGoingUp and close > demaFastlt and (HADecidedGreen or HADecidedGreen[1] or HADecidedGreen[2]);

#When testing need to turn the comment "" off
AddOrder(OrderType.SELL_AUTO, sellsignal equals 1, close, 1, Color.RED, Color.RED, "DEMA Sell @" + close);
AddOrder(OrderType.BUY_AUTO, buysignal equals 1, close[-1], 1, Color.GREEN, Color.GREEN, "DEMA Buy @" + close);

#AddVerticalLine(close crosses demaFastvlt, close, Color.yellow, Curve.SHORT_DASH);
#AddVerticalLine(buysignal, close, Color.GREEN, Curve.SHORT_DASH);

AddChartBubble(demaCrossingOverGoingUp, low,"DEMA HA",Color.dark_green);
AddChartBubble(demaCrossingOverGoingDown, high,"DEMA HA",Color.dark_red);

#Alert
def alerttrigger1 = buysignal;
def alerttrigger2 = sellsignal;

input alerttext1 = " ++++++++++ DEMA + HA +++++++++++ ";
input alerttext2 = " ---------- DEMA - HA ---------- ";
input UseAlerts = {false, default true};
input AlertType = {default "BAR", "ONCE", "TICK"};

def at = AlertType;

input AlertSound = {"Chimes", "Bell", default "Ring", "NoSound", "Ding"};

Alert (alerttrigger1 and UseAlerts, alerttext1, if at == 1 then Alert.ONCE else if at == 2 then Alert.TICK else Alert.BAR, AlertSound);

Alert (alerttrigger2 and UseAlerts, alerttext2, if at == 1 then Alert.ONCE else if at == 2 then Alert.TICK else Alert.BAR, AlertSound);
Did you get this code tested? does it give an actual results after you made the changes? Thank you
 
B

blackbox

New member
Hi! I was just looking for some clarity regarding the DEMA + HA strategy posted here:

The [-1] bars for the going up signal is throwing me off. Does that mean it is looking into the future to create the signal? but the going down seems normal. Would that cause a delay in the signal?

Thank you so much for your time!
 
  • Like
Reactions: ALF
C

csrkk

New member
VIP
@harber09 The script provided in this thread is a backtesting strategy; here is the modified version of it to be used as an indicator. I added the buy and sell arrows to help you scan for signals.

Code:
def demaSlow = DEMA(close, 8);
def demaFast = DEMA(close, 21);

def HAclose = ohlc4;
def HAopen = CompoundValue(1,(HAopen[1] + HAclose[1]) / 2,
(open[1] + close[1]) / 2);
def HAhigh = Max(Max(high, HAopen), HAclose);
def HAlow = Min(Min(low, HAopen), HAclose);

def demaCrossingOverGoingDown = demaSlow[1] > demaFast[1] and demaSlow < demaFast;
def demaCrossingOverGoingUp = demaSlow < demaFast and demaSlow[-1] > demaFast[-1];

def HARed = HAOpen > HAclose;
def HAGreen = !HARed;
def HADecidedRed = HARed and HAHigh == HAopen;
def HADecidedGreen = HAGreen and HAlow == HAOpen;

def sell = demaCrossingOverGoingDown and (HADecidedRed or HADecidedRed[1] or HADecidedRed[2]);

def buy = demaCrossingOverGoingUp and (HADecidedGreen or HADecidedGreen[1] or HADecidedGreen[2]);

# Plot Confirmed Signals
plot bullish = buy;
bullish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
bullish.SetDefaultColor(Color.CYAN);
bullish.SetLineWeight(1);
plot bearish = sell;
bearish.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
bearish.SetDefaultColor(Color.MAGENTA);
bearish.SetLineWeight(1);

Sometimes I am not seeing Bullish Signal though the candle looks green and price go up. Is this fully tested? Can you please confirm.
 

Similar threads

Top