One of the goals is to also gather things which have already been posted by community members. Always retaining the copyright they have posted, as for example:
Docs - ZigZag indicator for live trading
If you believe you have something to contribute, post it and it will be considered, just like anything else, but picked up things (initial idea) will be mostly related to indicators.
Hello backtrader community
First of all, thanks for this awesome piece of software. My favorite backtesting framework by far. ;)
Recently, I started working with mixed time frames and stumbled upon the following problem.
I have a daily data feed and a weekly data feed. At the end of the week, when the weekly bar forms, next seems to be called twice. The output for the code below looks like the following:
2018-02-02 27.3 27.3
2018-02-05 36.51 27.3
2018-02-06 35.5 27.3
2018-02-07 36.94 27.3
2018-02-08 45.79 27.3
2018-02-09 41.48 27.3
2018-02-09 41.48 41.48
2018-02-12 39.71 41.48
2018-02-13 39.75 41.48
2018-02-14 35.16 41.48
2018-02-15 34.41 41.48
2018-02-16 34.95 41.48
2018-02-16 34.95 34.95
2018-02-20 36.4 34.95
2018-02-21 36.81 34.95
2018-02-22 36.3 34.95
2018-02-23 33.49 34.95
2018-02-23 33.49 33.49
2018-02-26 32.1 33.49
2018-02-27 35.13 33.49
2018-02-28 36.7 35.13
import matplotlib.pyplot as plt
import backtrader as bt
import backtrader.indicators as btind
import backtrader.analyzers as btanalyzers
import pandas as pd
print(self.datetime.date(), self.datas.close, self.datas.close)
fromdate = "2018-02-01"
todate = "2018-03-01"
fromdate = datetime.datetime.strptime(fromdate, "%Y-%m-%d")
todate = datetime.datetime.strptime(todate, "%Y-%m-%d")
cerebro = bt.Cerebro()
# add a strategy
# load data
dataframe = pd.read_csv("/path/to/data.csv")
dataframe = dataframe[["timestamp", "open", "high", "low", "close", "volume"]].copy()
dataframe = dataframe.set_index("timestamp")
dataframe.index = pd.to_datetime(dataframe.index)
# convert the dataframe to a data feed, the timeframe of the original data feed must be set
data = bt.feeds.PandasData(dataname=dataframe, timeframe=bt.TimeFrame.Minutes, compression=1, fromdate=fromdate, todate=todate, plot=True)
# add the original data feed to cerebro
# add upsampled data feeds
cerebro.resampledata(data, timeframe=bt.TimeFrame.Days, compression=1)
cerebro.resampledata(data, timeframe=bt.TimeFrame.Weeks, compression=1)
except Exception as e:
results = cerebro.run()
if __name__ == "__main__":
I don't really understand this behavior. Maybe you could help me out. When I plot the results, the bars seem to be correctly aligend. Thanks a lot for your help.
@Dimasik007 said in Observers/Analyzers per asset in multi-asset strategy?:
Is it possible to modify DataTrades Observer to implement something similar to what I've made on the picture below?
You can for sure write an Observer which looks at the current position value to determine the PnL.
All-In is not the new black.
I am still trying to find out where is the book (or the cryptocurrency guru) that is spreading the word on All-In
@Jingsi said in wrong price when cheat-on-open and resampledata at the same time:
after more research, I found the key is order_target_percent default use close value instead of open value under cheat-on-open condition.
cheat-on-open gives you the chance to manually specify things. It's not a magic tool.
From: Docs - Strategy - order_target_percent - https://www.backtrader.com/docu/strategy/
It uses order_target_value to execute the order.
From the same documentation page
order_target_value(data=None, target=0.0, price=None, **kwargs)
which means you can specify the price at which the execution (and hence the calculations have to take place).
After which things can still fail ... for obvious things like floating point precision.