Hello,
I found this sample C# algorithm Lean/CustionDataBitcoinAlgorithm
and I tried to adapt it to multiple forex pairs that I have in the form of OHLC CSV files.
The default format of those files is: unix timespamp, open, high, low, close.
I decided to split the custom data class into 2 part: base Forex class that has the methods for importing the data and child classes with their own constructors and source files. One such instance is class NzdUsd in the code below.
Here is my code:
using System;
using System.Globalization;
using Newtonsoft.Json;
using QuantConnect.Data;
namespace QuantConnect.Algorithm.CSharp
{
public class CustomDataForexAlgorithm : QCAlgorithm
{
public override void Initialize()
{
//Weather data we have is within these days:
SetStartDate(2005, 8, 14);
SetEndDate(2005, 8, 14);
//Set the cash for the strategy:
SetCash(100000);
AddData<NzdUsd>("NZDUSD");
}
public void OnData(NzdUsd data)
{
if (!Portfolio.Invested)
{
if (data.close != 0)
{
Order(
"NZDUSD",
(Portfolio.MarginRemaining / Math.Abs(data.close + 1))
);
}
Console.WriteLine("Buying NZDUSD: " + data.close);
}
}
public class Forex : BaseData
{
[JsonProperty("timestamp")]
public DateTime timestamp;
[JsonProperty("Open")]
public decimal open = 0;
[JsonProperty("High")]
public decimal high = 0;
[JsonProperty("Low")]
public decimal low = 0;
[JsonProperty("Close")]
public decimal close = 0;
// default constructor - should exits
public Forex()
{
Symbol = "FX";
}
// reader method: reads one #line at a time
public override BaseData Reader
(
SubscriptionDataConfig config,
string line
DateTime date,
bool isLiveMode
)
{
var fx = new Forex();
try
{
string[] data = line.Split(',');
fx.timestamp = uni_to_dt(Convert.ToDouble(data[0]));
fx.open = Convert.ToDecimal(data[1], CultureInfo.InvariantCulture);
fx.high = Convert.ToDecimal(data[2], CultureInfo.InvariantCulture);
fx.low = Convert.ToDecimal(data[3],CultureInfo.InvariantCulture);
fx.close = Convert.ToDecimal(data[4], CultureInfo.InvariantCulture);
}
catch { /* Do nothing, skip first title row */ }
return fx;
}
public static DateTime uni_to_dt( double unixTimeStamp )
{
// Unix timestamp is seconds past epoch
System.DateTime dtDateTime = new DateTime(1970,1,1,0,0,0,0,System.DateTimeKind.Utc);
dtDateTime = dtDateTime.AddSeconds( unixTimeStamp ).ToLocalTime();
return dtDateTime;
}
}
public class NzdUsd : Forex
{
public NzdUsd()
{
Symbol = "NZDUSD";
}
string source = @"G:\home\marin\tmp\histdata\NZDUSD\new\DAT_ASCII_NZDUSD_M1_2005_test.csv" ;
public override SubscriptionDataSource GetSource
(
SubscriptionDataConfig config,
DateTime date,
bool isLiveMode
)
{
return new SubscriptionDataSource
(
source,
SubscriptionTransportMedium.LocalFile,
FileFormat.Csv
);
}
}
}
}
I am able to compile this as a QC project it doesn't seem to import the custom data. Any ideas?
Shile Wen
Hi Marin,
If you are using Lean on the QuantConnect website, we don't have access to the user's local files for security reasons. I suggest uploading this file to a remote file hosting service, such as Dropbox or Github. Furthermore, NZDUSD is one of our supported pairs (docs).
Best,
Shile Wen
Marin Draganov
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!