0
  • I want to run/execute the script every 1 minute.
  • Everytime it checks if there is no position opened.
  • If there is no position opened then it opens a new position.

I just need a hint how to open a position every 1 minute.

I am going to implement it into the following strategy

//@version=5
strategy("Jopin - modify 1", overlay=true)

// Setting up timeperiod for testing
startPeriodYear = input(2023, "Backtest Start Year")
startPeriodMonth = input(4, "Backtest Start Month")
startPeriodDay = input(1, "Backtest Start Day")
testPeriodStart = timestamp(startPeriodYear, startPeriodMonth, startPeriodDay, 0, 0)

stopPeriodYear = input(2023, "Backtest Stop Year")
stopPeriodMonth = input(4, "Backtest Stop Month")
stopPeriodDay = input(30, "Backtest Stop Day")
testPeriodStop = timestamp(stopPeriodYear, stopPeriodMonth, stopPeriodDay, 0, 0)

// Moving Averages
ema14 = ema(close, 14)
ema28 = ema(close, 28)
sma56 = sma(close, 56)


// Strategy
goLong = cross(ema14, sma56) and ema14 > ema28
goShort = cross(ema14, sma56) and ema14 < ema28

// Strategy.When to enter
if time >= testPeriodStart
    if time <= testPeriodStop
        strategy.entry("Go Long", strategy.long, 1.0, when=goLong)
        strategy.entry("Go Short", strategy.short, 1.0, when=goShort)

// Strategy.When to take profit 
if time >= testPeriodStart 
    if time <= testPeriodStop 
        strategy.exit("Close Long", "Go Long", profit=2000) 
        strategy.exit("Close Short", "Go Short", profit=2000)

I've edited the following code

//@version=2
strategy("Jopin - modify 2", overlay=true)

// Setting up timeperiod for testing
startPeriodYear = input(2023, "Backtest Start Year")
startPeriodMonth = input(3, "Backtest Start Month")
startPeriodDay = input(1, "Backtest Start Day")
testPeriodStart = timestamp(startPeriodYear, startPeriodMonth, startPeriodDay, 0, 0)

stopPeriodYear = input(2023, "Backtest Stop Year")
stopPeriodMonth = input(3, "Backtest Stop Month")
stopPeriodDay = input(30, "Backtest Stop Day")
testPeriodStop = timestamp(stopPeriodYear, stopPeriodMonth, stopPeriodDay, 0, 0)

// Strategy.When to enter
if time >= testPeriodStart
    if time <= testPeriodStop
        strategy.entry("Go Long", strategy.long, 1.0)
        strategy.entry("Go Short", strategy.short, 1.0)

// Strategy.When to take profit 
if time >= testPeriodStart 
    if time <= testPeriodStop 
        strategy.exit("Close Long", "Go Long", profit=2000) 
        strategy.exit("Close Short", "Go Short", profit=2000)

I need a function which every minute trigger my code (1. check if there is opened a LONG or SHORT position, 2. IF NOT... Then it opens a LONG or SHORT position).

1 Answers1

0

- I want to run/execute the script every 1 minute.
In pinescript, your script is executed on each bar close. So choose a 1 minute timeframe on your chart and your script will execute each minute.

- Everytime it checks if there is no position opened.
To check if no posiion is already opened test strategy.opentrades value

if strategy.opentrades == 0
    // there is no trades already opened
else
    // there is at least one trade open

- If there is no position opened then it opens a new position.
You can open the position in the first branch of the if above, like this :

if strategy.opentrades == 0
    // there is no trades already opened
    strategy.entry("MyLong", strategy.long)
else
    // there is at least one trade open
G.Lebret
  • 2,826
  • 2
  • 16
  • 27
  • Thank you. Can you elaborate a bit more about this? In pinescript, your script is executed on each bar close. So choose a 1 minute timeframe on your chart and your script will execute each minute. – Jozef Pintér Apr 17 '23 at 16:39
  • I don't understand this part: So choose a 1 minute timeframe on your chart and your script will execute each minute. – Jozef Pintér Apr 17 '23 at 16:41
  • 1
    That's the fundation of pinescript : your code in pinescript will execute each time a bar close on the tradingview chart. So if you choose a 1minute timeframe on your tradingview chart, the strategy you add to your chart will be executed each time a bar close on your chart = each minute. – G.Lebret Apr 17 '23 at 17:10
  • Please check my code above. It triggers goLong and goShort on the crossing of moving averages. Now I want replace this crossing of moving averages with "each time a bar close on the tradingview chart". What can I do? – Jozef Pintér Apr 17 '23 at 17:26
  • Delete your code under // Strategy and paste my second code : this will generate an entry each minute (if no trade is already opened) – G.Lebret Apr 17 '23 at 17:30
  • I can't remove my code at the bottom. I need the part with the timeperiod for testing. Everything works fine. I just need replace the trigger. Crossing of Moving averages with when a bar close. – Jozef Pintér Apr 17 '23 at 17:43
  • delete the goLong and goShort lines and the 2 when=... your entry will occur each minute – G.Lebret Apr 17 '23 at 18:23
  • It works but only on 30 minutes candlesticks and above. 1. I really don't understand how this works, 2. nothing is defined, 3. I need to triger it to work every 1 minute. – Jozef Pintér Apr 17 '23 at 18:50
  • you need to select the 1 minute timeframe in your chart. – G.Lebret Apr 18 '23 at 02:43
  • I know that. I've tested it. – Jozef Pintér Apr 18 '23 at 08:17
  • you didn't apply the solution : try Delete your code under // Strategy and paste my second code : this will generate an entry each minute (if no trade is already opened) – G.Lebret Apr 18 '23 at 08:35