bt.And exists because
and cannot be overloaded. But you can pass any number of arguments to
bt.And or nest them.
I want to buy a position and sell it after 2 days
Notice that using anything which is
datetime related won't usually work unless you don't mind weekends and trading holidays adding days to the count.
Without really understanding what you mean with "rates", it seems the value of your asset moves around
1.14-1.15 with a very small variation.
The variation is so small that
matplotlib changes the representation to accommodate such a small variation, indicating the reference value at the top with
You will have to dig in the
matplotlib documentation to understand how this may be changed.
x = bt.If(d(-3) > e(-2) > f(-1), d(-1), float('Inf')) # new error here
The magic of "overloading" cannot handle the chained comparison:
d(-3) > e(-2) > f(-1). Hence
condition = bt.And(d(-3) > e(-2), e(-2) > f(-1)) bt.If(condition, result_if_True, result_if_False)
From my point of view,
Interesting point of view. Go to any exchange, send a
Market order and try to cancel it.
Come then back and let us know how things worked out.
why not cancel on present bar in backtesting?
Because "present" in "present bar" is a fallacy. That bar is already the PAST, because if you see that it has ALREADY happened.
If you go to the market thinking in terms of bars your money will be gone very soon. The market is made of TICKS which is a single price point representing an execution (in FOREX not even that, as most data providers provide the
Bid price even if no execution has happened).
Market order may actually be the one that generates the next tick.
Using bars is simply a representation of the market.
StopTrail order as sent to the broker is also immediately executed because it will be immediately triggered with the given parameters.
TradeAnalyzerwhich already includes this calculation - https://www.backtrader.com/docu/analyzers-reference/#tradeanalyzer
Cerebro returns the strategy instances that have run.
The definition of what's returned is in the
Cerebro reference: https://www.backtrader.com/docu/cerebro/, section Returning the Results
You may pass a dataframe and later get it from the executed strategy.
import backtrader.indicator as btind
Notice that the original problem is the lack on an
s. The code is importing
backtrader.indicator and not
MovAv.Simple and all other aliases aforementioned, do exist.
By the way ... in all cases it's a very bad idea to go
All-In is not the new black)
If you use futures and try to go
All-In, the slightest move against your entry price will have your broker closing your position until you meet the margin again (read the documentation from your broker), which means you can no longer 100% recover.
Use proper position sizing.
I am working with BitMEX perpetual contracts.
You are using futures. Configure the platform for FUTURES.
I'm assuming that funding fees are balancing themselves out over the long run and hence should not be included in the calculation.
I'm also assuming that if I use 4x leverage then I can use up to 4x the funds I have for both longs and shorts.
I'm also assuming that by just allowing leverage, my bet sizes will not change.
Stop assuming and start by configuring things right, before claiming a BUG is there.
If you have futures and you have the link above (and there links close to it about "Futures")
Read the futures specification on Bitmex and find out about those details.
Read how to work with FUTURES.
You will now clearly see that the bug is in your code, which isn't properly configuring the platform for the trading scenario you are simulating.
Everything looks pretty much as I'd expect it to. Green arrow for first entry is not visible on main plot but that's minor.
Ok. So you think there is some artificial intelligence at work that will do as you please. You may also read the docs and understand how you can plot and get the arrow plotted. But hey, it is much better to claim that there is a "bug" that shows up when using leverage.
With regards to leverage ... what amazes me is that you apparently want to run a leverage strategy using the standard shorting of stocks that pumps money into your account.
What is your expectation here? To go to the broker sell 100 stocks of A and get for free 4 times the money they are worth? That would be fantastic. I pay interest on 100 stocks but may money on the value of 4 x 100 stocks.
You could start by stating what type of asset that is, how it behaves ...
One important thing about algotrading is knowing how to trade. Not so long ago you didn't even know that shorting pumps money in your account (when shorting stocks, the same doesn't happen when selling a future which is also going short) => https://community.backtrader.com/topic/1887/spikes-in-cash
2018-01-02T17:10:00, BUY EXECUTED, Price: 67.58, Cost: 199766.48, Comm: 199.77 2018-01-03T14:35:00, SELL EXECUTED, Price: 67.72, Cost: -600954.80, Comm: 1000.90 2018-01-03T14:35:00, PNL: GROSS 413.84, NET 13.89 2018-01-03T17:25:00, Status: Margin
What price is going to be bought or sold? What size? Not the results ... but the intent, plus how much cash is there, the account value, ...
I've found the issue with condition bt.If (in this nice threat: https://community.backtrader.com/topic/531/indicator-development-missing-a-key-piece/3),
I guess your intention is to say that you have found out how to address your issue with information from that thread.
The issue at hand is that you are not managing the division by zero condition. You need
DivByZero, which executes a division with parts (i.e.: lines) working inside backtrader and delivers a specific result if a division by zero happens.
def notify_trade(self, trade): print("TRADE:", self.datetime.datetime(ago=0), trade.status, self.data0.close)
Adding to the answers above.
ORDER: 2019-01-01 00:00:02.247996 2 4 1.00367 TRADE: 2019-01-01 00:00:02.247996 1 1.00367
That's NOT the trade time or the order execution time. That's the current
datetime kept by the strategy by tracking the latest timestamp reported by the different data feeds.
Order execution data is kept in the attribute
Furthermore, an order has different states notified and the code is accounting for none to understand when the execution is reported as
The same with trade.
Seems to me there are several bugs in Backtrader.
1st post: NON-WORKING BROKEN CODE
2nd post: CORRECTION WHICH STILL RENDERS THE CODE AS NON-WORKING
3rd post: The bugs are in the platform. Proof: NONE.
Sample data? No
You have to indicate the position in which column each field is and which fields are not present (with
In your loading example not a single column is specified and the code tries to find the values in the default column ordering (datetime, OHLCV)
In any case, your data is NOT tick data. Your data is
bid/ask, which isn't the same. You have to choose in which fields you want to load the data.
cheat-on-close. See: Docs - Broker - https://www.backtrader.com/docu/broker/