Moving Average Master Strategy for ThinkOrSwim

Awesome. Thanks. Only issue I get is that the close signal isn't working. I assume we need a close at displace and the a new buy/sell signal to fire on the next candle. There must be a way to trigger the close but then also take advantage of the next move. Maybe adding another Hull average that can be used as a close trigger. I tested this using two different Hull MAs. Looks Like it may be doable. Would be nice to have the option to change both the "Hull to Close" and the "Hull to buy/sell" Lengths so we can use on different TFs etc. What are your thoughts?
Also, Can the time to trade just be removed or at least disabled. Not sure how important this is.

eXecoIh.jpg
If you move the hma length to 4, and the hma displace to 4, you get a P/L that is more than double. with the HMA length and HMA displayed set at 2 for /ES 5D 5M I got a P/L of 1687, When they were both changed to 4, I got a P/L of 5462. With less signals. (Fewer trades). try this Grid 4AnyHnU
 
Last edited:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

If you move the hma length to 4, and the hma displace to 4, you get a P/L that is more than double. with the HMA length and HMA displayed set at 2 for /ES 5D 5M I got a P/L of 1687, When they were both changed to 4, I got a P/L of 5462. With less signals. (Fewer trades).
Oh yeah. I know making those changes will affect the P/L.
 
i never had luck with using displacement you can simply use two 9ema displaced one by 2 and one by 1 and you get good pl, but live you cant trade it as you would need to enter 2 candles ago but get your signal on current
 
Awesome. Thanks. Only issue I get is that the close signal isn't working. I assume we need a close at displace and the a new buy/sell signal to fire on the next candle. There must be a way to trigger the close but then also take advantage of the next move. Maybe adding another Hull average that can be used as a close trigger. I tested this using two different Hull MAs. Looks Like it may be doable. Would be nice to have the option to change both the "Hull to Close" and the "Hull to buy/sell" Lengths so we can use on different TFs etc. What are your thoughts?
Also, Can the time to trade just be removed or at least disabled. Not sure how important this is.

eXecoIh.jpg
Try this (untested) .. I see you have Renko candles on your chart .. this would change the strategy.

Code:
declare upper;
input HMA_Fast_Length = 9;
input HMA_Slow_Length = 21;
input ShowBackTestPositions = yes;
input BackTestTradeSize = 1;

def HMA_Fast = HullMovingAvg(price = close, length = HMA_Fast_Length);
def HMA_Slow = HullMovingAvg(price = close, length = HMA_Slow_Length);

def BuyToOpenSignal=   HMA_Fast > HMA_Fast[1] and HMA_Fast > HMA_Slow;
def SellToCloseSignal=  HMA_Fast < HMA_Fast[1];

def SellToOpenSignal=  HMA_Fast < HMA_Fast[1] and HMA_Fast < HMA_Slow;
def BuyToCloseSignal=   HMA_Fast > HMA_Fast[1];

AddOrder(OrderType.BUY_TO_OPEN, BuyToOpenSignal and ShowBackTestPositions ,   open[-1], BackTestTradeSize, Color.CYAN, Color.CYAN, "");
AddOrder(OrderType.SELL_TO_CLOSE, SellToCloseSignal and ShowBackTestPositions, open[-1], BackTestTradeSize, Color.CYAN, Color.CYAN, "");

AddOrder(OrderType.SELL_TO_OPEN, SellToOpenSignal and ShowBackTestPositions,  open[-1], BackTestTradeSize, Color.RED, Color.RED,"");
AddOrder(OrderType.BUY_TO_CLOSE,  BuyToCloseSignal and ShowBackTestPositions,  open[-1], BackTestTradeSize, Color.RED, Color.RED,"");

def OpenOrders = GetQuantity();
AddLabel(yes, "     BUY      ", if BuyToOpenSignal[1]  and OpenOrders < 1 then CreateColor(153, 255, 153) else Color.White);
AddLabel(yes, "     SELL     ", if SellToOpenSignal[1]  and OpenOrders > -1 then CreateColor(255, 102, 102) else Color.White);
AddLabel(yes, "     CLOSE     ", if  (SellToCloseSignal[1] or BuyToCloseSignal[1]) and OpenOrders != 0  then  Color.Yellow else Color.White);
 
Last edited:
Awesome. Thanks. Only issue I get is that the close signal isn't working. I assume we need a close at displace and the a new buy/sell signal to fire on the next candle. There must be a way to trigger the close but then also take advantage of the next move. Maybe adding another Hull average that can be used as a close trigger. I tested this using two different Hull MAs. Looks Like it may be doable. Would be nice to have the option to change both the "Hull to Close" and the "Hull to buy/sell" Lengths so we can use on different TFs etc. What are your thoughts?
Also, Can the time to trade just be removed or at least disabled. Not sure how important this is.

eXecoIh.jpg
This would be how you do it using Renko candles:

Ruby:
declare upper;
input AverageType = AverageType.HULL;
input price = OHLC4;
input HMA_Fast_Length = 1;
input HMA_Slow_Length = 3;
input ShowBackTestPositions = yes;
input BackTestTradeSize = 1;

plot HMA_Fast = MovingAverage(AverageType, price, HMA_Fast_Length);
plot HMA_Slow =  MovingAverage(AverageType, price, HMA_Slow_Length);

def BuyToOpenSignal=   HMA_Fast > HMA_Fast[1] and HMA_Fast[1] > HMA_Slow[1] and HMA_Slow > HMA_Slow[1];
def SellToCloseSignal=  HMA_Fast[1] < HMA_Slow[1];

def SellToOpenSignal=  HMA_Fast < HMA_Fast[1] and HMA_Fast[1] < HMA_Slow[1] and HMA_Slow < HMA_Slow[1];
def BuyToCloseSignal=   HMA_Fast[1] > HMA_Slow[1];

AddOrder(OrderType.BUY_TO_OPEN, BuyToOpenSignal and ShowBackTestPositions ,   open[-1], BackTestTradeSize, Color.CYAN, Color.CYAN, "");
AddOrder(OrderType.SELL_TO_CLOSE, SellToCloseSignal and ShowBackTestPositions, open[-1], BackTestTradeSize, Color.CYAN, Color.CYAN, "");

AddOrder(OrderType.SELL_TO_OPEN, SellToOpenSignal and ShowBackTestPositions,  open[-1], BackTestTradeSize, Color.RED, Color.RED,"");
AddOrder(OrderType.BUY_TO_CLOSE,  BuyToCloseSignal and ShowBackTestPositions,  open[-1], BackTestTradeSize, Color.RED, Color.RED,"");

def OpenOrders = GetQuantity();
AddLabel(yes, "     BUY      ", if BuyToOpenSignal[1]  and OpenOrders < 1 then CreateColor(153, 255, 153) else Color.White);
AddLabel(yes, "     SELL     ", if SellToOpenSignal[1]  and OpenOrders > -1 then CreateColor(255, 102, 102) else Color.White);
AddLabel(yes, "     CLOSE     ", if  (SellToCloseSignal[1] or BuyToCloseSignal[1]) and OpenOrders != 0  then  Color.Yellow else Color.White);

Tested and modified @ 8:10PM
 
Last edited:
Try this (untested) .. I see you have Renko candles on your chart .. this would change the strategy.

Code:
declare upper;
input HMA_Fast_Length = 9;
input HMA_Slow_Length = 21;
input ShowBackTestPositions = yes;
input BackTestTradeSize = 1;

def HMA_Fast = HullMovingAvg(price = close, length = HMA_Fast_Length);
def HMA_Slow = HullMovingAvg(price = close, length = HMA_Slow_Length);

def BuyToOpenSignal=   HMA_Fast > HMA_Fast[1] and HMA_Fast > HMA_Slow;
def SellToCloseSignal=  HMA_Fast < HMA_Fast[1];

def SellToOpenSignal=  HMA_Fast < HMA_Fast[1] and HMA_Fast < HMA_Slow;
def BuyToCloseSignal=   HMA_Fast > HMA_Fast[1];

AddOrder(OrderType.BUY_TO_OPEN, BuyToOpenSignal and ShowBackTestPositions ,   open[-1], BackTestTradeSize, Color.CYAN, Color.CYAN, "");
AddOrder(OrderType.SELL_TO_CLOSE, SellToCloseSignal and ShowBackTestPositions, open[-1], BackTestTradeSize, Color.CYAN, Color.CYAN, "");

AddOrder(OrderType.SELL_TO_OPEN, SellToOpenSignal and ShowBackTestPositions,  open[-1], BackTestTradeSize, Color.RED, Color.RED,"");
AddOrder(OrderType.BUY_TO_CLOSE,  BuyToCloseSignal and ShowBackTestPositions,  open[-1], BackTestTradeSize, Color.RED, Color.RED,"");

def OpenOrders = GetQuantity();
AddLabel(yes, "     BUY      ", if BuyToOpenSignal[1]  and OpenOrders < 1 then CreateColor(153, 255, 153) else Color.White);
AddLabel(yes, "     SELL     ", if SellToOpenSignal[1]  and OpenOrders > -1 then CreateColor(255, 102, 102) else Color.White);
AddLabel(yes, "     CLOSE     ", if  (SellToCloseSignal[1] or BuyToCloseSignal) and OpenOrders != 0  then  Color.Yellow else Color.White);
Thanks. i will give it a try. I do not use Renko candles. These are just regular candles not Heikin.
 
Try this (untested) .. I see you have Renko candles on your chart .. this would change the strategy.

Code:
declare upper;
input HMA_Fast_Length = 9;
input HMA_Slow_Length = 21;
input ShowBackTestPositions = yes;
input BackTestTradeSize = 1;

def HMA_Fast = HullMovingAvg(price = close, length = HMA_Fast_Length);
def HMA_Slow = HullMovingAvg(price = close, length = HMA_Slow_Length);

def BuyToOpenSignal=   HMA_Fast > HMA_Fast[1] and HMA_Fast > HMA_Slow;
def SellToCloseSignal=  HMA_Fast < HMA_Fast[1];

def SellToOpenSignal=  HMA_Fast < HMA_Fast[1] and HMA_Fast < HMA_Slow;
def BuyToCloseSignal=   HMA_Fast > HMA_Fast[1];

AddOrder(OrderType.BUY_TO_OPEN, BuyToOpenSignal and ShowBackTestPositions ,   open[-1], BackTestTradeSize, Color.CYAN, Color.CYAN, "");
AddOrder(OrderType.SELL_TO_CLOSE, SellToCloseSignal and ShowBackTestPositions, open[-1], BackTestTradeSize, Color.CYAN, Color.CYAN, "");

AddOrder(OrderType.SELL_TO_OPEN, SellToOpenSignal and ShowBackTestPositions,  open[-1], BackTestTradeSize, Color.RED, Color.RED,"");
AddOrder(OrderType.BUY_TO_CLOSE,  BuyToCloseSignal and ShowBackTestPositions,  open[-1], BackTestTradeSize, Color.RED, Color.RED,"");

def OpenOrders = GetQuantity();
AddLabel(yes, "     BUY      ", if BuyToOpenSignal[1]  and OpenOrders < 1 then CreateColor(153, 255, 153) else Color.White);
AddLabel(yes, "     SELL     ", if SellToOpenSignal[1]  and OpenOrders > -1 then CreateColor(255, 102, 102) else Color.White);
AddLabel(yes, "     CLOSE     ", if  (SellToCloseSignal[1] or BuyToCloseSignal[1]) and OpenOrders != 0  then  Color.Yellow else Color.White);
So far this appears to work really well. I am testing /ES ATM using paper. I noticed the Close label turned yellow without a buy or sell to close signal. So far the close label has stayed yellow.

seJZpzp.jpg
 
Last edited:
So far this appears to work really well. I am testing /ES ATM using paper. I noticed the Close label turned yellow without a buy or sell to close signal. So far the close label has stayed yellow.

seJZpzp.jpg

I had a typo. Last line of code should read:

AddLabel(yes, " CLOSE ", if (SellToCloseSignal[1] or BuyToCloseSignal[1]) and OpenOrders != 0 then Color.Yellow else Color.White);
 
Great additional to the arsenal of tools that @dap711 is building out.

I am deploying a grid every morning with about 18 combination of different time and length parameters on tickers that I trade. (1,2,3 min charts & 4 to 8 in terms of lengths on the strategy setting - using ASAP original the main scanner) Looking for a shallow red patterns and a building green histogram. Helps identify pattern rather quickly and higher propensity of success when multiple time frames and lengths are showing green.

I let the MR run Friday, and it worked out really well on SPY.

Will try a HotKey - Stream Deck approach with multiple asset classes on Monday. (a little more manual, but could potentially give a better return on time)

Keep the ideas coming @dap711 :)
 
Eazy-Peezy:

Ruby:
declare upper;
input HMA_Length = 2;
input HMA_Displace = 2;
input ShowBackTestPositions = yes;
input BackTestTradeSize = 1;
input StartTrading = 0927;
input StopTrading = 1457;

def OkToTrade = secondsFromTime(StartTrading) > 0 and secondsFromTime(StopTrading)<0;
def HMA = HullMovingAvg(price = close, length = HMA_Length);
def BuyToOpenSignal=   HMA > HMA[HMA_Displace];
def SellToOpenSignal=  HMA < HMA[HMA_Displace];

AddOrder(OrderType.BUY_AUTO, BuyToOpenSignal and ShowBackTestPositions and OkToTrade,   open[-1], BackTestTradeSize, Color.CYAN, Color.CYAN, "");
AddOrder(OrderType.SELL_AUTO, SellToOpenSignal and ShowBackTestPositions and OkToTrade,  open[-1], BackTestTradeSize, Color.RED, Color.RED,"");
AddOrder(OrderType.SELL_TO_CLOSE, secondsFromTime(StopTrading)> 0 and ShowBackTestPositions, open[-1], BackTestTradeSize, Color.CYAN, Color.CYAN, "");
AddOrder(OrderType.BUY_TO_CLOSE, secondsFromTime(StopTrading)> 0 and ShowBackTestPositions,  open[-1], BackTestTradeSize, Color.RED, Color.RED,"");

def OpenOrders = GetQuantity();
AddLabel(yes, "     BUY      ", if BuyToOpenSignal[1]  and OpenOrders < 1 and OkToTrade then CreateColor(153, 255, 153) else Color.White);
AddLabel(yes, "     SELL     ", if SellToOpenSignal[1]  and OpenOrders > -1 and OkToTrade then CreateColor(255, 102, 102) else Color.White);
AddLabel(yes, "     CLOSE     ", if  secondsFromTime(StopTrading)> 0 and OpenOrders != 0  then  Color.Yellow else Color.White);
Hey @dap711 I tested this today using Option chains and the label kept staying their trigger color.

jczVXDi.jpg
 
Testing in Paper and the Hull strat is pretty dang good. only issue is that my labels keep staying their color. Testing on SPX options chain and was working great but then the sell label never reset to white.. Also, another observation. I hope this can be addressed. I would like for the Trigger to happen at the crossover of the Hull MAs. Like in the picture example I provided. What is actually happening is triggers are happening in many various places. Sometimes the trigger is right on the HMA (SLOW) TO BUY, other times the trigger is so far away from the cross that it results in a loss. Same with the close triggers. Is there a way to make this constant (maybe with a bar displacement option) Also would be great to show the HMAs on the chart so we can double check the entries/exits. Maybe an input option to show or not.
Example:
<blockquote class="imgur-embed-pub" lang="en" data-id="9Y8XOPF"><a href=" ">View post on imgur.com</a></blockquote><script async src="//s.imgur.com/min/embed.js" charset="utf-8"></script>
 
Last edited:
@METAL @dap711 - with quite a few variations of Dap711 work out there, would love to hear which one of these you are seeing produce most consistent results. I continue to test the ASAP original with my morning grid set up (mentioned above) and find tight 1 penny bid/ask spreads stocks to deploy it on. Last week was hit and miss with a few chop days, but overall the ASAP OG strategy is producing good results.

Have you guys tried to program MR to run multiple assets at once ?
@dap711 - have you played around with the Option Alpha Bots service at all? Is it possible to recreate your work via their Bot service ?

Would love to hear your progress.
 
No, it does not repaint.

Repaint .. what a subject .. I've noticed that almost all the MTF indicators in ToS are not written correctly. Bold statement right? There is a simple way write MTF code that will overcome repainting. Let's take a moving average for example. A 100 period moving average on the one minute chart is the same as a 20 period moving average on the 5 minute chart (100 divided by 5 min = 20 .. try it if you don't believe me!), so to show the 20 period five minute on a one minute chart, just do a 100 period and the indicator won't repaint! Couldn't be easier! You can apply the same rule to almost any indicator based on price movement and some that aren't. There are other ways to overcome repainting, but those are my trade secrets .. LOL

mod note:
On a daily chart a 50 SMA would equal a 10 SMA on a weekly chart
On a 1 Min Chart a 50 SMA would equal a 10 SMA on a 5 Min Chart
On a 1 Min Chart a 300 SMA would equal a 10 SMA on a Hourly Chart
On a 1 Min Chart a 1950 SMA would equal a 10 SMA on a Daily Chart
So what would be the MA on a 5-minute chart which represents a 21 Ma on a daily chart?
 
So what would be the MA on a 5-minute chart which represents a 21 Ma on a daily chart?
Well that’s one hell of a gap between timeframes…
Don’t think it’s gunna work out… would be 6048 if using 24hr day

MA on Lower TF chart = (Higher Timeframe Length / Lower Timeframe Length) x EMA Period
 
I get an "Invalid statement: AddLabel at 55:1" Any thoughts? Thanks in advance. - Jim
I agree. The 1st script in the 1st post is riddled with syntax errors.
The OP is no longer active on this forum, so he isn't available to provide a working version.
Maybe one of the other members of this thread will see your plight and offer a shared link to a working script.
 

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
266 Online
Create Post

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

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?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top