Darvas Box Strategy V2 For ThinkOrSwim

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

https://www.tradingview.com/v/Uamkz8Ma/

// 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")

this seems close

Ruby:
# convert_pine_darvas_v2_0

#https://usethinkscript.com/threads/convert-tradingview-darvas-box-strategy-v2.12140/
#Tradingview Convert Tradingview Darvas Box Strategy V2
# Thread starterEuxkan  Start dateFriday at 5:47 PM

#https://www.tradingview.com/v/Uamkz8Ma/

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




#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

#// 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")
#


gE1ubqP.jpg
 
this seems close

Ruby:
# convert_pine_darvas_v2_0

#https://usethinkscript.com/threads/convert-tradingview-darvas-box-strategy-v2.12140/
#Tradingview Convert Tradingview Darvas Box Strategy V2
# Thread starterEuxkan  Start dateFriday at 5:47 PM

#https://www.tradingview.com/v/Uamkz8Ma/

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




#\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

#// 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")
#


gE1ubqP.jpg
This indicator is actually a real great tool! Question? I trade Options. I have 2 separate chart windows, 1 for calls and the other for puts. Now This indicator is spot on for calls, which is obvious because the chart goes the same direction as the normal Stock price (SPY). On puts, its another story. Puts chart shows ticker going up, but normal chart showing downward move....anywaysss....would it be possible (Yes, I tried to mess with the script for couple hours but nada), to have this indicator reversed specifically for the put side of the house? It cant be that hard, I am just drawing a blank..well, actually errors ....
 
This indicator is actually a real great tool! Question? I trade Options. I have 2 separate chart windows, 1 for calls and the other for puts. Now This indicator is spot on for calls, which is obvious because the chart goes the same direction as the normal Stock price (SPY). On puts, its another story. Puts chart shows ticker going up, but normal chart showing downward move....anywaysss....would it be possible (Yes, I tried to mess with the script for couple hours but nada), to have this indicator reversed specifically for the put side of the house? It cant be that hard, I am just drawing a blank..well, actually errors ....
The ToS platform does not provide calls and puts in the data feed
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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