thanks for your reply, I understand it now. From the log information, I see the buy order is submitted, accepted and completed on the next bar, So it cannot be cancelled. The system is designed wisely.
Many thanks for the detailed answer! I totally agree with most of the points you made. Though, I do think people also opt for Java and other strongly typed languages for other reasons than just ease of integration with legacy systems (stability, speed, better concurrency support, better inheritance support, etc.).
Out of curiosity, how do you see the emergence of languages like Julia? Do you think it has a chance to bridge the worlds of HPC and the development speed of languages like Python and R?
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 1.145
You will have to dig in the matplotlib documentation to understand how this may be changed.
Track it with the notifications of notify_order or notify_trade - https://www.backtrader.com/docu/strategy/
Use the TradeAnalyzer which already includes this calculation - https://www.backtrader.com/docu/analyzers-reference/#tradeanalyzer
Write a custom analyzer which only tracks that value.
@Roger-Bos said in Full example of custom indicator:
import backtrader.indicator as btind
Notice that the original problem is the lack on an s. The code is importing backtrader.indicator and not backtrader.indicators
And yes, MovAv.Simple and all other aliases aforementioned, do exist.
I totally agree. I think that the quality of the documentation is really world class, all thanks to @backtrader
Nevertheless, having an online course would probably bring traders like myself up the learning curve much faster.
And I don't mind paying a small premium for the course.
At the risk of embarrassing myself in my very first post in this community by creating a zombie in replying to an old thread, i would like to say I found this thread useful, but runonce=False not so much. I bumped into:
ValueError: x and y must have same first dimension, but have shapes (1796,) and (1795,)"
when first adding just now resampling to daily data on a 1795-long pre-existing intra-day strategy with a bunch of my own indicators and observers being plotted.
I poked around a bit in the code before I found another work-around (and then, because I was hoping to do better, this thread...) to keep running back-tests seeing most of the plot with:
cerebro.plot(end = end-datetime.timedelta(hours = 8, minutes = 0, seconds = 0.01)) # hack to get around resample extra bar plot or whatever...
We'll see how long that lasts as I continue to develop with mixed timescales now. I have no idea why stopping the plot 8+epsilon hours early avoids the issue in my particular code-case (for now). But maybe the same idea might help someone else, so I bother to resurrect the zombie.
As an aside, I am somewhat stunned, as a total py newbie, that in a fairly short time I have been able to pull together a system that is downloading from IB, running my own custom strategy and indicators and observers on their live or my captured CSVs, producing plots that show what is going on...
Very nice job on the infrastructure. Kudos @backtrader.
PS: I also noticed that a prenext in one of my observers failed to execute after adding:
cerebro.resampledata(data0, name = 'data2', timeframe=bt.TimeFrame.Days,
which I thought odd, but not odd enough in itself to trouble anyone about. Now. On to inter-day, with almost all my plot, I hope.
By the way ... in all cases it's a very bad idea to go All-In (no, 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.
@kr said in Limit order trade entry not correct:
def notify_trade(self, trade):
print("TRADE:", self.datetime.datetime(ago=0), trade.status, self.data0.close)
Adding to the answers above.
@kr said in Limit order trade entry not correct:
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 order.executed.
See: Docs - Order - https://www.backtrader.com/docu/order/
Furthermore, an order has different states notified and the code is accounting for none to understand when the execution is reported as Complete.
The same with trade.
See: Docs - Trade - https://www.backtrader.com/docu/trade/
You have to indicate the position in which column each field is and which fields are not present (with -1)
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.