what lines are there in a strategy?
-
The document says "Data Feeds, Indicators and Strategies have lines.". I wonder what lines there are in a strategy. In strategy.py, there is a definition ines = ('datetime',) under class Strategy, so I gusess that the line in a strategy is datetime line, am I right?
Another question is when we define our own strategy sub class, we can use len(self) to get bars processed. I wonder if the len(self) return the length of datetime line until now?
any help is appreciated.
-
To answer your question regarding len(self), it will return the running count of how many bars have been processed.
def next(self): self.log(f"Number of bars {len(self)}")
Using a file I have with 100 bars for testing, the results are as follows:
'''
/home/runout/projects/scratch/venv/bin/python "/home/runout/projects/scratch/20200713 lines_question.py"
2020-02-03 09:01:00, Number of bars 1
2020-02-03 09:02:00, Number of bars 2
2020-02-03 09:03:00, Number of bars 3
2020-02-03 09:04:00, Number of bars 4
2020-02-03 09:05:00, Number of bars 5
2020-02-03 09:06:00, Number of bars 6
2020-02-03 09:07:00, Number of bars 7
2020-02-03 09:08:00, Number of bars 8
2020-02-03 09:09:00, Number of bars 9
2020-02-03 09:10:00, Number of bars 10
2020-02-03 09:11:00, Number of bars 11
2020-02-03 09:12:00, Number of bars 12
2020-02-03 09:13:00, Number of bars 13
2020-02-03 09:14:00, Number of bars 14
2020-02-03 09:15:00, Number of bars 15
2020-02-03 09:16:00, Number of bars 16
2020-02-03 09:17:00, Number of bars 17
2020-02-03 09:18:00, Number of bars 18
2020-02-03 09:19:00, Number of bars 19
2020-02-03 09:20:00, Number of bars 20
2020-02-03 09:21:00, Number of bars 21
2020-02-03 09:22:00, Number of bars 22
2020-02-03 09:23:00, Number of bars 23
2020-02-03 09:24:00, Number of bars 24
2020-02-03 09:25:00, Number of bars 25
2020-02-03 09:26:00, Number of bars 26
2020-02-03 09:27:00, Number of bars 27
2020-02-03 09:28:00, Number of bars 28
2020-02-03 09:29:00, Number of bars 29
2020-02-03 09:30:00, Number of bars 30
2020-02-03 09:31:00, Number of bars 31
2020-02-03 09:32:00, Number of bars 32
2020-02-03 09:33:00, Number of bars 33
2020-02-03 09:34:00, Number of bars 34
2020-02-03 09:35:00, Number of bars 35
2020-02-03 09:36:00, Number of bars 36
2020-02-03 09:37:00, Number of bars 37
2020-02-03 09:38:00, Number of bars 38
2020-02-03 09:39:00, Number of bars 39
2020-02-03 09:40:00, Number of bars 40
2020-02-03 09:41:00, Number of bars 41
2020-02-03 09:42:00, Number of bars 42
2020-02-03 09:43:00, Number of bars 43
2020-02-03 09:44:00, Number of bars 44
2020-02-03 09:45:00, Number of bars 45
2020-02-03 09:46:00, Number of bars 46
2020-02-03 09:47:00, Number of bars 47
2020-02-03 09:48:00, Number of bars 48
2020-02-03 09:49:00, Number of bars 49
2020-02-03 09:50:00, Number of bars 50
2020-02-03 09:51:00, Number of bars 51
2020-02-03 09:52:00, Number of bars 52
2020-02-03 09:53:00, Number of bars 53
2020-02-03 09:54:00, Number of bars 54
2020-02-03 09:55:00, Number of bars 55
2020-02-03 09:56:00, Number of bars 56
2020-02-03 09:57:00, Number of bars 57
2020-02-03 09:58:00, Number of bars 58
2020-02-03 09:59:00, Number of bars 59
2020-02-03 10:00:00, Number of bars 60
2020-02-03 10:01:00, Number of bars 61
2020-02-03 10:02:00, Number of bars 62
2020-02-03 10:03:00, Number of bars 63
2020-02-03 10:04:00, Number of bars 64
2020-02-03 10:05:00, Number of bars 65
2020-02-03 10:06:00, Number of bars 66
2020-02-03 10:07:00, Number of bars 67
2020-02-03 10:08:00, Number of bars 68
2020-02-03 10:09:00, Number of bars 69
2020-02-03 10:10:00, Number of bars 70
2020-02-03 10:11:00, Number of bars 71
2020-02-03 10:12:00, Number of bars 72
2020-02-03 10:13:00, Number of bars 73
2020-02-03 10:14:00, Number of bars 74
2020-02-03 10:15:00, Number of bars 75
2020-02-03 10:16:00, Number of bars 76
2020-02-03 10:17:00, Number of bars 77
2020-02-03 10:18:00, Number of bars 78
2020-02-03 10:19:00, Number of bars 79
2020-02-03 10:20:00, Number of bars 80
2020-02-03 10:21:00, Number of bars 81
2020-02-03 10:22:00, Number of bars 82
2020-02-03 10:23:00, Number of bars 83
2020-02-03 10:24:00, Number of bars 84
2020-02-03 10:25:00, Number of bars 85
2020-02-03 10:26:00, Number of bars 86
2020-02-03 10:27:00, Number of bars 87
2020-02-03 10:28:00, Number of bars 88
2020-02-03 10:29:00, Number of bars 89
2020-02-03 10:30:00, Number of bars 90
2020-02-03 10:31:00, Number of bars 91
2020-02-03 10:32:00, Number of bars 92
2020-02-03 10:33:00, Number of bars 93
2020-02-03 10:34:00, Number of bars 94
2020-02-03 10:35:00, Number of bars 95
2020-02-03 10:36:00, Number of bars 96
2020-02-03 10:37:00, Number of bars 97
2020-02-03 10:38:00, Number of bars 98
2020-02-03 10:39:00, Number of bars 99
2020-02-03 10:40:00, Number of bars 100Process finished with exit code 0
'''
-
The 'lines' are typically whatever you put into your strategy. The starting point would be your trading data, OHLCV open interest. This of course would have a datetime element.
But you could also add custom information or signal lines to your trading data using a custom data class and these custom data lines would also show up in your strategy.
And then also you could use indicators in your strategies which have lines defined as part of the indicator or defined by you in a custom indicator.
-
Thank you for your help. According to my understanding, self.datas[0] holds lines such as open, close, high, etc. Do you mean strategies also have these lines? Could we print out the name of a line by some api?
-
@Li-Mike said in what lines are there in a strategy?:
Could we print out the name of a line by some api?
When putting your data in, make sure you add a name.
cerebro.adddata(data, name="AAPL")
Then you can retrieve the name of the data line as follows:
self.datas[0]._name
With the number '0' being the number indicating the order the lines were entered into the system.
If by chance you need to reteive datalines buy name, as opposed to just printing the name, I have had success building a dictionary by looping through all my datas and making the key from the name, and putting the data object in as the value.
# Goes through the datas and makes a lookup dictionary of {symbols:d} self.stock_list = {} for d in self.datas: self.stock_list[d._name] = d
This way you can find the AAPL data object like:
self.stock_list["AAPL"]
-
Thank you for your kindly help and your patience. I am not good at English. So I may not express my idea clearly.
Let me give a pseudo code:
for l in self.datas[0].lines: print(l.name)
I expect it to print out 'close', 'high', 'low', 'open', etc. Can we achieve this?
-
@Li-Mike No problem, your English is fine. I think you are trying for:
for l in self.datas[0].datafields: print(l)
Output
datetime, open, high, low, close, volume, openinterest,
-
That's what I want, thank you.