ng_consecutive_contract_volume.plot(xlim=['2016-10-01','2017-08-01'])
# Trading activity jumps from one contract to the next. Transitions happen just prior to the delivery date of each contract.
# This phenomenon can make it difficult to work with futures. Having to explicitly reference a series of transient contracts when trading or simulating futures can be a hassle.
# In order to trade consecutive contracts for the same underlying future, we can use what's called a "Continuous Future".
# # Continuous Futures
# Continuous futures are abstractions over the 'underlying' commodities/assets/indexes of futures. For example, if we wanted to trade crude oil, we could create a reference to CL, instead of a series of CL contracts. Continuous futures essentially maintain a reference to a 'current' contract deemed to be the active contract for the particular underlying.
# We use the continuous futures objects as part of the platform to get a continuous chain of historical data for futures contracts, taking these concerns into account. There are several ways to adjust for the cost of carry when looking at historical data, though people differ on what they prefer. The general consensus is that an adjustment should be done.
# Continuous futures are not tradable assets. They maintain a reference to the current active contract related to a given underlying.
print(continuous_future.__doc__)
# There are 4 arguments that we need to consider.
# - **`root_symbol`**: The root symbol of the underlying. For example, 'CL' for crude oil.
# - **`offset`**: The distance from the primary contract. 0 = primary, 1 = secondary, etc. We'll get into this more later.
# - **`roll`**: How to determine the 'current' contract of the continuous future. Current options are **`'volume'`** and **`'calendar'`**. The 'volume' approach chooses the current active contract based on trading volume. The 'calendar' approach chooses the current active contract based simply on the `auto_close_date`s of each contract.**
# - **`adjustment`**: How to adjust historical prices from earlier contracts. We'll get into this more later. Options are **`'mul'`**, **`'add'`**, or **`'None'`**.
# In[97]:
continuous_ng = continuous_future('NG', offset=0, roll='volume', adjustment='mul')
# In[98]:
continuous_ng
# In[99]:
ng_cont_active = history(continuous_ng, 
                    fields=['contract','price','volume'] ,
                    frequency='daily', 
                    start_date='2016-10-01', 
                    end_date='2017-08-01')
ng_cont_active.head()
# In[101]:
ng_cont_active['price'].plot()
# In[102]:
ng_cont_active['volume'].plot()
# In[103]:
ng_consecutive_contract_volume = history(ng_contracts, 
Beispiel #2
0
"""

future_contract = symbols('NGF20')  # NG (Natural Gas) F (January Future) 20 (2020)
for key in future_contract.to_dict():
    print("{} -> {}".format(key, future_contract.to_dict()[key]))

futures_position_value = get_pricing(future_contract, start_date='2017-01-01', end_date='2018-01-01')
futures_position_value.name = futures_position_value.name.symbol
futures_position_value.plot()
# NOW, LET'S GET SOME HISTORICAL DATA
ngf20 = future_contract  # RESET A NEW SHORTER VARIABLE :')
ngf20_data = history(ngf20, fields=['price', 'open_price', 'high', 'low', 'close_price', 'volume', 'contract'],
                     frequency='daily', start_date='2017-06-01', end_date='2017-08-01')
ngf20_data.head(); ngf20_data.plot()  # WORK WITH THIS DATAFRAME HOWEVER
# F - January, G - February, ...
ng_contracts = symbols(['NGF18', 'NGG18', 'NGH18', 'NGJ18', 'NGK18', 'NGM18'])

#   FIRST, WORKING WITH CONSECUTIVE FUTURES
ng_consecutive_contract_volume = history(ng_contracts, fields='volume', frequency='daily',
                                         start_date='2017-08-01', end_date='2017-08-01')
ng_consecutive_contract_volume.plot()  # ANOTHER DATAFRAME :)
# VOLUME BEING TRADED INCREASES FUTURE'S PRICE AS IT ALSO INCREASES
# BUT ALSO, AS THE FUTURE'S MATURITY DATE APPROACHES, IT'S PRICE BEGINS TO REDUCE GRADUALLY

#   NOW, WORKING WITH CONTINUOUS FUTURES
ng_continuous = continuous_future('NG', offset=0, roll='volume', adjustment='mul')  # / 'add' / 'nul'
ng_continuous_active = history(ng_continuous, fields=['contract', 'price', 'volume'],
                               frequency='daily', start_date='2018-10-01', end_date='2019-08-01')
ng_continuous_active.head(); ng_continuous_active['contract'].unique()
ng_continuous_active['price'].plot(); ng_continuous_active['volume'].plot();
#Creating Arrays
FutureTicker = [
    "CN", "CL", "ED", "ES", "ER", "ET", "FC", "GC", "OA", "PA", "NG", "LB",
    "LH", "LC", "SY", "SV", "HG", "WC"
]
Prices = []
Correlation = []
Order = []

# In[ ]:

#Pulls data for all futures listed above and curve fits them via Math filter we created

for x in FutureTicker:

    y = continuous_future(x, roll='volume')

    y = history(y,
                fields='price',
                frequency='daily',
                start='2018-10-21',
                end='2018-10-21')

    x = pd.Series(
        scipy.signal.savgol_filter(y, 31, 2, 0, 1.0, -1, 'interp', 0.0))

    Prices.append(x)

# In[40]:

#Finds every possible Correlation between futures

import numpy as np
import pandas as pd
from quantopian.research.experimental import history
from quantopian.research.experimental import continuous_future
import scipy
import numpy
numpy.arange
import matplotlib.pyplot as plt


# In[3]:


WTI = continuous_future( 'CL', roll = 'volume' ) #Crude Oil Futures

WTI = history(
    WTI, 
    fields='price', 
    frequency='daily', 
    start='2008-08-31', 
    end='2018-08-31'
)

Corn = continuous_future( 'CN' , roll = 'volume')  #Corn Futures

Corn = history(
    Corn, 
    fields='price', 
    frequency='daily',