AlgoTraderAlgoTrader Documentation

Chapter 3. Domain Model

3.1. Security Visitors
3.2. Data access objects (DAOs)
3.3. Services
3.3.1. Private Services
3.3.2. Order Services
3.3.3. Market Data Services
3.3.4. Historical Data Services
3.3.5. Reference Data Services

The Visitor Pattern is a way of separating an algorithm from an object structure on which it operates. Using this pattern it is possible to implement custom Logic per Entity without polluting the Entity code itself.

AlgoTrader provides the interface ch.algotrader.visitor.EntityVisitor which must be implemented by all Entity Visitors. Each Entity Visitor has two generic type parameters R and P. R is the return type (or java.lang.Void) returned by all visit methods and P is an arbitrary parameter object that can be added to the visit methods.

In addition there is the ch.algotrader.visitor.PolymorphicEntityVisitor which reflects the entire inheritance tree of all Securities. For example if there is no visitFuture method the PolymorphicEntityVisitor will automatically invoke the visitSecurity method.

The accept method of each Entity can be used to process an arbitrary Visitor like this:


In AlgoTrader there are two Visitors available which are used by the AlgoTrader Server


Is used to make sure certain Hibernate Entity References are initialized / loaded.


is used to scale quantities and prices


Is used to determine if a particular Security is supposed to report volumes


Used to validate a Tick by rules defined per Security

The AlgoTrader DAO framework of consists of several main components

It is possible to add custom DAOs to the platform. To accomplish this one needs to create a DAO interface extending either ReadOnlyDao or ReadWriteDao, add custom operations such as entity specific finders and then create a custom DAO class extending AbstractDao and implementing the custom DAO interface.

public class MyEntity implements BaseEntityI {

    private long id;
    private String name;
    public long getId() {
    protected void setId(final long id) { = id;
    public boolean isInitialized() {
        return true;
    public String getName() {
    public void setName(final String name) { = name;
public interface MyEntityDao extends ReadWriteDao<MyEntity> {

    public MyEntity findByName(String name);

public class MyEntityDaoImpl extends AbstractDao<MyEntity> implements MyEntityDao {
    public MyEntityDaoImpl(final SessionFactory sessionFactory) {
        super(MyEntity.class, sessionFactory);
    public Strategy findByName(final String name) {
        return findUniqueCaching(
            "from MyEntity where name = :name", QueryType.HQL, new NamedParam("name", name));

HQL and SQL queries used by AlgoTrader DAO components are externalized and stored in Hibernate.hbm.xml file. This allows for better management and for easier re-use of queries.

<query name='Strategy.findByName'>
    from StrategyImpl
    where name = :name

Queries can be accessed from DAO classes or custom components by their names

public class StrategyDaoImpl extends AbstractDao<Strategy> implements StrategyDao {

    public Strategy findByName(final String name) {
        return findUniqueCaching(
            "Strategy.findByName", QueryType.BY_NAME, new NamedParam("name", name));

Table 3.1. Private Services

AlgoOrderService Order Services responsible for handling of AlgoOrders (delegates to AlgoOrderExecServices)
AlgoOrderExecService Abstract Base Class for all Algo Execution Order Services
ExternalAccountService Abstract Base Class for all external Account Interfaces
ExternalMarketDataService Abstract Base Class for all external Market Data Interfaces
FixSessionService Exposes properties of FIX sessions
ForexService Responsible for the FX Hedging functionality
GenericOrderService Parent Class for all Order Services
MarketDataPersistenceService Responsible for persisting Market Data to the database
OrderExecutionService Responsible for handling of persistence and propagation various trading events such as order status update and order fills as well as maintaining order execution status in the order book.
OrderPersistenceService Responsible for persisting Orders and OrderStatus to the database
ReconciliationService Responsible for reconciliation of reports provided by the broker
ResetService Responsible for resetting the DB state (e.g. before the start of a simulation)
ServerLookupService Provides internal data lookup operations to other server side services
SimpleOrderService Order Service responsible for handling of Simple Orders (delegates to SimpleOrderExecServices)
SimpleOrderExecService Abstract Base Class for all Simple Order Execution Services
StrategyPersistenceService Handles persistence of Strategy Entities
TransactionPersistenceService Responsible for the persistence of Transactions, Positions updates and Cash Balance updates to the database
TransactionService Responsible for handling of incoming Fills