1

Context: I'm backtesting the below strategy. What I observed is, when ever the BUY or SELL signal alert triggers, instead of executing the corresponding order, it executing both SELL&BUY orders. It doesn't matter which signal is triggered. But if BUY signal triggered, buy order is getting executed and at the sametime SELL order also getting executed and viceversa. I request the help of Pinecoders to fix this issue to execute only the order that received the alert, thanks in advance.

//@version=3
strategy("EMA Slope + EMA Cross Strategy (by ChartArt)", shorttitle="CA_-_EMA_slope_cross", overlay=true)

// ChartArt's EMA Slope + EMA Cross Strategy
//
// Version 1.0
// Idea by ChartArt on March 10, 2018.
//
// This strategy uses divergences between
// three moving averages and their slope
// directions as well as crosses between
// the price and the moving averages
// to switch between long/short positions.
//
// The strategy is non-stop in the market
// and always either long or short.
// 
// In addition the moving averages are colored
// depending if they are trending up or down.
//
// List of my work: 
// https://www.tradingview.com/u/ChartArt/

// Input
price = input(close)
MA1_Length = input(2,step=1, title="EMA 1 Length")
MA2_Length = input(4,step=1, title="EMA 2 Length")
MA3_Length = input(20,step=1, title="EMA 3 Length")

switch1=input(true, title="Show Bar Color?")
switch2=input(true, title="Show Moving Averages?")

// Calculation
MA1 = ema(price, MA1_Length)
MA2 = ema(price, MA2_Length)
MA3 = ema(price, MA3_Length)

// Strategy
long = crossunder(price, MA3) or ( change(price)<0 and change(MA1)<0 and crossunder(price,MA1) and change(MA2)>0 )
short = crossover(price, MA3) or ( change(price)>0 and change(MA1)>0 and crossover(price,MA1)  and change(MA2)<0 ) 

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

if short
    strategy.entry("Short", strategy.short, comment="Short")

// Strategy Alert
alertcondition(long, title='EMA Slope + EMA Cross Strategy, Long Alert', message='Go Long!')
alertcondition(short, title='EMA Slope + EMA Cross Strategy, Short Alert', message='Go Short!')

// MA trend bar color
up =  change(MA2)>0 and change(MA3)>0
dn =  change(MA2)<0 and change(MA3)<0
bar_color = up?green:dn?red:blue
barcolor(switch1?bar_color:na)

// MA trend output color
MA2_color = change(MA2)>0?lime:change(MA2)<0?red:blue
MA3_color = change(MA3)>0?lime:change(MA3)<0?red:blue

// MA output
EMA2 = plot(switch2?MA2:na, title="EMA 2", style=linebr, linewidth=2, color=MA2_color)
EMA3 = plot(switch2?MA3:na, title="EMA 3", style=linebr, linewidth=4, color=MA3_color)
fill(EMA2, EMA3, color=silver, transp=50)

//plot(strategy.equity, title="equity", color=red, linewidth=2, style=areabr)

enter image description here

ClaudiaR
  • 3,108
  • 2
  • 13
  • 27
Ravi Kiran
  • 11
  • 1

1 Answers1

0

It can happen that on a bar close both your conditions happen to be true the same time. In this case the strategy.oca options cannot help you unfortunately either.
If it is not possible to refine your conditionals, maybe it's possible to prefer one to the other. In my example I prefer long to short and should they both appear on a candle close, only my long will be executed:

...  
bool conditionAlreadyMet = false
if long
    strategy.entry("Long", strategy.long, comment="Long")  
    conditionAlreadyMet := true

if short and not conditionAlreadyMet
    strategy.entry("Short", strategy.short, comment="Short")
...

This solution is not elegant, but based on your needs may work just well.

elod008
  • 1,227
  • 1
  • 5
  • 15
  • Unfortunately that didn't resolve the issue. Kindly share if you have alternatives, thanks. – Ravi Kiran Oct 10 '22 at 05:14
  • I see you're probably getting an error message due to version=3. My mistake. I only develop in version 5 (latest) and suggest you do too because it has many advantages. For version=3 remove "bool" from "bool conditionAlreadyMet = false" and your script should work fine without an error. or change your script to version 5. I tested your script once again - version=3 this time - and my solution is doing what I explained and you may want. – elod008 Oct 10 '22 at 08:05
  • Appreciate your response. I have converted the script to V5. Error triggered because of "bool" has gone, but still my BUY and SELL orders are getting executed at the same time. If you don't mind and if you are comfortable, i request your permission to setup a quick call through google meet or skype for 5-10mins. Kindly permit and drop me a mail to rkpivot@gmail.com with your convenient time. By the way, i'm from India. thanks. – Ravi Kiran Oct 11 '22 at 05:11