Of the various smoothing methods commonly available to traders and technical analysts,
Code: Select all
{ TSM Moving Average : Moving average system
Copyright 2025 P J Kaufman. All rights reserved. }
{ strategy (1 - 10) as follows:
1. Moving average
2. Average-off
3. Weighted moving average
4. Triangular moving average
5. Pivot point moving average
6. Standard deviation moving average
7. Moving median
8. Geometric moving average
9. Exponential moving average
10. Linear regression
}
input: strategy(1), period(20), SDvalue(0.05),
usefutures(false), longonly(false);
vars: signal(0), psignal(0), trend(0), size(1), ix(0), INT
middle(0),
value(0), SD(0), SDpercent(0), prod(0), root(0),
smooth(0), newprice(0), sumofprices(0), sumofweights(0),
INT start(0), stockinvestment(10000), futuresinvestment(25000),
ATRper(20), equity(0), adate(" "), totalPL(0),
NAV(100),
returns(0), cumreturn(0), todayPL(0), totalLong(0), totalShort(0);
array: pricearray[150](0), sortprices[150](0), sortedarray[150](0);
if usefutures then
size = futuresinvestment/(Avgtruerange(ATRper
)*bigpointvalue)
else
size = stockinvestment/close;
// 1. Moving Average
if strategy = 1 then begin
trend = average(close,period);
end;
// 2. Average off
if strategy = 2 then begin
If Currentbar = 1 then
trend = average(close,period)
else
trend = ((period - 1)*trend[1] + close)/period;
end;
// 3. Weighted moving average
if strategy = 3 then begin
sumofprices = 0;
sumofweights = 0;
for ix = 0 to period - 1 begin
sumofweights = sumofweights + ix + 1;
sumofprices = sumofprices + (ix+1)*close[ix];
end;
trend = sumofprices/sumofweights;
end;
// 4. Triangulation
if strategy = 4 then begin
middle = period/2;
// increase weights towards middle
sumofprices = 0;
sumofweights = 0;
// prices increase towards middle
for ix = 0 to middle begin
sumofweights = sumofweights + ix + 1;
sumofprices = sumofprices + (ix+1)*close[ix];
end;
value = 0;
for ix = period to middle - 1 begin
value = value + 1;
sumofweights = sumofweights + value;
sumofprices = sumofprices + value*close[ix];
end;
trend = sumofprices/sumofweights;
end;
// 5. Pivot-Point moving average
if strategy = 5 then begin
start = period - period/3;
sumofprices = 0;
sumofweights = 0;
value = start + 1;
for ix = 0 to period - 1 begin
value = value - 1;
sumofweights = sumofweights + value;
sumofprices = sumofprices + value*close[ix];
end;
trend = sumofprices/sumofweights;
end;
// 6. Standard deviation moving average
if strategy = 6 then begin
SD = stddev(close,period);
SDpercent = (SD - SD[1])/SD;
trend = average(close,period) +
SDvalue*SDpercent;
end;
// 7. Moving median
if strategy = 7 then begin
// move prices to array
for ix = 0 to period - 1 begin
pricearray[ix + 1] = close[ix];
end;
value1 = sortarray(pricearray,period,1);
trend = pricearray[period/2];
end;
// 8. Geometric moving average
if strategy = 8 then begin
prod = 1;
for ix = 0 to period - 1 begin
prod = prod*close[ix];
end;
root = 1/period;
trend = power(prod,root);
end;
// 9. Exponential smoothing
if strategy = 9 then begin
smooth = 2/(period + 1);
trend = trend[1] + smooth*(close - trend[1]);
end;
// 10. Linear regression, slope increasing or decreasing
if strategy = 10 then begin
trend = linearregslope(close,period);
if trend > 0 then begin
buy to cover all shares next bar on open;
buy size share next bar on open;
signal = 1;
end
else if trend < 0 then begin
sell all shares next bar on open;
if longonly = false then begin
sell short size share next bar on open;
end;
signal = -1;
end;
end;
// common entry code
if strategy <> 10 then begin
if trend > trend[1] then signal = 1
else if trend < trend[1] then signal = -1;
// new long position
if psignal <= 0 and signal > 0 then begin
buy to cover all shares next bar on open;
buy size shares next bar on open;
end
else if psignal > 0 and signal < 0 then begin
sell all shares next bar on open;
if longonly = false then begin
sell short size shares next bar on
open;
end;
end;
end;
psignal = signal;
equity = netprofit + openpositionprofit;
todayPL = equity - equity[1];
if psignal > 0 then
totalLong = totalLong + todayPL
else if psignal < 0 then
totalShort = totalShort + todayPL;
totalPL = totalPL + equity - equity[1];
returns = (equity - equity[1])/stockinvestment;
cumreturn = cumreturn + returns;
if usefutures = false then
NAV = 100
else
NAV = NAV[1]*(1 + returns);
adate = ELdatetostring(date);
If Currentbar = 1 then print(file("c:\TradeStation\
Smoothing.csv"),
"Date,Close,Signal,Size,TotalShort,TotalLong,To
talPL,Returns,CumReturn,NAV");
print(file("c:\TradeStation\Smoothing.csv"),adate, ",",
close:8:6, ",",
signal:5:0, ",", size:8:3, ",", totalShort:8:0, ",", totalLong:8:0, ",",
totalPL:8:0, ",", returns:6:4, ",", cumreturn:8:4, ",",
NAV:9:4);