简体   繁体   中英

How do I make my script work for both buy and sell indicators for Macd and ema as conditions in pine scripting

So I have a problem with my pine script and I just got into pine scripting so after browsing through some answers on here for some of the related questions I came up with a modified script but I have some problems because my script does what I want it to to when it comes to the sell indicator but it doesn't give me what I want from the buy indicator.

Basically I want it to show the sell indicator when my macd crosses below ema which I plotted on the the script and also it should show me the buy signal only if macd crosses above the ema. kindly see the script below.

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

//@version=4
study("Fortune sell script", overlay=true, resolution="")

//input
emaLength = input(200, minval=1, title ="Length")
srce = input(close, title="Source")
offset = input(title="Offset", type=input.integer,  defval=0, minval=-500, maxval=500)
out = ema(srce,emaLength)
macd_default = input(title="macd length", type=input.integer, defval=9, minval=1)
fast_l = input(title="Fast length", type= input.integer, defval=12)
slow_l = input(title="Slow length", type= input.integer, defval=26)

//calculations
fast_ma= ema(close, fast_l)
slow_ma= ema(close, slow_l)
macd = fast_ma - slow_ma
amacd = ema(macd, macd_default)
d_macd = macd - amacd

// Deternine if we are currently LONG
isLong = false
isLong := nz(isLong[1], false)

// Determine if we are currently SHORT
isShort = false
isShort := nz(isShort[1], false)


halfMod =  out % 2

d_macdUp = crossover(d_macd, 0)

d_macdDwn = crossunder(d_macd, 0)


//creating conditions for crosses above ema and below ema for both buy and sell indicators
buySig = not isLong and (d_macdUp) and (close > out)
sellSig = not isShort and (d_macdDwn) and (close < out)

if (buySig)
    isLong := true
    isShort := false

if (sellSig)
    isLong := false
    isShort := true
    

//plot the shape of the indicators and colors
//plotshape(series=buySig, text="BUY", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)

plotshape(series=sellSig, text="SELL", style=shape.triangledown, location=location.abovebar, color= color.red, size= size.small)

//plot the ema to visualise the process
plot(out, title="EMA", color= color.blue, offset = offset)

plot(close)

when I now take out the part with buySig = not isLong and (d_macdUp) and (close > out) and replace it with buySig = not isLong and (d_macdUp) , then the sell signals show according to how I want but not when the code for buySig is the opposite of the code for sellSig .

What I need is a script that works the way the sell indicator works when I take that piece of code out and comment out the plotshape for buy signals. I need both buy and sell indicators to show based on the conditions specified.

Found an alternative eventually to this problem.

I added a and (d_macd >0) for buy and vice versa for the sell respectively to the lines

buySig = not isLong and (d_macdUp) and (close > out)
sellSig = not isShort and (d_macdDwn) and (close < out)

Buy = close > ema and LONG Sell = close < ema and SHORT

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM