Displaced Moving Average Strategy


Category:
0
0

Hello all,

I’ve been doing some backtesting on the “MovAvgStrat” that comes with the ToS platform, but I’d like to be able to displace the MA in addition to being able to set the length.

I attempted to create my own script with copy and pasting the original ToS script and then adding “input displace = 0;” to the MA criteria, but since this is a locked script, I’m assuming there’s actually more code then what I’m seeing.

 

Any thoughts?

 

Original ToS code for “MovAvgStrat”:

#
# TD Ameritrade IP Company, Inc. (c) 2013-2019
#

input price = close;
input length = 15;
input averageType = AverageType.SIMPLE;
input mode = {default “Trend Following”, “Reversal”};

plot Avg = MovingAverage(averageType, price, length);
Avg.SetDefaultColor(GetColor(0));

def crossAbove = price crosses above Avg;
def crossBelow = price crosses below Avg;
def buy;
def sell;
switch (mode) {
case “Trend Following”:
buy = crossAbove;
sell = crossBelow;
case “Reversal”:
buy = crossBelow;
sell = crossAbove;
}

AddOrder(OrderType.BUY_AUTO, buy, tickcolor = GetColor(2), arrowcolor = GetColor(2), name = “MovAvgStratLE”);
AddOrder(OrderType.SELL_AUTO, sell, tickcolor = GetColor(1), arrowcolor = GetColor(1), name = “MovAvgStratSE”);

 

Marked as spam
Posted by (Questions: 2, Answers: 1)
Asked on January 26, 2020 11:03 pm
663 views
0
Private answer

There is nothing hidden behind the scenes in that code. If you want to add a displacement input to the strategy you can simply examine the code for the chart study named "SimpleMovingAvg". Here are the first few line of code from that built-in study:

input price = close;
input length = 9;
input displace = 0;
input showBreakoutSignals = no;
plot SMA = Average(price[-displace], length);

Using this example, you should be able to see how the displace input is used within the line that generates the moving average.

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on January 27, 2020 9:25 am