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:

entity.accept(MyVisitor.INSTANCE);

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

InitializingVisitor

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

ScalingVisitor

is used to scale quantities and prices

SecurityVolumeVisitor

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

TickValidationVisitor

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;
    @Override
    public long getId() {
        return this.id;
    }
    protected void setId(final long id) {
        this.id = id;
    }
    @Override
    public boolean isInitialized() {
        return true;
    }
    public String getName() {
        return this.name;
    }
    public void setName(final String name) {
        this.name = name;
    }
}
public interface MyEntityDao extends ReadWriteDao<MyEntity> {


    public MyEntity findByName(String name);
}
@Repository

public class MyEntityDaoImpl extends AbstractDao<MyEntity> implements MyEntityDao {
    public MyEntityDaoImpl(final SessionFactory sessionFactory) {
        super(MyEntity.class, sessionFactory);
    }
    @Override
    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'>
<![CDATA[
    from StrategyImpl
    where name = :name
]]>
</query>
            

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



public class StrategyDaoImpl extends AbstractDao<Strategy> implements StrategyDao {
    ...
    @Override
    public Strategy findByName(final String name) {
        return findUniqueCaching(
            "Strategy.findByName", QueryType.BY_NAME, new NamedParam("name", name));
    }
            

Table 3.1. Private Services

ServiceDescription
AlgoOrderServiceOrder Services responsible for handling of AlgoOrders (delegates to AlgoOrderExecServices)
AlgoOrderExecServiceAbstract Base Class for all Algo Execution Order Services
ExternalAccountServiceAbstract Base Class for all external Account Interfaces
ExternalMarketDataServiceAbstract Base Class for all external Market Data Interfaces
FixSessionServiceExposes properties of FIX sessions
ForexServiceResponsible for the FX Hedging functionality
GenericOrderServiceParent Class for all Order Services
MarketDataPersistenceServiceResponsible for persisting Market Data to the database
OrderExecutionServiceResponsible 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.
OrderPersistenceServiceResponsible for persisting Orders and OrderStatus to the database
ReconciliationServiceResponsible for reconciliation of reports provided by the broker
ResetServiceResponsible for resetting the DB state (e.g. before the start of a simulation)
ServerLookupServiceProvides internal data lookup operations to other server side services
SimpleOrderServiceOrder Service responsible for handling of Simple Orders (delegates to SimpleOrderExecServices)
SimpleOrderExecServiceAbstract Base Class for all Simple Order Execution Services
StrategyPersistenceServiceHandles persistence of Strategy Entities
TransactionPersistenceServiceResponsible for the persistence of Transactions, Positions updates and Cash Balance updates to the database
TransactionServiceResponsible for handling of incoming Fills