# R/runExampleCalcs.R In SACCR: SA Counterparty Credit Risk under CRR2

#### Defines functions runExampleCalcs

```runExampleCalcs <-function(trades, csas, colls, simplified = FALSE, OEM = FALSE, ignore_margin = FALSE)
{
{
{
cat('All trades are sold options, EAD is zero')
return(0)
}
}

cpties = unique(unlist(lapply(trades,function(x) x\$Counterparty)))

if(length(cpties)>1)
{

for(cpty_counter in 1:length(cpties))
{

if(length(csas)!=0)
{

for(i in 1:(length(csas)+1))
{
if(i < (length(csas)+1))
{
if(cpties[cpty_counter] %in% unique(unlist(lapply(csas,function(x) x\$Counterparty))))
{
csa_currency = gsub("[']","",unlist(csas[[i]]\$Currency))

next

if(ignore_margin)
{     MF =1
}else {MF = csas[[i]]\$CalcMF(simplified = simplified)}
# calculating the add-on
trade_trees[[i]] = CalcAddon(trades_tree, MF, simplified = simplified, OEM = OEM)
} else
{

next

}
}
}
}else
{
}

{
# calculating the RC and the V-c amount
if(i>length(csas))
{
trade_trees[[i]]\$`Replacement Cost` <- CalcRC(trades_temp[[i]],simplified = simplified, ignore_margin = ignore_margin)
}else
{      trade_trees[[i]]\$`Replacement Cost` <- CalcRC(trades_temp[[i]], csas[[i]], colls,simplified = simplified, ignore_margin = ignore_margin)   }

# calculating the PFE after multiplying the addon with a factor if V-C<0

# calculating the Exposure-at-Default
trades_tree_unmmargined\$`Replacement Cost` <- CalcRC(trades_temp[[i]],simplified = simplified, ignore_margin = ignore_margin)

}
}

}else
{

if(length(csas)!=0)
{
for(i in 1:(length(csas)+1))
{
if(i < (length(csas)+1))
{
csa_currency = gsub("[']","",unlist(csas[[i]]\$Currency))

next

MF = csas[[i]]\$CalcMF(simplified = simplified)
# calculating the add-on
trade_trees[[i]] = CalcAddon(trades_tree, MF, simplified = simplified, OEM = OEM)
} else
{

next

}
}
} else
{
}

{
# calculating the RC and the V-c amount
if(i>length(csas))
{
trade_trees[[i]]\$`Replacement Cost` <- CalcRC(trades_temp[[i]],simplified = simplified, ignore_margin = ignore_margin)
}else
{      trade_trees[[i]]\$`Replacement Cost` <- CalcRC(trades_temp[[i]], csas[[i]], colls,simplified = simplified, ignore_margin = ignore_margin)   }

# calculating the PFE after multiplying the addon with a factor if V-C<0

# calculating the Exposure-at-Default

trades_tree_unmmargined\$`Replacement Cost` <- CalcRC(trades_temp[[i]],simplified = simplified, ignore_margin = ignore_margin)