Search code examples
pine-scriptpine-script-v5tradingalgorithmic-trading

My MA cross market entries in Pinescript V5 are generating when they should not be


I am starting to test a simple code in pinescript v5 which uses 2 moving averages to determine long and short trades. When backtesting it seems to work for the most part, however, I have noticed some trade entries that make no sense because they are many bars after the signal should have been generated. Does anyone know why this is happenening and what the solution is? I would be very grateful, as I am a complete coding noob!

In the screenshot you can see that a long trade was entered 16 bars after the signal should have been generated!

Here is the code below:

strategy("MA cross", overlay=true, initial_capital = 20000, default_qty_type = strategy.percent_of_equity, backtest_fill_limits_assumption=2)
//Input EMAs
ema8 = ta.sma(close, 8)
sma55 = ta.sma(close, 55)
ema200 = ta.ema(close, 200)

plot(ema8, color = color.yellow)
plot(sma55, color = color.white)
plot(ema200, color = color.rgb(58, 138, 61))

//trading logic
long = ta.crossover(ema8, sma55) 
short = ta.crossunder(ema8, sma55)

//Executing trades

if long
    strategy.entry ("long", strategy.long, limit = close) 

if short
    strategy.entry ("short", strategy.short, limit = close)



//stop loss long

Trailperc = 0.1
price_stop = 0.0

if (strategy.position_size > 0)
    stopValue = close * (1 - Trailperc)
    price_stop:= math.max(stopValue,price_stop[1])
else
    price_stop:=0

if (strategy.position_size > 0)
    strategy.exit("stoploss", stop = price_stop)


// stop loss short

price_stop_short=10000000.0

if (strategy.position_size < 0)
    stopValue = close * (1 + Trailperc)
    price_stop_short:= math.min(stopValue, price_stop_short[1])
else
    price_stop_short:=10000000000

if (strategy.position_size < 0)
    strategy.exit("stoploss", stop=price_stop_short)



Thanks in advance!














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

//@version=5


I am not sure what to do to solve this, I am a complete coding noob.


Solution

  • Replace your code with the below. It is good code btw

    //Executing trades

    if long
        strategy.entry ("long", strategy.long, limit = close * 1.01 ) 
    
    if short
        strategy.entry ("short", strategy.short, limit = close * 0.99 )
    

    Gaps is what I think is causing that entry delay.

    In the case of Longs, if the long signal candle closes, and then the next candle price gaps up- so it opens above the previous close price- so this limit will not execute as a long limit order will not buy if price is above the limit value. I have just made it so that the limit to buy will be the ' close + 1% ' (but you can change this % value).

    Note - if price gaps up higher than 1%, you will encounter the same problem as you were having before. So set this value how it suits you best.

    Note - this new code and your previous code, in the case of a long, if price gaps down any amount, you will still enter. Just FYI

    All the above applies to shorts after being adjusted for shorts

    I hope this helps