Universal: more refactor + cap rollout unit test

This commit is contained in:
sofusmortensen 2016-09-17 16:16:08 +02:00
parent 1d5c7963c4
commit 9c5ef6cdfe
14 changed files with 515 additions and 276 deletions

View File

@ -35,7 +35,6 @@ data class Transfer(val amount: Perceivable<BigDecimal>, val currency: Currency,
// The ``And`` combinator cannot be root in a arrangement. // The ``And`` combinator cannot be root in a arrangement.
data class And(val arrangements: Set<Arrangement>) : Arrangement data class And(val arrangements: Set<Arrangement>) : Arrangement
data class Action(val name: String, val condition: Perceivable<Boolean>, data class Action(val name: String, val condition: Perceivable<Boolean>,
val actors: Set<Party>, val arrangement: Arrangement) val actors: Set<Party>, val arrangement: Arrangement)

View File

@ -38,10 +38,11 @@ data class Const<T>(val value: T) : Perceivable<T> {
return false return false
} }
override fun hashCode(): Int { override fun hashCode(): Int =
val h = value!!.hashCode() if (value is BigDecimal)
return h value.toDouble().hashCode()
} else
value!!.hashCode()
} }
fun<T> const(k: T) = Const(k) fun<T> const(k: T) = Const(k)

View File

@ -17,13 +17,14 @@ fun swap(partyA: Party, amountA: BigDecimal, currencyA: Currency, partyB: Party,
fun fx_swap(expiry: String, notional: BigDecimal, strike: BigDecimal, fun fx_swap(expiry: String, notional: BigDecimal, strike: BigDecimal,
foreignCurrency: Currency, domesticCurrency: Currency, foreignCurrency: Currency, domesticCurrency: Currency,
partyA: Party, partyB: Party) = arrange { partyA: Party, partyB: Party) = arrange {
actions {
(partyA or partyB).may { (partyA or partyB).may {
"execute".givenThat(after(expiry)) { "execute".givenThat(after(expiry)) {
swap(partyA, notional * strike, domesticCurrency, partyB, notional, foreignCurrency) swap(partyA, notional * strike, domesticCurrency, partyB, notional, foreignCurrency)
} }
} }
} }
}
// building an fx swap using abstract swap // building an fx swap using abstract swap
fun fx_swap2(expiry: String, notional: Long, strike: Double, fun fx_swap2(expiry: String, notional: Long, strike: Double,

View File

@ -22,8 +22,44 @@ val Double.bd: BigDecimal get() = BigDecimal(this)
val zero = Zero() val zero = Zero()
class ActionsBuilder {
private var actions = mutableSetOf<Action>()
fun final() =
if (actions.isEmpty())
zero
else
Actions(actions.toSet())
fun Party.may(init: ActionBuilder.() -> Action): Action {
val builder = ActionBuilder(setOf(this))
builder.init()
actions.addAll( builder.actions )
return builder.actions.first()
}
fun Set<Party>.may(init: ActionBuilder.() -> Action): Action {
val builder = ActionBuilder(this)
builder.init()
actions.addAll( builder.actions )
return builder.actions.first()
}
infix fun Party.or(party: Party) = setOf(this, party)
infix fun Set<Party>.or(party: Party) = this.plus(party)
}
open class ContractBuilder { open class ContractBuilder {
val contracts = mutableListOf<Arrangement>() private val contracts = mutableListOf<Arrangement>()
fun actions(init: ActionsBuilder.() -> Action ) : Arrangement {
val b = ActionsBuilder()
b.init()
val c = b.final()
contracts.add(c)
return c
}
fun Party.gives(beneficiary: Party, amount: BigDecimal, currency: Currency): Transfer { fun Party.gives(beneficiary: Party, amount: BigDecimal, currency: Currency): Transfer {
val c = Transfer(const(amount), currency, this, beneficiary) val c = Transfer(const(amount), currency, this, beneficiary)
@ -37,25 +73,6 @@ open class ContractBuilder {
return c return c
} }
fun Party.may(init: ActionBuilder.() -> Unit): Actions {
val b = ActionBuilder(setOf(this))
b.init()
val c = Actions(b.actions.toSet())
contracts.add(c)
return c
}
fun Set<Party>.may(init: ActionBuilder.() -> Unit): Actions {
val b = ActionBuilder(this)
b.init()
val c = Actions(b.actions.toSet())
contracts.add(c)
return c
}
infix fun Party.or(party: Party) = setOf(this, party)
infix fun Set<Party>.or(party: Party) = this.plus(party)
@Deprecated(level = DeprecationLevel.ERROR, message = "Not allowed") @Deprecated(level = DeprecationLevel.ERROR, message = "Not allowed")
fun Action(@Suppress("UNUSED_PARAMETER") name: String, @Suppress("UNUSED_PARAMETER") condition: Perceivable<Boolean>, fun Action(@Suppress("UNUSED_PARAMETER") name: String, @Suppress("UNUSED_PARAMETER") condition: Perceivable<Boolean>,
@Suppress("UNUSED_PARAMETER") actors: Set<Party>, @Suppress("UNUSED_PARAMETER") arrangement: Arrangement) { @Suppress("UNUSED_PARAMETER") actors: Set<Party>, @Suppress("UNUSED_PARAMETER") arrangement: Arrangement) {
@ -77,7 +94,7 @@ open class ContractBuilder {
contracts.add( Transfer(amount, currency, this, beneficiary)) contracts.add( Transfer(amount, currency, this, beneficiary))
}*/ }*/
infix fun Arrangement.and(arrangement: Arrangement) = And(setOf(this, arrangement)) // infix fun Arrangement.and(arrangement: Arrangement) = And(setOf(this, arrangement))
val start = StartDate() val start = StartDate()
val end = EndDate() val end = EndDate()
@ -130,10 +147,12 @@ interface GivenThatResolve {
class ActionBuilder(val actors: Set<Party>) { class ActionBuilder(val actors: Set<Party>) {
val actions = mutableListOf<Action>() val actions = mutableListOf<Action>()
fun String.givenThat(condition: Perceivable<Boolean>, init: ContractBuilder.() -> Arrangement ) { fun String.givenThat(condition: Perceivable<Boolean>, init: ContractBuilder.() -> Arrangement ) : Action {
val b = ContractBuilder() val b = ContractBuilder()
b.init() b.init()
actions.add( Action(this, condition, actors, b.final() ) ) val a = Action(this, condition, actors, b.final() )
actions.add( a )
return a
} }
fun String.givenThat(condition: Perceivable<Boolean> ) : GivenThatResolve { fun String.givenThat(condition: Perceivable<Boolean> ) : GivenThatResolve {
@ -145,10 +164,12 @@ class ActionBuilder(val actors: Set<Party>) {
} }
} }
fun String.anytime(init: ContractBuilder.() -> Unit ) { fun String.anytime(init: ContractBuilder.() -> Unit ) : Action {
val b = ContractBuilder() val b = ContractBuilder()
b.init() b.init()
actions.add( Action(this, const(true), actors, b.final() ) ) val a = Action(this, const(true), actors, b.final() )
actions.add( a )
return a
} }
} }

View File

@ -1,5 +1,6 @@
package com.r3corda.contracts.universal package com.r3corda.contracts.universal
import com.r3corda.core.contracts.BusinessCalendar
import com.r3corda.core.contracts.FixOf import com.r3corda.core.contracts.FixOf
import com.r3corda.core.contracts.Frequency import com.r3corda.core.contracts.Frequency
import com.r3corda.core.contracts.Tenor import com.r3corda.core.contracts.Tenor
@ -23,12 +24,13 @@ class Cap {
val tradeDate: LocalDate = LocalDate.of(2016, 9, 1) val tradeDate: LocalDate = LocalDate.of(2016, 9, 1)
val contract = arrange { val contract = arrange {
rollOut("2016-09-01".ld, "2017-04-01".ld, Frequency.Quarterly) { rollOut("2016-09-01".ld, "2017-09-01".ld, Frequency.Quarterly) {
actions {
(acmeCorp or highStreetBank).may { (acmeCorp or highStreetBank).may {
"exercise".anytime { "exercise".anytime {
val floating = interest(notional, "act/365", fix("LIBOR", start, Tenor("3M")), start, end) val floating = interest(notional, "act/365", fix("LIBOR", start, Tenor("3M")), start, end)
val fixed = interest(notional, "act/365", 0.5.bd, start, end) val fixed = interest(notional, "act/365", 0.5.bd, start, end)
highStreetBank.gives(acmeCorp, (floating - fixed).plus(), currency) highStreetBank.gives(acmeCorp, floating - fixed, currency)
next() next()
} }
} }
@ -39,21 +41,22 @@ class Cap {
} }
} }
} }
}
val contractFixed = arrange { val contractFixed = arrange {
actions {
(acmeCorp or highStreetBank).may { (acmeCorp or highStreetBank).may {
"exercise".anytime() { "exercise".anytime() {
val floating1 = interest(notional, "act/365", 1.0.bd, "2016-09-01", "2016-12-01") val floating1 = interest(notional, "act/365", 1.0.bd, "2016-09-01", "2016-12-01")
val fixed1 = interest(notional, "act/365", 0.5.bd, "2016-09-01", "2016-12-01") val fixed1 = interest(notional, "act/365", 0.5.bd, "2016-09-01", "2016-12-01")
highStreetBank.gives(acmeCorp, (floating1 - fixed1).plus(), currency) highStreetBank.gives(acmeCorp, floating1 - fixed1, currency)
rollOut("2016-12-01".ld, "2017-04-01".ld, Frequency.Quarterly) { rollOut("2016-12-01".ld, "2017-09-01".ld, Frequency.Quarterly) {
actions {
(acmeCorp or highStreetBank).may { (acmeCorp or highStreetBank).may {
"exercise".anytime { "exercise".anytime {
val floating = interest(notional, "act/365", fix("LIBOR", start, Tenor("3M")), start, end) val floating = interest(notional, "act/365", fix("LIBOR", start, Tenor("3M")), start, end)
val fixed = interest(notional, "act/365", 0.5.bd, start, end) val fixed = interest(notional, "act/365", 0.5.bd, start, end)
highStreetBank.gives(acmeCorp, (floating - fixed).plus(), currency) highStreetBank.gives(acmeCorp, floating - fixed, currency)
next() next()
} }
} }
@ -65,14 +68,16 @@ class Cap {
} }
} }
} }
}
acmeCorp.may { acmeCorp.may {
"skip".anytime { "skip".anytime {
rollOut("2016-12-01".ld, "2017-04-01".ld, Frequency.Quarterly) { rollOut("2016-12-01".ld, "2017-09-01".ld, Frequency.Quarterly) {
actions {
(acmeCorp or highStreetBank).may { (acmeCorp or highStreetBank).may {
"exercise".anytime { "exercise".anytime {
val floating = interest(notional, "act/365", fix("LIBOR", start, Tenor("3M")), start, end) val floating = interest(notional, "act/365", fix("LIBOR", start, Tenor("3M")), start, end)
val fixed = interest(notional, "act/365", 0.5.bd, start, end) val fixed = interest(notional, "act/365", 0.5.bd, start, end)
highStreetBank.gives(acmeCorp, (floating - fixed).plus(), currency) highStreetBank.gives(acmeCorp, floating - fixed, currency)
next() next()
} }
} }
@ -85,15 +90,95 @@ class Cap {
} }
} }
} }
}
}
val contractFixed2 = arrange {
actions {
(acmeCorp or highStreetBank).may {
"exercise".anytime() {
val floating1 = interest(notional, "act/365", 1.0.bd, "2016-12-01", "2017-03-01")
val fixed1 = interest(notional, "act/365", 0.5.bd, "2016-12-01", "2017-03-01")
highStreetBank.gives(acmeCorp, floating1 - fixed1, currency)
rollOut("2017-03-01".ld, "2017-09-01".ld, Frequency.Quarterly) {
actions {
(acmeCorp or highStreetBank).may {
"exercise".anytime {
val floating = interest(notional, "act/365", fix("LIBOR", start, Tenor("3M")), start, end)
val fixed = interest(notional, "act/365", 0.5.bd, start, end)
highStreetBank.gives(acmeCorp, floating - fixed, currency)
next()
}
}
acmeCorp.may {
"skip".anytime {
next()
}
}
}
}
}
}
acmeCorp.may {
"skip".anytime {
rollOut("2017-03-01".ld, "2017-09-01".ld, Frequency.Quarterly) {
actions {
(acmeCorp or highStreetBank).may {
"exercise".anytime {
val floating = interest(notional, "act/365", fix("LIBOR", start, Tenor("3M")), start, end)
val fixed = interest(notional, "act/365", 0.5.bd, start, end)
highStreetBank.gives(acmeCorp, floating - fixed, currency)
next()
}
}
acmeCorp.may {
"skip".anytime {
next()
}
}
}
}
}
}
}
}
val contractAfterExecute = arrange {
rollOut("2016-12-01".ld, "2017-09-01".ld, Frequency.Quarterly) {
actions {
(acmeCorp or highStreetBank).may {
"exercise".anytime {
val floating = interest(notional, "act/365", fix("LIBOR", start, Tenor("3M")), start, end)
val fixed = interest(notional, "act/365", 0.5.bd, start, end)
highStreetBank.gives(acmeCorp, floating - fixed, currency)
next()
}
}
acmeCorp.may {
"skip".anytime {
next()
}
}
}
}
}
val paymentFirst = arrange { highStreetBank.gives(acmeCorp, 250.K, EUR) }
val stateStart = UniversalContract.State(listOf(DUMMY_NOTARY.owningKey), contract) val stateStart = UniversalContract.State(listOf(DUMMY_NOTARY.owningKey), contract)
val stateFixed = UniversalContract.State( listOf(DUMMY_NOTARY.owningKey), contractFixed) val stateFixed = UniversalContract.State( listOf(DUMMY_NOTARY.owningKey), contractFixed)
val stateAfterExecute = UniversalContract.State( listOf(DUMMY_NOTARY.owningKey), contractAfterExecute)
val statePaymentFirst = UniversalContract.State( listOf(DUMMY_NOTARY.owningKey), paymentFirst)
val stateFixed2 = UniversalContract.State( listOf(DUMMY_NOTARY.owningKey), contractFixed2)
val contractLimitedCap = arrange { val contractLimitedCap = arrange {
rollOut("2016-04-01".ld, "2017-04-01".ld, Frequency.Quarterly, object { rollOut("2016-04-01".ld, "2017-04-01".ld, Frequency.Quarterly, object {
val limit = variable(150.K) val limit = variable(150.K)
}) { }) {
actions {
(acmeCorp or highStreetBank).may { (acmeCorp or highStreetBank).may {
"exercise".anytime { "exercise".anytime {
val floating = interest(notional, "act/365", fix("LIBOR", start, Tenor("3M")), start, end) val floating = interest(notional, "act/365", fix("LIBOR", start, Tenor("3M")), start, end)
@ -110,6 +195,7 @@ class Cap {
} }
} }
} }
}
@Test @Test
fun issue() { fun issue() {
@ -131,12 +217,7 @@ class Cap {
} }
@Test @Test
fun `print debugging`() { fun `first fixing`() {
// debugprint(contract)
}
@Test
fun `fixing`() {
transaction { transaction {
input { stateStart } input { stateStart }
output { stateFixed } output { stateFixed }
@ -180,4 +261,68 @@ class Cap {
} }
} }
@Test
fun `first execute`() {
transaction {
input { stateFixed }
output { stateAfterExecute }
output { statePaymentFirst }
timestamp(TEST_TX_TIME_1)
tweak {
command(highStreetBank.owningKey) { UniversalContract.Commands.Action("some undefined name") }
this `fails with` "action must be defined"
}
command(highStreetBank.owningKey) { UniversalContract.Commands.Action("exercise") }
this.verifies()
}
}
@Test
fun `second fixing`() {
transaction {
input { stateAfterExecute }
output { stateFixed2 }
timestamp(TEST_TX_TIME_1)
tweak {
command(highStreetBank.owningKey) { UniversalContract.Commands.Action("some undefined name") }
this `fails with` "action must be defined"
}
tweak {
// wrong source
command(highStreetBank.owningKey) { UniversalContract.Commands.Fix(listOf(com.r3corda.core.contracts.Fix(FixOf("LIBORx", BusinessCalendar.parseDateFromString("2016-12-01"), Tenor("3M")), 1.0.bd))) }
this `fails with` "relevant fixing must be included"
}
tweak {
// wrong date
command(highStreetBank.owningKey) { UniversalContract.Commands.Fix(listOf(com.r3corda.core.contracts.Fix(FixOf("LIBOR", BusinessCalendar.parseDateFromString("2016-12-01").plusYears(1), Tenor("3M")), 1.0.bd))) }
this `fails with` "relevant fixing must be included"
}
tweak {
// wrong tenor
command(highStreetBank.owningKey) { UniversalContract.Commands.Fix(listOf(com.r3corda.core.contracts.Fix(FixOf("LIBOR", BusinessCalendar.parseDateFromString("2016-12-01"), Tenor("9M")), 1.0.bd))) }
this `fails with` "relevant fixing must be included"
}
tweak {
command(highStreetBank.owningKey) { UniversalContract.Commands.Fix(listOf(com.r3corda.core.contracts.Fix(FixOf("LIBOR", BusinessCalendar.parseDateFromString("2016-12-01"), Tenor("3M")), 1.5.bd))) }
this `fails with` "output state does not reflect fix command"
}
command(highStreetBank.owningKey) { UniversalContract.Commands.Fix(listOf(com.r3corda.core.contracts.Fix(FixOf("LIBOR", BusinessCalendar.parseDateFromString("2016-12-01"), Tenor("3M")), 1.0.bd))) }
this.verifies()
}
}
} }

View File

@ -24,6 +24,7 @@ class Caplet {
val currency = EUR val currency = EUR
val contract = arrange { val contract = arrange {
actions {
(acmeCorp or highStreetBank).may { (acmeCorp or highStreetBank).may {
"exercise".anytime() { "exercise".anytime() {
val floating = interest(notional, "act/365", fix("LIBOR", tradeDate, Tenor("6M")), "2016-04-01", "2016-10-01") val floating = interest(notional, "act/365", fix("LIBOR", tradeDate, Tenor("6M")), "2016-04-01", "2016-10-01")
@ -32,8 +33,10 @@ class Caplet {
} }
} }
} }
}
val contractFixed = arrange { val contractFixed = arrange {
actions {
(acmeCorp or highStreetBank).may { (acmeCorp or highStreetBank).may {
"exercise".anytime() { "exercise".anytime() {
val floating = interest(notional, "act/365", 1.0.bd, "2016-04-01", "2016-10-01") val floating = interest(notional, "act/365", 1.0.bd, "2016-04-01", "2016-10-01")
@ -42,6 +45,7 @@ class Caplet {
} }
} }
} }
}
val contractFinal = arrange { highStreetBank.gives(acmeCorp, 250.K, EUR) } val contractFinal = arrange { highStreetBank.gives(acmeCorp, 250.K, EUR) }

View File

@ -28,6 +28,7 @@ class ContractDefinition {
val cds_contract = arrange { val cds_contract = arrange {
actions {
acmeCorp.may { acmeCorp.may {
"payout".givenThat(acmeCorporationHasDefaulted and before("2017-09-01")) { "payout".givenThat(acmeCorporationHasDefaulted and before("2017-09-01")) {
highStreetBank.gives(acmeCorp, 1.M, USD) highStreetBank.gives(acmeCorp, 1.M, USD)
@ -39,9 +40,11 @@ class ContractDefinition {
} }
} }
} }
}
val american_fx_option = arrange { val american_fx_option = arrange {
actions {
acmeCorp.may { acmeCorp.may {
"exercise".anytime { "exercise".anytime {
highStreetBank.gives(acmeCorp, 1.M, EUR) highStreetBank.gives(acmeCorp, 1.M, EUR)
@ -54,9 +57,11 @@ class ContractDefinition {
} }
} }
} }
}
val european_fx_option = arrange { val european_fx_option = arrange {
actions {
acmeCorp.may { acmeCorp.may {
"exercise".anytime { "exercise".anytime {
(acmeCorp or highStreetBank).may { (acmeCorp or highStreetBank).may {
@ -73,6 +78,7 @@ class ContractDefinition {
} }
} }
} }
}
@Test @Test

View File

@ -15,6 +15,7 @@ class FXSwap {
val TEST_TX_TIME_TOO_EARLY: Instant get() = Instant.parse("2017-08-31T12:00:00.00Z") val TEST_TX_TIME_TOO_EARLY: Instant get() = Instant.parse("2017-08-31T12:00:00.00Z")
val contract = arrange { val contract = arrange {
actions {
(acmeCorp or highStreetBank).may { (acmeCorp or highStreetBank).may {
"execute".givenThat(after("2017-09-01")) { "execute".givenThat(after("2017-09-01")) {
highStreetBank.gives(acmeCorp, 1200.K, USD) highStreetBank.gives(acmeCorp, 1200.K, USD)
@ -22,6 +23,7 @@ class FXSwap {
} }
} }
} }
}
val transfer1 = arrange { highStreetBank.gives(acmeCorp, 1200.K, USD) } val transfer1 = arrange { highStreetBank.gives(acmeCorp, 1200.K, USD) }
val transfer2 = arrange { acmeCorp.gives(highStreetBank, 1.M, EUR) } val transfer2 = arrange { acmeCorp.gives(highStreetBank, 1.M, EUR) }

View File

@ -23,6 +23,7 @@ class IRS {
val contract = arrange { val contract = arrange {
rollOut("2016-09-01".ld, "2018-09-01".ld, Frequency.Quarterly) { rollOut("2016-09-01".ld, "2018-09-01".ld, Frequency.Quarterly) {
actions {
(acmeCorp or highStreetBank).may { (acmeCorp or highStreetBank).may {
val floating = interest(notional, "act/365", fix("LIBOR", start, Tenor("3M")), start, end) val floating = interest(notional, "act/365", fix("LIBOR", start, Tenor("3M")), start, end)
val fixed = interest(notional, "act/365", 0.5.bd, start, end) val fixed = interest(notional, "act/365", 0.5.bd, start, end)
@ -38,6 +39,7 @@ class IRS {
} }
} }
} }
}
val stateStart = UniversalContract.State( listOf(DUMMY_NOTARY.owningKey), contract) val stateStart = UniversalContract.State( listOf(DUMMY_NOTARY.owningKey), contract)

View File

@ -18,6 +18,7 @@ class RollOutTests {
val contract = arrange { val contract = arrange {
rollOut("2016-09-01".ld, "2017-09-01".ld, Frequency.Monthly) { rollOut("2016-09-01".ld, "2017-09-01".ld, Frequency.Monthly) {
actions {
(acmeCorp or highStreetBank).may { (acmeCorp or highStreetBank).may {
"transfer".givenThat(after(end)) { "transfer".givenThat(after(end)) {
highStreetBank.gives(acmeCorp, 10.K, USD) highStreetBank.gives(acmeCorp, 10.K, USD)
@ -26,9 +27,12 @@ class RollOutTests {
} }
} }
} }
}
val contract2 = arrange { val contract2 = arrange {
rollOut("2016-09-01".ld, "2017-09-01".ld, Frequency.Monthly) { rollOut("2016-09-01".ld, "2017-09-01".ld, Frequency.Monthly) {
actions {
(acmeCorp or highStreetBank).may { (acmeCorp or highStreetBank).may {
"transfer".givenThat(after(end)) { "transfer".givenThat(after(end)) {
highStreetBank.gives(acmeCorp, 10.K, USD) highStreetBank.gives(acmeCorp, 10.K, USD)
@ -37,10 +41,12 @@ class RollOutTests {
} }
} }
} }
}
val stateStart = UniversalContract.State(listOf(DUMMY_NOTARY.owningKey), contract) val stateStart = UniversalContract.State(listOf(DUMMY_NOTARY.owningKey), contract)
val contractStep1a = arrange { val contractStep1a = arrange {
rollOut("2016-10-03".ld, "2017-09-01".ld, Frequency.Monthly) { rollOut("2016-10-03".ld, "2017-09-01".ld, Frequency.Monthly) {
actions {
(acmeCorp or highStreetBank).may { (acmeCorp or highStreetBank).may {
"transfer".givenThat(after(end)) { "transfer".givenThat(after(end)) {
highStreetBank.gives(acmeCorp, 10.K, USD) highStreetBank.gives(acmeCorp, 10.K, USD)
@ -49,6 +55,7 @@ class RollOutTests {
} }
} }
} }
}
val contractStep1b = arrange { val contractStep1b = arrange {
highStreetBank.gives(acmeCorp, 10.K, USD) highStreetBank.gives(acmeCorp, 10.K, USD)
@ -64,43 +71,56 @@ class RollOutTests {
highStreetBank.gives(acmeCorp, 10.K, USD) highStreetBank.gives(acmeCorp, 10.K, USD)
} }
val contract_action1 = arrange { val contract_action1 = arrange {
actions {
highStreetBank.may { highStreetBank.may {
"do it".anytime { "do it".anytime {
highStreetBank.gives(acmeCorp, 10.K, USD) highStreetBank.gives(acmeCorp, 10.K, USD)
} }
} }
} }
}
val contract_action2 = arrange { val contract_action2 = arrange {
actions {
highStreetBank.may { highStreetBank.may {
"do it".anytime { "do it".anytime {
highStreetBank.gives(acmeCorp, 10.K, USD) highStreetBank.gives(acmeCorp, 10.K, USD)
} }
} }
} }
}
val contract_and1 = arrange { val contract_and1 = arrange {
actions {
highStreetBank.may { highStreetBank.may {
"do it".anytime { "do it".anytime {
highStreetBank.gives(acmeCorp, 10.K, USD) highStreetBank.gives(acmeCorp, 10.K, USD)
} }
} }
}
actions {
acmeCorp.may { acmeCorp.may {
"do it".anytime { "do it".anytime {
acmeCorp.gives(momAndPop, 10.K, USD) acmeCorp.gives(momAndPop, 10.K, USD)
} }
} }
}
next() next()
} }
val contract_and2 = arrange { val contract_and2 = arrange {
actions {
highStreetBank.may { highStreetBank.may {
"do it".anytime { "do it".anytime {
highStreetBank.gives(acmeCorp, 10.K, USD) highStreetBank.gives(acmeCorp, 10.K, USD)
} }
} }
}
actions {
acmeCorp.may { acmeCorp.may {
"do it".anytime { "do it".anytime {
acmeCorp.gives(momAndPop, 10.K, USD) acmeCorp.gives(momAndPop, 10.K, USD)
} }
} }
}
next() next()
} }

View File

@ -17,6 +17,7 @@ class Swaption {
val coupon = 1.5.bd val coupon = 1.5.bd
val dreary_contract = arrange { val dreary_contract = arrange {
actions {
(highStreetBank or acmeCorp).may { (highStreetBank or acmeCorp).may {
"proceed".givenThat(after("01/07/2015")) { "proceed".givenThat(after("01/07/2015")) {
highStreetBank.gives(acmeCorp, libor(notional, "01/04/2015", "01/07/2015"), currency) highStreetBank.gives(acmeCorp, libor(notional, "01/04/2015", "01/07/2015"), currency)
@ -26,11 +27,15 @@ class Swaption {
highStreetBank.gives(acmeCorp, libor(notional, "01/07/2015", "01/10/2015"), currency) highStreetBank.gives(acmeCorp, libor(notional, "01/07/2015", "01/10/2015"), currency)
acmeCorp.gives(highStreetBank, interest(notional, "act/365", coupon, "01/07/2015", "01/10/2015"), currency) acmeCorp.gives(highStreetBank, interest(notional, "act/365", coupon, "01/07/2015", "01/10/2015"), currency)
actions {
(highStreetBank or acmeCorp).may { (highStreetBank or acmeCorp).may {
"dummy".anytime { zero }
// etc ... // etc ...
} }
} }
} }
}
actions {
acmeCorp.may { acmeCorp.may {
"cancel".anytime { "cancel".anytime {
acmeCorp.gives(highStreetBank, 10.K, USD) acmeCorp.gives(highStreetBank, 10.K, USD)
@ -38,16 +43,19 @@ class Swaption {
} }
} }
} }
}
acmeCorp.may { acmeCorp.may {
"cancel".anytime { "cancel".anytime {
acmeCorp.gives(highStreetBank, 10.K, USD) acmeCorp.gives(highStreetBank, 10.K, USD)
} }
} }
} }
}
val elegant_contract = arrange { val elegant_contract = arrange {
rollOut("01/04/2015".ld, "01/04/2025".ld, Frequency.Quarterly) { rollOut("01/04/2015".ld, "01/04/2025".ld, Frequency.Quarterly) {
actions {
(highStreetBank or acmeCorp).may { (highStreetBank or acmeCorp).may {
"proceed".givenThat(after(start)) { "proceed".givenThat(after(start)) {
highStreetBank.gives(acmeCorp, libor(notional, start, end), currency) highStreetBank.gives(acmeCorp, libor(notional, start, end), currency)
@ -62,6 +70,7 @@ class Swaption {
} }
} }
} }
}
val strike = 1.2 val strike = 1.2
@ -69,10 +78,12 @@ class Swaption {
rollOut("01/04/2015".ld, "01/04/2016".ld, Frequency.Quarterly, object { rollOut("01/04/2015".ld, "01/04/2016".ld, Frequency.Quarterly, object {
val cap = variable(150.K) val cap = variable(150.K)
}) { }) {
actions {
acmeCorp.may { acmeCorp.may {
"exercise".givenThat(before(end)) { "exercise".givenThat(before(end)) {
val payout = (EUR / USD - strike).plus() * notional val payout = (EUR / USD - strike).plus() * notional
actions {
(acmeCorp or highStreetBank).may { (acmeCorp or highStreetBank).may {
"proceed".givenThat(after(end)) { "proceed".givenThat(after(end)) {
highStreetBank.gives(acmeCorp, payout, USD) highStreetBank.gives(acmeCorp, payout, USD)
@ -81,6 +92,7 @@ class Swaption {
} }
} }
} }
}
(acmeCorp or highStreetBank).may { (acmeCorp or highStreetBank).may {
"proceedWithoutExercise".givenThat(after(end)) { "proceedWithoutExercise".givenThat(after(end)) {
next() next()
@ -88,15 +100,18 @@ class Swaption {
} }
} }
} }
}
val tarf2 = arrange { val tarf2 = arrange {
rollOut("01/04/2015".ld, "01/04/2016".ld, Frequency.Quarterly, object { rollOut("01/04/2015".ld, "01/04/2016".ld, Frequency.Quarterly, object {
val uses = variable(4) val uses = variable(4)
}) { }) {
actions {
acmeCorp.may { acmeCorp.may {
"exercise".givenThat(before(end)) { "exercise".givenThat(before(end)) {
val payout = (EUR / USD - strike).plus() * notional val payout = (EUR / USD - strike).plus() * notional
actions {
(acmeCorp or highStreetBank).may { (acmeCorp or highStreetBank).may {
"proceed".givenThat(after(end)) { "proceed".givenThat(after(end)) {
highStreetBank.gives(acmeCorp, payout, currency) highStreetBank.gives(acmeCorp, payout, currency)
@ -105,6 +120,7 @@ class Swaption {
} }
} }
} }
}
(acmeCorp or highStreetBank).may { (acmeCorp or highStreetBank).may {
"proceedWithoutExercise".givenThat(after(end)) { "proceedWithoutExercise".givenThat(after(end)) {
next() next()
@ -113,3 +129,4 @@ class Swaption {
} }
} }
} }
}

View File

@ -12,21 +12,24 @@ import java.time.Instant
class ZeroCouponBond { class ZeroCouponBond {
val contract = arrange { val contract = arrange {
actions {
(acmeCorp or highStreetBank).may { (acmeCorp or highStreetBank).may {
"execute".givenThat(after("2017-09-01")) { "execute".givenThat(after("2017-09-01")) {
highStreetBank.gives(acmeCorp, 100.K, GBP) highStreetBank.gives(acmeCorp, 100.K, GBP)
} }
} }
} }
}
val contractMove = arrange { val contractMove = arrange {
actions {
(momAndPop or highStreetBank).may { (momAndPop or highStreetBank).may {
"execute".givenThat(after("2017-09-01")) { "execute".givenThat(after("2017-09-01")) {
highStreetBank.gives(momAndPop, 100.K, GBP) highStreetBank.gives(momAndPop, 100.K, GBP)
} }
} }
} }
}
val TEST_TX_TIME_1: Instant get() = Instant.parse("2017-09-02T12:00:00.00Z") val TEST_TX_TIME_1: Instant get() = Instant.parse("2017-09-02T12:00:00.00Z")

View File

@ -14,16 +14,19 @@ import java.util.*
// various example arrangements using basic syntax // various example arrangements using basic syntax
val cds_contract = arrange { val cds_contract = arrange {
actions {
acmeCorp.may { acmeCorp.may {
"claim".givenThat(acmeCorporationHasDefaulted and before("2017-09-01")) { "claim".givenThat(acmeCorporationHasDefaulted and before("2017-09-01")) {
highStreetBank.gives(acmeCorp, 1.M, USD) highStreetBank.gives(acmeCorp, 1.M, USD)
} }
} }
} }
}
// fx swap // fx swap
// both parties have the right to trigger the exchange of cash flows // both parties have the right to trigger the exchange of cash flows
val an_fx_swap = arrange { val an_fx_swap = arrange {
actions {
(acmeCorp or highStreetBank).may { (acmeCorp or highStreetBank).may {
"execute".givenThat(after("2017-09-01")) { "execute".givenThat(after("2017-09-01")) {
highStreetBank.gives(acmeCorp, 1200.K, USD) highStreetBank.gives(acmeCorp, 1200.K, USD)
@ -31,8 +34,10 @@ val an_fx_swap = arrange {
} }
} }
} }
}
val american_fx_option = arrange { val american_fx_option = arrange {
actions {
acmeCorp.may { acmeCorp.may {
"exercise".givenThat(before("2017-09-01")) { "exercise".givenThat(before("2017-09-01")) {
highStreetBank.gives(acmeCorp, 1200.K, USD) highStreetBank.gives(acmeCorp, 1200.K, USD)
@ -40,8 +45,10 @@ val american_fx_option = arrange {
} }
} }
} }
}
val european_fx_option = arrange { val european_fx_option = arrange {
actions {
acmeCorp.may { acmeCorp.may {
"exercise".givenThat(before("2017-09-01")) { "exercise".givenThat(before("2017-09-01")) {
fx_swap("2017-09-01", 1.M, 1.2.bd, EUR, USD, acmeCorp, highStreetBank) fx_swap("2017-09-01", 1.M, 1.2.bd, EUR, USD, acmeCorp, highStreetBank)
@ -53,23 +60,28 @@ val european_fx_option = arrange {
} }
} }
} }
}
val contractZeroCouponBond = arrange { val contractZeroCouponBond = arrange {
actions {
acmeCorp.may { acmeCorp.may {
"execute".givenThat(after("2017-11-01")) { "execute".givenThat(after("2017-11-01")) {
highStreetBank.gives(acmeCorp, 1.M, USD) highStreetBank.gives(acmeCorp, 1.M, USD)
} }
} }
} }
}
// maybe in the presence of negative interest rates you would want other side of contract to be able to take initiative as well // maybe in the presence of negative interest rates you would want other side of contract to be able to take initiative as well
val zero_coupon_bond_2 = arrange { val zero_coupon_bond_2 = arrange {
actions {
(acmeCorp or highStreetBank).may { (acmeCorp or highStreetBank).may {
"execute".givenThat(after("2017-09-01")) { "execute".givenThat(after("2017-09-01")) {
highStreetBank.gives(acmeCorp, 1.M, USD) highStreetBank.gives(acmeCorp, 1.M, USD)
} }
} }
} }
}
// no touch // no touch
// Party Receiver // Party Receiver
@ -81,17 +93,22 @@ val zero_coupon_bond_2 = arrange {
// Assume observable is using FX fixing // Assume observable is using FX fixing
// //
val no_touch = arrange { val no_touch = arrange {
actions {
(acmeCorp or highStreetBank).may { (acmeCorp or highStreetBank).may {
"execute".givenThat(after("2017-09-01")) { "execute".givenThat(after("2017-09-01")) {
highStreetBank.gives(acmeCorp, 1.M, USD) highStreetBank.gives(acmeCorp, 1.M, USD)
} }
} }
highStreetBank.may { highStreetBank.may {
"knock out".givenThat(EUR/USD gt 1.3) "knock out".givenThat(EUR / USD gt 1.3) {
zero
}
}
} }
} }
val one_touch = arrange { val one_touch = arrange {
actions {
highStreetBank.may { highStreetBank.may {
"expire".givenThat(after("2017-09-01")) { "expire".givenThat(after("2017-09-01")) {
zero zero
@ -103,3 +120,4 @@ val one_touch = arrange {
} }
} }
} }
}