Lamma I/O

Heads up! These docs are for v1.1.3, which is no longer officially supported. Check out the latest version of Lamma!

Tutorial 1: Basic Sequence Generation

All code used in this tutorial can be found here: Scala / Java

Let's get started by generating a date sequence. You will be able to understand several very important concepts in this tutorial. Please try not to skip this section even most likely your are looking for a more complicated use case.


Generate a date sequence

Generate every single day from 2014-05-10 to 2014-05-12

Lamma.sequence(Date(2014, 5, 10), Date(2014, 5, 12))
// always import these two lines when using Java, this will make our life a lot easier
import static io.lamma.LammaConversion.*;
import static io.lamma.LammaConst.*;

Lamma4j.sequence(date(2014, 5, 10), date(2014, 5, 12));

Result: every day generated into a sorted list

List(2014-05-10, 2014-05-11, 2014-05-12)


Generate a date sequence by week

Generate a date sequence from 2014-05-10 to 2014-05-24 by week

Lamma.sequence(Date(2014, 5, 10), Date(2014, 5, 24), EveryWeek)
// always import these two lines when using Java, this will make our life a lot easier
import static io.lamma.LammaConversion.*;
import static io.lamma.LammaConst.*;

Lamma4j.sequence(date(2014, 5, 10), date(2014, 5, 24), everyWeek());

Result: every following date generated as 7 days (one week) from the previous date

List(2014-05-10, 2014-05-17, 2014-05-24)


First Concept: Recurrence Pattern

In the above example, EveryWeek is used to define the date generation frequency. In Lamma, we call this frequency pattern Recurrence Pattern. The default Recurrence Pattern is EveryDay.


Similarly, we can generate a date sequence by month

Generate dates recurring every month from 2014-05-10 to 2014-07-10

Lamma.sequence(Date(2014, 5, 10), Date(2014, 7, 10), EveryMonth)
// always import these two lines when using Java, this will make our life a lot easier
import static io.lamma.LammaConversion.*;
import static io.lamma.LammaConst.*;

Lamma4j.sequence(date(2014, 5, 10), date(2014, 7, 10), everyMonth());

Result: a sequence starting from 2014-05-10 and recurring at the 10th day of each subsequent months

List(2014-05-10, 2014-06-10, 2014-07-10)


Month end is handled properly

Generate dates recurring every month from 2014-01-31 to 2014-04-30

Lamma.sequence(Date(2014, 1, 31), Date(2014, 4, 30), EveryMonth)
// always import these two lines when using Java, this will make our life a lot easier
import static io.lamma.LammaConversion.*;
import static io.lamma.LammaConst.*;

Lamma4j.sequence(date(2014, 1, 31), date(2014, 4, 30), everyMonth());

Result: month end handled properly even with different ending days of each month

List(2014-01-31, 2014-02-28, 2014-03-31, 2014-04-30)


and recurring by year

Generate dates recurring every year from 2014-05-10 to 2016-05-10

Lamma.sequence(Date(2014, 5, 10), Date(2016, 5, 10), EveryYear)
// always import these two lines when using Java, this will make our life a lot easier
import static io.lamma.LammaConversion.*;
import static io.lamma.LammaConst.*;

Lamma4j.sequence(date(2014, 5, 10), date(2016, 5, 10), everyYear());

Result: a sequence starting from 2014-05-10 and recurring on May 10th of each subsequent year

List(2014-05-10, 2015-05-10, 2016-05-10)


Leap year is handled properly

Generate dates recurring every year from 2012-02-29 to 2016-02-29

Lamma.sequence(Date(2012, 2, 29), Date(2016, 2, 29), EveryYear)
// always import these two lines when using Java, this will make our life a lot easier
import static io.lamma.LammaConversion.*;
import static io.lamma.LammaConst.*;

Lamma4j.sequence(date(2012, 2, 29), date(2016, 2, 29), everyYear());

Result: when starting the sequence on leap day of Feb 29th, all subsequent recurring dates in common years automatically adjusted to Feb 28th

List(2012-02-29, 2013-02-28, 2014-02-28, 2015-02-28, 2016-02-29)


More options of Recurrence Pattern

So far, we have used EveryDay, EveryWeek, EveryMonth and EveryYear. Then how about every two weeks? Or every three years?

The solution is Days(Int), Weeks(Int), Months(Int) and Years(Int).


Generate sequence by multiple days

Generate sequence recurring every 3 days from 2014-05-10 to 2014-05-19

Lamma.sequence(Date(2014, 5, 10), Date(2014, 5, 19), Days(3))
// always import these two lines when using Java, this will make our life a lot easier
import static io.lamma.LammaConversion.*;
import static io.lamma.LammaConst.*;

Lamma4j.sequence(date(2014, 5, 10), date(2014, 5, 19), days(3));

Result: a sequence starting from 2014-05-10 and recurring every 3 days

List(2014-05-10, 2014-05-13, 2014-05-16, 2014-05-19)


And by multiple months

Generate sequence recurring every 3 months from 2014-05-10 to 2014-11-10

Lamma.sequence(Date(2014, 5, 10), Date(2014, 11, 10), Months(3))
// always import these two lines when using Java, this will make our life a lot easier
import static io.lamma.LammaConversion.*;
import static io.lamma.LammaConst.*;

Lamma4j.sequence(date(2014, 5, 10), date(2014, 11, 10), months(3));

Result: a sequence starting from 2014-05-10 and recurring every 3 months

List(2014-05-10, 2014-08-10, 2014-11-10)


Recurring each leap year

Generate all leap days from 2012 to 2020

Lamma.sequence(Date(2012, 2, 29), Date(2020, 2, 29), Years(4))
// always import these two lines when using Java, this will make our life a lot easier
import static io.lamma.LammaConversion.*;
import static io.lamma.LammaConst.*;

Lamma4j.sequence(date(2012, 2, 29), date(2020, 2, 29), years(4));

Result: leap days from 2012 to 2020

List(2012-02-29, 2016-02-29, 2020-02-29)


As you have probably figured out by yourself, EveryDay is basically an alias of Days(1), and EveryWeek is Weeks(1).

Recurring by working days

We can also recur working days instead of calendar days.
An io.lamma.Calendar instance is required to provide holiday calendar details.

Working days example

Generate a date sequence recurring every 5 working days from 2015-10-05 to 2015-10-19

Lamma.sequence(Date(2015, 10, 5), Date(2015, 10, 19), Days.workingDays(5, WeekendCalendar))
// always import these two lines when using Java, this will make our life a lot easier
import static io.lamma.LammaConversion.*;
import static io.lamma.LammaConst.*;

Lamma4j.sequence(date(2015, 10, 5), date(2015, 10, 19), workingDays(5, weekendCalendar()));

Result: date sequence with non-working days skipped

List(2015-10-05, 2015-10-12, 2015-10-19)

In this case, WeekendCalendar is used for demo purpose. In a real application, this will be replaced by a valid trading calendar.


Recurring Backward

With Lamma, we can generate date sequence from end date and apply recurrence pattern backward until start date.
For each of the forward recurrence pattern: Days, Weeks, Months and Years,
there is a backward equivalent: DaysBackward, WeeksBackward, MonthsBackward and YearsBackward.


Recurring backward example

Generate a date from 2014-05-12 backward to 2014-05-10 recurring everyday

Lamma.sequence(Date(2014, 5, 10), Date(2014, 5, 12), DaysBackward(1))
// always import these two lines when using Java, this will make our life a lot easier
import static io.lamma.LammaConversion.*;
import static io.lamma.LammaConst.*;

Lamma4j.sequence(date(2014, 5, 10), date(2014, 5, 12), daysBackward(1));

Result: date sequence generated in backward direction but still sorted in ascending order

List(2014-05-10, 2014-05-11, 2014-05-12)

What's the difference?

Q: Isn't above sample exactly the same as the first one? What's the point of generating sequence from backward direction?

A: Recurring directions only matter when there is a fraction period.


Recurring forward with a fraction period

Generate a date from 2014-05-10 to 2014-10-20 recurring forward every 3 months

Lamma.sequence(Date(2014, 5, 10), Date(2014, 10, 20), Months(3))
// always import these two lines when using Java, this will make our life a lot easier
import static io.lamma.LammaConversion.*;
import static io.lamma.LammaConst.*;

Lamma4j.sequence(date(2014, 5, 10), date(2014, 10, 20), months(3));

Result: date sequence generated in forward direction and the last fraction of the period discarded

List(2014-05-10, 2014-08-10)


Recurring backward with a fraction period

Generate a date from 2014-05-10 to 2014-10-20 recurring backward every 3 months

Lamma.sequence(Date(2014, 5, 10), Date(2014, 10, 20), MonthsBackward(3))
// always import these two lines when using Java, this will make our life a lot easier
import static io.lamma.LammaConversion.*;
import static io.lamma.LammaConst.*;

Lamma4j.sequence(date(2014, 5, 10), date(2014, 10, 20), monthsBackward(3));

Result: date sequence generated in backward direction and the fraction at the start of the period discarded

List(2014-07-20, 2014-10-20)