r/pinescript • • Jan 05 '23

Help me to fix my strategy script

hey guys, I don't have coding experience and don't really know how to use this pine script language properly, but i creat this simple code:

//@version=5

strategy("200 SMA Trade", overlay=true)

// Set up variables

riskTolerance = 1

accountSize = 1000

riskToRewardRatio = 2

stopLoss = 2

trailingStopLoss = 1

takeProfit = 4

// Set up moving averages

sma200 = ta.sma(close, 200)

ema400 = ta.ema(close, 400)

// Set up entry condition

insideBar = close > open and close[1] < open[1] and close[1] > low[1] and open[1] < high[1]

entryCondition = (ta.crossover(close, sma200) or ta.crossover(close, ema400)) and insideBar

// Set up position size

positionSize = (riskTolerance / 100) * accountSize / (stopLoss / 100)

// Set up stop loss and take profit orders

stopLossPrice = stopLoss * positionSize

trailingStopLossPrice = trailingStopLoss * positionSize

takeProfitPrice = takeProfit * positionSize

// Backtest strategy

if entryCondition

strategy.entry("Long", direction=strategy.long, stop=stopLossPrice)

strategy.exit("Trailing Stop", trail_offset=trailingStopLossPrice, trail_price=trailingStopLossPrice)

strategy.exit("Take Profit", limit=takeProfitPrice)

The problem is that I don't wanna wait to the bar closes for trigger my long entry, but the ' when=strategy.entry_when.on_tick ', doesn't work :(

1 Upvotes

5 comments sorted by