@MuSaCN I think you're looking for that https://community.backtrader.com/topic/154/reach-_trades-list/4
Best posts made by balibou
-
RE: How to create pyfolio round trip tearsheet?
Hi @kian-hong-Tan ! Welcome to the community !
- Actually I have generated an output with the class
CashMarket
like this:
stock = cerebro.addanalyzer(btanalyzers.CashMarket, _name='cashmarket')
then inject it in QuantStats:
qs.reports.html(stock, "SPY")
But I'm stuck with an error
AttributeError: 'dict' object has no attribute 'min'
For info I have tried with dataset from a csv and data from panda file. Also I resample 1m bars to 4hours bars.
if @run-out you can help us :)
I suspect there is a min property to add to your CashMarket analyzer but not sure ...- Thanks for the link but I really prefer Quanstats one :)
- Actually I have generated an output with the class
-
RE: Setting multiple TP for the same buy position
Thanks for your feedback @ab_trader !
Actually I had a deeper understanding of sizers with this article: https://www.backtrader.com/blog/posts/2019-08-29-fractional-sizes/fractional-sizes/
I thought that the size parameter was in percents ... but it has been much clearer with this piece of code:
def notify_order(self, order): if order.alive(): return otypetxt = 'Buy ' if order.isbuy() else 'Sell' if order.status == order.Completed: self.loginfo( ('{} Order Completed - ' 'Size: {} @Price: {} ' 'Value: {:.2f} Comm: {:.2f}'), otypetxt, order.executed.size, order.executed.price, order.executed.value, order.executed.comm ) else: self.loginfo('{} Order rejected', otypetxt)
As you said, logs saved me on this topic !
I came with this solution, and it's working like a charm:
o1 = self.buy() self.sell(exectype=bt.Order.Limit, price=self.data.close * 1.03, size= o1.size * 0.25) self.sell(exectype=bt.Order.Limit, price=self.data.close * 1.06, size=o1.size * 0.25) self.sell(exectype=bt.Order.Limit, price=self.data.close * 1.12, size=o1.size * 0.25) self.sell(exectype=bt.Order.Limit, price=self.data.close * 1.20, size=o1.size * 0.25)
On each TP, I sell a fragment (25%) of my position (+3%, +6%, +12% and +20%)
-
RE: How to create pyfolio round trip tearsheet?
@kian-hong-Tan can you print the output of this line in your code:
stock = cerebro.addanalyzer(btanalyzers.CashMarket, _name='cashmarket')
I get something like this:
{ datetime.datetime(2020, 4, 29, 0, 0): (100000.0, 100000.0), datetime.datetime(2020, 4, 29, 4, 0): (100000.0, 100000.0), ... datetime.datetime(2020, 8, 1, 0, 0): (811.8994327605933, 110828.6750226959) }
-
RE: CCXTbt make market order if limit order is not filled in X minutes
Hello @Dimasik007 ! Maybe you should use the valid parameter for creating your orders (https://www.backtrader.com/docu/order/#order-creation)
for example when the buy order is filled, on
notify_trade
function you can create a sell order with avalid
in datetime (+ 2/3 minutes) -
RE: How to create pyfolio round trip tearsheet?
Thanks @run-out for your snippet !
Thanks @eprice122 for your feedback, I was trying to debug it without any success !
I am gonna have a deeper look at those functions -
RE: How to create pyfolio round trip tearsheet?
Ok so it sounds that for yfinance api:
- intraday data cannot extend last 60 days
- valid intervals are: 1m, 2m, 5m, 15m, 30m, 60m, 90m, 1h, 1d, 5d, 1wk, 1mo, 3mo
As I want to work with many timeframes (1h, 2h, 4h, ...) on different periods (weeks, months, years), I will probably have to deal with another strategy than using yahoo finance (probably still keeping SPY for benchmark, so I will just have to download SPY performance on different timeframes on csv files) ... What are your thoughts ?
Also what does your benchmark look alike @eprice122 ?
-
RE: How to create pyfolio round trip tearsheet?
I have stats displayed in the terminal with your piece of code @kian-hong-Tan.
The charts are rendered in a lot of different plots, I will investigate how to deal with that -
RE: Multiple TP Strategy with SL
Another update, I have fixed the issue about the size:
I added this to my strategy:
def notify_cashvalue(self, cash, value): if (not self.position): self.Totalfragment = math.floor(cash / self.data.close[0])
Then in my
next
funtion I set 4 sizes:OrderSize1 = self.Totalfragment * 0.25 OrderSize2 = self.Totalfragment * 0.25 OrderSize3 = self.Totalfragment * 0.25 OrderSize4 = self.Totalfragment - f1 - f2 - f3
and I set:
- brackets order 1 size with OrderSize1
- brackets order 2 size with OrderSize2
- brackets order 3 size with OrderSize3
- brackets order 4 size with OrderSize4
And everything work.
I don't think using
notify_cashvalue
for setting the order size is a good pattern ... Do you think there is a better implementation ?- Using in strategy the function setsizer ?
- Using in sizer the function _getsizing ?
Thanks for your feebacks !
-
RE: Problem with my custom Indicator, UIcerIndex
@Jakob-Schütte maybe
self.data[0].close.get(size=self.p.period)
instead ofself.data0.close.get(size=self.p.period)
? 🙂
Latest posts made by balibou
-
RE: Setting Sizer within Strategy Next for Bitcoin
If I well understood, you probably need to use a mix between multiple data strategy and managing the size of the position in the strategy like I did here
-
RE: When does the order execute?
It's both:
- You can set a limit order that will trigger as soon as the price hit the target price
- or you can set a simple buy order based on an SMA strategy like here that will trigger once the period is closed
-
RE: Error in generating create_full_tear_sheet (pyfolio)
@darkknight9394 @KK I tried to make it work here
html full report rendering is not working for the moment, I need to work on it ... if you find a solution in the mid time ... 😎
-
RE: Problem with my custom Indicator, UIcerIndex
@Jakob-Schütte maybe
self.data[0].close.get(size=self.p.period)
instead ofself.data0.close.get(size=self.p.period)
? 🙂 -
RE: Error in generating create_full_tear_sheet (pyfolio)
@KK as far as I know pyfolio is not supported anymore with backtrader (there is a topic about quantstats as an alternative)
-
RE: CCXTbt make market order if limit order is not filled in X minutes
Hello @Dimasik007 ! Maybe you should use the valid parameter for creating your orders (https://www.backtrader.com/docu/order/#order-creation)
for example when the buy order is filled, on
notify_trade
function you can create a sell order with avalid
in datetime (+ 2/3 minutes) -
RE: Lowest indicator issue
ok finally I went for a good old method:
print('--------------------') # calculating bbw bbw = (self.bbw.top - self.bbw.bot) / self.bbw.mid # listing last 20 bbw result_t = [self.calculateBBW(k) for k in range(-20, 0)] # filtering nan value in the list minbbw = 0 if math.isnan(min(result_t)) else min(result_t) # comparing values print(bbw <= minbbw) print('--------------------')
I really struggle sometimes to understand the documentation when there are no examples
-
RE: Lowest indicator issue
@ab_trader in the documentation it is explained that it calculates the lowest value for the data in a given period:
- lowest = min(data, period)
To me I assumed (and was mistaken as you said):
- that data is the bbw indicator
- period is the last 60 candles
So if I'm right, I should:
- create an array with the last 60 bbw values (bbwArray)
- find the minimum in the array
- compare this minmum with bbw[0]
bbw = (self.bbw.top - self.bbw.bot) / self.bbw.mid minbbw = min(bbwArray, 60) print(bbw == minbbw)
Right ?
-
Lowest indicator issue
I use the Lowest indicator.
I try to compare my bollinger bandwith value with the minimum one on a specific period (60).
Unfortunately the minimum one is always equal to my bollinger bandwith value calculated.Any thoughts ?
class BaseStrategy(bt.Strategy): def __init__(self): self.bbw = bt.indicators.BollingerBandsPct() def next(self): print('--------------------') bbw = (self.bbw.top - self.bbw.bot) / self.bbw.mid minbbw = min(bbw, 60) print(bbw == minbbw) # alway True print('--------------------')