/// <summary> /// 3. READER METHOD: Read 1 line from data source and convert it into Object. /// Each line of the CSV File is presented in here. The backend downloads your file, loads it into memory and then line by line /// feeds it into your algorithm /// </summary> /// <param name="line">string line from the data source file submitted above</param> /// <param name="config">Subscription data, symbol name, data type</param> /// <param name="date">Current date we're requesting. This allows you to break up the data source into daily files.</param> /// <param name="datafeed">Datafeed type - Backtesting or LiveTrading</param> /// <returns>New Weather Object which extends BaseData.</returns> public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, DataFeedEndpoint datafeed) { //New weather object Bitcoin coin = new Bitcoin(); try { //Example File Format: //Date, Open High Low Close Volume (BTC) Volume (Currency) Weighted Price //2011-09-13 5.8 6.0 5.65 5.97 58.37138238, 346.0973893944 5.929230648356 string[] data = line.Split(','); coin.Time = DateTime.Parse(data[0]); coin.Open = Convert.ToDecimal(data[1]); coin.High = Convert.ToDecimal(data[2]); coin.Low = Convert.ToDecimal(data[3]); coin.Close = Convert.ToDecimal(data[4]); coin.VolumeBTC = Convert.ToDecimal(data[5]); coin.VolumeUSD = Convert.ToDecimal(data[6]); coin.WeightedPrice = Convert.ToDecimal(data[7]); coin.Symbol = "BTC"; coin.Value = coin.Close; } catch { /* Do nothing, skip first title row */ } return(coin); }
/// <summary> /// Backtesting & Live Bitcoin Decoder: /// </summary> public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, DataFeedEndpoint datafeed) { Bitcoin coin = new Bitcoin(); switch (datafeed) { //Example Line Format: //Date Open High Low Close Volume (BTC) Volume (Currency) Weighted Price //2011-09-13 5.8 6.0 5.65 5.97 58.37138238, 346.0973893944 5.929230648356 case DataFeedEndpoint.FileSystem: case DataFeedEndpoint.Backtesting: try { string[] data = line.Split(','); coin.Time = DateTime.Parse(data[0]); coin.Open = Convert.ToDecimal(data[1], CultureInfo.InvariantCulture); coin.High = Convert.ToDecimal(data[2], CultureInfo.InvariantCulture); coin.Low = Convert.ToDecimal(data[3], CultureInfo.InvariantCulture); coin.Close = Convert.ToDecimal(data[4], CultureInfo.InvariantCulture); coin.VolumeBTC = Convert.ToDecimal(data[5], CultureInfo.InvariantCulture); coin.WeightedPrice = Convert.ToDecimal(data[7], CultureInfo.InvariantCulture); coin.Symbol = "BTC"; coin.Value = coin.Close; } catch { /* Do nothing, skip first title row */ } break; //Example Line Format: //{"high": "441.00", "last": "421.86", "timestamp": "1411606877", "bid": "421.96", "vwap": "428.58", "volume": "14120.40683975", "low": "418.83", "ask": "421.99"} case DataFeedEndpoint.LiveTrading: try { var liveBTC = JsonConvert.DeserializeObject <LiveBitcoin>(line); coin.Time = DateTime.Now; coin.Open = liveBTC.Last; coin.High = liveBTC.High; coin.Low = liveBTC.Low; coin.Close = liveBTC.Last; coin.VolumeBTC = liveBTC.Volume; coin.WeightedPrice = liveBTC.VWAP; coin.Symbol = "BTC"; coin.Value = coin.Close; } catch { /* Do nothing, possible error in json decoding */ } break; } System.Console.WriteLine(DateTime.Now.ToString("u") + " READER >> " + line + " COIN >> " + coin.Time.ToString("u")); return(coin); }
/// <summary> /// Event Handler for Bitcoin Data Events: These weather objects are created from our /// "Weather" type below and fired into this event handler. /// </summary> /// <param name="data">One(1) Weather Object, streamed into our algorithm synchronised in time with our other data streams</param> public void OnData(Bitcoin data) { //If we don't have any weather "SHARES" -- invest" if (!Portfolio.Invested) { //Weather used as a tradable asset, like stocks, futures etc. if (data.Close != 0) { Order("BTC", (Portfolio.Cash / Math.Abs(data.Close + 1))); } Console.WriteLine("Buying BTC 'Shares': BTC: " + data.Close); } Console.WriteLine("Time: " + Time.ToLongDateString() + " " + Time.ToLongTimeString() + data.Close.ToString()); }
/// <summary> /// Clone the bitcoin object, required for live data. /// </summary> /// <returns></returns> public override BaseData Clone() { Bitcoin coin = new Bitcoin(); coin.Close = this.Close; coin.High = this.High; coin.Low = this.Low; coin.Open = this.Open; coin.Symbol = this.Symbol; coin.Value = this.Close; coin.Time = this.Time; coin.VolumeBTC = this.VolumeBTC; coin.WeightedPrice = this.WeightedPrice; return coin; }
/// <summary> /// Event Handler for Bitcoin Data Events: These weather objects are created from our /// "Weather" type below and fired into this event handler. /// </summary> /// <param name="data">One(1) Weather Object, streamed into our algorithm synchronised in time with our other data streams</param> public void OnData(Bitcoin data) { //If we don't have any weather "SHARES" -- invest" if (!Portfolio.Invested) { //Weather used as a tradable asset, like stocks, futures etc. if (data.Close != 0) { Order("BTC", (Portfolio.Cash / Math.Abs(data.Close + 1))); } Console.WriteLine("Buying BTC 'Shares': BTC: " + data.Close); } Console.WriteLine("Time: " + Time.ToLongDateString() + " " + Time.ToLongTimeString() + data.Close.ToString()); }
/// <summary> /// Clone the bitcoin object, required for live data. /// </summary> /// <returns></returns> public override BaseData Clone() { Bitcoin coin = new Bitcoin(); coin.Close = this.Close; coin.High = this.High; coin.Low = this.Low; coin.Open = this.Open; coin.Symbol = this.Symbol; coin.Value = this.Close; coin.Time = this.Time; coin.VolumeBTC = this.VolumeBTC; coin.WeightedPrice = this.WeightedPrice; return(coin); }
/// <summary> /// 3. READER METHOD: Read 1 line from data source and convert it into Object. /// Each line of the CSV File is presented in here. The backend downloads your file, loads it into memory and then line by line /// feeds it into your algorithm /// </summary> /// <param name="line">string line from the data source file submitted above</param> /// <param name="config">Subscription data, symbol name, data type</param> /// <param name="date">Current date we're requesting. This allows you to break up the data source into daily files.</param> /// <param name="isLiveMode">true if we're in live mode, false for backtesting mode</param> /// <returns>New Bitcoin Object which extends BaseData.</returns> public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, bool isLiveMode) { var coin = new Bitcoin(); if (isLiveMode) { //Example Line Format: //{"high": "441.00", "last": "421.86", "timestamp": "1411606877", "bid": "421.96", "vwap": "428.58", "volume": "14120.40683975", "low": "418.83", "ask": "421.99"} try { var liveBTC = JsonConvert.DeserializeObject <LiveBitcoin>(line); coin.Time = DateTime.Now; coin.Open = liveBTC.Last; coin.High = liveBTC.High; coin.Low = liveBTC.Low; coin.Close = liveBTC.Last; coin.VolumeBTC = liveBTC.Volume; coin.WeightedPrice = liveBTC.VWAP; coin.Symbol = "BTC"; coin.Value = coin.Close; } catch { /* Do nothing, possible error in json decoding */ } return(coin); } //Example Line Format: //Date Open High Low Close Volume (BTC) Volume (Currency) Weighted Price //2011-09-13 5.8 6.0 5.65 5.97 58.37138238, 346.0973893944 5.929230648356 try { string[] data = line.Split(','); coin.Time = DateTime.Parse(data[0], CultureInfo.InvariantCulture); coin.Open = Convert.ToDecimal(data[1], CultureInfo.InvariantCulture); coin.High = Convert.ToDecimal(data[2], CultureInfo.InvariantCulture); coin.Low = Convert.ToDecimal(data[3], CultureInfo.InvariantCulture); coin.Close = Convert.ToDecimal(data[4], CultureInfo.InvariantCulture); coin.VolumeBTC = Convert.ToDecimal(data[5], CultureInfo.InvariantCulture); coin.VolumeUSD = Convert.ToDecimal(data[6], CultureInfo.InvariantCulture); coin.WeightedPrice = Convert.ToDecimal(data[7], CultureInfo.InvariantCulture); coin.Symbol = "BTC"; coin.Value = coin.Close; } catch { /* Do nothing, skip first title row */ } return(coin); }
//New Bitcoin Data Event: public void OnData(Bitcoin data) { if (LiveMode) //Live Mode Property { //Configurable title header statistics numbers SetRuntimeStatistic("BTC", data.Close.ToString("C")); } if (!Portfolio.HoldStock) { Order("BTC", 100); //Send a notification email/SMS/web request on events: Notify.Email("*****@*****.**", "Test", "Test Body", "test attachment"); Notify.Sms("+11233456789", Time.ToString("u") + ">> Test message from live BTC server."); Notify.Web("http://api.quantconnect.com", Time.ToString("u") + ">> Test data packet posted from live BTC server."); } }
/// <summary> /// New Bitcoin Data Event. /// </summary> /// <param name="data">Data.</param> public void OnData(Bitcoin data) { if (LiveMode) //Live Mode Property { //Configurable title header statistics numbers SetRuntimeStatistic("BTC", data.Close.ToString("C")); } if (!Portfolio.HoldStock) { Order("BTC", 100); //Send a notification email/SMS/web request on events: Notify.Email("*****@*****.**", "Test", "Test Body", "test attachment"); Notify.Sms("+11233456789", Time.ToString("u") + ">> Test message from live BTC server."); Notify.Web("http://api.quantconnect.com", Time.ToString("u") + ">> Test data packet posted from live BTC server."); } }
//Bitcoin Handler: public void OnData(Bitcoin data) { Debug(Time.ToLongTimeString() + " >> ALGO >> OnData(BTC) >> BTC: " + data.Close); }
/// <summary> /// 3. READER METHOD: Read 1 line from data source and convert it into Object. /// Each line of the CSV File is presented in here. The backend downloads your file, loads it into memory and then line by line /// feeds it into your algorithm /// </summary> /// <param name="line">string line from the data source file submitted above</param> /// <param name="config">Subscription data, symbol name, data type</param> /// <param name="date">Current date we're requesting. This allows you to break up the data source into daily files.</param> /// <param name="isLiveMode">true if we're in live mode, false for backtesting mode</param> /// <returns>New Bitcoin Object which extends BaseData.</returns> public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, bool isLiveMode) { var coin = new Bitcoin(); if (isLiveMode) { //Example Line Format: //{"high": "441.00", "last": "421.86", "timestamp": "1411606877", "bid": "421.96", "vwap": "428.58", "volume": "14120.40683975", "low": "418.83", "ask": "421.99"} try { var liveBTC = JsonConvert.DeserializeObject<LiveBitcoin>(line); coin.Time = DateTime.Now; coin.Open = liveBTC.Last; coin.High = liveBTC.High; coin.Low = liveBTC.Low; coin.Close = liveBTC.Last; coin.VolumeBTC = liveBTC.Volume; coin.WeightedPrice = liveBTC.VWAP; coin.Symbol = "BTC"; coin.Value = coin.Close; } catch { /* Do nothing, possible error in json decoding */ } return coin; } //Example Line Format: //Date Open High Low Close Volume (BTC) Volume (Currency) Weighted Price //2011-09-13 5.8 6.0 5.65 5.97 58.37138238, 346.0973893944 5.929230648356 try { string[] data = line.Split(','); coin.Time = DateTime.Parse(data[0]); coin.Open = Convert.ToDecimal(data[1], CultureInfo.InvariantCulture); coin.High = Convert.ToDecimal(data[2], CultureInfo.InvariantCulture); coin.Low = Convert.ToDecimal(data[3], CultureInfo.InvariantCulture); coin.Close = Convert.ToDecimal(data[4], CultureInfo.InvariantCulture); coin.VolumeBTC = Convert.ToDecimal(data[5], CultureInfo.InvariantCulture); coin.VolumeUSD = Convert.ToDecimal(data[6], CultureInfo.InvariantCulture); coin.WeightedPrice = Convert.ToDecimal(data[7], CultureInfo.InvariantCulture); coin.Symbol = "BTC"; coin.Value = coin.Close; } catch { /* Do nothing, skip first title row */ } return coin; }
/// <summary> /// Custom data event handler: /// </summary> /// <param name="data">Bitcoin - dictionary of TradeBarlike Bars of Bitcoin Data</param> public void OnData(Bitcoin data) { }
/// <summary> /// Custom data event handler: /// </summary> /// <param name="data">Bitcoin - dictionary of TradeBarlike Bars of Bitcoin Data</param> public void OnData(Bitcoin data) { }
//Bitcoin Handler: public void OnData(Bitcoin data) { Debug(Time.ToLongTimeString() + " >> ALGO >> OnData(BTC) >> BTC: " + data.Close); }
/// <summary> /// 3. READER METHOD: Read 1 line from data source and convert it into Object. /// Each line of the CSV File is presented in here. The backend downloads your file, loads it into memory and then line by line /// feeds it into your algorithm /// </summary> /// <param name="line">string line from the data source file submitted above</param> /// <param name="config">Subscription data, symbol name, data type</param> /// <param name="date">Current date we're requesting. This allows you to break up the data source into daily files.</param> /// <param name="datafeed">Datafeed type - Backtesting or LiveTrading</param> /// <returns>New Weather Object which extends BaseData.</returns> public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, DataFeedEndpoint datafeed) { //New weather object Bitcoin coin = new Bitcoin(); try { //Example File Format: //Date, Open High Low Close Volume (BTC) Volume (Currency) Weighted Price //2011-09-13 5.8 6.0 5.65 5.97 58.37138238, 346.0973893944 5.929230648356 string[] data = line.Split(','); coin.Time = DateTime.Parse(data[0]); coin.Open = Convert.ToDecimal(data[1]); coin.High = Convert.ToDecimal(data[2]); coin.Low = Convert.ToDecimal(data[3]); coin.Close = Convert.ToDecimal(data[4]); coin.VolumeBTC = Convert.ToDecimal(data[5]); coin.VolumeUSD = Convert.ToDecimal(data[6]); coin.WeightedPrice = Convert.ToDecimal(data[7]); coin.Symbol = "BTC"; coin.Value = coin.Close; } catch { /* Do nothing, skip first title row */ } return coin; }
/// <summary> /// Backtesting & Live Bitcoin Decoder: /// </summary> public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, DataFeedEndpoint datafeed) { Bitcoin coin = new Bitcoin(); switch (datafeed) { //Example Line Format: //Date Open High Low Close Volume (BTC) Volume (Currency) Weighted Price //2011-09-13 5.8 6.0 5.65 5.97 58.37138238, 346.0973893944 5.929230648356 case DataFeedEndpoint.FileSystem: case DataFeedEndpoint.Backtesting: try { string[] data = line.Split(','); coin.Time = DateTime.Parse(data[0]); coin.Open = Convert.ToDecimal(data[1], CultureInfo.InvariantCulture); coin.High = Convert.ToDecimal(data[2], CultureInfo.InvariantCulture); coin.Low = Convert.ToDecimal(data[3], CultureInfo.InvariantCulture); coin.Close = Convert.ToDecimal(data[4], CultureInfo.InvariantCulture); coin.VolumeBTC = Convert.ToDecimal(data[5], CultureInfo.InvariantCulture); coin.WeightedPrice = Convert.ToDecimal(data[7], CultureInfo.InvariantCulture); coin.Symbol = "BTC"; coin.Value = coin.Close; } catch { /* Do nothing, skip first title row */ } break; //Example Line Format: //{"high": "441.00", "last": "421.86", "timestamp": "1411606877", "bid": "421.96", "vwap": "428.58", "volume": "14120.40683975", "low": "418.83", "ask": "421.99"} case DataFeedEndpoint.LiveTrading: try { var liveBTC = JsonConvert.DeserializeObject<LiveBitcoin>(line); coin.Time = DateTime.Now; coin.Open = liveBTC.Last; coin.High = liveBTC.High; coin.Low = liveBTC.Low; coin.Close = liveBTC.Last; coin.VolumeBTC = liveBTC.Volume; coin.WeightedPrice = liveBTC.VWAP; coin.Symbol = "BTC"; coin.Value = coin.Close; } catch { /* Do nothing, possible error in json decoding */ } break; } System.Console.WriteLine(DateTime.Now.ToString("u") + " READER >> " + line + " COIN >> " + coin.Time.ToString("u")); return coin; }