Joao Silva
Joao Silva

Reputation: 55

Long/Short strategy only shows close entry order for long

A newbie here. I am working on a strategy long/short. When i run my strategy the Long positions are closed as expected but the Short positions i believe are not. Because i have the fees setup to USD per order i want to make sure that every market order is opened/closed so i can back test my strategy. My code as follows.

    hist = macd - signal
if (hist > 0.15)
    strategy.entry("Long", strategy.long, comment="Long")
if (hist < -0.15)
    strategy.close("Long")
if (hist < -0.15)
    strategy.entry("Short", strategy.short, comment="Short")
if (hist > 0.15)
    strategy.close("Short")

Chart

Thanks!

Upvotes: 1

Views: 1949

Answers (1)

Starr Lucky
Starr Lucky

Reputation: 1961

This is correct behaivour as your conditions in fact defined as

hist = macd - signal

if (hist > 0.15)
    strategy.entry("Long", strategy.long, comment="Long")
    strategy.close("Short")
    
if (hist < -0.15)
    strategy.close("Long")
    strategy.entry("Short", strategy.short, comment="Short")

So when you enter Short position in (hist < -0.15) condition, at the next bars, when if (hist > 0.15) is true you will revert your Short position by strategy.entry("Long", strategy.long, comment="Long").

Upvotes: 1

Related Questions