Asset Classes

Free investment financial education

More Campus Resources

Useful Tools and Information

Language

Multilingual content from IBKR

# Python Itertools Tutorial – Part V

###### Posted July 2, 2020 at 9:40 am
QuantInsti

Get up-to-speed in this series with Part IPart IIPart III and Part IV.

The islice() iterator

This iterator has four parameters which can be passed, the element, starting element variable, ending variable and the number of elements to be skipped.

Suppose, we wanted the Adj. Close price every third day, we would write the code as follows:

# Islice() itertool
# data = tesla[‘daily_returns’]
selection = itertools.islice(tesla[‘Adj Close’], 0, 15,2)
for each in selection:
print(each)

The output would be as follows:

743.6199951171875
749.5
703.47998046875
645.3300170898438
560.5499877929688
445.07000732421875
361.2200012207031
427.5299987792969

Well, these were some terminating iterators. Now, we will see the next type of iterators, which are more concerned with the selection and the arrangement of the values.

Combinatoric iterators

We have all studied permutations and combinations before. These iterators make it really easy to list down all the possible values in a simple manner. Let’s start with the first one right away.

The combinations() iterator

As the name specifies, this iterator helps us illustrate all the possible combinations present in the list. The only parameters we have to pass are the elements and the number of values in a combination. Let’s see it in action right now:

# Combinations itertool
stocks_NYSE = [‘TSLA’, ‘MSFT’, ‘NVDA’, ‘GOOGL’ , ‘AAPL’ , ‘INTC’]
result = itertools.combinations(stocks_NYSE, 2)
for each in result:
print(each)

The output will be as follows:

(‘TSLA’, ‘MSFT’)
(‘TSLA’, ‘NVDA’)
(‘TSLA’, ‘GOOGL’)
(‘TSLA’, ‘AAPL’)
(‘TSLA’, ‘INTC’)
(‘MSFT’, ‘NVDA’)
(‘MSFT’, ‘GOOGL’)
(‘MSFT’, ‘AAPL’)
(‘MSFT’, ‘INTC’)
(‘NVDA’, ‘GOOGL’)
(‘NVDA’, ‘AAPL’)
(‘NVDA’, ‘INTC’)
(‘GOOGL’, ‘AAPL’)
(‘GOOGL’, ‘INTC’)
(‘AAPL’, ‘INTC’)

In the next installment, the author will discuss the combinations_with_replacement() iterator.

Visit https://www.quantinsti.com/ for ready-to-use Python functions as applied in trading and data analysis.

###### Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from QuantInsti and is being posted with its permission. The views expressed in this material are solely those of the author and/or QuantInsti and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.