Convert Darvas Box Strategy V2 for TOS

zac31

New member
Platform
  1. Thinkorswim
Can anyone convert this for TOS? Thanks in advance.


Code:
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ceyhun

//@version=4
strategy ("Darvas Box Strategy V2",overlay=true)

boxp=input(5, "BOX LENGTH")

LL = lowest(low,boxp)
k1 = highest(high,boxp)
k2 = highest(high,boxp-1)
k3 = highest(high,boxp-2)

NH   = valuewhen(high>k1[1],high,0)
box1 = k3<k2
TopBox = valuewhen(barssince(high>k1[1])==boxp-2 and box1, NH, 0)
BottomBox = valuewhen(barssince(high>k1[1])==boxp-2 and box1, LL, 0)

plot(TopBox, linewidth=2, color=#00FF00, title="TopBox")
plot(BottomBox, linewidth=2, color=#FF0000, title="BottomBox")

if crossover(close,TopBox)
    strategy.entry("Long", strategy.long, comment="Long")

if crossunder(close,BottomBox)
    strategy.entry("Short", strategy.short, comment="Short")
 
Last edited by a moderator:

barbaros

Administrator
Staff member
Try this

Code:
script valuewhen {
    input cond = 0;
    input pr = 0;
    input n2 = 0;
    def n = n2 + 1;
# start at 0 so it looks at current bar
    def offset2 = fold j = 0 to 200
    with p
    while p < n + 1
    do p + ( if p == n then j - n else if GetValue(cond, j) then 1 else 0 );
# def bnz = bn - offset2 + 1;
    plot price = GetValue(pr, offset2 - 1);
    plot offset = offset2;
}


script barssince {
   input cond = 0;
   def a = fold i = 0 to 200
   with p
   while getvalue(cond, i) == 0
   do p + 1;
   plot z = a;
}


#-------------------------------
def bn = BarNumber();
def na = Double.NaN;
#-------------------------------


input boxp = 5;

def LL = Lowest(low, boxp);
def k1 = Highest(high, boxp);
def k2 = Highest(high, boxp - 1);
def k3 = Highest(high, boxp - 2);

def NH = valuewhen(high > k1[1], high, 0);
def box1 = k3 < k2;

def TopBox = valuewhen(barssince(high > k1[1]) == boxp - 2 and box1, NH, 0);
def BottomBox = valuewhen(barssince(high > k1[1]) == boxp - 2 and box1, LL, 0);

plot z1 = TopBox;
z1.setlineweight(2);
z1.setdefaultcolor(color.green);

plot z2 = bottomBox;
z2.setlineweight(2);
z2.setdefaultcolor(color.red);



#if crossover(close,TopBox)
#    strategy.entry("Long", strategy.long, comment="Long")
plot long1 = (close crosses above topbox);
long1.SetPaintingStrategy(PaintingStrategy.boolean_ARROW_UP);
long1.SetDefaultColor(Color.WHITE);
long1.SetLineWeight(3);
long1.HideBubble();



#if crossunder(close,BottomBox)
#    strategy.entry("Short", strategy.short, comment="Short")
plot short1 = (close crosses below bottombox);
short1.SetPaintingStrategy(PaintingStrategy.boolean_ARROW_down);
short1.SetDefaultColor(Color.WHITE);
short1.SetLineWeight(3);
short1.HideBubble();
 
Top