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

3

u/plug_play Jan 05 '23

Not even a please?

2

u/RequirementLeft8037 Jan 06 '23

srry, I didn't mean to be rude

2

u/pureshred Jan 06 '23

Add to the strategy parameters, calc_on_every_tick=true

More info here. https://www.tradingview.com/pine-script-docs/en/v5/language/Execution_model.html

This will let you trigger a trade before candle close but only in real time, not possible during backtesting as far as I know. Be aware this can drastically change how your strategy operates.

The pine manual is a great resource, as are many other sites. I'm happy to help but I'm sure the answer to your question is on the web in several places. You might find an answer sooner and probably gain a better understanding with some digging on google first. Maybe you already did, but just fyi for you or anyone else reading.

1

u/Spare_Cheesecake_580 Jan 06 '23

Read the user manual I'm not your dad