SPX Trading Strategy for ThinkorSwim

Status
Not open for further replies.

Ben's Swing Trading Strategy + Indicator

I wouldn't call this a course. My goal is zero fluff. I will jump right into my current watchlist, tell you the ThinkorSwim indicator that I'm using, and past trade setups to help you understand my swing trading strategy.

I'm Interested

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Is there anyway to make this chart work for all stocks? Or is it only possible to make this work on SPX and ES? Thank you in advance

In the parameters you can choose if u want Spx only or no
Thank you for the very quick response! Is there anyway you could screen shot where it is exactly? I have looked in the code itself and the settings tab and I dont see that option anywhere. Sorry for being a pain.
 
Last edited:
Well, it seems like I can't multitask as much as I think I can. I ended up posting this update to the wrong thread.

Python:
# SPX Strategy
# Strategy designed by Hypoluxa
# barbaros - v1 - 2021/02/25
# barbaros - v2 - 2021/02/25 - added warning arrows, alerts, statistics and non SPX trading option
# barbaros - v3 - 2021/02/26 - added SMA plots, chart type, and price plot
# barbaros - v4 - 2021/03/02 - added trade time limit and do not cross before start time
# barbaros - v5 - 2021/03/02 - added direction label

# Setup
# 10 Min chart
# Heikin Ashi candles
# SMA 3/9
# MACD Bollinger Bands (the one Ben has posted on here a while back) 8,16,36 and uncheck "show plot" on the BB upper, BB lower and BB mid line. You will only be using the zero line, MACD dots and MACD Line for entry purposes. The inputs of "b length" and "bb num dev" are irrelevant here since you will remove the the plots that I mentioned.
# ErgodicOsc 2,10,36 (changed the default negative to red)

# Option call logic:
# 1. The SMA's have to cross first.
# 2. For a call opportunity, you will then wait to see if the MACD dots cross above the MACD zero line. Its critical here to wait until one dot has cleared the zero line...you MUST see a gap....never enter with the dot on the line. Dots must be consistent as well...if its going up...then they must all be white...if a red dot populates between the time a white dot hits the zero line and the time one crosses clear....don't enter. I have a screenshot below showing this example - 13:00 a red dot appears.
# 3. The ErgodicOsc HAS to be green when the MACD dot crosses above the zero line. You can hold the trade most of the time until this turns from green to red....but I always set a sell limit just in case it whips back in the opposite direction.

# Option put logic:
# Obviously its the complete opposite of what I've described above for a call. BUT - the SMA's still have to cross first.

# Note:
# Historical price action testing shows that if the MACDBB crosses zerobase before 9:40 EST, do not take the trade. Also, there might be too much choppiness after 15:00 EST.

declare upper;

input price = close;
input SMAFastLength = 3;
input SMASlowLength = 9;
input BBlength = 30;
input BBNum_Dev = 0.8;
input BBCrossInBars = 3;
input BBCrossDistance = 1.0;
input MACDfastLength = 8;
input MACDslowLength = 16;
input MACDLength = 36;
input ERGODICLongLength = 2;
input ERGODICShortLength = 10;
input ERGODICSignalLength = 36;
input ERGODICAverageType = {"SIMPLE", default "EXPONENTIAL", "WEIGHTED", "WILDERS", "HULL"};
input ShowWarnArrows = no;
input ShowLabels = no;
input ShowStatistics = no;
input ProfitDelta = 1.0;
input LimitTime = yes;
input StartTime = 940;
input EndTime = 1500;
input DoNotCrossBeforeStart = yes;
input AllowNonSPX = no;

# Check for 10min chart
Assert(AllowNonSPX or GetAggregationPeriod() == AggregationPeriod.TEN_MIN, "Incorrect Chart Time, use 10m");
Assert(AllowNonSPX or GetSymbol() == "SPX", "Incorrect Chart Time, use 10m");

# MACD
def MACD_Data = MACD(fastLength = MACDfastLength, slowLength = MACDslowLength, MACDLength = MACDLength);
def MACD_Direction = if MACD_Data > MACD_Data[1] then 1 else -1;

# Ergodic
def Ergodic_Data = ErgodicOsc("long length" = ERGODICLongLength, "short length" = ERGODICShortLength, "signal length" = ERGODICSignalLength, "average type" = ERGODICAverageType).ErgodicOsc;

# SMAs
def SMA_Fast = SimpleMovingAvg(price, SMAFastLength);
def SMA_Slow = SimpleMovingAvg(price, SMASlowLength);

# Time Limit
def isTradeTime = if LimitTime then SecondsFromTime(StartTime) >= 0 and SecondsTillTime(EndTime) >= 0 else yes;
def isNotCrossBeforeStart = if DoNotCrossBeforeStart and SecondsFromTime(StartTime) == 0 then !((MACD_Data crosses above 0 within 2 bars) or (MACD_Data crosses below 0 within 2 bars)) else yes;

# Signals
def buySignal = isTradeTime and isNotCrossBeforeStart and SMA_Fast > SMA_Slow and Ergodic_Data > 0 and MACD_Direction == 1 and MACD_Data >= BBCrossDistance and MACD_Data crosses above 0 within BBCrossInBars bars;
def buyWarnSignal = isTradeTime and isNotCrossBeforeStart and SMA_Fast > SMA_Slow and Ergodic_Data > 0 and MACD_Direction == 1 and MACD_Data crosses above 0 within BBCrossInBars bars;
#def sellSignal = isTradeTime and isNotCrossBeforeStart and SMA_Fast < SMA_Slow and Ergodic_Data < 0 and MACD_Direction == -1 and MACD_Data <= -BBCrossDistance and MACD_Data crosses below 0 within BBCrossInBars bars;
#def sellWarnSignal = isTradeTime and isNotCrossBeforeStart and SMA_Fast < SMA_Slow and Ergodic_Data < 0 and MACD_Direction == -1 and MACD_Data crosses below 0 within BBCrossInBars bars;

def sellSignal = isTradeTime and isNotCrossBeforeStart and SMA_Fast < SMA_Slow and Ergodic_Data < 0 and MACD_Direction == -1 and MACD_Data <= -BBCrossDistance and MACD_Data crosses below 0 within BBCrossInBars bars;
def sellWarnSignal = isTradeTime and isNotCrossBeforeStart and SMA_Fast < SMA_Slow and Ergodic_Data < 0 and MACD_Direction == -1 and MACD_Data crosses below 0 within BBCrossInBars bars;

# Plots
plot buy = buySignal and !buySignal[1];
buy.setPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
buy.setDefaultColor(Color.GREEN);
buy.setLineWeight(3);

plot buyWarn = if ShowWarnArrows then buyWarnSignal and !buyWarnSignal[1] else Double.NaN;
buyWarn.setPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
buyWarn.setDefaultColor(Color.CYAN);
buyWarn.setLineWeight(1);
buyWarn.setHiding(!ShowWarnArrows);

plot sell = sellSignal and !sellSignal[1];
sell.setPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
sell.setDefaultColor(Color.RED);
sell.setLineWeight(3);

plot sellWarn = if ShowWarnArrows then sellWarnSignal and !sellWarnSignal[1] else Double.NaN;
sellWarn.setPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
sellWarn.setDefaultColor(Color.MAGENTA);
sellWarn.setLineWeight(1);
sellWarn.setHiding(!ShowWarnArrows);

plot lastPrice = price;
lastPrice.setPaintingStrategy(PaintingStrategy.POINTS);
lastPrice.setDefaultColor(Color.GREEN);

plot fast = SMA_Fast;
fast.setPaintingStrategy(PaintingStrategy.LINE);
fast.setDefaultColor(Color.CYAN);
fast.setLineWeight(1);

plot slow = SMA_Slow;
slow.setPaintingStrategy(PaintingStrategy.LINE);
slow.setDefaultColor(Color.MAGENTA);
slow.setLineWeight(1);

SetChartType(ChartType.HEIKIN_ASHI);

# Alerts
Alert(buy, "Buy", Alert.BAR, Sound.Ring);
Alert(sell, "Sell", Alert.BAR, Sound.Ring);
Alert(buyWarn, "Buy Warning", Alert.BAR, Sound.Ding);
Alert(sellWarn, "Sell Warning", Alert.BAR, Sound.Ding);

# PnL
def entryPrice = if (buySignal and !buySignal[1]) or (sellSignal and !sellSignal[1]) then close else entryPrice[1];
def profitTarget = if buySignal and !buySignal[1] then entryPrice + ProfitDelta else if sellSignal and !sellSignal[1] then entryPrice - ProfitDelta else profitTarget[1];
def orderDir = if BarNumber() == 1 then 0
               else if orderDir[1] == 0 and buySignal and !buySignal[1] then 1
               else if orderDir[1] == 1 and (MACD_Direction != 1 or close crosses above profitTarget) then 0
               else if orderDir[1] == 0 and sellSignal and !sellSignal[1] then -1
               else if orderDir[1] == -1 and (MACD_Direction != -1 or close crosses below profitTarget) then 0
               else orderDir[1];

def isOrder = if IsNaN(orderDir) then no else orderDir crosses 0;
def orderCount = CompoundValue(1, if IsNaN(isOrder) then 0 else if isOrder then orderCount[1] + 1 else orderCount[1], 0);

def orderWinners = if BarNumber() == 1 then 0
                    else if orderDir[1] == 1 and orderDir == 0 then
                        if close >= profitTarget[1] then orderWinners[1] + 1 else orderWinners[1]
                    else if orderDir[1] == -1 and orderDir == 0 then
                        if close <= profitTarget[1] then orderWinners[1] + 1 else orderWinners[1]
            else orderWinners[1];
def winRate = orderWinners / orderCount;

AddLabel(ShowLabels, "SPX Strategy", Color.WHITE);
AddLabel(ShowLabels, if buy then "Buy"
                     else if sell then "Sell"
                     else if buyWarn or sellWarn then "Warn"
                     else if orderDir == 1 then "Long"
                     else if orderDir == -1 then "Short"
                     else "Neutral",
                     if buy then Color.GREEN
                     else if sell then Color.RED
                     else if buyWarn then Color.YELLOW
                     else if sellWarn then Color.LIGHT_RED
                     else if orderDir == 1 then Color.YELLOW
                     else if orderDir == -1 then Color.LIGHT_RED
                     else Color.GRAY
);
AddLabel(ShowLabels and ShowStatistics, "" + orderCount + " Trades | " + AsPercent(winRate), if winRate > 0.5 then Color.GREEN else Color.RED);
 
Well, it seems like I can't multitask as much as I think I can. I ended up posting this update to the wrong thread.

Python:
# SPX Strategy
# Strategy designed by Hypoluxa
# barbaros - v1 - 2021/02/25
# barbaros - v2 - 2021/02/25 - added warning arrows, alerts, statistics and non SPX trading option
# barbaros - v3 - 2021/02/26 - added SMA plots, chart type, and price plot
# barbaros - v4 - 2021/03/02 - added trade time limit and do not cross before start time
# barbaros - v5 - 2021/03/02 - added direction label

# Setup
# 10 Min chart
# Heikin Ashi candles
# SMA 3/9
# MACD Bollinger Bands (the one Ben has posted on here a while back) 8,16,36 and uncheck "show plot" on the BB upper, BB lower and BB mid line. You will only be using the zero line, MACD dots and MACD Line for entry purposes. The inputs of "b length" and "bb num dev" are irrelevant here since you will remove the the plots that I mentioned.
# ErgodicOsc 2,10,36 (changed the default negative to red)

# Option call logic:
# 1. The SMA's have to cross first.
# 2. For a call opportunity, you will then wait to see if the MACD dots cross above the MACD zero line. Its critical here to wait until one dot has cleared the zero line...you MUST see a gap....never enter with the dot on the line. Dots must be consistent as well...if its going up...then they must all be white...if a red dot populates between the time a white dot hits the zero line and the time one crosses clear....don't enter. I have a screenshot below showing this example - 13:00 a red dot appears.
# 3. The ErgodicOsc HAS to be green when the MACD dot crosses above the zero line. You can hold the trade most of the time until this turns from green to red....but I always set a sell limit just in case it whips back in the opposite direction.

# Option put logic:
# Obviously its the complete opposite of what I've described above for a call. BUT - the SMA's still have to cross first.

# Note:
# Historical price action testing shows that if the MACDBB crosses zerobase before 9:40 EST, do not take the trade. Also, there might be too much choppiness after 15:00 EST.

declare upper;

input price = close;
input SMAFastLength = 3;
input SMASlowLength = 9;
input BBlength = 30;
input BBNum_Dev = 0.8;
input BBCrossInBars = 3;
input BBCrossDistance = 1.0;
input MACDfastLength = 8;
input MACDslowLength = 16;
input MACDLength = 36;
input ERGODICLongLength = 2;
input ERGODICShortLength = 10;
input ERGODICSignalLength = 36;
input ERGODICAverageType = {"SIMPLE", default "EXPONENTIAL", "WEIGHTED", "WILDERS", "HULL"};
input ShowWarnArrows = no;
input ShowLabels = no;
input ShowStatistics = no;
input ProfitDelta = 1.0;
input LimitTime = yes;
input StartTime = 940;
input EndTime = 1500;
input DoNotCrossBeforeStart = yes;
input AllowNonSPX = no;

# Check for 10min chart
Assert(AllowNonSPX or GetAggregationPeriod() == AggregationPeriod.TEN_MIN, "Incorrect Chart Time, use 10m");
Assert(AllowNonSPX or GetSymbol() == "SPX", "Incorrect Chart Time, use 10m");

# MACD
def MACD_Data = MACD(fastLength = MACDfastLength, slowLength = MACDslowLength, MACDLength = MACDLength);
def MACD_Direction = if MACD_Data > MACD_Data[1] then 1 else -1;

# Ergodic
def Ergodic_Data = ErgodicOsc("long length" = ERGODICLongLength, "short length" = ERGODICShortLength, "signal length" = ERGODICSignalLength, "average type" = ERGODICAverageType).ErgodicOsc;

# SMAs
def SMA_Fast = SimpleMovingAvg(price, SMAFastLength);
def SMA_Slow = SimpleMovingAvg(price, SMASlowLength);

# Time Limit
def isTradeTime = if LimitTime then SecondsFromTime(StartTime) >= 0 and SecondsTillTime(EndTime) >= 0 else yes;
def isNotCrossBeforeStart = if DoNotCrossBeforeStart and SecondsFromTime(StartTime) == 0 then !((MACD_Data crosses above 0 within 2 bars) or (MACD_Data crosses below 0 within 2 bars)) else yes;

# Signals
def buySignal = isTradeTime and isNotCrossBeforeStart and SMA_Fast > SMA_Slow and Ergodic_Data > 0 and MACD_Direction == 1 and MACD_Data >= BBCrossDistance and MACD_Data crosses above 0 within BBCrossInBars bars;
def buyWarnSignal = isTradeTime and isNotCrossBeforeStart and SMA_Fast > SMA_Slow and Ergodic_Data > 0 and MACD_Direction == 1 and MACD_Data crosses above 0 within BBCrossInBars bars;
#def sellSignal = isTradeTime and isNotCrossBeforeStart and SMA_Fast < SMA_Slow and Ergodic_Data < 0 and MACD_Direction == -1 and MACD_Data <= -BBCrossDistance and MACD_Data crosses below 0 within BBCrossInBars bars;
#def sellWarnSignal = isTradeTime and isNotCrossBeforeStart and SMA_Fast < SMA_Slow and Ergodic_Data < 0 and MACD_Direction == -1 and MACD_Data crosses below 0 within BBCrossInBars bars;

def sellSignal = isTradeTime and isNotCrossBeforeStart and SMA_Fast < SMA_Slow and Ergodic_Data < 0 and MACD_Direction == -1 and MACD_Data <= -BBCrossDistance and MACD_Data crosses below 0 within BBCrossInBars bars;
def sellWarnSignal = isTradeTime and isNotCrossBeforeStart and SMA_Fast < SMA_Slow and Ergodic_Data < 0 and MACD_Direction == -1 and MACD_Data crosses below 0 within BBCrossInBars bars;

# Plots
plot buy = buySignal and !buySignal[1];
buy.setPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
buy.setDefaultColor(Color.GREEN);
buy.setLineWeight(3);

plot buyWarn = if ShowWarnArrows then buyWarnSignal and !buyWarnSignal[1] else Double.NaN;
buyWarn.setPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
buyWarn.setDefaultColor(Color.CYAN);
buyWarn.setLineWeight(1);
buyWarn.setHiding(!ShowWarnArrows);

plot sell = sellSignal and !sellSignal[1];
sell.setPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
sell.setDefaultColor(Color.RED);
sell.setLineWeight(3);

plot sellWarn = if ShowWarnArrows then sellWarnSignal and !sellWarnSignal[1] else Double.NaN;
sellWarn.setPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
sellWarn.setDefaultColor(Color.MAGENTA);
sellWarn.setLineWeight(1);
sellWarn.setHiding(!ShowWarnArrows);

plot lastPrice = price;
lastPrice.setPaintingStrategy(PaintingStrategy.POINTS);
lastPrice.setDefaultColor(Color.GREEN);

plot fast = SMA_Fast;
fast.setPaintingStrategy(PaintingStrategy.LINE);
fast.setDefaultColor(Color.CYAN);
fast.setLineWeight(1);

plot slow = SMA_Slow;
slow.setPaintingStrategy(PaintingStrategy.LINE);
slow.setDefaultColor(Color.MAGENTA);
slow.setLineWeight(1);

SetChartType(ChartType.HEIKIN_ASHI);

# Alerts
Alert(buy, "Buy", Alert.BAR, Sound.Ring);
Alert(sell, "Sell", Alert.BAR, Sound.Ring);
Alert(buyWarn, "Buy Warning", Alert.BAR, Sound.Ding);
Alert(sellWarn, "Sell Warning", Alert.BAR, Sound.Ding);

# PnL
def entryPrice = if (buySignal and !buySignal[1]) or (sellSignal and !sellSignal[1]) then close else entryPrice[1];
def profitTarget = if buySignal and !buySignal[1] then entryPrice + ProfitDelta else if sellSignal and !sellSignal[1] then entryPrice - ProfitDelta else profitTarget[1];
def orderDir = if BarNumber() == 1 then 0
               else if orderDir[1] == 0 and buySignal and !buySignal[1] then 1
               else if orderDir[1] == 1 and (MACD_Direction != 1 or close crosses above profitTarget) then 0
               else if orderDir[1] == 0 and sellSignal and !sellSignal[1] then -1
               else if orderDir[1] == -1 and (MACD_Direction != -1 or close crosses below profitTarget) then 0
               else orderDir[1];

def isOrder = if IsNaN(orderDir) then no else orderDir crosses 0;
def orderCount = CompoundValue(1, if IsNaN(isOrder) then 0 else if isOrder then orderCount[1] + 1 else orderCount[1], 0);

def orderWinners = if BarNumber() == 1 then 0
                    else if orderDir[1] == 1 and orderDir == 0 then
                        if close >= profitTarget[1] then orderWinners[1] + 1 else orderWinners[1]
                    else if orderDir[1] == -1 and orderDir == 0 then
                        if close <= profitTarget[1] then orderWinners[1] + 1 else orderWinners[1]
            else orderWinners[1];
def winRate = orderWinners / orderCount;

AddLabel(ShowLabels, "SPX Strategy", Color.WHITE);
AddLabel(ShowLabels, if buy then "Buy"
                     else if sell then "Sell"
                     else if buyWarn or sellWarn then "Warn"
                     else if orderDir == 1 then "Long"
                     else if orderDir == -1 then "Short"
                     else "Neutral",
                     if buy then Color.GREEN
                     else if sell then Color.RED
                     else if buyWarn then Color.YELLOW
                     else if sellWarn then Color.LIGHT_RED
                     else if orderDir == 1 then Color.YELLOW
                     else if orderDir == -1 then Color.LIGHT_RED
                     else Color.GRAY
);
AddLabel(ShowLabels and ShowStatistics, "" + orderCount + " Trades | " + AsPercent(winRate), if winRate > 0.5 then Color.GREEN else Color.RED);
Thank you so much for taking the time to do this! Really appreciate it!
 
@Hypoluxa I took a shot at putting your strategy into a study. All parameters are customizable. It will plot buy and sell signals and alert when signals show up. I was thinking that the BB dots being above and below the zero line can be turned into a percent up or down. Let me know if you can think of any enhancements.

Python:
# SPX Strategy
# Strategy designed by Hypoluxa
# barbaros - 2021/02/25

declare upper;

input price = close;
input SMAFastLength = 3;
input SMASlowLength = 9;
input BBlength = 30;
input BBNum_Dev = 0.8;
input BBCrossInBars = 3;
input BBCrossDistance = 1;
input MACDfastLength = 8;
input MACDslowLength = 16;
input MACDLength = 36;
input ERGODICLongLength = 2;
input ERGODICShortLength = 10;
input ERGODICSignalLength = 36;
input ERGODICAverageType = {"SIMPLE", default "EXPONENTIAL", "WEIGHTED", "WILDERS", "HULL"};

# Check for 10min chart
Assert(GetAggregationPeriod() == AggregationPeriod.TEN_MIN, "Incorrect Chart Time, use 10m");

# MACD
def MACD_Data = MACD(fastLength = MACDfastLength, slowLength = MACDslowLength, MACDLength = MACDLength);
def MACD_Direction = if MACD_Data > MACD_Data[1] then 1 else -1;

# Ergodic
def Ergodic_Data = ErgodicOsc("long length" = ERGODICLongLength, "short length" = ERGODICShortLength, "signal length" = ERGODICSignalLength, "average type" = ERGODICAverageType).ErgodicOsc;

# SMAs
def SMA_Fast = SimpleMovingAvg(price, SMAFastLength);
def SMA_Slow = SimpleMovingAvg(price, SMASlowLength);

# Signals
def buySignal = SMA_Fast > SMA_Slow and Ergodic_Data > 0 and MACD_Direction == 1 and MACD_Data >= BBCrossDistance and MACD_Data crosses above 0 within BBCrossInBars bars;
def sellSignal = SMA_Fast < SMA_Slow and Ergodic_Data < 0 and MACD_Direction == -1 and MACD_Data <= -BBCrossDistance and MACD_Data crosses below 0 within BBCrossInBars bars;

# Plots
plot buy = buySignal and !buySignal[1];
buy.setPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
buy.setDefaultColor(Color.GREEN);
buy.setLineWeight(3);

plot sell = sellSignal and !sellSignal[1];
sell.setPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
sell.setDefaultColor(Color.RED);
sell.setLineWeight(3);

# Alerts
Alert(buy, "Buy", Alert.BAR, Sound.Ring);
Alert(sell, "Sell", Alert.BAR, Sound.Ring);
Today ( March 2nd) at 10:50 am; the white dot crosses the zero line, the SMAs are crossed at ten, however the green arrow pop up at 11:10? And yes, you will be in profit but i am not sure if that is the correct strategy. Any feedback on this?
 
Hey @Martian. There are settings you can adjust to make it more sensitive as MACD dots cross the zero line. MACDCrossDistance. It is set at 1.0 right now. It means that until the MACD line is at least 1.0 value away from the zero line, it is not considered a valid cross. The original strategy suggests that you need to wait for the crossed dot to move away from the zero line. It is hard to code a vague value so I made it a parameter to adjust. You can adjust it to your liking.

Newer version of the script also has warning arrows. These arrows have no distance filter. You can go in with these arrows too if you are feeling lucky.
 
Well, it seems like I can't multitask as much as I think I can. I ended up posting this update to the wrong thread.

Python:
# SPX Strategy
# Strategy designed by Hypoluxa
# barbaros - v1 - 2021/02/25
# barbaros - v2 - 2021/02/25 - added warning arrows, alerts, statistics and non SPX trading option
# barbaros - v3 - 2021/02/26 - added SMA plots, chart type, and price plot
# barbaros - v4 - 2021/03/02 - added trade time limit and do not cross before start time
# barbaros - v5 - 2021/03/02 - added direction label

# Setup
# 10 Min chart
# Heikin Ashi candles
# SMA 3/9
# MACD Bollinger Bands (the one Ben has posted on here a while back) 8,16,36 and uncheck "show plot" on the BB upper, BB lower and BB mid line. You will only be using the zero line, MACD dots and MACD Line for entry purposes. The inputs of "b length" and "bb num dev" are irrelevant here since you will remove the the plots that I mentioned.
# ErgodicOsc 2,10,36 (changed the default negative to red)

# Option call logic:
# 1. The SMA's have to cross first.
# 2. For a call opportunity, you will then wait to see if the MACD dots cross above the MACD zero line. Its critical here to wait until one dot has cleared the zero line...you MUST see a gap....never enter with the dot on the line. Dots must be consistent as well...if its going up...then they must all be white...if a red dot populates between the time a white dot hits the zero line and the time one crosses clear....don't enter. I have a screenshot below showing this example - 13:00 a red dot appears.
# 3. The ErgodicOsc HAS to be green when the MACD dot crosses above the zero line. You can hold the trade most of the time until this turns from green to red....but I always set a sell limit just in case it whips back in the opposite direction.

# Option put logic:
# Obviously its the complete opposite of what I've described above for a call. BUT - the SMA's still have to cross first.

# Note:
# Historical price action testing shows that if the MACDBB crosses zerobase before 9:40 EST, do not take the trade. Also, there might be too much choppiness after 15:00 EST.

declare upper;

input price = close;
input SMAFastLength = 3;
input SMASlowLength = 9;
input BBlength = 30;
input BBNum_Dev = 0.8;
input BBCrossInBars = 3;
input BBCrossDistance = 1.0;
input MACDfastLength = 8;
input MACDslowLength = 16;
input MACDLength = 36;
input ERGODICLongLength = 2;
input ERGODICShortLength = 10;
input ERGODICSignalLength = 36;
input ERGODICAverageType = {"SIMPLE", default "EXPONENTIAL", "WEIGHTED", "WILDERS", "HULL"};
input ShowWarnArrows = no;
input ShowLabels = no;
input ShowStatistics = no;
input ProfitDelta = 1.0;
input LimitTime = yes;
input StartTime = 940;
input EndTime = 1500;
input DoNotCrossBeforeStart = yes;
input AllowNonSPX = no;

# Check for 10min chart
Assert(AllowNonSPX or GetAggregationPeriod() == AggregationPeriod.TEN_MIN, "Incorrect Chart Time, use 10m");
Assert(AllowNonSPX or GetSymbol() == "SPX", "Incorrect Chart Time, use 10m");

# MACD
def MACD_Data = MACD(fastLength = MACDfastLength, slowLength = MACDslowLength, MACDLength = MACDLength);
def MACD_Direction = if MACD_Data > MACD_Data[1] then 1 else -1;

# Ergodic
def Ergodic_Data = ErgodicOsc("long length" = ERGODICLongLength, "short length" = ERGODICShortLength, "signal length" = ERGODICSignalLength, "average type" = ERGODICAverageType).ErgodicOsc;

# SMAs
def SMA_Fast = SimpleMovingAvg(price, SMAFastLength);
def SMA_Slow = SimpleMovingAvg(price, SMASlowLength);

# Time Limit
def isTradeTime = if LimitTime then SecondsFromTime(StartTime) >= 0 and SecondsTillTime(EndTime) >= 0 else yes;
def isNotCrossBeforeStart = if DoNotCrossBeforeStart and SecondsFromTime(StartTime) == 0 then !((MACD_Data crosses above 0 within 2 bars) or (MACD_Data crosses below 0 within 2 bars)) else yes;

# Signals
def buySignal = isTradeTime and isNotCrossBeforeStart and SMA_Fast > SMA_Slow and Ergodic_Data > 0 and MACD_Direction == 1 and MACD_Data >= BBCrossDistance and MACD_Data crosses above 0 within BBCrossInBars bars;
def buyWarnSignal = isTradeTime and isNotCrossBeforeStart and SMA_Fast > SMA_Slow and Ergodic_Data > 0 and MACD_Direction == 1 and MACD_Data crosses above 0 within BBCrossInBars bars;
#def sellSignal = isTradeTime and isNotCrossBeforeStart and SMA_Fast < SMA_Slow and Ergodic_Data < 0 and MACD_Direction == -1 and MACD_Data <= -BBCrossDistance and MACD_Data crosses below 0 within BBCrossInBars bars;
#def sellWarnSignal = isTradeTime and isNotCrossBeforeStart and SMA_Fast < SMA_Slow and Ergodic_Data < 0 and MACD_Direction == -1 and MACD_Data crosses below 0 within BBCrossInBars bars;

def sellSignal = isTradeTime and isNotCrossBeforeStart and SMA_Fast < SMA_Slow and Ergodic_Data < 0 and MACD_Direction == -1 and MACD_Data <= -BBCrossDistance and MACD_Data crosses below 0 within BBCrossInBars bars;
def sellWarnSignal = isTradeTime and isNotCrossBeforeStart and SMA_Fast < SMA_Slow and Ergodic_Data < 0 and MACD_Direction == -1 and MACD_Data crosses below 0 within BBCrossInBars bars;

# Plots
plot buy = buySignal and !buySignal[1];
buy.setPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
buy.setDefaultColor(Color.GREEN);
buy.setLineWeight(3);

plot buyWarn = if ShowWarnArrows then buyWarnSignal and !buyWarnSignal[1] else Double.NaN;
buyWarn.setPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
buyWarn.setDefaultColor(Color.CYAN);
buyWarn.setLineWeight(1);
buyWarn.setHiding(!ShowWarnArrows);

plot sell = sellSignal and !sellSignal[1];
sell.setPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
sell.setDefaultColor(Color.RED);
sell.setLineWeight(3);

plot sellWarn = if ShowWarnArrows then sellWarnSignal and !sellWarnSignal[1] else Double.NaN;
sellWarn.setPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
sellWarn.setDefaultColor(Color.MAGENTA);
sellWarn.setLineWeight(1);
sellWarn.setHiding(!ShowWarnArrows);

plot lastPrice = price;
lastPrice.setPaintingStrategy(PaintingStrategy.POINTS);
lastPrice.setDefaultColor(Color.GREEN);

plot fast = SMA_Fast;
fast.setPaintingStrategy(PaintingStrategy.LINE);
fast.setDefaultColor(Color.CYAN);
fast.setLineWeight(1);

plot slow = SMA_Slow;
slow.setPaintingStrategy(PaintingStrategy.LINE);
slow.setDefaultColor(Color.MAGENTA);
slow.setLineWeight(1);

SetChartType(ChartType.HEIKIN_ASHI);

# Alerts
Alert(buy, "Buy", Alert.BAR, Sound.Ring);
Alert(sell, "Sell", Alert.BAR, Sound.Ring);
Alert(buyWarn, "Buy Warning", Alert.BAR, Sound.Ding);
Alert(sellWarn, "Sell Warning", Alert.BAR, Sound.Ding);

# PnL
def entryPrice = if (buySignal and !buySignal[1]) or (sellSignal and !sellSignal[1]) then close else entryPrice[1];
def profitTarget = if buySignal and !buySignal[1] then entryPrice + ProfitDelta else if sellSignal and !sellSignal[1] then entryPrice - ProfitDelta else profitTarget[1];
def orderDir = if BarNumber() == 1 then 0
               else if orderDir[1] == 0 and buySignal and !buySignal[1] then 1
               else if orderDir[1] == 1 and (MACD_Direction != 1 or close crosses above profitTarget) then 0
               else if orderDir[1] == 0 and sellSignal and !sellSignal[1] then -1
               else if orderDir[1] == -1 and (MACD_Direction != -1 or close crosses below profitTarget) then 0
               else orderDir[1];

def isOrder = if IsNaN(orderDir) then no else orderDir crosses 0;
def orderCount = CompoundValue(1, if IsNaN(isOrder) then 0 else if isOrder then orderCount[1] + 1 else orderCount[1], 0);

def orderWinners = if BarNumber() == 1 then 0
                    else if orderDir[1] == 1 and orderDir == 0 then
                        if close >= profitTarget[1] then orderWinners[1] + 1 else orderWinners[1]
                    else if orderDir[1] == -1 and orderDir == 0 then
                        if close <= profitTarget[1] then orderWinners[1] + 1 else orderWinners[1]
            else orderWinners[1];
def winRate = orderWinners / orderCount;

AddLabel(ShowLabels, "SPX Strategy", Color.WHITE);
AddLabel(ShowLabels, if buy then "Buy"
                     else if sell then "Sell"
                     else if buyWarn or sellWarn then "Warn"
                     else if orderDir == 1 then "Long"
                     else if orderDir == -1 then "Short"
                     else "Neutral",
                     if buy then Color.GREEN
                     else if sell then Color.RED
                     else if buyWarn then Color.YELLOW
                     else if sellWarn then Color.LIGHT_RED
                     else if orderDir == 1 then Color.YELLOW
                     else if orderDir == -1 then Color.LIGHT_RED
                     else Color.GRAY
);
AddLabel(ShowLabels and ShowStatistics, "" + orderCount + " Trades | " + AsPercent(winRate), if winRate > 0.5 then Color.GREEN else Color.RED);
When i posted this new version its not working...if possible can you please share ur flexible grid or share study as a link? thanks
 
Sorry didnt see it only works on 10 mns.. which post should i refer to understand that how does this work? or i you have any manual that you an share. thanks.
You can look at the first page of this thread for detailed discussion in the beginning. You can also look at the comment block at the top of the script.
 
BTW for the SPX strategy, I added the condition of the trigger with SMA 89... if above go long and if below go short... that would have avoided the trade that happened this morning...here is the updated code: it will reduce the number of trades though...

Code:
declare upper;

input price = close;
input SMAFastLength = 3;
input SMASlowLength = 9;
input SMA89 = 89;
input BBlength = 30;
input BBNum_Dev = 0.8;
input BBCrossInBars = 3;
input BBCrossDistance = 1;
input BBCrossDistance1 = -1;
input BBCrossDistance2 = 1;
input MACDfastLength = 8;
input MACDslowLength = 16;
input positionSize = 10000.0;
input entryPrice = close;
input entryPriceOffset = 0;
input MACDLength = 36;
input ERGODICLongLength = 2;
input ERGODICShortLength = 10;
input ERGODICSignalLength = 36;
input ERGODICAverageType = {"SIMPLE", default "EXPONENTIAL", "WEIGHTED", "WILDERS", "HULL"};
input ShowWarnArrows = yes;
input ShowStatistics = yes;
input ProfitDelta = 1.0;


# Check for 10min chart
Assert(GetAggregationPeriod() == AggregationPeriod.TEN_MIN, "Incorrect Chart Time, use 10m");

# MACD
def MACD_Data = MACD(fastLength = MACDfastLength, slowLength = MACDslowLength, MACDLength = MACDLength);
def MACD_Direction = if MACD_Data > MACD_Data[1] then 1 else -1;
def MACD_Down = if MACD_Data < MACD_Data[1] then 1 else -1;
def MACD_up = if MACD_Data > MACD_DATA[1] then 1 else -1;

# Ergodic
def Ergodic_Data = ErgodicOsc("long length" = ERGODICLongLength, "short length" = ERGODICShortLength, "signal length" = ERGODICSignalLength, "average type" = ERGODICAverageType).ErgodicOsc;

# SMAs
def SMA_Fast = SimpleMovingAvg(price, SMAFastLength);
def SMA_Slow = SimpleMovingAvg(price, SMASlowLength);
def SMA_89 = SimpleMovingAvg(price, SMA89);

# Signals
def buySignal = SMA_Fast > SMA_Slow and close > SMA_89 and Ergodic_Data > 0 and MACD_Direction == 1 and MACD_Data >= BBCrossDistance and MACD_Data crosses above 0 within BBCrossInBars bars;
def sellSignal = SMA_Fast < SMA_Slow and close < SMA_89 and Ergodic_Data < 0 and MACD_Direction == -1 and MACD_Data <= -BBCrossDistance and MACD_Data crosses below 0 within BBCrossInBars bars;

addOrder(OrderType.BUY_To_Open,buySignal and !buySignal[1],  entryPrice[entryPriceOffset], round(positionSize / close, 0), color.gREEN, color.gREEN, name="LE");
addorder(orderType.SELL_to_close,MACD_Direction == -1 , entryPrice[entryPriceOffset], tickColor=color.reD, arrowColor=color.reD, name="LX");

addOrder(OrderType.SELL_To_Open, sellSignal and !sellSignal[1], entryPrice[entryPriceOffset], round(positionSize / close, 0), color.dark_gREEN, color.dark_gREEN, name="SE");
addorder(orderType.BUY_to_close,MACD_Direction == 1, entryPrice[entryPriceOffset], tickColor=color.dark_reD,arrowColor=color.dark_reD, name="SX");

# Plots
plot buy = buySignal and !buySignal[1];
buy.setPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
buy.setDefaultColor(Color.GREEN);
buy.setLineWeight(3);



plot sell = sellSignal and !sellSignal[1];
sell.setPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
sell.setDefaultColor(Color.RED);
sell.setLineWeight(3);



# Alerts
Alert(buy, "Buy", Alert.BAR, Sound.Ring);
Alert(sell, "Sell", Alert.BAR, Sound.Ring);

# PnL
def entryPrice1 = if (buySignal and !buySignal[1]) or (sellSignal and !sellSignal[1]) then close else entryPrice1[1];
def profitTarget = if buySignal and !buySignal[1] then entryPrice + ProfitDelta else if sellSignal and !sellSignal[1] then entryPrice - ProfitDelta else profitTarget[1];
def orderDir = if BarNumber() == 1 then 0
               else if orderDir[1] == 0 and buySignal and !buySignal[1] then 1
               else if orderDir[1] == 1 and (MACD_Direction != 1 or close crosses above profitTarget) then 0
               else if orderDir[1] == 0 and sellSignal and !sellSignal[1] then -1
               else if orderDir[1] == -1 and (MACD_Direction != -1 or close crosses below profitTarget) then 0
               else orderDir[1];

def isOrder = if IsNaN(orderDir) then no else orderDir crosses 0;
def orderCount = CompoundValue(1, if IsNaN(isOrder) then 0 else if isOrder then orderCount[1] + 1 else orderCount[1], 0);

def orderWinners = if BarNumber() == 1 then 0
                    else if orderDir[1] == 1 and orderDir == 0 then
                        if close >= profitTarget[1] then orderWinners[1] + 1 else orderWinners[1]
                    else if orderDir[1] == -1 and orderDir == 0 then
                        if close <= profitTarget[1] then orderWinners[1] + 1 else orderWinners[1]
            else orderWinners[1];
def winRate = orderWinners / orderCount;

AddLabel(ShowStatistics, "Ergo Strategy", Color.Cyan);
AddLabel(ShowStatistics, "" + orderCount + " Trades | " + AsPercent(winRate), if winRate > 0.5 then Color.GREEN else Color.RED);
I copied the code and posted to my TOS platform. But nothing is happening. Do you know what can be gone wrong. Thanks
 
Impressive short entry and short exit signals from yesterday 3:30pm and today 10:40am

Capture.png
 
Status
Not open for further replies.

New Indicator: Buy the Dip

Check out our Buy the Dip indicator and see how it can help you find profitable swing trading ideas. Scanner, watchlist columns, and add-ons are included.

Download the indicator

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
365 Online
Create Post

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

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.

How do I get started?

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.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top