In [1]:
%matplotlib inline
# Imports
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Common")
AddReference("QuantConnect.Jupyter")
AddReference("QuantConnect.Indicators")
from System import *
from QuantConnect import *
from QuantConnect.Data.Custom import *
from QuantConnect.Data.Market import TradeBar, QuoteBar
from QuantConnect.Jupyter import *
from QuantConnect.Indicators import *
from datetime import datetime, timedelta
import matplotlib.pyplot as plt
import pandas as pd

# Create an instance
qb = QuantBook()

# Select asset data
qb.AddCrypto("BTCUSD", Resolution.Daily, Market.Bitfinex)
qb.AddCrypto("ETHUSD", Resolution.Daily, Market.Bitfinex)
qb.AddCrypto("XRPUSD", Resolution.Daily, Market.Bitfinex)
qb.AddCrypto("LTCUSD", Resolution.Daily, Market.Bitfinex)
qb.AddCrypto("EOSUSD", Resolution.Daily, Market.Bitfinex)
qb.AddCrypto("NEOUSD", Resolution.Daily, Market.Bitfinex)
qb.AddCrypto("XMRUSD", Resolution.Daily, Market.Bitfinex)
qb.AddCrypto("TRXUSD", Resolution.Daily, Market.Bitfinex)
Out[1]:
<QuantConnect.Securities.Crypto.Crypto at 0x7f8b2c400f60>

Historical Data Requests¶

We can use the QuantConnect API to make Historical Data Requests. The data will be presented as multi-index pandas.DataFrame where the first index is the Symbol.

For more information, please follow the link.

In [2]:
# Gets historical data from the subscribed assets, the last 360 datapoints with daily resolution
h1 = qb.History(qb.Securities.Keys, 360, Resolution.Daily)

# Plot closing prices from asset 
h1.loc["EOSUSD"]["close"].plot()
Out[2]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f8b207625c0>

Beräkna avkastning per vecka och tillgång. Se länk för beräkning: https://www.quantconnect.com/tutorials/introduction-to-financial-python/rate-of-return,-mean-and-variance

In [3]:
import numpy as np
rate_return = 102.0/100 - 1
print(rate_return)
0.020000000000000018
In [4]:
assets = ['BTCUSD', 'ETHUSD', 'XRPUSD', 'LTCUSD', 'EOSUSD', 'NEOUSD', 'XMRUSD', 'TRXUSD']


for i in assets:
    h1.loc[i]["close"][0]
    h1.loc[i]["close"][6]
    print(i)
BTCUSD
ETHUSD
XRPUSD
LTCUSD
EOSUSD
NEOUSD
XMRUSD
TRXUSD
In [ ]: