# define e-signal and crossover point
def Eup = MA1 > MA2 && MA2 > MA3;
def Edn = MA1 < MA2 && MA2 < MA3;
def CrossUp = close > MA1 && Eup && !Eup[1];
def CrossDn = close < MA1 && Edn && !Edn[1];
I added it but I noticed it's not taking all the trades example I put 100 and 200 EXP and when the MA is crossing back and forth its not taking all the trades only som, in the "Orders area" would I need to adjust anything, Should it be set to auto for the buy and for the sell? I want to enter on the upward cross and I want to exit on the downward cross of the moving averages let me please thanks.@Trading51 No need to use this script or make a backtesting strategy out of it. There is already a backtesting strategy in ThinkorSwim called GoldenCrossBreakouts. You can use that instead. Just adjust the two moving averages to whichever period you want.
Just to confirm I only need to add the Golden cross script once not twice to add these 4 inputs ?@Trading51 If you want to include both long and short strategy, you would need to edit the script. By default, it's set to Auto, which will likely not include everything. You want a Buy to Open / Sell to Close and Sell to Open / Buy to Close.
input show_arrows = yes;
# Plot Up / Down Arrow
plot up_signal = SignalUp;
up_signal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
up_signal.SetDefaultColor(Color.GREEN);
up_signal.SetHiding(!show_arrows);
plot down_signal = SignalDn;
down_signal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
down_signal.SetDefaultColor(Color.RED);
down_signal.SetHiding(!show_arrows);
@BenTen I couldn't add all 4 inputs to one code so i added the code twice and added all for but it still doesn't capture all the crosses of the Moving averages, can you let me know what I'm missing?@Trading51 If you want to include both long and short strategies, you would need to edit the script. By default, it's set to Auto, which will likely not include everything. You want a Buy to Open / Sell to Close and Sell to Open / Buy to Close.
# 3-in-1 moving avg crossover watchlist column
declare upper;
input lookback = 5;
input short_average = 5;
input medium_average = 8;
input long_average = 13;
input average_type = {default "EMA", "SMA"};
input show_arrows = yes;
def MA1;
def MA2;
def MA3;
switch (average_type) {
case "SMA":
MA1 = Average(close, short_average);
MA2 = Average(close, medium_average);
MA3 = Average(close, long_average);
case "EMA":
MA1 = ExpAverage(close, short_average);
MA2 = ExpAverage(close, medium_average);
MA3 = ExpAverage(close, long_average);
}
# define e-signal and crossover point
def Eup = MA1 > MA2 && MA2 > MA3;
def Edn = MA1 < MA2 && MA2 < MA3;
def CrossUp = close > MA1 && Eup && !Eup[1];
def CrossDn = close < MA1 && Edn && !Edn[1];
# Define up and down signals
def higherHigh = close > Highest(max(open,close), 3)[1];
def lowerLow = close < Lowest(min(open,close), 3)[1];
def SignalUp = if (CrossUp && higherHigh)
then 1
else if (CrossUp[1] && higherHigh && !higherHigh[1])
then 1
else if (CrossUp[2] && higherHigh && !higherHigh[1] && !higherHigh[2])
then 1
else 0;
def SignalDn = if (CrossDn && lowerLow)
then 1
else if (CrossDn[1] && lowerLow && !lowerLow[1])
then 1
else if (CrossDn[2] && lowerLow && !lowerLow[1] && !lowerLow[2])
then 1
else 0;
def bull_lookback = highest(SignalUp, lookback);
def bear_lookback = highest(SignalDn, lookback);
plot signal = if bull_lookback then 2 else if bear_lookback then 1 else 0;
signal.AssignValueColor(if signal == 2 then Color.Dark_Green else if signal == 1 then Color.Dark_Red else Color.WHITE);
AssignBackgroundCOlor(if signal == 2 then Color.Dark_Green else if signal == 1 then Color.Dark_Red else Color.WHITE);
declare lower;
input lookback = 1;
input short_average = 13;
input medium_average = 30;
input long_average = 200;
input averageType = AverageType.EXPONENTIAL;
input showBreakoutSignals = no;
def MA1;
def MA2;
def MA3;
MA1 = Average(close, short_average);
MA2 = Average(close, medium_average);
MA3 = Average(close, long_average);
# define e-signal and crossover point
def Eup = MA1 > MA2 && MA2 > MA3;
def Edn = MA1 < MA2 && MA2 < MA3;
plot signal = if Eup then 2 else if Edn then 1 else 0;
signal.AssignValueColor(if signal == 2 then Color.Dark_Green else if signal == 1 then Color.Dark_Red else Color.Dark_Orange);
AssignBackgroundCOlor(if signal == 2 then Color.Dark_Green else if signal == 1 then Color.Dark_Red else Color.Dark_Orange);
#Alert(close >= 5 and close < 10, "100 <= Tick < 10!", #Alert.TICK, Sound.Ding);
#Alert(close >= 5, "Tick > 10!", Alert.TICK, Sound.Chimes);
# E-Charts v2
declare upper;
input short_average = 5;
input medium_average = 10;
input long_average = 20;
input average_type = {default "SMA", "EMA"};
input show_vertical_line = no;
input show_bubble_labels = yes;
def MA1;
def MA2;
def MA3;
switch (average_type) {
case "SMA":
MA1 = Average(close, short_average);
MA2 = Average(close, medium_average);
MA3 = Average(close, long_average);
case "EMA":
MA1 = ExpAverage(close, short_average);
MA2 = ExpAverage(close, medium_average);
MA3 = ExpAverage(close, long_average);
}
# define e-signal and crossover point
def Eup = MA1 > MA2 && MA2 > MA3;
def Edn = MA1 < MA2 && MA2 < MA3;
def CrossUp = close > MA1 && Eup && !Eup[1];
def CrossDn = close < MA1 && Edn && !Edn[1];
# Define up and down signals
def higherHigh = close > Highest(max(open,close), 3)[1];
def lowerLow = close < Lowest(min(open,close), 3)[1];
def SignalUp = if (CrossUp && higherHigh)
then 1
else if (CrossUp[1] && higherHigh && !higherHigh[1])
then 1
else if (CrossUp[2] && higherHigh && !higherHigh[1] && !higherHigh[2])
then 1
else Double.NaN;
def SignalDn = if (CrossDn && lowerLow)
then 1
else if (CrossDn[1] && lowerLow && !lowerLow[1])
then 1
else if (CrossDn[2] && lowerLow && !lowerLow[1] && !lowerLow[2])
then 1
else Double.NaN;
# Plot the moving average lines
plot ln1 = MA1;
ln1.SetDefaultColor(CreateColor(145, 210, 144));
ln1.SetLineWeight(2);
plot ln2 = MA2;
ln2.SetDefaultColor(CreateColor(111, 183, 214));
ln2.SetLineWeight(2);
plot ln3 = MA3;
ln3.SetDefaultColor(CreateColor(249, 140, 182));
ln3.SetLineWeight(2);
# Draw vertical line to indicate call and put signals
AddVerticalLine(SignalUp && show_vertical_line, "Up", Color.UPTICK);
AddVerticalLine(SignalDn && show_vertical_line, "Down", Color.LIGHT_RED);
# Show Call / Put Signal in a Chart Bubble
AddChartBubble(SignalUp && show_bubble_labels, low - 0.3, "Up", Color.UPTICK, no);
AddChartBubble(SignalDn && show_bubble_labels, high + 0.3, "Dn", Color.LIGHT_RED);
# Add label for Eup or Edn
AddLabel(Eup, "E Up", Color.GREEN);
AddLabel(Edn, "E Dn", Color.RED);
Join useThinkScript to post your question to a community of 21,000+ developers and traders.
Start a new thread and receive assistance from our community.
useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.
We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.
If you are new, or just looking for guidance, here are some helpful links to get you started.