Thinkscript strategy translation


Category:
0
0

Hi Pete, Thanks for all the great resources.  I am new to thinkscript and have been working on a EMA cross strategy in TOS that I have pieced together from several different resources online.  Below is the entire script:

input tradeSize = 100;

def signal = MovAvgExponential(“length” = 22).”AvgExp” crosses above MovAvgExponential(“length” = 40).”AvgExp”
and MovAvgExponential(“length” = 30).”AvgExp” is less than close(“period” = AggregationPeriod.MONTH);
addOrder(orderType.BUY_TO_OPEN, signal, open[-100], tradeSize, color.CYAN, color.CYAN);

def exit = MovAvgExponential(“length” = 22).”AvgExp” crosses below MovAvgExponential(“length” = 40).”AvgExp”;

addOrder(orderType.SELL_TO_CLOSE, exit, open[-100], tradeSize, color.MAGENTA, color.MAGENTA);

It’s not too long, so I included it with altered values for context.

The section I’m not fully understanding and would really appreciate if you could translate for me what the code is doing is this part:

and MovAvgExponential(“length” = 30).”AvgExp” is less than close(“period” = AggregationPeriod.MONTH);
addOrder(orderType.BUY_TO_OPEN, signal, open[-1], tradeSize, color.CYAN, color.CYAN);

If you could help me translate what this code is actually doing on the chart it would really help my understanding with this strategy and learning thinkscript.  Thanks so much

Marked as spam
Posted by (Questions: 1, Answers: 0)
Asked on February 18, 2020 1:51 am
501 views
0
Private answer

The "plain English" translation of the following code:

and MovAvgExponential(“length” = 30).”AvgExp” is less than close(“period” = AggregationPeriod.MONTH);
addOrder(orderType.BUY_TO_OPEN, signal, open[-1], tradeSize, color.CYAN, color.CYAN);

"and the 30 period exponential moving average is less than the close of the current month"

"buy to open at the opening price of the bar that immediately follows the signal"

This code is completely incompatible with a properly functioning chart strategy. The reason for this is that the code is reading from a higher time frame (assuming the chart is set to a time frame less than Monthly). And when you reference higher time frames in chart strategies it is absolutely required that you reference the previous candle of the higher time frame and never the current candle of the higher time frame.

Marked as spam
Posted by (Questions: 37, Answers: 4084)
Answered on February 18, 2020 9:19 am