Accessing stats["_trades"] during the test #722
-
I'm trying to code a variation of Dollar Cost Averaging and therefore need to know the trades that have already been made during the test to determine if I should buy or not. Initializing self.stats = self.stats["_trades"] in the strategy gives me an error. Does anyone know if this is possible at all? class DCA_every_week(Strategy):
amount_to_invest = 10000
def init(self):
self.day_of_week = self.I(
lambda x: x,
self.data.Close.s.index.dayofweek,
plot = False,
)
self.stats = self.stats["_trades"]
def next(self):
if self.day_of_week[-1] == 4:
self.buy(size = math.floor(self.amount_to_invest / self.data.Close[-1])) # Buys largest number of stocks possible
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
There is no |
Beta Was this translation helpful? Give feedback.
-
Thanks! If anyone else is wondering, I found them using Strategy.trades (with DCA you only buy and don't sell). |
Beta Was this translation helpful? Give feedback.
-
One more question: I can get the length of the list of active trades through len(self.trades), however I cannot access the variables of trades. When I try: When I try to get the latest trade by using: Does anyone know how to access the instance variables of a trade? |
Beta Was this translation helpful? Give feedback.
There is no
Strategy.stats
!stats
are returned byBacktest.run()
.During the run, you can maybe look in
Strategy.closed_trades
?