# Author - Dwain Pattillo 2022
#1. Set the Trend MA type, price, and length. We only take buys if price is above the trend line and sells below it.
#2. Adjust the MA length to achieve the best profit from the floating P/L.
#3. Adjust the MA displacement for the highest profit.
#4. Repeat steps 2 and 3 until you are satisfied with the results. Now you see where this is going! LOL
#5. Share you insights for the best time frames and settings to help everyone make some money!
input BackTestTradeSize = 1;
input ShowBackTestPositions = yes;
input TrendAvgType = AverageType.EXPONENTIAL;
input TrendPrice = CLOSE;
input TrendLength = 200;
input UseVolumeAsFilter = yes;
input VolumeLength = 200;
input MAType = AverageType.HULL;
input MAPrice = Open;
input MALength = 2.1;
input MADisplace = 1;
input AlertOnSignal = no;
input ShowAutoKeyLabels = yes; #(I use Macro Recorder to auto trade. Ask me about the setup at [email protected])
#Average the volume and only open trades when volume is higher than the average.
def VolStrength = lg(volume[1]) - lg(SimpleMovingAvg(volume[1], VolumeLength));
#MA Magic goes here
def Diff = MovingAverage(MAType, MAPrice, MALength*100) - MovingAverage(MAType, MAPrice, MALength*100)[MADisplace];
#Trendline
plot MA = MovingAverage(TrendAvgType, TrendPrice, TrendLength);
MA.DefineColor("Up", GetColor(1));
MA.DefineColor("Down", GetColor(0));
MA.AssignValueColor(if MA > MA[1] then MA.color("Up") else MA.color("Down"));
#Add the signal labels (use with Macro Recorder or not)
def OpenOrders = GetQuantity();
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));
AddLabel(ShowAutoKeyLabels and ( diff[1] < diff[2] or close < MA ) and OpenOrders > 0, " CLOSE ", CreateColor(102, 0, 0));
AddLabel(ShowAutoKeyLabels and diff[1] < diff[2] and high < MA and OpenOrders > -1 and (if UseVolumeAsFilter then VolStrength>0 else yes), " SELL ", CreateColor(255, 102, 102));
AddLabel(ShowAutoKeyLabels and ( diff[1] > diff[2] or close > MA ) and OpenOrders < 0 , " CLOSE ", CreateColor(102, 0, 0));
#Sound the bell. (If alerts are turn on)
Alert(diff[1] > diff[2] and low > MA and OpenOrders < 1 and AlertOnSignal, "Buy Open Signal", Alert.Bar, Sound.Ding);
Alert( ( diff[1] < diff[2] or close < MA ) and OpenOrders > 0 and AlertOnSignal, "Buy Close Signal", Alert.Bar, Sound.Bell);
Alert(diff[1] < diff[2] and high < MA and OpenOrders > -1 and AlertOnSignal, "Sell Open Signal", Alert.Bar, Sound.Ding);
Alert( ( diff[1] > diff[2] or close > MA ) and OpenOrders < 0 and AlertOnSignal, "Sell Close Signal", Alert.Bar, Sound.Bell);
AddLabel(UseVolumeAsFilter, if VolStrength > 0 then " Trading allowed. Volume Strength: " + (VolStrength) + " " else " Trading isn't allowed - Volume Strength: " + (VolStrength) + " " , if (VolStrength < 0) then Color.RED else Color.Red);