
I've been using TOS for quite awhile, and I like to see MA cross overs on the chart...but it seemed a bit bulky to keep adding MA's then the crossovers for each.
Here's what, I think, is a bit of a short cut and allows for 4 different MA's to be plotted (using simple, exponential, weighted, and hull types) with the ability to
allow any of the MA's to show arrows when there is a MA cross.
It's one script instead of multiple.
I'd like to submit it to the useThinkScript community as a thank you for everyone else's much better work, and I hope it has enough value to be added to the great list of useable scripts.
mod note:
Benefits:
Moving average crossovers in day trading offer the benefit of simplifying trend identification and generating potential trading signals. By observing how shorter-term moving averages cross longer-term moving averages, traders can get a quick visual representation of market direction and potential trend changes, aiding in decision-making.
Dangers:
When you see moving averages bunched up close together on your chart, it's generally a signal to be cautious, as trend-following strategies are less effective and whipsaws are common. Wait for the moving averages to start separating and sloping in the same direction.
Code:
# Dual MA Crossover Arrows with Organized Plot Window
input price = close;
# --- MA Inputs ---
input length1 = 9;
input averageType1 = AverageType.Simple;
input length2 = 21;
input averageType2 = AverageType.Exponential;
input length3 = 50;
input averageType3 = AverageType.Weighted;
input length4 = 200;
input averageType4 = AverageType.Hull;
# --- MA Calculations ---
def MA1 = MovingAverage(averageType1, price, length1);
def MA2 = MovingAverage(averageType2, price, length2);
def MA3 = MovingAverage(averageType3, price, length3);
def MA4 = MovingAverage(averageType4, price, length4);
# --- Plot Moving Averages First ---
plot PlotMA1 = MA1;
PlotMA1.SetDefaultColor(Color.CYAN);
PlotMA1.SetLineWeight(2);
PlotMA1.SetStyle(Curve.FIRM);
plot PlotMA2 = MA2;
PlotMA2.SetDefaultColor(Color.MAGENTA);
PlotMA2.SetLineWeight(2);
PlotMA2.SetStyle(Curve.SHORT_DASH);
plot PlotMA3 = MA3;
PlotMA3.SetDefaultColor(Color.YELLOW);
PlotMA3.SetLineWeight(2);
PlotMA3.SetStyle(Curve.LONG_DASH);
plot PlotMA4 = MA4;
PlotMA4.SetDefaultColor(Color.WHITE);
PlotMA4.SetLineWeight(2);
PlotMA4.SetStyle(Curve.MEDIUM_DASH);
# --- MA Pair A Selection ---
input maX_A = {default "MA1", "MA2", "MA3", "MA4"};
input maY_A = {default "MA2", "MA3", "MA4", "MA1"};
# --- MA Pair B Selection ---
input maX_B = {default "MA1", "MA2", "MA3", "MA4"};
input maY_B = {default "MA3", "MA4", "MA1", "MA2"};
# --- Resolve MA Values for Crossover Pairs ---
def maXval_A =
if maX_A == maX_A."MA1" then MA1
else if maX_A == maX_A."MA2" then MA2
else if maX_A == maX_A."MA3" then MA3
else MA4;
def maYval_A =
if maY_A == maY_A."MA1" then MA1
else if maY_A == maY_A."MA2" then MA2
else if maY_A == maY_A."MA3" then MA3
else MA4;
def maXval_B =
if maX_B == maX_B."MA1" then MA1
else if maX_B == maX_B."MA2" then MA2
else if maX_B == maX_B."MA3" then MA3
else MA4;
def maYval_B =
if maY_B == maY_B."MA1" then MA1
else if maY_B == maY_B."MA2" then MA2
else if maY_B == maY_B."MA3" then MA3
else MA4;
# --- Crossover Logic ---
def crossAbove_A = Crosses(maXval_A, maYval_A, CrossingDirection.ABOVE);
def crossBelow_A = Crosses(maXval_A, maYval_A, CrossingDirection.BELOW);
def crossAbove_B = Crosses(maXval_B, maYval_B, CrossingDirection.ABOVE);
def crossBelow_B = Crosses(maXval_B, maYval_B, CrossingDirection.BELOW);
# --- Plot Crossover Arrows After MAs ---
plot UpArrowA = crossAbove_A;
UpArrowA.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
UpArrowA.SetDefaultColor(Color.GREEN);
UpArrowA.SetLineWeight(2);
plot DownArrowA = crossBelow_A;
DownArrowA.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
DownArrowA.SetDefaultColor(Color.RED);
DownArrowA.SetLineWeight(2);
plot UpArrowB = crossAbove_B;
UpArrowB.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
UpArrowB.SetDefaultColor(Color.LIGHT_GREEN);
UpArrowB.SetLineWeight(2);
plot DownArrowB = crossBelow_B;
DownArrowB.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
DownArrowB.SetDefaultColor(Color.PINK);
DownArrowB.SetLineWeight(2);
Shortcut to add to TOS:
http://tos.mx/!P1IPTSTL
Last edited by a moderator: