Posts made by backtrader
-
Release 1.9.75.123
1.9.75.123:
- Adding extra day before dtcmp calc, as otherwise the extradays
have no effect (#388) - Fixing the issue with TWS API Bust events (err code 10225) (#396)
- Add support for ASK quotes for CASH assets (#395) plus fixes
- Remove duplicated note (#386)
- Fixing time.clock for python>=3.8 (#394)
- Changed file initiation for WriterFile to make it work under
multi-process optimization (#397) plus fixes - Fixed backend loading if a backend is loaded (Google Collab) and
backend to use on MacOSX - Fix: crumb in feeds.YahooFinanceData (#400)
- Fix color assignments, ticks line widths and some pep-8 improvements
- Fix timeframe/compression detection when plotting
- Fix default value for ticks display format on X-axis
- Sample with ta-lib SAR test
- Generic support of multiple "text/*" content types for Yahoo
- Adding extra day before dtcmp calc, as otherwise the extradays
-
RE: Backtrader's Future
Dear @vladisld,
I have just read this tread and you statement
@vladisld said in Backtrader's Future:
Since no one I guess has a full understanding of the platform yet, we probably should limit ourselves to bug fixing only, at least initially - and see how it goes.
This is probably spot on and something I have failed to realize. Since your fork doesn't yet contain any changes, I am adding a few bug fixes to the main repository and release a new version with them.
-
RE: cerebro.resample() introduces data in the future
@xyshell said in cerebro.resample() introduces data in the future:
Note: 6219.15 is the close price of the 1h data at 2020-03-20 01:00:00, while 6219.15 is also the open price of the 1min data
No. There is no such thing as the closing price of the
1-hour
data, because that data doesn't exist. Naming things properly does help.Additionally the information you provide is wrong, which is confirmed by looking at your data.
Note for the other readers: for whatever the reason, this data defies all established standards and has the following format:
CHLOVTimestamp
Your data indicates that
6219.15
is the closing price of the1-min
bar at00:59:00
, hence the last bar to go into the resampling for1-hour
between00:00:00
and00:59:00
(60 bars -if all are present-) which is first delivered to you as a resampled bar at01:00:00
6219.15
is the opening price of the1-min
bar at01:00:00
At
01:00:00
you have two bits of information available:- The current
1-min
bar for01:00:00
- The
1-hour
resampled data for the period00:00:00
to00:59:00
As expected.
-
RE: Timers
@benjomeyer said in Timers:
timername='selltimer',
Given how you pass it ... you could probably try this better
def notify_timer(self, timer, when, timername): if timername=='buytimer': self.buy_stocks() elif timername=='selltimer': self.sell_stocks()
given that you have no additional
*args
or**kwargs
. But a more general approach I would suggest thisdef notify_timer(self, timer, when, **kwargs): timername = kwargs.get('timername', None) if timername=='buytimer': self.buy_stocks() elif timername=='selltimer': self.sell_stocks()
where you can also decide if a default logic for
None
is needed.Note: notice the usage of
elif
-
RE: Building Sentiment Indicator class: TypeError: must be real number, not LineBuffer
@Kevin-Fu said in Building Sentiment Indicator class: TypeError: must be real number, not LineBuffer:
def next(self): self.date = self.data.datetime date = bt.num2date(self.date[0]).date() prev_sentiment = self.sentiment if date in date_sentiment: self.sentiment = date_sentiment[date] self.lines.sentiment[0] = self.sentiment
In any case that's probably where the error happens. There is no definition of
self.sentiment
and the indicator understands you are looking for the line namedsentiment
. The problems- Lack of variable initialization
- Conflicting naming
-
RE: Building Sentiment Indicator class: TypeError: must be real number, not LineBuffer
@Kevin-Fu said in Building Sentiment Indicator class: TypeError: must be real number, not LineBuffer:
Any input would be greatly appreciated
But you provide no input with regards to the error. Only
TypeError: must be real number, not LineBuffer
Python exceptions provided a stacktrace which points to the different line numbers in the stack ... allowing to trace the error to the origin point (and showing any potential intermediate conflict)
The error is only telling us that you are passing an object (a
LineBuffer
) there where afloat
should have been passed by you. But not where you are doing it ... which is in the stacktrace ... -
RE: DataCls does autoregister
@Kevin-Galkov said in DataCls does autoregister:
If I run the mementum/backtrader/samples/oandatest/oandatest.py , it complains:
In any, it does complay because you don't have the proper package installed, you have something for
v20
. -
RE: DataCls does autoregister
You have to use this:
The built-in module is only compatible with the old
Oanda
API.And
oandatest.py
works with the old built-in module. -
RE: Timers
Timers
go tonotify_timer
which receives thetimer
,when
it is happening and any extra*args
and**kwargs
you may created the timer with. You can use any of the latter to differentiate timers.Or you can simply use the
timer id
to associate different logic to each timer. -
bta-lib 1.0.0 Released
- https://pypi.org/project/bta-lib/
- https://btalib.backtrader.com/
- https://github.com/mementum/bta-lib
Covering all non-candlestick indicators offered by
ta-lib
implemented in Python and being able to work as a drop-in replacement ofta-lib
if compatibility is activated. -
RE: Cancelling Bracket order following closing of the main side
The fundamental problem is that a position open by a bracket should not have additional logic to close the position independently.
But if you insist in shooting yourself in the foot, the only thing you have to do is to keep a reference to any of the bracketing orders (stop-loss side or take-profit side) and cancel it whenever you close the position with the bracket-foreign logic.
if logic_orders_to_close_the_position: self.cancel(take_profit_order)
-
RE: Plotting a boolean indicator
To do that, look at the definition of how lines are plotted in the
BuySell
observer. -
RE: [Errno 2] No such file or directory
If the data cannot be downloaded, there will an attempt to open a cached version from a file with the same name as the ticker, which obviously does not exist. This is probably a Yahoo problem.
-
RE: Optstrategy and PivotPoint indicator pickle error
The sample code is incomplete but the presence of
@Benjamin-Kesby said in Optstrategy and PivotPoint indicator pickle error:
if __name__ == "__main__": pass
does clearly point in the direction indicated by @vladisld and the scoping problems that
pickle
has. The solution would be the same as the one outlined in the referenced post. -
RE: Indicators outside of strategy?
The indicators are tightly coupled to support things like managing different timeframes, remain synchronized to the data, play well with replay and resample.
Using them outside of a strategy is not possible. You can follow the path recommended by @lampalork or wait a bit until things get settled with
bta-lib
: https://btalib.backtrader.com -
RE: Crossover indicator doesn't plot as expected
@xyshell said in Crossover indicator doesn't plot as expected:
self.adx_crossover = bt.ind.CrossOver( self.adx_fast, self.adx_slow, plot=True, subplot=True)
Indicators are plotted subordinated to its
master
. The crossover happens on two subindicators of an indicator, so thedata
ceased to be the master a couple of levels before the current point.Use
plotmaster=self.data
See: Docs - Plotting - https://www.backtrader.com/docu/plotting/plotting/
-
RE: Crossover indicator doesn't plot as expected
@xyshell said in Crossover indicator doesn't plot as expected:
but adx_crossover indicator doesn't show up as expected.
Understanding how something doesn't show up as expected without knowing what's expected and what's actually happening is a challenging problem. A real conundrum.
@xyshell said in Crossover indicator doesn't plot as expected:
What's going on here?
A picture would probably let us experience what's actually going on.
-
RE: Issue with extending a data feed
The resampler filter wouldn't know what to do with extra lines (add, max, min, sub, multiply, take the 3rd one, take the last ...) and works only with the fields for which it has a rule, the standard ones.
You can create a 2nd data feed, assign your desired fields to
open
(keep 1st),high/low
(keep max/min),close
(keep last),volume
(add) and use that resampled to match the timeframe of your master data feed.Or create your own resampling filter.
-
RE: strategy.getposition returning different values for same ticker on different timeframes
@Mark-Weaver said in strategy.getposition returning different values for same ticker on different timeframes:
I expected
You want to do "algotrading", remove then "expect" from your vocabulary. As soon as possible.
@Mark-Weaver said in strategy.getposition returning different values for same ticker on different timeframes:
if I have a DataFeed for FCEL on a 5m aggregation, and I send a buy order through the broker API, what is it actually buying, that's different than, say, the underlying instrument behind the FCEL datafeed on a 1M aggregration?
You know nothing is different. But the code treats each single data feed as an independent data feed. Hence accounting is different.
Why should the code be making efforts to try to identify things as being the same thing? That will fail in an epic manner when the data feed provider provides no identification means.
Your AI is still believing that there are many other AI entities in place.
@Mark-Weaver said in strategy.getposition returning different values for same ticker on different timeframes:
Is that how it would work in live trading?
I don't know, I didn't write the alpaca broker, you will have to ask alpaca, but I guess it will be so.
@Mark-Weaver said in strategy.getposition returning different values for same ticker on different timeframes:
I don't see it as a parameter in the buy function
The
buy
method has a clear parameterdata
which is the target of your acquisition. Track anything you want on5min
and buy the1min
feed always (and check its position)@Mark-Weaver said in strategy.getposition returning different values for same ticker on different timeframes:
So why would the broker interface return different position values for FCEL 5M vs FCEL 1M?
Because they are different data feeds.