Drewdavid
Drewdavid

Reputation: 3192

How to stop a strategy after N number of trades are completed?

How can I tell a strategy to stop running after taking N number of trades (eg, 1 trade)?

And then how I can "reset" the strategy to take another trade - Eg. would I have to edit my script and save it again, or is there a more elegant way?

EDIT:

Someone on the TradingView chat referred me to strategy.closedtrades which could help with with limiting the number of trades before stopping the strategy.

However, I still don't know how to "restart" the strategy to allow it to run again.

Upvotes: 0

Views: 519

Answers (1)

Bjorgum
Bjorgum

Reputation: 2310

Try adding these inputs to your strategy to create a date window in which your trade entries are calculated

startTime = input(timestamp("01 Jan 2000 00:00 GMT-7"), "Start Filter", input.time)
endTime = input(timestamp("01 Jan 2099 00:00 GMT-7"), "End Filter", input.time)

dateFilter = time >= startTime and time <= endTime

long = condition1 and dateFilter

short = condition2 and dateFilter

Before you create the alert, set the start data to the time now. If you wish to restart the strategy then delete the old alert and create a new one from the current time once again. The closed trades will return to zero and begin calculating once again.

Cheers and best of luck with your trading and coding my friend!

Upvotes: 1

Related Questions