Skip to content

Commit

Permalink
Add future contracts for NK, IN, TW and HSI
Browse files Browse the repository at this point in the history
  • Loading branch information
Anand Vishnu committed Jun 29, 2020
1 parent 5131b94 commit ec6abfa
Show file tree
Hide file tree
Showing 13 changed files with 1,488 additions and 13 deletions.
14 changes: 13 additions & 1 deletion Common/Market.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public static class Market

Tuple.Create(COMEX, 22),
Tuple.Create(CME, 23),
Tuple.Create(SGX, 24),
Tuple.Create(HKFE, 25),
};

static Market()
Expand Down Expand Up @@ -136,7 +138,17 @@ static Market()
/// CME
/// </summary>
public const string CME = "cme";


/// <summary>
/// Singapore Exchange
/// </summary>
public const string SGX = "sgx";

/// <summary>
/// Hong Kong Exchange
/// </summary>
public const string HKFE = "hkfe";

/// <summary>
/// GDAX
/// </summary>
Expand Down
23 changes: 23 additions & 0 deletions Common/Securities/Future/Futures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,29 @@ public static class Indices
/// </summary>
/// <returns>The symbol</returns>
public const string USDDenominatedIbovespa = "IBV";

/// <summary>
/// USD-Denominated MSCI Taiwan Index Futures
/// </summary>
/// <returns>The symbol</returns>
public const string MSCITaiwanIndex = "TW";

/// <summary>
/// Nikkei-225 Yen denominated Index Futures
/// </summary>
/// <returns>The symbol</returns>
public const string Nikkei225Yen = "NK";

/// <summary>
/// Nifty 50 Index Futures
/// </summary>
/// <returns>The symbol</returns>
public const string Nifty50 = "IN";

/// <summary>
/// Hang Seng Index
/// </summary>
public const string HangSeng = "HSI";
}

/// <summary>
Expand Down
83 changes: 83 additions & 0 deletions Common/Securities/Future/FuturesExpiryFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,89 @@ public static Func<DateTime, DateTime> FuturesExpiryFunction(Symbol symbol)
return closestWednesday.Add(new TimeSpan(20, 0, 0));
})
},

// JPY-Denominated Nikkie 225 Index Futures: https://www2.sgx.com/derivatives/products/nikkei225futuresoptions?cc=NK#Contract%20Specifications
{Symbol.Create(Futures.Indices.Nikkei225Yen, SecurityType.Future, Market.SGX), (time =>
{
// The day before the second Friday of the contract month. Trading Hours on Last Day is normal Trading Hours (session T)
// T Session
// 7.15 am - 2.30 pm
var secondFriday = FuturesExpiryUtilityFunctions.SecondFriday(time);
var priorBusinessDay = secondFriday.AddDays(-1);

var holidays = MarketHoursDatabase.FromDataFolder()
.GetEntry(Market.SGX, Futures.Indices.Nikkei225Yen, SecurityType.Future)
.ExchangeHours
.Holidays;

while (holidays.Contains(priorBusinessDay) || !priorBusinessDay.IsCommonBusinessDay())
{
priorBusinessDay = priorBusinessDay.AddDays(-1);
}
return priorBusinessDay.Add(new TimeSpan(14, 30, 0));
})
},

// MSCI Taiwan Index Futures: https://www2.sgx.com/derivatives/products/timsci?cc=TW
{Symbol.Create(Futures.Indices.MSCITaiwanIndex, SecurityType.Future, Market.SGX), (time =>
{
// Second last business day of the contract month. Same as T Session trading hours
var lastDay = new DateTime(time.Year, time.Month, DateTime.DaysInMonth(time.Year, time.Month));
var priorBusinessDay = lastDay.AddDays(-1);

var holidays = MarketHoursDatabase.FromDataFolder()
.GetEntry(Market.SGX, Futures.Indices.MSCITaiwanIndex, SecurityType.Future)
.ExchangeHours
.Holidays;

while (holidays.Contains(priorBusinessDay) || !priorBusinessDay.IsCommonBusinessDay())
{
priorBusinessDay = priorBusinessDay.AddDays(-1);
}
return priorBusinessDay.Add(new TimeSpan(13, 45, 0));
})
},

// Nifty 50 Index Futures: https://www.sgx.com/derivatives/products/nifty#Contract%20Specifications
{Symbol.Create(Futures.Indices.Nifty50, SecurityType.Future, Market.SGX), (time =>
{
// Last Thursday of the expiring contract month. If this falls on an NSE non-business day, the last trading day shall be the preceding business day.
// The expiring contract shall close on its last trading day at 6.15 pm.

var holidays = MarketHoursDatabase.FromDataFolder()
.GetEntry(Market.NSE, Futures.Indices.Nifty50, SecurityType.Future)
.ExchangeHours
.Holidays;

var expiryday = FuturesExpiryUtilityFunctions.LastThursday(time);

while (holidays.Contains(expiryday) || !expiryday.IsCommonBusinessDay())
{
expiryday = expiryday.AddDays(-1);
}
return expiryday.Add(new TimeSpan(18, 15, 0));
})
},

// HSI Index Futures:https://www.hkex.com.hk/Products/Listed-Derivatives/Equity-Index/Hang-Seng-Index-(HSI)/Hang-Seng-Index-Futures?sc_lang=en#&product=HSI
{Symbol.Create(Futures.Indices.HangSeng, SecurityType.Future, Market.HKFE), (time =>
{
// The Business Day immediately preceding the last Business Day of the Contract Month
var lastDay = new DateTime(time.Year, time.Month, DateTime.DaysInMonth(time.Year, time.Month));
var priorBusinessDay = lastDay.AddDays(-1);

var holidays = MarketHoursDatabase.FromDataFolder()
.GetEntry(Market.HKFE, Futures.Indices.HangSeng, SecurityType.Future)
.ExchangeHours
.Holidays;

while (holidays.Contains(priorBusinessDay) || !priorBusinessDay.IsCommonBusinessDay())
{
priorBusinessDay = priorBusinessDay.AddDays(-1);
}
return priorBusinessDay.Add(new TimeSpan(16, 0, 0));
})
},
// Forestry Group
// Random Length Lumber (LBS): https://www.cmegroup.com/trading/agricultural/lumber-and-pulp/random-length-lumber_contract_specifications.html
{Symbol.Create(Futures.Forestry.RandomLengthLumber, SecurityType.Future, Market.CME), (time =>
Expand Down
43 changes: 35 additions & 8 deletions Common/Securities/Future/FuturesExpiryUtilityFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,23 @@ public static DateTime NthBusinessDay(DateTime time, int nthBusinessDay, IEnumer
/// <param name="time">Date from the given month</param>
/// <param name="n">The order of the Friday in the period</param>
/// <returns>Nth Friday of given month</returns>
public static DateTime NthFriday(DateTime time, int n)
public static DateTime NthFriday(DateTime time, int n) => NthWeekday(time, n, DayOfWeek.Friday);

/// <summary>
/// Method to retrieve third Wednesday of the given month (usually Monday).
/// </summary>
/// <param name="time">Date from the given month</param>
/// <returns>Third Wednesday of the given month</returns>
public static DateTime ThirdWednesday(DateTime time) => NthWeekday(time, 3, DayOfWeek.Wednesday);

/// <summary>
/// Method to retrieve the Nth Weekday of the given month
/// </summary>
/// <param name="time">Date from the given month</param>
/// <param name="n">The order of the Weekday in the period</param>
/// <param name="dayofWeek">The day of the week</param>
/// <returns>Nth Weekday of given month</returns>
public static DateTime NthWeekday(DateTime time, int n, DayOfWeek dayofWeek)
{
if (n < 1 || n > 5)
{
Expand All @@ -209,23 +225,34 @@ public static DateTime NthFriday(DateTime time, int n)

var daysInMonth = DateTime.DaysInMonth(time.Year, time.Month);
return (from day in Enumerable.Range(1, daysInMonth)
where new DateTime(time.Year, time.Month, day).DayOfWeek == DayOfWeek.Friday
where new DateTime(time.Year, time.Month, day).DayOfWeek == dayofWeek
select new DateTime(time.Year, time.Month, day)).ElementAt(n - 1);
}


/// <summary>
/// Method to retrieve third Wednesday of the given month (usually Monday).
/// Method to retrieve the last weekday of any month
/// </summary>
/// <param name="time">Date from the given month</param>
/// <returns>Third Wednesday of the given month</returns>
public static DateTime ThirdWednesday(DateTime time)
/// <param name="dayofWeek">the last weekday to be found</param>
/// <returns>Last day of the we</returns>
public static DateTime LastWeekday(DateTime time, DayOfWeek dayofWeek)
{

var daysInMonth = DateTime.DaysInMonth(time.Year, time.Month);
return (from day in Enumerable.Range(1, daysInMonth)
where new DateTime(time.Year, time.Month, day).DayOfWeek == DayOfWeek.Wednesday
select new DateTime(time.Year, time.Month, day)).ElementAt(2);
return (from day in Enumerable.Range(1, daysInMonth).Reverse()
where new DateTime(time.Year, time.Month, day).DayOfWeek == dayofWeek
select new DateTime(time.Year, time.Month, day)).First();
}

/// <summary>
/// Method to retrieve the last weekday of any month
/// </summary>
/// <param name="time">Date from the given month</param>
/// <param name="dayofWeek">the last weekday to be found</param>
/// <returns>Last day of the we</returns>
public static DateTime LastThursday(DateTime time) => LastWeekday(time, DayOfWeek.Thursday);

/// <summary>
/// Method to check whether a given time is holiday or not
/// </summary>
Expand Down
58 changes: 58 additions & 0 deletions Data/hkfe/margins/HSI.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
date,initial,maintenance
20160601,7993,6397
20160704,8416,6734
20160801,8861,7088
20160901,9198,7354
20161003,9541,7633
20161101,9542,7633
20161201,9154,7323
20170103,8799,7039
20170201,9234,7391
20170301,9628,7703
20170320,10088,8070
20170403,10439,8351
20170502,10272,8218
20170601,10847,8678
20170703,10918,8734
20170801,11440,9152
20170901,11702,9362
20171003,11833,9467
20171101,12010,9608
20171201,12691,10153
20180102,12475,9980
20180201,14099,11279
20180208,16355,13084
20180215,17513,14010
20180301,18658,14926
20180305,18644,14915
20180403,18420,14736
20180502,17943,14355
20180601,18216,14573
20180703,17412,13930
20180801,16995,13596
20180903,14029,11223
20181002,13987,11190
20181101,12837,10270
20181203,13215,10572
20190102,13012,10409
20190201,13780,11024
20190301,14573,11658
20190401,14791,11833
20190502,15233,12186
20190603,13909,11127
20190702,14537,11630
20190801,14557,11646
20190902,13287,10629
20191002,13298,10638
20191101,13508,10807
20191202,13480,10784
20200102,14246,11397
20200203,14344,11475
20200302,14070,11256
20200313,16137,12910
20200320,16982,13586
20200323,17929,14343
20200327,19936,15949
20200401,19935,15948
20200504,16948,13558
20200601,16952,13562
Loading

0 comments on commit ec6abfa

Please sign in to comment.