@albertoburchi There is no admin or access to admin for this forum except by the creator of Backtrader and he is not active at the moment. Just do what you can. Good luck.
Posts made by run-out
RE: Index out of range error using CSV file
@zugba Thank you for your code. I copied and ran your code using both your data and a sample of backtrader data. I used both the standard csv loader and your custom class.
All ran smoothly with backtrader data. The only error was when I use your data with your custom class. This suggest an error in your data. Try using a different data source.
Here's the data I used. 2006-volume-day-001.txt
RE: Strategy - Error IndexError: array index out of range
@eff You are using
getsomewhere but I cannot see where as you only included limited code.
getlooks backward to get an array of size
n. You must ensure there is a minimum period so that you can
nnumbers. Use addminperiod
RE: Accessing strategy results, orders and trades
@protrasol I think my best recommendation for you would be to use your debugging tool. Just put a stop right after
cerebro.run()is returned to
strat, and you can explore everything that is inside the
stratobject. From there you can plan how to extract data.
Which IDE are you using? Pycharm and VSCode are free and have great debuggers.
Coding without this technique is like driving with a blindfold on. You are constantly trying to figure out what's happening.
RE: Error with simple custom indicator
self.emaVariation = vol.PercentVariationInPeriod(self.ema_slow, plotname='EMA Variation', subplot=True)
You can use them in your indicator like this:
class PercentVariationInPeriod(bt.Indicator): lines = ("Variation",) params = (("period", 180),) def __init__(self): high = bt.ind.MaxN(period=self.p.period) low = bt.ind.MinN(period=self.p.period) self.lines.Variation = (high - low) / low
RE: Order Executed Price Error
Looking at your code I couldn't see anything wrong with it. So I copied and ran it using just tsla downloaded data from yahoo and it seems to run fine. Maybe try another data source?
Starting Portfolio Value: 1000000.00 2014-01-02, Close, 30.02 2014-01-02, BUY CREATE, 30.02 2014-01-03, BUY EXECUTED, 30.00 2014-01-03, Close, 29.91 2014-01-06, Close, 29.40 2014-01-07, Close, 29.87 2014-01-08, Close, 30.26 2014-01-09, Close, 29.51 2014-01-10, Close, 29.14 2014-01-10, SELL CREATE, 29.14 2014-01-13, SELL EXECUTED, 29.16 2014-01-13, Close, 27.87 2014-01-13, BUY CREATE, 27.87 2014-01-14, BUY EXECUTED, 28.10 2014-01-14, Close, 32.25
RE: Used of timer with cheat on open
Trying cheat-on-open The sample below has a strategy with 2 different behaviors: If cheat-on-open is True, it will only operate from next_open If cheat-on-open is False, it will only operate from next In both cases the matching price must be the same If not cheating, the order is issued at the end of the previous day and will be matched with the next incoming price which is the open price If cheating, the order is issued on the same day it is executed. Because the order is issued before the broker has evaluated orders, it will also be matched with the next incoming price, the open price. This second scenario, allows calculation of exact stakes for all-in strategies, because one can directly access the current open price.
RE: datafeeds with cross-section OHCL data
Is there to derive a feed from existing structure to support loading data from multiple dataframes,
Yes. Please search google, the community and the docs. Use these keywords:
backtrader, pandas, feeds, multi, multiple,
RE: Bracket Order help
@protrasol I couldn't find an example to share. But I use dictionaries when the complexity goes up. Say for example if you are trading bracket orders for many stocks. In this type of scenario you might want a dictionary with the data as key, then a dict for each data with perhaps each order type as keys.
self.tracking_dict = dict( data=dict( enter_order=`the_entry_order`, stop_order=`the_stop_order`, limit_order=`the_limit_order`, )
This method allows you to track other information associated with the bracket order if you wish. It provides an ability to track info related to the bracket and then act on that info during the backtest.
RE: Bracket Order help
@protrasol Your answer lies in the docs. Have a look at the bracket orders sample code. Follow the use of
orefs. In the sample code
orefsis a list you create and manage to track the orders. You can use this list for a wide variety of purposes including printing out any current order status. Make sure you follow all uses throughout the code since you must manage the orders contained in
You can also use a dictionary if you like using keys to label the orders.
RE: Need help to run minimal simple example
Also, when and how do I know if have to feed self.data to the indicator? Sometime you have it and sometimes not.
bt.ind.SMA(period = self.params.slow_length)
you have no data....
Sometimes I see
data.close or data ....
RE: Bactrader broker portfolio value increases instead of decreasing when only buying the stocks.
@run-out And also the fact that no shares are sold.