Moving Average Master Strategy for ThinkOrSwim

So, I've been trying to figure out a way to overcome the "stuck" label generation issue when trading paper. I think the best fix is to do crossovers instead of < and >.

Example:
AddLabel(ShowAutoKeyLabels and diff[1] > diff[2] and low > MA and OpenOrders < 1 and (if UseVolumeAsFilter then VolStrength>0 else yes), " BUY ", CreateColor(153, 255, 153));

Change to
AddLabel(ShowAutoKeyLabels and diff[1] crosses above diff[2] and low > MA and OpenOrders < 1 and (if UseVolumeAsFilter then VolStrength>0 else yes), " BUY ", CreateColor(153, 255, 153));

This way the label would only be generated for the one candle. You would then need to set the "Wait" in Macro Recorder to the same amount of time as the candles on the chart. Trading one minute, wait one minute .. ect. With those 2 modifications, you would be able to trade paper without issue.

Dwain
 

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

Has anyone ever used ZigZagHighLow as a trend indicator? I'm thinking that would eliminate the need to optimize a moving average to get the trend. Just thinking out loud .. :)
 
1. I tune before and during the day on a daily bases. Today I chose SPY to trade. I was expecting a big bounce from Fridays market. I started with the one minute chart and started losing. I went into the red $180, so I switch to the 5 minute chart and re-optimized. Made the $150 Back and closed the day $268 in the green. (If I had only taken a little more to optimize, I could have had a green $400 day.)
2. Yes, there isn't a limit to number "labels" or positions it can monitor. The only draw back I have found about the software, is that you can only have one instance of it open at a time.
Thank you for sharing your approach.
Just curious, what is the advantage in running a strategy vs the indicator? There's still a way to produce labels and arrows? Or..you leave the strategy on for tuning P/L?
 
Thank you for sharing your approach.
Just curious, what is the advantage in running a strategy vs the indicator? There's still a way to produce labels and arrows? Or..you leave the strategy on for tuning P/L?
A strategy has the ability to show you the back test. An indicator/study doesn't. Would you take a signal from an indicator without knowing the past performance? It "looks good" just isn't enough in todays markets. ;)
 
Todays trading is on QQQ. Here are my settings:

Trend Avg Type: HULL
Trend Price: CLOSE
Trend Length: 97
Use Volume as Filter: NO
Volume Length: 200
MA Type: SIMPLE
MA Price: OPEN
MA Length: 1.8
MA Displace: 1
 
Last edited:
Todays trading is on QQQ. Here are my settings:

Trend Avg Type: HULL
Trend Price: CLOSE
Trend Length: 97
Use Volume as Filter: NO
Volume Length: 200
MA Type: SIMPLE
MA Price: OPEN
MA Length: 1.8
MA Displace: 1
How are you doing so far? Just got home so I can trade. Also, I do not see the Volume length option. I downloaded your last link on page 2.
I also just noticed a bug, I think. When I put 1.8 as MA length, it resets to 1.
 
Last edited:
Top settings are not working because of the consolidation right now. Moving to higher time frame.

QQQ M15 chart

Trend Avg Type: HULL
Trend Price: CLOSE
Trend Length: 25
Use Volume as Filter: NO
Volume Length: 200
MA Type: SIMPLE
MA Price: OPEN
MA Length: 3.0
MA Displace: 2
 
How are you doing so far? Just got home so I can trade. Also, I do not see the Volume length option. I downloaded your last link on page 2.
I also just noticed a bug, I think. When I put 1.8 as MA length, it resets to 1.
Not sure what you mean by reset? .. If you typed the 1.8 and didn't hit "Apply" .. ToS won't keep the setting. Type 1.8 .. then click in another box .. then click Apply.
 
I'm testing a new mod .. I added SMI as a filter to stay out of trades while the market is consolidating (ranging) .. So far it's looking good. I'll add the update to post one after market close.
 
Last edited:
Top settings are not working because of the consolidation right now. Moving to higher time frame.

QQQ M15 chart

Trend Avg Type: HULL
Trend Price: CLOSE
Trend Length: 25
Use Volume as Filter: NO
Volume Length: 200
MA Type: SIMPLE
MA Price: OPEN
MA Length: 3.0
MA Displace: 2
Hey Dwain, I do not have the Volume Length option available. Also, How many days back do you set your chart for testing?
 
Not sure what you mean by reset? .. If you typed the 1.8 and didn't hit "Apply" .. ToS won't keep the setting. Type 1.8 .. then click in another box .. then click Apply.
It just changes back to 1. I assume a whole number. I type in 1.8 and it automatically turns back to 1. I have tried other decimals and all of them turn back to a whole number. I am typing it in and selecting another box so it stays and that is when it turns back. Also, enter works for setting the input option in the box.
 
Check out this chart. I used a 2 min. QQQ. And I couldn't get the sell to happen where I circled blue. Do you mind checking to see if you can get better and let me know what I did wrong.
gdLeP9m.jpg

Here is my settings.
V6xd0jQ.jpg
 
Check out this chart. I used a 2 min. QQQ. And I couldn't get the sell to happen where I circled blue. Do you mind checking to see if you can get better and let me know what I did wrong.
gdLeP9m.jpg

Here is my settings.
V6xd0jQ.jpg

You need the latest version in post one. The new version will fix your decimal problem as well.

I think I may have stumbled onto something amazing using SMI .. working on a whole new strategy and will be testing it tomorrow. Stay tuned! If I had been trading 100 shares of Tesla today with the new strategy, I would have made $1,930.82 (168 orders) and the new strategy has only 1 setting to optimize!
 
Last edited:
@dap711
I am highly impressed of your strategy and your video explanation was awesome...
Thank you very much
Please, keep the great work.....(y)

Could you, please answer my question :
Why I don't have the "trend period" display?
What I am doing wrong with the application of your code from the post#2 ?

Thank you in advance:)
 
@dap711
I am highly impressed of your strategy and your video explanation was awesome...
Thank you very much
Please, keep the great work.....(y)

Could you, please answer my question :
Why I don't have the "trend period" display?
What I am doing wrong with the application of your code from the post#2 ?

Thank you in advance:)

@ziko Thanks for the kind words.

The current version doesn't have a "trend period". It does have a "Trend Length" which would be the same thing .. please make sure you have latest code from post one.
 
I'm beta testing this new strategy if you would like to join me. If there is enough interest, I'll start a new thread.

Ruby:
# Author Dwain Pattillo - 2023

# Q. Why do I share my strategies?
# A. It would be completely and totally silly of me (and you!) to keep them to myself. Who remembers what happen with GameStop? The more people doing the exact same thing at the same time in the market, the more power they have in moving the market in their favor! If we had an army of traders using the same system, oh what power we would have! We (the retail trader) could take the power back from the market makers!

# 1. Adjust the length setting to the best FloatingPL and then let'r rip!

# In most cases, the lower the timeframe of the chart, the lower the length will be .. I trade one minute and a good starting length is 4.
# If you are auto trading 24/5 on the futures or forex, I would suggest turning on the volume filter and optimizing it, otherwise leave it off.
# Don't stop adjusting the length just because the P/L goes down! It's almost like you are tuning the strategy to the Elliot waves of the market.

input BackTestTradeSize = 1;
input ShowBackTestPositions = yes;
input Length = 4;
input UseVolumeAsFilter = no;
input VolumeLength = 600;
input AlertOnSignal = no;

#Average the volume and only open trades when volume is higher than the average.
def VolStrength = lg(volume[1]) - lg(SimpleMovingAvg(volume[1], VolumeLength));

#Add your indicator code here (if you want to use this as a template) :)   
def min_low = Lowest(low, Length+1);
def max_high = Highest(high, Length+1);
def rel_diff = close - (max_high + min_low) / 2;
def range = max_high - min_low;
def avgrel = ExpAverage(ExpAverage(rel_diff, Length), Length);
def avgdiff = ExpAverage(ExpAverage(range, Length), Length);
def SMI = if avgdiff != 0 then avgrel / (avgdiff / 2) * 100 else 0;

#Define the signals
def BuyToOpenSignal   =  SMI > SMI[1] and (if UseVolumeAsFilter then VolStrength>0 else yes);
def SellToCloseSignal = ( SMI < SMI[1] );
def SellToOpenSignal  = SMI < SMI[1]  and (if UseVolumeAsFilter then VolStrength>0 else yes);
def BuyToCloseSignal  = ( SMI > SMI[1] );

#Open the orders on the chart for back testing and optimizing the setting
AddOrder(OrderType.BUY_AUTO, BuyToOpenSignal and ShowBackTestPositions, open[-1], BackTestTradeSize, Color.CYAN, Color.CYAN, "");
AddOrder(OrderType.SELL_AUTO, SellToOpenSignal and ShowBackTestPositions, open[-1], BackTestTradeSize, Color.RED, Color.RED,"");

#Add the signal labels to the chart
def OpenOrders = GetQuantity();
AddLabel(BuyToOpenSignal[1]  and OpenOrders < 1 ,  "     BUY      ", CreateColor(153, 255, 153));
AddLabel(SellToOpenSignal[1]  and OpenOrders > -1, "     SELL     ", CreateColor(255, 102, 102));

#Sound the bell. (If alerts are turn on)
Alert(BuyToOpenSignal[1]  and OpenOrders < 1 and AlertOnSignal, "Buy Open Signal", Alert.Bar, Sound.Ding);
Alert(SellToOpenSignal[1]  and OpenOrders > -1 and AlertOnSignal, "Sell Open Signal", Alert.Bar, Sound.Ding);

#“Make everything as simple as possible, but not simpler.” Albert Einstein.
 
# Q. Why do I share my strategies?
# A. It would be completely and totally silly of me (and you!) to keep them to myself. Who remembers what happen with GameStop?
The more people doing the exact same thing at the same time in the market, the more power they have in moving the market in their favor!
If we had an army of traders using the same system, oh what power we would have! We (the retail trader) could take the power back from the market makers!

The OP is achieving his goal of creating an army of traders.
This thread has had over 2000 views in just 5 days!
It is the most viral thread of 2023! 👏
 
I'm beta testing this new strategy if you would like to join me. If there is enough interest, I'll start a new thread.

Ruby:
# Author Dwain Pattillo - 2023

# Q. Why do I share my strategies?
# A. It would be completely and totally silly of me (and you!) to keep them to myself. Who remembers what happen with GameStop? The more people doing the exact same thing at the same time in the market, the more power they have in moving the market in their favor! If we had an army of traders using the same system, oh what power we would have! We (the retail trader) could take the power back from the market makers!

# 1. Adjust the length setting to the best FloatingPL and then let'r rip!

# In most cases, the lower the timeframe of the chart, the lower the length will be .. I trade one minute and a good starting length is 4.
# If you are auto trading 24/5 on the futures or forex, I would suggest turning on the volume filter and optimizing it, otherwise leave it off.
# Don't stop adjusting the length just because the P/L goes down! It's almost like you are tuning the strategy to the Elliot waves of the market.

input BackTestTradeSize = 1;
input ShowBackTestPositions = yes;
input Length = 4;
input UseVolumeAsFilter = no;
input VolumeLength = 600;
input AlertOnSignal = no;

#Average the volume and only open trades when volume is higher than the average.
def VolStrength = lg(volume[1]) - lg(SimpleMovingAvg(volume[1], VolumeLength));

#Add your indicator code here (if you want to use this as a template) :)   
def min_low = Lowest(low, Length+1);
def max_high = Highest(high, Length+1);
def rel_diff = close - (max_high + min_low) / 2;
def range = max_high - min_low;
def avgrel = ExpAverage(ExpAverage(rel_diff, Length), Length);
def avgdiff = ExpAverage(ExpAverage(range, Length), Length);
def SMI = if avgdiff != 0 then avgrel / (avgdiff / 2) * 100 else 0;

#Define the signals
def BuyToOpenSignal   =  SMI > SMI[1] and (if UseVolumeAsFilter then VolStrength>0 else yes);
def SellToCloseSignal = ( SMI < SMI[1] );
def SellToOpenSignal  = SMI < SMI[1]  and (if UseVolumeAsFilter then VolStrength>0 else yes);
def BuyToCloseSignal  = ( SMI > SMI[1] );

#Open the orders on the chart for back testing and optimizing the setting
AddOrder(OrderType.BUY_AUTO, BuyToOpenSignal and ShowBackTestPositions, open[-1], BackTestTradeSize, Color.CYAN, Color.CYAN, "");
AddOrder(OrderType.SELL_AUTO, SellToOpenSignal and ShowBackTestPositions, open[-1], BackTestTradeSize, Color.RED, Color.RED,"");

#Add the signal labels to the chart
def OpenOrders = GetQuantity();
AddLabel(BuyToOpenSignal[1]  and OpenOrders < 1 ,  "     BUY      ", CreateColor(153, 255, 153));
AddLabel(SellToOpenSignal[1]  and OpenOrders > -1, "     SELL     ", CreateColor(255, 102, 102));

#Sound the bell. (If alerts are turn on)
Alert(BuyToOpenSignal[1]  and OpenOrders < 1 and AlertOnSignal, "Buy Open Signal", Alert.Bar, Sound.Ding);
Alert(SellToOpenSignal[1]  and OpenOrders > -1 and AlertOnSignal, "Sell Open Signal", Alert.Bar, Sound.Ding);

#“Make everything as simple as possible, but not simpler.” Albert Einstein.

Please continue sharing your findings and thoughts.
Happy to test along with other tools I use.
Hope others will support and join for a ride!
 

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
543 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