Jurgen
Pine Script Master
Pine Script Master
Posts: 7
Joined: September 4th, 2020
Location: Belgium
Contact: TradingView Profile

Script for long & short postions

I created this script to indicate moments to go long or short:

https://pastebin.com/2eJ21fqx

When the price crosses above the previous week high, and then the second day, if the price stays above or crosses again the previous week high, the signal is given. The problem I have is that the signal is not indicated when it does not cross again the second day, but on the third day or later.
I can't figure it out.
Anymone ideas?

https://www.tradingview.com/x/nVR7nj4X/

Deep-Wave
Pine Script Master
Pine Script Master
Posts: 44
Joined: September 4th, 2020
Contact: TradingView Profile

Re: Script for long & short postions

Hi Jurgen,

I took a look at your script and I have some suggestions (see the code below).
Bare in mind, I am not sure I fully understand what you want the script to do exactly but what I got is the following (correct me if I am wrong):

Long Setup:
Day 1 : The high price crosses above the previous week high.
Day 2 : The high price crosses above the previous week high again or stays above it.

If these conditions are met, a long signal is generated as soon as the high price on day 2 is above the previous week high.
The trade should be closed when the low crosses the previous week low.
The short setup is the exact same logic in opposite direction.

If I understood correctly, you could implement it as follows:

Code: Select all

// long and short stops
Stopshort = crossover (high, prevWeekHigh)
Stoplong = crossunder (low, prevWeekLow)

// Entry & Exit Conditions
var Golong = false // variable that is initialized once
LongEntry = crossover (high[1], prevWeekHigh) and high > prevWeekHigh //the previous high is greater then the previous week high and so is the current high

if LongEntry 
    Golong := true
if Golong and Stoplong
    Golong := false

var Goshort = false
ShortEntry = crossunder (low[1], prevWeekLow) and low < prevWeekLow 

if ShortEntry
    Goshort := true

if Goshort and Stopshort
    Goshort := false
I hope this is useful to you. :wink:

Cheers,

Mark

Jurgen
Pine Script Master
Pine Script Master
Posts: 7
Joined: September 4th, 2020
Location: Belgium
Contact: TradingView Profile

Re: Script for long & short postions

Hi Mark,

Thank you for your help.
The conditions are slightly different.

Long Setup:
Day 1 : The high price crosses above the previous week high.
Any day after day 1 : The high price crosses above the previous week high again (or stays there from the previous day).

If these conditions are met, a long signal is generated as soon as the high price on a particular day after day 1 is above the previous week high.
The trade should be closed when the low crosses the previous week low.
The short setup is the exact same logic in opposite direction.

In the script you propose I get too many signals, see picture, but I don't know how to do it.
https://www.tradingview.com/x/9WuShm2B/

Regards,

Jurgen

Deep-Wave
Pine Script Master
Pine Script Master
Posts: 44
Joined: September 4th, 2020
Contact: TradingView Profile

Re: Script for long & short postions

Hi Jurgen,

thanks for clarifying your setup. I made some modifications to your script, you can check it below.

Fyi: I think you received too many signals because of the conditions you plot your signals on. In your original script it were the Golong/Goshort variables that would plot a signal. If you incorporate my code snippet you would need to plot your signals on the conditions LongEntry/ShortEntry.

I coded the changes by the following logic.
If for example on a Friday the current high crosses the previous week high (or stayed there from the previous day) it checks if the day before (Thursday) a crossover occurred, or the day before that (Wednesday), or the day before that and so on (and we also check that there is no open position). All in all for 4 days out of a 5 day week, then the cycle begins again with a new weekly high/low.

I am sure there is a much more elegant way to program that, but I hope this helps never the less.

Cheers,

ps. I also made some changes to account for repainting issues with the weekly highs and lows and to the way trade exit signals are plotted.

Code: Select all

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=4
//By Juros

study(title="Universal weekly breakout + universal filter", shorttitle="weekly breakout+ filter", overlay=true, precision=8)
prevwkH = input(true, title="Show previous week high?")
prevwkL = input(true, title="show previous week low?")

//previous week high and low
prevWeekHigh = security(syminfo.tickerid, 'W', high[barstate.isrealtime ? 1:0])
prevWeekLow = security(syminfo.tickerid, 'W', low[barstate.isrealtime ? 1:0])

//previous Week high and low Plots
plot(prevwkH ? prevWeekHigh : na, title="Prev Week High", style=plot.style_stepline, linewidth=1, color=color.fuchsia, transp=20)
plot(prevwkL ? prevWeekLow : na, title="Prev Week Low", style=plot.style_stepline, linewidth=1, color=color.fuchsia, transp=20)

//------------------------------------------------------------------------------------------

// stop location for short- and long position (at crossover of previous week low or high)
Stopshort = crossover (high, prevWeekHigh)
Stoplong = crossunder (low, prevWeekLow)

Longposmemo = false
Longposmemo := Stopshort ? true : Stoplong ? false : Longposmemo[1]

Longcond = Stopshort and not Longposmemo[1]
color_1 = color.new(color.green, 70)


Shortposmemo = false
Shortposmemo := Stoplong ? true : Stopshort ? false : Shortposmemo[1]

Shortcond = Stoplong and not Shortposmemo[1]
color_2 = color.new(color.red, 70)
//bgcolor(Sellnow ? color_2 : na)

//------------------------------------------------------------------------------------------

// Entry & Exit Conditions
var Golong = false
var Goshort = false

LongEntry = high > prevWeekHigh and not Golong and (crossover (high[1], prevWeekHigh) or
 crossover (high[2], prevWeekHigh) or
 crossover (high[3], prevWeekHigh) or
 crossover (high[4], prevWeekHigh))

if LongEntry 
    Golong := true
    Goshort :=false

ShortEntry = low < prevWeekLow and not Goshort and (crossunder (low[1], prevWeekLow) or 
 crossunder (low[2], prevWeekLow) or 
 crossunder (low[3], prevWeekLow) or 
 crossunder (low[4], prevWeekLow))

if ShortEntry
    Goshort := true
    Golong := false
    
LSL =  (Golong and Stoplong) ? 1 : 0
SSL = (Goshort and Stopshort) ? 1 : 0


//------------------------------------------------------------------------------------------

// plots : X where the position is stopped
plotchar (LSL, char='X', location=location.abovebar, color=color.yellow)
plotchar (SSL, char='X', location=location.belowbar, color=color.yellow)

plotshape (LongEntry ? high : na, style = shape.labelup, location = location.belowbar, color=color.lime, size=size.tiny, text = 'L', textcolor=color.black)
plotshape (ShortEntry ? low : na, style = shape.labeldown, location = location.abovebar, color=color.fuchsia, size=size.tiny, text ='S', textcolor=color.black)

// alerts
alertcondition(Golong, title="Weakly breakout go long", message="WBO go long")
alertcondition(Goshort, title="weakly breakout go short", message="WBO go short")

//------------------------------------------------------------------------------------------

// Filter to show only those signals that follow on the previous position (long or short) that was a loss.


Jurgen
Pine Script Master
Pine Script Master
Posts: 7
Joined: September 4th, 2020
Location: Belgium
Contact: TradingView Profile

Re: Script for long & short postions

Hello Mark,

Thanks for your effort. I follow your reasoning, it makes sense. I need to look deeper into it, because there is still going something wrong.
The script is usable, but in some occasions, the signals are not correct,

Regards,

Jurgen

Deep-Wave
Pine Script Master
Pine Script Master
Posts: 44
Joined: September 4th, 2020
Contact: TradingView Profile

Re: Script for long & short postions

Hi Jurgen,

Best of luck with this script, when you get it done feel free to share it here, would be very interesting. :wink:

yohukm
Pine Script Rookie
Pine Script Rookie
Posts: 3
Joined: September 16th, 2020

Re: Script for long & short postions

i try in btc for week its good
https://www.tradingview.com/x/GYle3BaB/

but i try i day it not accurate
https://www.tradingview.com/x/IQUBYHbf/

so i change res and it good
https://www.tradingview.com/x/hqaJxvM8/

i thing you must set auto res for script

Code: Select all

res = input(title="Resolution", type=input.resolution, defval="W")

//previous week high and low
prevWeekHigh = security(syminfo.tickerid, res, high[1], lookahead=true)
prevWeekLow = security(syminfo.tickerid, res, low[1], lookahead=true)


Jurgen
Pine Script Master
Pine Script Master
Posts: 7
Joined: September 4th, 2020
Location: Belgium
Contact: TradingView Profile

Re: Script for long & short postions

Thanks,

The script is ment to only be used on a day view.
I guess when you want to use it on a week view it must be changed to crossover of weekly price with previous month high and low.
But it has to be backtested to see if that is profitable.

Regards,

Jurgen

yohukm
Pine Script Rookie
Pine Script Rookie
Posts: 3
Joined: September 16th, 2020

Re: Script for long & short postions

Jurgen wrote:
Thu Sep 10, 2020 8:29 pm
I created this script to indicate moments to go long or short:

https://pastebin.com/2eJ21fqx

When the price crosses above the previous week high, and then the second day, if the price stays above or crosses again the previous week high, the signal is given. The problem I have is that the signal is not indicated when it does not cross again the second day, but on the third day or later.
I can't figure it out.
Anymone ideas?

https://www.tradingview.com/x/nVR7nj4X/
in this screen shot you use 1d time frame
but source code you use resolution for week

Jurgen
Pine Script Master
Pine Script Master
Posts: 7
Joined: September 4th, 2020
Location: Belgium
Contact: TradingView Profile

Re: Script for long & short postions

Do you mean this part: "precision=8" ?

Return to “Share Your Scripts”