0

I wrote a simple strategy in pine scrip, which is based on crossover/crossunder for two different SMAs. It is important for me to test my strategy in some time frames, especially in the last quarter. It doesn't work. I got only a few results. I should get results for whole period (colored on green), when SMAs crossed. Unwanted result

My script is working very well, when I don't use time range or when I set dateCond on true value. Below I present source code:

//@version=4
strategy("Moving Average Cross 1", initial_capital=1000, overlay=true)

start = timestamp(syminfo.timezone, 2021, 1, 1, 0, 0)
end = timestamp(syminfo.timezone, 2021, 4, 1, 0, 0)

fastSMA = sma(close, 9)
slowSMA = sma(close, 50) 

long = crossover(fastSMA, slowSMA)
short = crossunder(fastSMA, slowSMA)

orderSize = floor(strategy.equity / close)

plot(fastSMA, title="20", color=#00ffaa, linewidth=3)
plot(slowSMA, title="50", color=#FFC1CC, linewidth=2)

dateCond = time > start
// dateCond = true

bgcolor(dateCond ? #00ffaa : na)

if dateCond
    strategy.entry("long", strategy.long, qty=orderSize, when = long)
    strategy.entry("short", strategy.short, qty=orderSize, when = short)
strategy.close("long", when = short)
strategy.close("short", when = long)

I tried with different time ranges (when I set start date on the 1 January 2020 it works very well). I additionally colored a background to check condition, but coloring it also work good. I haven't more ideas why for the last quarter it isn't working properly. I will appreciate any help.

I tested script mainly for pair ETH/USDT (Binance)

Czarek
  • 3
  • 3

1 Answers1

0

Here we use larger capital and close positions before entering a new one. This way the whole position is closed before a new one is opened:

//@version=4
strategy("Moving Average Cross 1", initial_capital=100000, overlay=true)

start = timestamp(syminfo.timezone, 2021, 1, 1, 0, 0)
end = timestamp(syminfo.timezone, 2021, 4, 1, 0, 0)

fastSMA = sma(close, 9)
slowSMA = sma(close, 50) 

long = crossover(fastSMA, slowSMA)
short = crossunder(fastSMA, slowSMA)

orderSize = floor(strategy.equity / close)

plot(fastSMA, title="20", color=#00ffaa, linewidth=3)
plot(slowSMA, title="50", color=#FFC1CC, linewidth=2)

dateCond = time > start
// dateCond = true

bgcolor(dateCond ? #00ffaa : na)

strategy.close("long", when = short)
strategy.close("short", when = long)
if dateCond
    strategy.entry("long", strategy.long, qty=orderSize, when = long)
    strategy.entry("short", strategy.short, qty=orderSize, when = short)

enter image description here

PineCoders-LucF
  • 8,288
  • 2
  • 12
  • 21