Three-bar High/Low

thiagofnsouza

New member
Hey guys,

I'm new here and i'm not a programmer. i'm just a hard-working person who tries a lot a do a lot of research to code with a lot of errors. That being said, i'm trying to do TOS coding the famous Larry Williams strategy.

The strategy is to buy at the price of the three-bar moving average of the lows if the trend is positive, and take profits at the three-bar moving average of the highs.

Sell signals are just the opposite. This means you will sell short at the three-bar moving average of the highs (if the trend is negative) and take profits at the three-bar moving average of the lows.

The way i have setted the trend is EMA9, but when i run the code, not every time the code plot a order at the market and he is buying/selling in a non sense target sometimes. Can you guys help me?

Here's my code:

input price = close;
input priceLast = PriceType.LAST;
input priceH = high;
input priceL = low;
input Media3High = 3;
input Media3Low = 3;
input length = 9;
input displace = 0;
input averageType = AverageType.SIMPLE;
input averageType2 = AverageType.EXPONENTIAL;

def bid = close (priceType = priceLast);
def ask = close (priceType = priceLast);

plot avgSELL = MovingAverage(averageType, high[-displace], Media3High);
plot avgBUY = MovingAverage(averageType, low[-displace], Media3Low);

plot AVG = MovingAverage(averageType2, price[-displace], length);
AVG.AssignValueColor(if AVG > AVG[1] then Color.GREEN else if AVG < AVG[1] then Color.RED else Color.YELLOW);
AVG.SetLineWeight(2);

avgSELL.AssignValueColor(Color.RED);

def sell = ask or bid crosses avgSELL;

AddOrder(OrderType.SELL_AUTO, sell, avgSELL, 1, tickcolor = Color.RED, arrowcolor = Color.WHITE);

---------------------------------------------------------------------------------------------------------------------------

Last edited by a moderator:
Solution
Hey guys,

I'm new here and i'm not a programmer. i'm just a hard-working person who tries a lot a do a lot of research to code with a lot of errors. That being said, i'm trying to do TOS coding the famous Larry Williams strategy.

The strategy is to buy at the price of the three-bar moving average of the lows if the trend is positive, and take profits at the three-bar moving average of the highs.

Sell signals are just the opposite. This means you will sell short at the three-bar moving average of the highs (if the trend is negative) and take profits at the three-bar moving average of the lows.

The way i have setted the trend is EMA9, but when i run the code, not every time the code plot a order at the market and he is...
Hey guys,

I'm new here and i'm not a programmer. i'm just a hard-working person who tries a lot a do a lot of research to code with a lot of errors. That being said, i'm trying to do TOS coding the famous Larry Williams strategy.

The strategy is to buy at the price of the three-bar moving average of the lows if the trend is positive, and take profits at the three-bar moving average of the highs.

Sell signals are just the opposite. This means you will sell short at the three-bar moving average of the highs (if the trend is negative) and take profits at the three-bar moving average of the lows.

The way i have setted the trend is EMA9, but when i run the code, not every time the code plot a order at the market and he is buying/selling in a non sense target sometimes. Can you guys help me?

Here's my code:

input price = close;
input priceLast = PriceType.LAST;
input priceH = high;
input priceL = low;
input Media3High = 3;
input Media3Low = 3;
input length = 9;
input displace = 0;
input averageType = AverageType.SIMPLE;
input averageType2 = AverageType.EXPONENTIAL;

def bid = close (priceType = priceLast);
def ask = close (priceType = priceLast);

plot avgSELL = MovingAverage(averageType, high[-displace], Media3High);
plot avgBUY = MovingAverage(averageType, low[-displace], Media3Low);

plot AVG = MovingAverage(averageType2, price[-displace], length);
AVG.AssignValueColor(if AVG > AVG[1] then Color.GREEN else if AVG < AVG[1] then Color.RED else Color.YELLOW);
AVG.SetLineWeight(2);

avgSELL.AssignValueColor(Color.RED);

def sell = ask or bid crosses avgSELL;

AddOrder(OrderType.SELL_AUTO, sell, avgSELL, 1, tickcolor = Color.RED, arrowcolor = Color.WHITE);

---------------------------------------------------------------------------------------------------------------------------

are you trying to compare bid and ask to a number?

def sell = ask or bid crosses avgSELL;

this won't work as intended.
The or will make the formula treat the ask as a 1 or 0 value Instead of checking if it crosses an average.
You would have to spell it out completely, like,

Not the exact question you're looking for?

87k+ Posts
129 Online

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?