from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Common")
AddReference("QuantConnect.Indicators") # technical indicators
from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Data import SubscriptionDataSource
from QuantConnect.Python import PythonData
from QuantConnect.Indicators import *
from datetime import date, timedelta, datetime
from collections import deque
import numpy as np
import pandas as pd
import json
import requests
class CustomDataBitcoinAlgorithm(QCAlgorithm):
# Initialize
def Initialize(self):
self.SetStartDate(2019, 6, 22)
self.SetCash(1000000)
self.AddCrypto('BTCUSD', Resolution.Minute, Market.Bitfinex)
consolidator = QuoteBarConsolidator(timedelta(minutes = 5))
consolidator.DataConsolidated += self.OnDataConsolidated
self.SubscriptionManager.AddConsolidator("BTCUSD", consolidator)
self.rsi = RelativeStrengthIndex(10, MovingAverageType.Simple) # define Indicator
def OnDataConsolidated(self, sender, bar):
agg_data = self.get_Bitfinex_data()
close = agg_data[3]
self.rsi.Update(self.Time, agg_data[3])
if self.rsi.IsReady:
rsi_value = self.rsi.Current.Value
self.Debug('rsi_value {}'.format(rsi_value))
## Insert trading logic using the RSI values, place orders, etc.
def get_Bitfinex_data(self):
# download data from Bitfinex API
end_time = self.Time # past 5 minutes time
start_time = end_time - timedelta(minutes=5) # convert current time to timestamp
end_tstamp = round(datetime.timestamp(end_time)*1000,0) # convert past 5 minutes to timestamp
start_tstamp = round(datetime.timestamp(start_time)*1000,0)
getdata_time = datetime.now()
url = 'https://api-pub.bitfinex.com/v2/trades/tBTCUSD/hist?limit=1&start={}&end={}&sort=-1'.format(start_tstamp, end_tstamp)
#url = 'https://api-pub.bitfinex.com/v2/trades/tBTCUSD/hist?limit=1'
response = self.Download(url, user = 'hieppham', )
self.Debug('data downloading time: {}'.format(datetime.now()-getdata_time))
data = json.loads(response)
data_df = pd.DataFrame(data, columns = ['id','date','volume','price'])
self.Debug('data size {} rows'.format(data_df.shape[0]))
# aggregate the data
volume = data_df.volume.abs().sum()
volume_pos = data_df.volume.apply(lambda x: x if x >0 else 0).sum() # only take trade volume resulted from a taker buy order
open_price = data_df.price.values[0]
close_price = data_df.price.values[-1]
high_price = data_df.price.max()
low_price = data_df.price.min()
agg_data = [open_price, high_price, low_price, close_price, volume, volume_pos]
return agg_data