Situation which can (probably) only triggered when working with, not so usual, things like spreads (or else the stock/future you have in your portfolio is really worth nothing)
RE: Problem with Trade @ 0 and multi time series
data = data or self.datas
This is because when you run this code in
next, you are in the
Stage 2mode and a boolean evaluation of a lines object (data feeds, indicators, observers, ...) uses the current
point. This is done by overloading the operators and intended to support code patterns like
class MyStrategy(bt.Strategy): def __init__(self): self.signal = bt.ind.SMA(self.data) > self.data def next(self): if self.signal: self.buy()
The different overloading during Stage 1 and Stage 2 allows for example the lazy evaluation of the logical comparison seen above during
This is documented here: Docs - Platform Concepts (look for Stage 1 and Stage 2)
isoperator is the way to go.
RE: What's the right way to use data from API in a strategy?
I could call it in the strategy's next() method
That would probably be fatal. But it depends on the speed at which you have to make decisions. You'd be blocking the main thread.
What's the preferred way to do this?
There is no preferred way. My personal opinion:
It is a light processing stream of events: put it in a background thread and put the incoming events in a
queue.Queue, which is thread safe. When entering next, you can check if an event is waiting in the queue (pushed by the background thread) or not and do something else.
Even with the
GILand because processing is light, using threads seems sensible.
The stream of events needs some serious processing before being passed to next.
multiprocessingto really use multiple cores and avoid blocking the main thread. The communication channels to pass things back to the main thread have the same (or very similar) APIs, although they are a bit slower because data has to be copied from process to process (this time should be marginal compared with the actual processing time to make it worthwhile)
Using a data feed
It's also a sensible opinion. You have in this case to provide a timestamp in the
datetimeline. You may simply use it as a timeframe
Ticksdata feed, without resampling/replaying to get every event. Remember that your real data feeds (which will probably have larger timeframes) will be available, but in most cases won't move forward (the
lenremains the same) when your event is seen in
RE: Writing data at to  element of line
self.data_0 = 111
seems to change the [-1]
It, for sure, doesn't. If you really believe it to be so, a sample proving it would be much appreciated.
RE: How to access the "params" tuple?
I started with the Quickstart tutorials and found some confusing code (see attached). The "params" tuple is created with two separate tuples inside. However, I can access these inside tuples by typing params.maperiod .
What's exactly the confusing part? You declare
params(which you can also declare as a
dict) and you can access them. It's a declarative style. As pointed out by @Laurent-Michelizza , there is a shorhand notation where
paramsis shortened to
However, if I rename params to another name I cannot access them anymore.
Of course. How are you expecting the platform to know which
paramshave been defined if you give the declaration a different name?
RE: Creating an indicator for multiple data feeds
So in short, there is some issue with having a loop in the init
There is for sure no issue, because there are several tens of scripts doing a loop and accessing all data feeds.
I fail to understand the complexities of the script. If you want to diagnose a problem, you should remove everything but the data feeds that you are inputting and then see if that feeds and the values produced by them are available to you. The rest of the code is simply bothering your ability to diagnose any mistake you are making.
For completeness here is the code and output with the [i] at the end of self.newSMA:
self.newSMA[i] = btind.MovingAverageSimple(day_vol, period = self.params.period)
AttributeError: 'Lines_LineSeries_LineIterator_DataAccessor_Strateg' object has no attribute 'newSMA'
It that's one of the major problems you are facing, I can only very sincerely suggest: master the basics of programming and Python before carrying on with algotrading. This is not meant with arrogance and not meant as patronizing. It is meant as sincere advice.
RE: Using Analyzer in Strategy and Log Files
I notice that the analyzer.next()'s are called after strategy.next(). This has created a delay when I try to use the analyzers in the strategy itself
The analyzer is analyzing what has happened. If it were to be called before
Strategy.nextit wouldn't know that the strategy is doing.
I am using the analyzer to determine the trading itself
Probably the wrong place. Logic belongs in the strategy and not in something which is making an analysis (and not executing logic, although as proven by yourself and being this Python, you can bend the rules as much as you wish)
What would you suggest to do so that I can have this data available without the delay during strategy.next(), or is there another development pattern I should follow?
Do it in the strategy
RE: Adding money flow field to data series / resampling
You can either add a filter Docs - Filters and manipulate the field in the way you want (record previous values and add them)
You can create an Indicator and keep the money flow in it - Docs - Indicator Development
Or you can extend any data feed with new fields with no implications: Docs - Extending a data feed
Any of the 3 approaches is generic.