# 2 HA Grn and 2 HA Red convert scans to strategies

#### Lesnewby

##### New 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:

#### Lesnewby

##### New member
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");

#### generic

##### Active member
@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

##### New member
@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.

#### Lesnewby

##### New member
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.

#### Lesnewby

##### New member
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.

#### Lesnewby

##### New member
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;

#### generic

##### Active member
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.

#### Lesnewby

##### New member
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");