Is there a wait function? #265
Unanswered
pascalchristian
asked this question in
Q&A
Replies: 2 comments 4 replies
-
I'll be very interested in seeing a better answer here, I solved it in what is probably the most ugly manner. class example(Strategy):
n1 = 1
n2 = 20
relax = 5 # how many days/periods to wait before trading again
relax = pd.to_timedelta(relax, unit="D") # convert to a timedelta object
def init(self):
super().init()
self.sma1 = self.I(SMA, self.data.Close, self.n1)
self.sma2 = self.I(SMA, self.data.Close, self.n2)
def next(self):
super().next()
price = self.data.Close[-1]
if len(self.closed_trades) == 0: # If there hasn't been any trades yet
last_trade_time = self.data.index[0]
elif len(self.trades) == 0: # We are in cash no open trades
last_trade_time = self.closed_trades[-1].exit_time # When did we exit our last trade
else: # We have an open position
last_trade_time = max(
self.trades[-1].entry_time, self.closed_trades[-1].exit_time
) # the max of when we either entered that trade or exited it last
current_time = self.data.index[-1]
dayssince = (
current_time - last_trade_time
) # this will be used in concert with relax variable
# Bullish Markets ---------------------------------
if (
not self.position.is_long
and crossover(self.sma1, self.sma2)
):
self.buy(size=0.9)
elif (
self.position.is_long
and crossover(self.sma1, self.sma2)
and dayssince > self.relax # Checks to make sure it has been at least the proper amount of days since last trade
):
...rest of code here.... |
Beta Was this translation helpful? Give feedback.
2 replies
-
How about: import pandas as pd
...
def next(self):
cur_time = self.data.index[-1]
for trade in self.trades:
if cur_time - trade.entry_time >= pd.Timedelta('5 days'):
trade.close() |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Is there a wait function for closing/reversing a position? For example entering into a short position (based on criterias) then wait X trading days, then close the position? Thanks
Beta Was this translation helpful? Give feedback.
All reactions