Darvas Box with Target Levels for ThinkorSwim (Scalping Strategy)

chewie76

Well-known member
VIP
VIP Enthusiast
If you are familiar with the Darvas Box, you'll quickly realize that when the box lines are broken, there is no target level that price will tend to break to. This indicator adds a target level (CYAN dashed lines). Adding these target levels on the chart gives traders a perfect scalping trading tool. Place your entry trade at the box level and set your closing trade at the target line. This works on any timeframe. I also suggest adding a 20 EMA to your chart to see the trend level. You'll notice in the picture when the 20 is slopping down, Darvas Box target levels to the downside are hit. In the below example, the yellow arrows highlight targets being hit. There was one time that the box was broken and the target was not hit, highlighted with a yellow X. I updated this to make the box horizontal lines to clean the chart. I also added a 61.8 and a double target line.

tFkKYll.jpg



G1N7H3v.jpg


I also added the 50% halfway line between the box levels. (yellow line). This can also be monitored for support and resistance. If the price can't break through the 50 yellow line, it is an early indication that the target may be hit. For example, in the below picture, price doesn't break above the 50, and it is used as resistance and the lower box level is broken and target is hit.


722Cn8b.jpg


Check out the DAILY chart on SPY. It hits these targets.

RTb1waR.jpg


download link: http://tos.mx/MWNNnFj

Code:
#
#
# TD Ameritrade IP Company, Inc. (c) 2011-2021
#
# Chewie added the 50 and 23.6 and 61.8 fib extentions and Double Target on 1/8/2022

input T1 = yes;
input T2 = yes;
input DT = yes;

def state = {default state_1, state_2, state_3, state_4, state_5};

def upper;
def lower;

def prevLower = CompoundValue(1, lower[1], low);
def prevUpper = CompoundValue(1, upper[1], high);

switch (state[1]) {
case state_1:
    lower = low;
    if (prevUpper >= high) {
        upper = high[1];
        state = state.state_2;
    } else {
        upper = high;
        state = state.state_1;
    }
case state_2:
    if (prevUpper >= high) {
        lower = low;
        upper = prevUpper;
        state = state.state_3;
    } else {
        lower = low;
        upper = high;
        state = state.state_1;
    }
case state_3:
    if (prevUpper < high) {
        lower = low;
        upper = high;
        state = state.state_1;
    } else if (prevLower > low) {
        lower = low;
        upper = prevUpper;
        state = state.state_3;
    } else {
        lower = prevLower;
        upper = prevUpper;
        state = state.state_4;
    }
case state_4:
    if (prevUpper < high) {
        lower = low;
        upper = high;
        state = state.state_1;
    } else if (prevLower > low) {
        lower = low;
        upper = prevUpper;
        state = state.state_3;
    } else {
        lower = prevLower;
        upper = prevUpper;
        state = state.state_5;
    }
case state_5:
    if (prevUpper < high) {
        lower = low;
        upper = high;
        state = state.state_1;
    } else if (prevLower > low) {
        lower = low;
        upper = high;
        state = state.state_1;
    } else {
        lower = prevLower;
        upper = prevUpper;
        state = state.state_5;
    }
}

def barNumber = BarNumber();
def barCount = HighestAll(If(IsNaN(close), 0, barNumber));
def boxNum;
def boxUpperIndex;

plot "Upper Band";
plot "Lower Band";
"Upper Band".setPaintingStrategy(paintingStrategy.HORIZONTAL);
"Lower Band".setPaintingStrategy(paintingStrategy.HORIZONTAL);
plot "Buy Signal" = CompoundValue(1, state[1] == state.state_5 and prevUpper < high, no);
plot "Sell Signal" = CompoundValue(1, state[1] == state.state_5 and prevLower > low, no);

if (IsNaN(close)) {
    boxNum = boxNum[1] + 1;
    boxUpperIndex = 0;
    "Upper Band" = Double.NaN;
    "Lower Band" = Double.NaN;
} else {
    boxNum = TotalSum("Buy Signal" or "Sell Signal");
    boxUpperIndex = fold indx = 0 to barCount - barNumber + 2 with valInd = Double.NaN
        while IsNaN(valInd)
        do if (GetValue(boxNum, -indx) != boxNum)
            then indx
            else Double.NaN;
    "Upper Band" = GetValue(upper, -boxUpperIndex + 1);
    "Lower Band" = GetValue(lower, -boxUpperIndex + 1);
}

"Upper Band".SetDefaultColor(color.dark_green);
"Lower Band".SetDefaultColor(Color.RED);
"Sell Signal".SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
"Sell Signal".SetDefaultColor(Color.RED);
"Buy Signal".SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
"Buy Signal".SetDefaultColor(color.dark_green);

plot FIB50 = ("Upper Band"-"Lower Band")/2 + "Lower Band";
FIB50.setDefaultColor(color.yellow);
FIB50.setPaintingStrategy(paintingStrategy.HORIZONTAL);

plot FIB236 = if T1 then ("Upper Band"-"Lower Band")*0.236 + "Upper Band" else double.nan;
FIB236.setDefaultColor(color.light_green);
FIB236.setPaintingStrategy(paintingStrategy.DASHES);

plot FIBN236 = if T1 then "Lower Band" -("Upper Band"-"Lower Band")*0.236 else double.nan;
FIBN236.setDefaultColor(color.PINK);
FIBN236.setPaintingStrategy(paintingStrategy.DASHES);

plot FIB618 = if T2 then ("Upper Band"-"Lower Band")*0.618 + "Upper Band" else double.nan;
FIB618.setDefaultColor(color.yellow);
FIB618.setPaintingStrategy(paintingStrategy.DASHES);

plot FIBN618 = if T2 then "Lower Band" -("Upper Band"-"Lower Band")*0.618 else double.nan;
FIBN618.setDefaultColor(color.yellow);
FIBN618.setPaintingStrategy(paintingStrategy.DASHES);

plot DTH = if DT then ("Upper Band"-"Lower Band") + "Upper Band" else double.nan;
DTH.setDefaultColor(color.GRAY);
DTH.setPaintingStrategy(paintingStrategy.DASHES);

plot DTL = if DT then "Lower Band" - ("Upper Band"-"Lower Band") else double.nan;
DTL.setDefaultColor(color.GRAY);
DTL.setPaintingStrategy(paintingStrategy.DASHES);
Enjoy!
 
Last edited:

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

If you are familiar with the Darvas Box, you'll quickly realize that when the box lines are broken, there is no target level that price will tend to break to. This indicator adds a target level (CYAN dashed lines). Adding these target levels on the chart gives traders a perfect scalping trading tool. Place your entry trade at the box level and set your closing trade at the target line. This works on any timeframe. I also suggest adding a 20 EMA to your chart to see the trend level. You'll notice in the picture when the 20 is slopping down, Darvas Box target levels to the downside are hit. In the below example, the yellow arrows highlight targets being hit. There was one time that the box was broken and the target was not hit, highlighted with a yellow X.

tFkKYll.jpg



G1N7H3v.jpg


I also added the 50% halfway line between the box levels. (yellow line). This can also be monitored for support and resistance. If the price can't break through the 50 yellow line, it is an early indication that the target may be hit. For example, in the below picture, price doesn't break above the 50, and it is used as resistance and the lower box level is broken and target is hit.


722Cn8b.jpg


download link: http://tos.mx/IlEEK2L

Code:
#
# TD Ameritrade IP Company, Inc. (c) 2011-2021
#
# Chewie added the 50 and 23.6 fib extentions on 1/8/2022

def state = {default state_1, state_2, state_3, state_4, state_5};

def upper;
def lower;

def prevLower = CompoundValue(1, lower[1], low);
def prevUpper = CompoundValue(1, upper[1], high);

switch (state[1]) {
case state_1:
    lower = low;
    if (prevUpper >= high) {
        upper = high[1];
        state = state.state_2;
    } else {
        upper = high;
        state = state.state_1;
    }
case state_2:
    if (prevUpper >= high) {
        lower = low;
        upper = prevUpper;
        state = state.state_3;
    } else {
        lower = low;
        upper = high;
        state = state.state_1;
    }
case state_3:
    if (prevUpper < high) {
        lower = low;
        upper = high;
        state = state.state_1;
    } else if (prevLower > low) {
        lower = low;
        upper = prevUpper;
        state = state.state_3;
    } else {
        lower = prevLower;
        upper = prevUpper;
        state = state.state_4;
    }
case state_4:
    if (prevUpper < high) {
        lower = low;
        upper = high;
        state = state.state_1;
    } else if (prevLower > low) {
        lower = low;
        upper = prevUpper;
        state = state.state_3;
    } else {
        lower = prevLower;
        upper = prevUpper;
        state = state.state_5;
    }
case state_5:
    if (prevUpper < high) {
        lower = low;
        upper = high;
        state = state.state_1;
    } else if (prevLower > low) {
        lower = low;
        upper = high;
        state = state.state_1;
    } else {
        lower = prevLower;
        upper = prevUpper;
        state = state.state_5;
    }
}

def barNumber = BarNumber();
def barCount = HighestAll(If(IsNaN(close), 0, barNumber));
def boxNum;
def boxUpperIndex;

plot "Upper Band";
plot "Lower Band";
plot "Buy Signal" = CompoundValue(1, state[1] == state.state_5 and prevUpper < high, no);
plot "Sell Signal" = CompoundValue(1, state[1] == state.state_5 and prevLower > low, no);

if (IsNaN(close)) {
    boxNum = boxNum[1] + 1;
    boxUpperIndex = 0;
    "Upper Band" = Double.NaN;
    "Lower Band" = Double.NaN;
} else {
    boxNum = TotalSum("Buy Signal" or "Sell Signal");
    boxUpperIndex = fold indx = 0 to barCount - barNumber + 2 with valInd = Double.NaN
        while IsNaN(valInd)
        do if (GetValue(boxNum, -indx) != boxNum)
            then indx
            else Double.NaN;
    "Upper Band" = GetValue(upper, -boxUpperIndex + 1);
    "Lower Band" = GetValue(lower, -boxUpperIndex + 1);
}

"Upper Band".SetDefaultColor(CreateColor(10, 156, 35));
"Lower Band".SetDefaultColor(Color.RED);
"Sell Signal".SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
"Sell Signal".SetDefaultColor(Color.RED);
"Buy Signal".SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
"Buy Signal".SetDefaultColor(CreateColor(10, 156, 35));

plot FIB50 = ("Upper Band"-"Lower Band")/2 + "Lower Band";
FIB50.setDefaultColor(color.yellow);

plot FIB236 = ("Upper Band"-"Lower Band")*0.236 + "Upper Band";
FIB236.setDefaultColor(color.cyan);
FIB236.setPaintingStrategy(paintingStrategy.DASHES);

plot FIBN236 = "Lower Band" -("Upper Band"-"Lower Band")*0.236;
FIBN236.setDefaultColor(color.cyan);
FIBN236.setPaintingStrategy(paintingStrategy.DASHES);
Enjoy!
Good Morning, do the arrows repaint? Do they plot immediately at the start of the candle?
Thanks
 
Chewie
You always come up with the most interesting indicators, I just want to say thank you for sharing
them.
 
@chewie76 to simplify this.. are we buying on the red line, selling on the green (or cyan price target?) Or should we wait for an arrow to say buy/sell? Please see the attached screenshot.. if you bought puts on this line at 4581, ema 20 was going down.. but then a few bars after it reverses and heads back up. It had a red arrow at 12:45 PM. Any way to avoid things like that? Am I looking at this indicator completely wrong? I am using the 1D/5M chart on SPX. Do you use any additional confirmation before entering? If it opens up under the box, do you just wait for it to pick a direction and play from there? Thanks!

 
Last edited:
@chewie76 to simplify this.. are we buying on the red line, selling on the green (or cyan price target?) Or should we wait for an arrow to say buy/sell? Please see the attached screenshot.. if you bought puts on this line at 4581, ema 20 was going down.. but then a few bars after it reverses and heads back up. It had a red arrow at 12:45 PM. Any way to avoid things like that? Am I looking at this indicator completely wrong? I am using the 1D/5M chart on SPX. Do you use any additional confirmation before entering? If it opens up under the box, do you just wait for it to pick a direction and play from there? Thanks!

Buy or sell when the box breaks. See green arrow. This example hit target 1,2 and 3.
 
Hi @chewie76,

Interesting study for sure. What time frames do you typically use these on? I see your charts are showing daily but curious if this will work on lower time frames like 5m for scalping?
 
If you are familiar with the Darvas Box, you'll quickly realize that when the box lines are broken, there is no target level that price will tend to break to. This indicator adds a target level (CYAN dashed lines). Adding these target levels on the chart gives traders a perfect scalping trading tool. Place your entry trade at the box level and set your closing trade at the target line. This works on any timeframe. I also suggest adding a 20 EMA to your chart to see the trend level. You'll notice in the picture when the 20 is slopping down, Darvas Box target levels to the downside are hit. In the below example, the yellow arrows highlight targets being hit. There was one time that the box was broken and the target was not hit, highlighted with a yellow X. I updated this to make the box horizontal lines to clean the chart. I also added a 61.8 and a double target line.

tFkKYll.jpg



G1N7H3v.jpg


I also added the 50% halfway line between the box levels. (yellow line). This can also be monitored for support and resistance. If the price can't break through the 50 yellow line, it is an early indication that the target may be hit. For example, in the below picture, price doesn't break above the 50, and it is used as resistance and the lower box level is broken and target is hit.


722Cn8b.jpg


Check out the DAILY chart on SPY. It hits these targets.

RTb1waR.jpg


download link: http://tos.mx/MWNNnFj

Code:
#
#
# TD Ameritrade IP Company, Inc. (c) 2011-2021
#
# Chewie added the 50 and 23.6 and 61.8 fib extentions and Double Target on 1/8/2022

input T1 = yes;
input T2 = yes;
input DT = yes;

def state = {default state_1, state_2, state_3, state_4, state_5};

def upper;
def lower;

def prevLower = CompoundValue(1, lower[1], low);
def prevUpper = CompoundValue(1, upper[1], high);

switch (state[1]) {
case state_1:
    lower = low;
    if (prevUpper >= high) {
        upper = high[1];
        state = state.state_2;
    } else {
        upper = high;
        state = state.state_1;
    }
case state_2:
    if (prevUpper >= high) {
        lower = low;
        upper = prevUpper;
        state = state.state_3;
    } else {
        lower = low;
        upper = high;
        state = state.state_1;
    }
case state_3:
    if (prevUpper < high) {
        lower = low;
        upper = high;
        state = state.state_1;
    } else if (prevLower > low) {
        lower = low;
        upper = prevUpper;
        state = state.state_3;
    } else {
        lower = prevLower;
        upper = prevUpper;
        state = state.state_4;
    }
case state_4:
    if (prevUpper < high) {
        lower = low;
        upper = high;
        state = state.state_1;
    } else if (prevLower > low) {
        lower = low;
        upper = prevUpper;
        state = state.state_3;
    } else {
        lower = prevLower;
        upper = prevUpper;
        state = state.state_5;
    }
case state_5:
    if (prevUpper < high) {
        lower = low;
        upper = high;
        state = state.state_1;
    } else if (prevLower > low) {
        lower = low;
        upper = high;
        state = state.state_1;
    } else {
        lower = prevLower;
        upper = prevUpper;
        state = state.state_5;
    }
}

def barNumber = BarNumber();
def barCount = HighestAll(If(IsNaN(close), 0, barNumber));
def boxNum;
def boxUpperIndex;

plot "Upper Band";
plot "Lower Band";
"Upper Band".setPaintingStrategy(paintingStrategy.HORIZONTAL);
"Lower Band".setPaintingStrategy(paintingStrategy.HORIZONTAL);
plot "Buy Signal" = CompoundValue(1, state[1] == state.state_5 and prevUpper < high, no);
plot "Sell Signal" = CompoundValue(1, state[1] == state.state_5 and prevLower > low, no);

if (IsNaN(close)) {
    boxNum = boxNum[1] + 1;
    boxUpperIndex = 0;
    "Upper Band" = Double.NaN;
    "Lower Band" = Double.NaN;
} else {
    boxNum = TotalSum("Buy Signal" or "Sell Signal");
    boxUpperIndex = fold indx = 0 to barCount - barNumber + 2 with valInd = Double.NaN
        while IsNaN(valInd)
        do if (GetValue(boxNum, -indx) != boxNum)
            then indx
            else Double.NaN;
    "Upper Band" = GetValue(upper, -boxUpperIndex + 1);
    "Lower Band" = GetValue(lower, -boxUpperIndex + 1);
}

"Upper Band".SetDefaultColor(color.dark_green);
"Lower Band".SetDefaultColor(Color.RED);
"Sell Signal".SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
"Sell Signal".SetDefaultColor(Color.RED);
"Buy Signal".SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
"Buy Signal".SetDefaultColor(color.dark_green);

plot FIB50 = ("Upper Band"-"Lower Band")/2 + "Lower Band";
FIB50.setDefaultColor(color.yellow);
FIB50.setPaintingStrategy(paintingStrategy.HORIZONTAL);

plot FIB236 = if T1 then ("Upper Band"-"Lower Band")*0.236 + "Upper Band" else double.nan;
FIB236.setDefaultColor(color.light_green);
FIB236.setPaintingStrategy(paintingStrategy.DASHES);

plot FIBN236 = if T1 then "Lower Band" -("Upper Band"-"Lower Band")*0.236 else double.nan;
FIBN236.setDefaultColor(color.PINK);
FIBN236.setPaintingStrategy(paintingStrategy.DASHES);

plot FIB618 = if T2 then ("Upper Band"-"Lower Band")*0.618 + "Upper Band" else double.nan;
FIB618.setDefaultColor(color.yellow);
FIB618.setPaintingStrategy(paintingStrategy.DASHES);

plot FIBN618 = if T2 then "Lower Band" -("Upper Band"-"Lower Band")*0.618 else double.nan;
FIBN618.setDefaultColor(color.yellow);
FIBN618.setPaintingStrategy(paintingStrategy.DASHES);

plot DTH = if DT then ("Upper Band"-"Lower Band") + "Upper Band" else double.nan;
DTH.setDefaultColor(color.GRAY);
DTH.setPaintingStrategy(paintingStrategy.DASHES);

plot DTL = if DT then "Lower Band" - ("Upper Band"-"Lower Band") else double.nan;
DTL.setDefaultColor(color.GRAY);
DTL.setPaintingStrategy(paintingStrategy.DASHES);
Enjoy!

To the OP....my trading partner and I came across your strategy. Great strategy btw! I have some questions for you about the strategy to try to understand what is happening and how to best trade it. When I have the strategy working, live, we noticed that a "0" is printed signaling getting out of the market, entirely. But after some bars (sometimes its just 2 and sometimes we have seen it up to 7 bars afterwards), the "0" disappears and it keeps you in the market. Of course, in live trading, this is not possible. Is there anyway around this issue? I should note, we are trying to build conditional orders in TOS to try and trade this strategy as automated as possible (which isn't as automated as we would like lol) and we quickly figured out that TOS does not have the capacity to hold complicated conditional orders as the ones we are trying to build for this strategy. Any help would greatly be appreciated, thank you!
 
To the OP....my trading partner and I came across your strategy. Great strategy btw! I have some questions for you about the strategy to try to understand what is happening and how to best trade it. When I have the strategy working, live, we noticed that a "0" is printed signaling getting out of the market, entirely. But after some bars (sometimes its just 2 and sometimes we have seen it up to 7 bars afterwards), the "0" disappears and it keeps you in the market. Of course, in live trading, this is not possible. Is there anyway around this issue? I should note, we are trying to build conditional orders in TOS to try and trade this strategy as automated as possible (which isn't as automated as we would like lol) and we quickly figured out that TOS does not have the capacity to hold complicated conditional orders as the ones we are trying to build for this strategy. Any help would greatly be appreciated, thank you!
I'm not sure how to make that adjustment.
 
Let me save a lot of people, A LOT of time, heartache and headaches...this strategy DOES NOT WORK. The code/script has recursion (if you do not know what that is, I encourage you to research it so you know what it is and what you are looking for in a strategy). This strategy repaints signals. Long story short...at times, it signals you to get out of the market completely, and after "x" amount of bars (and under the right conditions of course), it repaints making it seem like you should have entered into a trade (but back in the past, which we all know that is impossible to do) and thus the P/L looks fabulous. DO NOT USE IN REAL TIME. I am and have been trying to figure out a way to make this strategy work but I can't seem to figure out a way to remove the recursion portion on the code. Nothing here to see other than to build upon another strategy or for educational purposes ONLY.
 
Let me save a lot of people, A LOT of time, heartache and headaches...this strategy DOES NOT WORK. The code/script has recursion (if you do not know what that is, I encourage you to research it so you know what it is and what you are looking for in a strategy). This strategy repaints signals. Long story short...at times, it signals you to get out of the market completely, and after "x" amount of bars (and under the right conditions of course), it repaints making it seem like you should have entered into a trade (but back in the past, which we all know that is impossible to do) and thus the P/L looks fabulous. DO NOT USE IN REAL TIME. I am and have been trying to figure out a way to make this strategy work but I can't seem to figure out a way to remove the recursion portion on the code. Nothing here to see other than to build upon another strategy or for educational purposes ONLY.
The indicator works fine for me. I'm not using it as a strategy the way you are using it. I'm looking for set exit levels to scalp to.
 
Yes! This version has recent boxes. http://tos.mx/K5pKe4M
Thank you so much for your help. I loaded the updated version. I still see two or three boxes depends on the input total candles. Please see the below screenshot. Is it possible to show only the last box and the extension also should be showned only for the last box. I will add reference of paid version. Can we tweak a little bit to get it same as the reference screenshot?

Please see the paid version for reference.
 
Last edited by a moderator:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
477 Online
Create Post

Similar threads

Similar threads

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