Multiple aggregations in strategy


Category:
0
0

Hi Pete. First I just want to offer a sincere thank you for this great resource, for your generosity, and for all you do for the community. You are to be commended sir.

Now on to my question. I’m in the process of learning how to code while at the same time, building a multi-timeframe strategy. I’m making good progress but I’ve hit a roadblock and can’t seem to find the solution. Everything is fine until I try to enter the different aggregation periods. Here’s a small portion of the code that is causing me problems. I would really appreciate your expertise on this. Thank you so much!

def agg1 = AggregationPeriod.MIN;
def agg2 = AggregationPeriod.TWO_MIN;
def agg5 = AggregationPeriod.FIVE_MIN;

input smaPrice = close;
input smaLength = 20;

def SMA1 = Average(smaPrice, smaLength, (period == agg1));

def SMA2 = Average(smaPrice, smaLength, (period == agg2));

def SMA5 = Average(smaPrice, smaLength, (period == agg5));

Marked as spam
Posted by (Questions: 5, Answers: 9)
Asked on June 10, 2020 8:21 pm
656 views
0
Private answer

Whoa! Slow down. These two things are like water and oil:

  1. I’m in the process of learning how to code
  2. while at the same time, building a multi-timeframe strategy

The average person might spend 3-5 years of writing basic common chart studies before they are ready to attempt working with multiple aggregation periods. Likewise, one might expect 3-5 years of writing basic common chart studies before they are ready to attempt building chart strategies.

But to combine these two? That takes a very long time indeed. (just because you can, doesn't mean you should) But so long as you love to learn by bashing your skull against the keyboard then I say "Go For It!". That's how you learn, by making mistakes and fixing them. So how about we let you fix this one?

First item on the list. How many arguments does the function named "Average()" accept? Need some help?

https://toslc.thinkorswim.com/center/reference/thinkScript/Functions/Tech-Analysis/Average

Now, how many arguments did you try to include in your statements?

def SMA1 = Average(smaPrice, smaLength, (period == agg1));

I count three. Busted right there. The language reference clearly tells us there are only two arguments for that function. First argument is the price and second argument is the length.

So you have to write that statement with only two arguments:

def SMA1 = Average(close, smaLength);

But that still doesn't get you to the aggregation period you assigned to the variable named "agg1". Which just so happens to be the very lowest aggregation period you can assign to a chart. So... that was pointless. Ok, let's do that again.

So you have to write that statement with only two arguments:

def SMA2 = Average(close, smaLength);

There, for now we'll assume the chart is set to 1 min time frame and all we want is a 20 period simple moving average of the close from the 2 min time frame. How do we get there from here?

Need a hint?

https://toslc.thinkorswim.com/center/reference/thinkScript/tutorials/Advanced/Chapter-11---Referencing-Secondary-Aggregation

Write, so then we have the following:

def SMA2 = Average(close(period = agg2), smaLength);

Wonderful. We have solved the very first issue that was preventing you from completing this. Are you ready for the next problem to solve? That statement references the current value of the 2 min bar's 20 period simple moving average. What happens if that bar changes direction between the first 1 min bar and the second 1 min bar? Busted and broken. Imagine how bad it gets when you reference the 5 min data? Five 1 min candles, each one able to present and then vanish a valid signal. MADNESS!!!

When constructing chart strategies for back testing, ones that use data from higher time frames, you must construct them using the previous higher time frame data. Otherwise your signals can repaint. All you get from chart strategies that reference the current data of the higher time frames is pure garbage. Signals that can never by repeated in a live market. Because it creates a leak into the future. Oh man... you have no idea how far up the creek you are right now.

Best thing I can recommend is that you turn around and go the opposite direction. Go fishing, watch a movie, have a beer.... However if you happen to be one of those crazy wackjobs that loves to bash their skull against the keyboard (that's me). Well then you can get all the details on how to get out of the rabbit hole and on to the straight and narrow path right here:

https://www.hahn-tech.com/thinkorswim-strategy-guide-mtf/

 

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on June 10, 2020 9:34 pm
0
Pete, you are the BEST! Thank you so much for your quick and detailed response to my question. You were extremely helpful and even made me laugh in the process. And yes, I guess I am one of those people you referenced that likes to bash their skull against the keyboard. I’ve done that on more than one occasion since I started this journey of attempting to learn how to thinkscript. Thankfully I have a hard head. I fully understand everything you wrote, and I had also recently viewed the “strategy-guide” video you referenced in your post, along with a number of other excellent video’s you have posted. In fact, it was those very video’s that led me to believe I could do this on my own, so this is all your fault! (lol). That being said, based on results it would probably be a good idea for me to watch it again which I will absolutely do first chance I get, so thank you for the suggestion. Thank you again sir and good health to you always.
( at June 11, 2020 3:14 am)
0
Glad to know this was helpful. Be very careful to learn the correct way to build strategies. Thinkorswim does not have any safeguards in place to prevent a chart strategy from producing back test results that can never be repeated in live trading. Whereas TradeStation enforces very strict limits that make is impossible to do so. One of the pitfalls with Thinkorswim is that it is all to easy to miss-align your entry and exit levels, even to the extent that theoretical orders can post an entry or exit level that does not even exist in the price data! Details here: https://www.hahn-tech.com/ans/parablic-sar-signal-not-firing-in-tos-strategy/
( at June 11, 2020 9:06 am)
0
Pete, thank you so much for your helpful advice and for the link. I read the post associated with that link, and I think I understand it, but I'm still mentally processing it. And I will absolutely heed your warning. Thank you again!
( at June 11, 2020 5:22 pm)