Darvas Box Indicator for ThinkorSwim

Good afternoon everyone! Before I get into my question, I spent some time trying to find my answer instead of just throwing it up here. I have been talking with my circle about finding a way to acquire an indicator on Thinkorswim for the Darvas Box. Versions of this question have been asked, and I saw a post for a consolidation box, but it was not what we were looking for.

Is there a way to develop a custom Darvas Box indicator?

Additionally, TOS has a darvas box study, but it too does not do what we are looking for.

This indicator that we are looking for would need to have the ability to draw a box that follows the definition/criteria of a Darvas Box.

I do not mean to ask in a vague way, but I just am at a road block. Any insight would be appreciated!

Code:
# DARVASBOX
# DREWGRIFFITH15 (C) 2014

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 < close, no);
plot "Sell Signal" = CompoundValue(1, state[1] == state.state_5 and prevLower > close, no);
plot "Rating" = if CompoundValue(1, state[1] == state.state_5 and prevUpper < close, no) then 2
                else if CompoundValue(1, state[1] == state.state_5 and prevLower > close, no) then 1
                else 0;

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.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.GREEN);
"Rating".hide();
"Rating".HideBubble();
 

rad14733

Well-known member
VIP
@ProfessorAR15 Here is another version of the Darvas Box script...

Code:
# ************************************
#
# Simpler Trading Darvas Box
# By Eric Purdy
# Revision Date: 09/20/17
# (c) 2017 Simpler Trading, LLC
#
# ************************************

input plotBackwardsExtensions = yes;

DefineGlobalColor("HighFirstBox", color.yellow);
DefineGlobalColor("LowFirstBox", color.plum);

def state = {default init, one, two, three, four, five};

def boxHigh;
def boxLow;
def breakoutLong;
def breakoutShort;
def highFirst;
def lowFirst;

switch (state[1]) {

case init:

    boxHigh = high;
    boxLow = low;
    state = state.one;
    breakoutLong = no;
    breakoutShort = no;
    highFirst = yes;
    lowFirst = no;

case one:

    breakoutLong = no;
    breakoutShort = no;
    if highFirst[1]
    then {
        highFirst = highFirst[1];
        lowFirst = lowFirst[1];
        if (high > boxHigh[1])
        then {
            boxHigh = high;
            boxLow = boxLow[1];
            state = state.one;
        } else {
            state = state.two;
            boxHigh = boxHigh[1];
            boxLow = boxLow[1];
        }
    } else {
        highFirst = highFirst[1];
        lowFirst = lowFirst[1];
        if (low < boxLow[1])
        then {
            boxHigh = boxHigh[1];
            boxLow = low;
            state = state.one;
        } else {
            state = state.two;
            boxHigh = boxHigh[1];
            boxLow = boxLow[1];
        }
    }
case two:

    breakoutLong = no;
    breakoutShort = no;
    if highFirst[1]
    then {
        highFirst = highFirst[1];
        lowFirst = lowFirst[1];
        if (high > boxHigh[1])
        then {
            boxHigh = high;
            boxLow = boxLow[1];
            state = state.one;
        } else {
            state = state.three;
            boxHigh = boxHigh[1];
            boxLow = low;
        }
    } else {
        highFirst = highFirst[1];
        lowFirst = lowFirst[1];
        if (low < boxLow[1])
        then {
            boxHigh = boxHigh[1];
            boxLow = low;
            state = state.one;
        } else {
            state = state.three;
            boxHigh = high;
            boxLow = boxLow[1];
        }
    }

case three:

    breakoutLong = no;
    breakoutShort = no;
    if highFirst[1]
    then {
        highFirst = highFirst[1];
        lowFirst = lowFirst[1];
        if (high > boxHigh[1])
        then {
            boxHigh = high;
            boxLow = boxLow[1];
            state = state.one;
        } else if (low < boxLow[1]) {
            state = state.three;
            boxLow = low;
            boxHigh = boxHigh[1];
        } else {
            state = state.four;
            boxHigh = boxHigh[1];
            boxLow = boxLow[1];
        }
    } else {
        highFirst = highFirst[1];
        lowFirst = lowFirst[1];
        if (low < boxLow[1])
        then {
            boxHigh = boxHigh[1];
            boxLow = low;
            state = state.one;
        } else if (high > boxHigh[1]) {
            state = state.three;
            boxLow = boxLow[1];
            boxHigh = high;
        } else {
            state = state.four;
            boxHigh = boxHigh[1];
            boxLow = boxLow[1];
        }
    }
case four:

    breakoutLong = no;
    breakoutShort = no;
    if highFirst[1]
    then {
        highFirst = highFirst[1];
        lowFirst = lowFirst[1];
        if (high > boxHigh[1])
        then {
            boxHigh = high;
            boxLow = boxLow[1];
            state = state.one;
        } else if (low < boxLow[1]) {
            state = state.three;
            boxLow = low;
            boxHigh = boxHigh[1];
         } else {
            state = state.five;
            boxHigh = boxHigh[1];
            boxLow = boxLow[1];
        }
    } else {
        highFirst = highFirst[1];
        lowFirst = lowFirst[1];
        if (low < boxLow[1])
        then {
            boxHigh = boxHigh[1];
            boxLow = low;
            state = state.one;
        } else if (high > boxHigh[1]) {
            state = state.three;
            boxLow = boxLow[1];
            boxHigh = high;     
        } else {
            state = state.five;
            boxHigh = boxHigh[1];
            boxLow = boxLow[1];
        }
    }
case five:

if (high > boxHigh[1] or low < boxLow[1])
    then {
        state = state.one;
        breakoutLong = if (high > boxHigh[1]) then  yes else no;
        breakoutShort = if (low < boxLow[1]) then yes else no;
        boxHigh = high;
        boxLow = low;
        highFirst = if breakoutLong then yes else no;
        lowFirst = if breakoutShort then yes else no;
   } else {
        state = state.five;
        boxHigh = boxHigh[1];
        boxLow = boxLow[1];
        highFirst = highFirst[1];
        lowFirst = lowFirst[1];
        breakoutLong = no;
        breakoutShort = no;
   }
}

def boxNumber = if state==state.five and state[1] != state.five then boxNumber[1]+1 else boxNumber[1];

plot boxHighPlot = if state == state.five and !isNan(close) then boxHigh else if  ((breakoutLong or breakoutShort) and !isNan(close)) then boxHigh[1] else double.nan;
boxHighPlot.setLineWeight(3);
boxHighPlot.setPaintingStrategy(paintingStrategy.HORIZONTAL);
boxHighPlot.assignValueColor(if (highFirst and !(breakoutLong or breakoutShort)) or ((breakoutLong or breakOutShort) and highFirst[1]) then GlobalColor("HighFirstBox") else  GlobalColor("LowFirstBox"));

plot boxLowPlot = if state == state.five and !isNan(close) then boxLow else if ((breakoutLong or breakoutShort) and !isNan(close)) then boxLow[1] else double.nan;
boxLowPlot.setLineWeight(3);
boxLowPlot.setPaintingStrategy(paintingStrategy.HORIZONTAL);
boxLowPlot.assignValueColor(if (highFirst and !(breakoutLong or breakoutShort)) or ((breakoutLong or breakOutShort) and highFirst[1])  then GlobalColor("HighFirstBox") else GlobalColor("LowFirstBox"));

plot longBoxBreak = if breakoutLong then 1 else double.nan;
longBoxBreak.setPaintingStrategy(paintingStrategy.BOOLEAN_ARROW_UP);
longBoxBreak.setLineWeight(2);
longBoxBreak.setDefaultColor(color.green);

plot shortBoxBreak = if breakoutShort then 1 else double.nan;
shortBoxBreak.setPaintingStrategy(paintingStrategy.BOOLEAN_ARROW_DOWN);
shortBoxBreak.setLineWeight(2);
shortBoxBreak.setDefaultColor(color.red);

boxHighPlot.hideBubble();
boxLowPlot.hideBubble();
longBoxBreak.hideBubble();
shortBoxBreak.hideBubble();

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

plot boxHighExtension;
plot boxLowExtension;

if (IsNaN(close)) {
    boxNum = boxNum[1] + 1;
    boxUpperIndex = 0;
    boxHighExtension = Double.NaN;
    boxLowExtension = Double.NaN;
} else {
    boxNum = TotalSum(breakoutLong or breakoutShort);
    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;
    boxHighExtension = if plotBackwardsExtensions and !(breakoutLong or breakoutShort) then GetValue(boxHighPlot, -boxUpperIndex + 1) else double.nan;
    boxLowExtension =  if plotBackwardsExtensions and !(breakoutLong or breakoutShort) then GetValue(boxLowPlot, -boxUpperIndex + 1) else double.nan;
}

boxHighExtension.assignValueColor(if highFirst then GlobalColor("HighFirstBox") else GlobalColor("LowFirstBox") );
boxHighExtension.setpaintingStrategy(paintingStrategy.HORIZONTAL);
boxHighExtension.hideBubble();
boxHighExtension.setLineWeight(2);
boxLowExtension.assignValueColor(if highFirst then GlobalColor("HighFirstBox") else GlobalColor("LowFirstBox") );
boxLowExtension.setpaintingStrategy(paintingStrategy.HORIZONTAL);
boxLowExtension.hideBubble();
boxLowExtension.setLineWeight(2);
 
Last edited:

3AMBH

Member
2019 Donor
@ProfessorAR15 Here is another version of the Darvas Box script...

Code:
# ************************************
#
# Simpler Trading Darvas Box
# By Eric Purdy
# Revision Date: 09/20/17
# (c) 2017 Simpler Trading, LLC
#
# ************************************

input plotBackwardsExtensions = yes;

DefineGlobalColor("HighFirstBox", color.yellow);
DefineGlobalColor("LowFirstBox", color.plum);

def state = {default init, one, two, three, four, five};

def boxHigh;
def boxLow;
def breakoutLong;
def breakoutShort;
def highFirst;
def lowFirst;

switch (state[1]) {

case init:

    boxHigh = high;
    boxLow = low;
    state = state.one;
    breakoutLong = no;
    breakoutShort = no;
    highFirst = yes;
    lowFirst = no;

case one:

    breakoutLong = no;
    breakoutShort = no;
    if highFirst[1]
    then {
        highFirst = highFirst[1];
        lowFirst = lowFirst[1];
        if (high > boxHigh[1])
        then {
            boxHigh = high;
            boxLow = boxLow[1];
            state = state.one;
        } else {
            state = state.two;
            boxHigh = boxHigh[1];
            boxLow = boxLow[1];
        }
    } else {
        highFirst = highFirst[1];
        lowFirst = lowFirst[1];
        if (low < boxLow[1])
        then {
            boxHigh = boxHigh[1];
            boxLow = low;
            state = state.one;
        } else {
            state = state.two;
            boxHigh = boxHigh[1];
            boxLow = boxLow[1];
        }
    }
case two:

    breakoutLong = no;
    breakoutShort = no;
    if highFirst[1]
    then {
        highFirst = highFirst[1];
        lowFirst = lowFirst[1];
        if (high > boxHigh[1])
        then {
            boxHigh = high;
            boxLow = boxLow[1];
            state = state.one;
        } else {
            state = state.three;
            boxHigh = boxHigh[1];
            boxLow = low;
        }
    } else {
        highFirst = highFirst[1];
        lowFirst = lowFirst[1];
        if (low < boxLow[1])
        then {
            boxHigh = boxHigh[1];
            boxLow = low;
            state = state.one;
        } else {
            state = state.three;
            boxHigh = high;
            boxLow = boxLow[1];
        }
    }

case three:

    breakoutLong = no;
    breakoutShort = no;
    if highFirst[1]
    then {
        highFirst = highFirst[1];
        lowFirst = lowFirst[1];
        if (high > boxHigh[1])
        then {
            boxHigh = high;
            boxLow = boxLow[1];
            state = state.one;
        } else if (low < boxLow[1]) {
            state = state.three;
            boxLow = low;
            boxHigh = boxHigh[1];
        } else {
            state = state.four;
            boxHigh = boxHigh[1];
            boxLow = boxLow[1];
        }
    } else {
        highFirst = highFirst[1];
        lowFirst = lowFirst[1];
        if (low < boxLow[1])
        then {
            boxHigh = boxHigh[1];
            boxLow = low;
            state = state.one;
        } else if (high > boxHigh[1]) {
            state = state.three;
            boxLow = boxLow[1];
            boxHigh = high;
        } else {
            state = state.four;
            boxHigh = boxHigh[1];
            boxLow = boxLow[1];
        }
    }
case four:

    breakoutLong = no;
    breakoutShort = no;
    if highFirst[1]
    then {
        highFirst = highFirst[1];
        lowFirst = lowFirst[1];
        if (high > boxHigh[1])
        then {
            boxHigh = high;
            boxLow = boxLow[1];
            state = state.one;
        } else if (low < boxLow[1]) {
            state = state.three;
            boxLow = low;
            boxHigh = boxHigh[1];
         } else {
            state = state.five;
            boxHigh = boxHigh[1];
            boxLow = boxLow[1];
        }
    } else {
        highFirst = highFirst[1];
        lowFirst = lowFirst[1];
        if (low < boxLow[1])
        then {
            boxHigh = boxHigh[1];
            boxLow = low;
            state = state.one;
        } else if (high > boxHigh[1]) {
            state = state.three;
            boxLow = boxLow[1];
            boxHigh = high;    
        } else {
            state = state.five;
            boxHigh = boxHigh[1];
            boxLow = boxLow[1];
        }
    }
case five:

if (high > boxHigh[1] or low < boxLow[1])
    then {
        state = state.one;
        breakoutLong = if (high > boxHigh[1]) then  yes else no;
        breakoutShort = if (low < boxLow[1]) then yes else no;
        boxHigh = high;
        boxLow = low;
        highFirst = if breakoutLong then yes else no;
        lowFirst = if breakoutShort then yes else no;
   } else {
        state = state.five;
        boxHigh = boxHigh[1];
        boxLow = boxLow[1];
        highFirst = highFirst[1];
        lowFirst = lowFirst[1];
        breakoutLong = no;
        breakoutShort = no;
   }
}

def boxNumber = if state==state.five and state[1] != state.five then boxNumber[1]+1 else boxNumber[1];

plot boxHighPlot = if state == state.five and !isNan(close) then boxHigh else if  ((breakoutLong or breakoutShort) and !isNan(close)) then boxHigh[1] else double.nan;
boxHighPlot.setLineWeight(3);
boxHighPlot.setPaintingStrategy(paintingStrategy.HORIZONTAL);
boxHighPlot.assignValueColor(if (highFirst and !(breakoutLong or breakoutShort)) or ((breakoutLong or breakOutShort) and highFirst[1]) then GlobalColor("HighFirstBox") else  GlobalColor("LowFirstBox"));

plot boxLowPlot = if state == state.five and !isNan(close) then boxLow else if ((breakoutLong or breakoutShort) and !isNan(close)) then boxLow[1] else double.nan;
boxLowPlot.setLineWeight(3);
boxLowPlot.setPaintingStrategy(paintingStrategy.HORIZONTAL);
boxLowPlot.assignValueColor(if (highFirst and !(breakoutLong or breakoutShort)) or ((breakoutLong or breakOutShort) and highFirst[1])  then GlobalColor("HighFirstBox") else GlobalColor("LowFirstBox"));

plot longBoxBreak = if breakoutLong then 1 else double.nan;
longBoxBreak.setPaintingStrategy(paintingStrategy.BOOLEAN_ARROW_UP);
longBoxBreak.setLineWeight(2);
longBoxBreak.setDefaultColor(color.green);

plot shortBoxBreak = if breakoutShort then 1 else double.nan;
shortBoxBreak.setPaintingStrategy(paintingStrategy.BOOLEAN_ARROW_DOWN);
shortBoxBreak.setLineWeight(2);
shortBoxBreak.setDefaultColor(color.red);

boxHighPlot.hideBubble();
boxLowPlot.hideBubble();
longBoxBreak.hideBubble();
shortBoxBreak.hideBubble();

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

plot boxHighExtension;
plot boxLowExtension;

if (IsNaN(close)) {
    boxNum = boxNum[1] + 1;
    boxUpperIndex = 0;
    boxHighExtension = Double.NaN;
    boxLowExtension = Double.NaN;
} else {
    boxNum = TotalSum(breakoutLong or breakoutShort);
    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;
    boxHighExtension = if plotBackwardsExtensions and !(breakoutLong or breakoutShort) then GetValue(boxHighPlot, -boxUpperIndex + 1) else double.nan;
    boxLowExtension =  if plotBackwardsExtensions and !(breakoutLong or breakoutShort) then GetValue(boxLowPlot, -boxUpperIndex + 1) else double.nan;
}

boxHighExtension.assignValueColor(if highFirst then GlobalColor("HighFirstBox") else GlobalColor("LowFirstBox") );
boxHighExtension.setpaintingStrategy(paintingStrategy.HORIZONTAL);
boxHighExtension.hideBubble();
boxHighExtension.setLineWeight(2);
boxLowExtension.assignValueColor(if highFirst then GlobalColor("HighFirstBox") else GlobalColor("LowFirstBox") );
boxLowExtension.setpaintingStrategy(paintingStrategy.HORIZONTAL);
boxLowExtension.hideBubble();
boxLowExtension.setLineWeight(2);
Hi Rad, thanks for the script. When combined with the MACD on a 5 minute chart it looks like a winner tool. Stay safe!
 

Similar threads

Top