# 2 HA Grn and 2 HA Red Strategy For ThinkOrSwim

#### Lesnewby

##### Member
How can I convert the two scans/studies into a strategy that allows me to backcheck a bunch of selected stocks with the results in a tos Report.
Make a strategy from a scan: The way I use the scans is to look for two consecutive green candles in Heiken-Ashe Chart, confirm with a 20 day SMA over a 100 day SMA for an up trend and sell a credit spread with a 45 to 60 day expiration. Not all stocks are good candidates, so I wish to use a strategy that sells to open at the close of the second green candle and buy back at the close of the second RED candle. Have used this manual with quite good success.

#2GrnBars
def haClose = ohlc4;
def haOpen = if haOpen[1] == 0 then haClose[1] else (haOpen[1] + haClose[1]) / 2;
def haHigh = Max(high, Max(haClose, haOpen));
def haLow = Min(low, Min(haClose, haOpen));
def haColor = haClose > haOpen;
def trendUp = haColor and haColor[1] and !haColor[2];
plot scan = trendUp;

XXXX

#2RedBars
def haClose = ohlc4;
def haOpen = if haOpen[1] == 0 then haClose[1] else (haOpen[1] + haClose[1]) / 2;
def haHigh = Max(high, Max(haClose, haOpen));
def haLow = Min(low, Min(haClose, haOpen));
def haColor = haClose < haOpen;
def trendUp = haColor and haColor[1] and !haColor[2];
plot scan = trendUp;

#Template for Strategies

def signal = 0;

def exit = 0;

#Template for Lower Studies

declare lower;
plot signal = 0;
plot exit = 0;

Last edited:
Did some more work on script on turning these scans to a strategy, BUT, not getting the results I was looking for. I wanted a SHORT TO OPEN order on the close of the second GREEN Heiken-Ashi (HA) candle and a LONG TO CLOSE order on the close of the second RED HA candle. It ain't happening. Can anyone help, Please!!

This is the code:

#2GrnBars
def haClose = ohlc4;
def haOpen = if haOpen[1] == 0 then haClose[1] else (haOpen[1] + haClose[1]) / 2;
def haHigh = Max(high, Max(haClose, haOpen));
def haLow = Min(low, Min(haClose, haOpen));
def haColor = haClose > haOpen;
def trendUp = haColor and haColor[1] and !haColor[2];

addOrder(OrderType.SELL_TO_OPEN,trendUp, haClose > haOpen, haColor and haColor[1] and !haColor[2], name = "SE");

#2RedBars
def haCloseR = ohlc4;
def haOpenR = if haOpen[1] == 0 then haClose[1] else (haOpen[1] + haClose[1]) / 2;
def haHighR = Max(high, Max(haClose, haOpen));
def haLowR = Min(low, Min(haClose, haOpen));
def haColorR = haClose < haOpen;
def trendDn = haColor and haColor[1] and !haColor[2];

addOrder(OrderType.Buy_TO_CLOSE,trendDn, haClose < haOpen, haColor and haColor[1] and !haColor[2], name = "LX");

@Lesnewby
Code:
``````def haClose = ohlc4;
def haOpen = if haOpen[1] == 0 then haClose[1] else (haOpen[1] + haClose[1]) / 2;
def haHigh = Max(high, Max(haClose, haOpen));
def haLow = Min(low, Min(haClose, haOpen));
def ha_up = haClose > haOpen;
def ha_dn = haClose < haOpen;
def sto = ha_up and ha_up[1];
def btc = ha_dn and ha_dn[1];

addOrder(OrderType.SELL_TO_OPEN, sto, close, name = "SE");

@Lesnewby
Code:
``````def haClose = ohlc4;
def haOpen = if haOpen[1] == 0 then haClose[1] else (haOpen[1] + haClose[1]) / 2;
def haHigh = Max(high, Max(haClose, haOpen));
def haLow = Min(low, Min(haClose, haOpen));
def ha_up = haClose > haOpen;
def ha_dn = haClose < haOpen;
def sto = ha_up and ha_up[1];
def btc = ha_dn and ha_dn[1];

addOrder(OrderType.SELL_TO_OPEN, sto, close, name = "SE");
Will install and run, thanks for your help! Will get back with results.

Hi Generic, Great, it worked. When I ran the report it quickly showed me that selling short is not a good idea, Buying long has merit, however one needs to study each symbol selected and understand how the price moves. As always, if one does their homework the results can be spectacular.

Some more feedback to Generic,

def sto = ha_up and ha_up[1];
def btc = ha_dn and ha_dn[1];

I replace the [1] with [0] so the indicator settles on the second candle, not the third. I will be placing my orders between 15:00hrs and close of the market if the trend (20 over 100day SMA) is still up in both a weekly and daily chart and the candle is still green.

ToS Strategy report suggests that buying at the end of the day on a second green bar and exiting on two consecutive red bars gives good profits. Use a scan to select stocks from the S&P 500.
Scan:

#2GrnBars
def haClose = ohlc4;
def haOpen = if haOpen[1] == 0 then haClose[1] else (haOpen[1] + haClose[1]) / 2;
def haHigh = Max(high, Max(haClose, haOpen));
def haLow = Min(low, Min(haClose, haOpen));
def haColor = haClose > haOpen;
def trendUp = haColor and haColor[1] and !haColor[2];
plot scan = trendUp;

Some more feedback to Generic,

def sto = ha_up and ha_up[1];
def btc = ha_dn and ha_dn[1];

I replace the [1] with [0] so the indicator settles on the second candle, not the third. I will be placing my orders between 15:00hrs and close of the market if the trend (20 over 100day SMA) is still up in both a weekly and daily chart and the candle is still green.
[1] is the second candle back. ha_up and ha_up[0] are both current candle. The strategy will plot on the bar after the confirmation so it looks like it's 1 bar late but the entry price is second bar close.

Thanks Generic, Now that I have the strategy working I need a way to select the stocks I will be using from the S&P 500. I was wondering.....
I am executing a SELL to open credit spread on the second Green Candle if the daily trend is up at 15:00hrs and if the Weekly HA is also showing two green bars. I place a closing order to Buy to close a debit spread with a profit of 50%. And I continue this every day with the same stock as long as my conditions are met. I am currently using 5 contracts. Any ideas on how best to test the strategy over 15 years to give me a sense which stocks have long green runs, at least longer than most. I used the Strategy

def haClose = ohlc4;
def haOpen = if haOpen[1] == 0 then haClose[1] else (haOpen[1] + haClose[1]) / 2;
def haHigh = Max(high, Max(haClose, haOpen));
def haLow = Min(low, Min(haClose, haOpen));
def ha_up = haClose > haOpen;
def ha_dn = haClose < haOpen;
def sto = ha_up and ha_up[1];
def btc = ha_dn and ha_dn[1];

addOrder(OrderType.SELL_TO_OPEN, sto, close, name = "SE");

ToS Strategy report gave me the following:
 Strategy Report on 2Green Bars in Heiken-Ashi Time span: July 30th, 2001 to July 9th, 2021 Duration: Weekly chart Sym Trades ADBE - 92​ AMZN 85​ BA 95​ CAT 93​ COST 92​ CRM 73​ DIA 86​ FB 37​ GS 94​ MSFT 83​ NFLX 85​ NVDA 88​ QQQ 81​ ROKU 16​ SPX 86​ SPY 90​ UNH 92​

This report provides only entry for the first two Green Candles. If you have a run of Green Candles for a few days I would be executing orders on the same stock every day until the conditions changed.
Any thoughts or ideas how to better select, scan, for these special stocks that have long runs of Green HA Bars???

87k+ Posts
184 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?