SPX Trading Strategy for ThinkorSwim

Status
Not open for further replies.
What settings on DMI are you using? Seems like it tells you good things on days like Tuesday and today but what do other days look like?
This is a Red days, about S&P down -70 point from crossover to the other.

rGSIOuN.png
 

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

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

I missed a big run today because of a red BB dot shaken me out, btw I followed some @Hypoluxa 's post from the other threads about DMI Strategies, I added DMI Oscillator and DI+ DI- to this SPX strategies, I think I find the better way to entry and exit. I will enter the trade as soon as the DI+ and DI- Crossover, if DI+ cross above DI- you enter a call, and DI+ cross below DI- you enter a put... and you can stay as long as DMI Oscillator Green or until both DI's cross over again... this is my ideas anyone have suggestions or advice for this please post here. thanks

1ynQSWL.png


gjPvw8K.png
it did not provide accurate one as i was checking on back dates. (10Mins chart) I will stick with the original one for now. I apologize that i have not read any DMI strategies but that crosses was even before the SMAs crossed and not sure if that is even part of this strategy. Like this morning. Also 12.50 PM today, there is no signal for the puts but we have got put signal. Is it only for bullish momentum ?
 
it did not provide accurate one as i was checking on back dates. (10Mins chart) I will stick with the original one for now. I apologize that i have not read any DMI strategies but that crosses was even before the SMAs crossed and not sure if that is even part of this strategy. Like this morning. Also 12.50 PM today, there is no signal for the puts but we have got put signal. Is it only for bullish momentum ?
have you change the DMI setting to 36?
 
Thanks to all those involved with making and improving this study. I read through every post in the thread to learn as much as possible. Is there a way to use this study with regular candles, or is Heikin Ashi required to make it work? For some reason, my brain doesn't like HA candles and it throws me off.
 
Thanks to all those involved with making and improving this study. I read through every post in the thread to learn as much as possible. Is there a way to use this study with regular candles, or is Heikin Ashi required to make it work? For some reason, my brain doesn't like HA candles and it throws me off.
I don't use the heikin candles. They show the trend but for me the MACD and Ergo do the same.
 
I don't use the heikin candles. They show the trend but for me the MACD and Ergo do the same.
Thanks for responding. How do you use it with normal candles? When I tried the study, it forces Heikin Ashi candles. Can I disable it in the code? And does changing the candles mess with the system since HA candles are different in size than normal candles (i.e. the take profit/exit line)?
 
Last edited:
Not to throw a curveball into things....but I have tried to fine tune this setup a little more....I get bored sometimes and keep tweaking different setups. 🤓
VIX is almost a 100% inversion of SPX....so I have them side by side in this setup. And FYI - this is a setup that works well for SPX PUTS ONLY....Calls don't seem to be very consistent here....I have something separate I'm working on for that.
For this setup, the VIX TTM needs to be light blue with a green dot on top and the SPX TTM needs to be red with a green dot on top AND VIX needs to have a white MACDBB dot over the zero line while SPX has a red one going below the zero line.
You'll notice that the VIX TTM has a 10 and 0.4 while SPX has a 9 and 0.5....that is correct and not a mistake.
Give it a shot if you want too...it looks great since January 4th and looks to keep us out of very short lived PUT situations.
 
Thanks for responding. How do you use it with normal candles? When I tried the study, it forces Heikin Ashi candles. Can I disable it in the code? And does changing the candles mess with the system since HA candles are different in size than normal candles (i.e. closing above and below the signal line)?

You aren't forced to use Heikin Ashi candles unless you are loading from a shared link... You can easily select standard Japanese candlesticks via Style > Chart Type > Candles, or any of a number of other ways to choose candles...

The reason behind the use of Heikin Ashi candles is that aside from showing trend, they help reduce the anxiety some traders get when they see a down candle which can cause them to exit prematurely... I've been there and done that myself... I can now toggle between candle types but usually run Heikin Ashi candles, except on my lowest timeframe charts where I want to see real time price action...
 
The script forces you to use HK candles. I’ll make it an option later tonight so you can disable it.
@barbaros Thank you so much for your work (and the work of others here) to develop this. You are much appreciated. If I understand correctly from everything I've read thus far, the strat would with regular candles as well and HA was just a preference, correct?
 
@barbaros Thank you so much for your work (and the work of others here) to develop this. You are much appreciated. If I understand correctly from everything I've read thus far, the strat would with regular candles as well and HA was just a preference, correct?
Correct. I will make this an option in a few mins.
 
Not to throw a curveball into things....but I have tried to fine tune this setup a little more....I get bored sometimes and keep tweaking different setups. 🤓
VIX is almost a 100% inversion of SPX....so I have them side by side in this setup. And FYI - this is a setup that works well for SPX PUTS ONLY....Calls don't seem to be very consistent here....I have something separate I'm working on for that.
For this setup, the VIX TTM needs to be light blue with a green dot on top and the SPX TTM needs to be red with a green dot on top AND VIX needs to have a white MACDBB dot over the zero line while SPX has a red one going below the zero line.
You'll notice that the VIX TTM has a 10 and 0.4 while SPX has a 9 and 0.5....that is correct and not a mistake.
Give it a shot if you want too...it looks great since January 4th and looks to keep us out of very short lived PUT situations.
As VIX is exactly inverse how does it help, can we not rather watch SPX only?
 
As VIX is exactly inverse how does it help, can we not rather watch SPX only?
It helps as a gauge as to what SPX is going to do and possibly for how long. If VIX is in a downfall...then SPX will be going up and vice versa. It’s just an additional confirmation.
kind of like sports...one team is winning and one is losing.
 
Guys, I removed the chart type overwriting. Since @Hypoluxa's strategy doesn't depend on the chart type, this was unnecessary. I personally like the HK candles and that's why there are dots showing the close price of that candle on the chart. You can hide the price dots if it is no use to you.

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
# barbaros - v6  - 2021/03/03 - added alerts, exit at profit delta, arrows for exiting position, and changed defaults to show all
# barbaros - v7  - 2021/03/03 - removed inputs for disabling arrows - this can be done in ToS already,
#                              added profit target line, ExitAt selection and ATR target
# barbaros - v8  - 2021/03/05 - added profit target label, changed the exit strategy to high/low instead of close
# barbaros - v9  - 2021/03/06 - added entry price label
# barbaros - v10 - 2021/03/11 - trying to fix before 9:40am EST crossing filter
# barbaros - v11 - 2021/03/11 - removed chart type overwriting

# 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 AllowNonSPX = no;
input ShowLabels = yes;
input ShowStatistics = yes;
input ExitAt = {default "profitDeltaOrMACD", "MACDOnly", "ATRorMACD"};
input ProfitDelta = 1.0;
input ATRLength = 7.0;
input ATRMult = 0.5;
input LimitTime = yes;
input StartTime = 940;
input StopTime = 1500;
input DoNotCrossBeforeStart = yes;

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"};

# 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;
def MACD_CrossBar = if MACD_Data crosses above 0 within 1 bar or MACD_Data crosses below 0 within 1 bar then BarNumber() else MACD_CrossBar[1];
def MACD_CrossBarAgo = BarNumber() - MACD_CrossBar;

# 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);

# ATR for progit target
def atrDelta = ATR(ATRLength) * ATRMult;

# Time Limit
def isTradeTime = if LimitTime then SecondsFromTime(StartTime) >= 0 and SecondsTillTime(StopTime) >= 0 else yes;
def isNotCrossBeforeStart = if DoNotCrossBeforeStart and SecondsFromTime(StartTime) == 0 and MACD_CrossBarAgo == 1 then no
                            else if DoNotCrossBeforeStart and SecondsFromTime(StartTime) != 0 and MACD_CrossBar[1] != MACD_CrossBar then yes
                            else isNotCrossBeforeStart[1];

# 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_CrossBar[1] != MACD_CrossBar;

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_CrossBar[1] != MACD_CrossBar;

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

plot buyWarn = buyWarnSignal and !buyWarnSignal[1];
buyWarn.setPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
buyWarn.setDefaultColor(Color.CYAN);
buyWarn.setLineWeight(1);

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

plot sellWarn = sellWarnSignal and !sellWarnSignal[1];
sellWarn.setPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
sellWarn.setDefaultColor(Color.MAGENTA);
sellWarn.setLineWeight(1);

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);

# PnL
def entryPrice = if (buySignal and !buySignal[1]) or (sellSignal and !sellSignal[1]) then open[-1] else entryPrice[1];
def profitDeltaTarget = if ExitAt == ExitAt.profitDeltaOrMACD then ProfitDelta else if ExitAt == ExitAt.ATRorMACD then atrDelta else 0;
def profitTarget = if buySignal and !buySignal[1] then entryPrice + profitDeltaTarget
                   else if sellSignal and !sellSignal[1] then entryPrice - profitDeltaTarget
                   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 (ExitAt != ExitAt.MACDOnly and high >= 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 (ExitAt != ExitAt.MACDOnly and low <= 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 high >= profitTarget[1] then orderWinners[1] + 1 else orderWinners[1]
                    else if orderDir[1] == -1 and orderDir == 0 then
                        if low <= profitTarget[1] then orderWinners[1] + 1 else orderWinners[1]
            else orderWinners[1];
def winRate = orderWinners / orderCount;

# Plot Exit Positions
plot buyExit = orderDir[1] == 1 and orderDir != 1;
buyExit.setPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
buyExit.setDefaultColor(Color.GREEN);
buyExit.setLineWeight(3);

plot sellExit = orderDir[1] == -1 and orderDir != -1;
sellExit.setPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
sellExit.setDefaultColor(Color.RED);
sellExit.setLineWeight(3);

plot profitTargetLevel = if orderDir != 0 or orderDir[1] != 0 then profitTarget else Double.NaN;
profitTargetLevel.setPaintingStrategy(PaintingStrategy.LINE);
profitTargetLevel.setDefaultColor(Color.GREEN);
profitTargetLevel.setLineWeight(3);

# Labels
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 orderDir != 0 and !isNaN(entryPrice), "Entry " + AsDollars(entryPrice), Color.GRAY);
AddLabel(ShowLabels and orderDir != 0 and !isNaN(profitTarget), "Target " + AsDollars(profitTarget), Color.GRAY);
AddLabel(ShowLabels and ShowStatistics, "" + orderCount + " Trades | " + AsPercent(winRate), if winRate > 0.5 then Color.GREEN else Color.RED);

# 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);
Alert(buyExit, "Buy Exit", Alert.BAR, Sound.Ring);
Alert(sellExit, "Sell Exit", Alert.BAR, Sound.Ring);
 
Guys, I removed the chart type overwriting. Since @Hypoluxa's strategy doesn't depend on the chart type, this was unnecessary. I personally like the HK candles and that's why there are dots showing the close price of that candle on the chart. You can hide the price dots if it is no use to you.

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
# barbaros - v6  - 2021/03/03 - added alerts, exit at profit delta, arrows for exiting position, and changed defaults to show all
# barbaros - v7  - 2021/03/03 - removed inputs for disabling arrows - this can be done in ToS already,
#                              added profit target line, ExitAt selection and ATR target
# barbaros - v8  - 2021/03/05 - added profit target label, changed the exit strategy to high/low instead of close
# barbaros - v9  - 2021/03/06 - added entry price label
# barbaros - v10 - 2021/03/11 - trying to fix before 9:40am EST crossing filter
# barbaros - v11 - 2021/03/11 - removed chart type overwriting

# 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 AllowNonSPX = no;
input ShowLabels = yes;
input ShowStatistics = yes;
input ExitAt = {default "profitDeltaOrMACD", "MACDOnly", "ATRorMACD"};
input ProfitDelta = 1.0;
input ATRLength = 7.0;
input ATRMult = 0.5;
input LimitTime = yes;
input StartTime = 940;
input StopTime = 1500;
input DoNotCrossBeforeStart = yes;

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"};

# 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;
def MACD_CrossBar = if MACD_Data crosses above 0 within 1 bar or MACD_Data crosses below 0 within 1 bar then BarNumber() else MACD_CrossBar[1];
def MACD_CrossBarAgo = BarNumber() - MACD_CrossBar;

# 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);

# ATR for progit target
def atrDelta = ATR(ATRLength) * ATRMult;

# Time Limit
def isTradeTime = if LimitTime then SecondsFromTime(StartTime) >= 0 and SecondsTillTime(StopTime) >= 0 else yes;
def isNotCrossBeforeStart = if DoNotCrossBeforeStart and SecondsFromTime(StartTime) == 0 and MACD_CrossBarAgo == 1 then no
                            else if DoNotCrossBeforeStart and SecondsFromTime(StartTime) != 0 and MACD_CrossBar[1] != MACD_CrossBar then yes
                            else isNotCrossBeforeStart[1];

# 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_CrossBar[1] != MACD_CrossBar;

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_CrossBar[1] != MACD_CrossBar;

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

plot buyWarn = buyWarnSignal and !buyWarnSignal[1];
buyWarn.setPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
buyWarn.setDefaultColor(Color.CYAN);
buyWarn.setLineWeight(1);

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

plot sellWarn = sellWarnSignal and !sellWarnSignal[1];
sellWarn.setPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
sellWarn.setDefaultColor(Color.MAGENTA);
sellWarn.setLineWeight(1);

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);

# PnL
def entryPrice = if (buySignal and !buySignal[1]) or (sellSignal and !sellSignal[1]) then open[-1] else entryPrice[1];
def profitDeltaTarget = if ExitAt == ExitAt.profitDeltaOrMACD then ProfitDelta else if ExitAt == ExitAt.ATRorMACD then atrDelta else 0;
def profitTarget = if buySignal and !buySignal[1] then entryPrice + profitDeltaTarget
                   else if sellSignal and !sellSignal[1] then entryPrice - profitDeltaTarget
                   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 (ExitAt != ExitAt.MACDOnly and high >= 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 (ExitAt != ExitAt.MACDOnly and low <= 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 high >= profitTarget[1] then orderWinners[1] + 1 else orderWinners[1]
                    else if orderDir[1] == -1 and orderDir == 0 then
                        if low <= profitTarget[1] then orderWinners[1] + 1 else orderWinners[1]
            else orderWinners[1];
def winRate = orderWinners / orderCount;

# Plot Exit Positions
plot buyExit = orderDir[1] == 1 and orderDir != 1;
buyExit.setPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
buyExit.setDefaultColor(Color.GREEN);
buyExit.setLineWeight(3);

plot sellExit = orderDir[1] == -1 and orderDir != -1;
sellExit.setPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
sellExit.setDefaultColor(Color.RED);
sellExit.setLineWeight(3);

plot profitTargetLevel = if orderDir != 0 or orderDir[1] != 0 then profitTarget else Double.NaN;
profitTargetLevel.setPaintingStrategy(PaintingStrategy.LINE);
profitTargetLevel.setDefaultColor(Color.GREEN);
profitTargetLevel.setLineWeight(3);

# Labels
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 orderDir != 0 and !isNaN(entryPrice), "Entry " + AsDollars(entryPrice), Color.GRAY);
AddLabel(ShowLabels and orderDir != 0 and !isNaN(profitTarget), "Target " + AsDollars(profitTarget), Color.GRAY);
AddLabel(ShowLabels and ShowStatistics, "" + orderCount + " Trades | " + AsPercent(winRate), if winRate > 0.5 then Color.GREEN else Color.RED);

# 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);
Alert(buyExit, "Buy Exit", Alert.BAR, Sound.Ring);
Alert(sellExit, "Sell Exit", Alert.BAR, Sound.Ring);
Magnificent, thanks. What stop loss is it using to determine the winrate/statistics?

Also, it seems that if you choose option to not limit crossover, the statistics don't work/compute.
 
Magnificent, thanks. What stop loss is it using to determine the winrate/statistics?

Also, it seems that if you choose option to not limit crossover, the statistics don't work/compute.
You pick your exit with "ExitAt" option. What combination doesn't work for you?
 
You pick your exit with "ExitAt" option. What combination doesn't work for you?
1. I'm asking regarding the winrate reported in the top left corner. I'm curious how it's calculated because it seems that some entries will hit a reasonable stop loss before turning around and becoming profitable. It's currently showing me a 97% winrate, but I don't understand how that's possible. Does this study provide a stop loss as well, or is that for the user to determine?

2. I'm pointing out that if I choose "no" in options "do not cross before start," then the winrate statistic goes away completely and seems to not work.
 
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

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
206 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