org.marketcetera.strategy
Class AbstractRunningStrategy

java.lang.Object
  extended by org.marketcetera.strategy.AbstractRunningStrategy
All Implemented Interfaces:
RunningStrategy
Direct Known Subclasses:
Strategy, Strategy

public abstract class AbstractRunningStrategy
extends Object
implements RunningStrategy

Base class for running strategies.

Since:
1.0.0
Version:
$Id: AbstractRunningStrategy.java 16154 2012-07-14 16:34:05Z colin $
Author:
Colin DuPlantis

Constructor Summary
AbstractRunningStrategy()
           
 
Method Summary
protected  void cancelAllDataRequests()
          Cancels all data requests from this strategy.
protected  int cancelAllOrders()
          Submits cancel requests for all OrderSingle open orders owned by the strategy's owner.
protected  void cancelDataFlow(DataFlowID inDataFlowID)
          Cancels a data flow identified by the supplied data flow ID.
protected  void cancelDataRequest(int inRequestID)
          Cancels the given data request.
protected  OrderCancel cancelOrder(OrderID inOrderID, boolean inSendOrder)
          Submits a request to cancel the OrderSingle with the given OrderID.
protected  OrderReplace cancelReplace(OrderID inOrderID, OrderSingle inNewOrder, boolean inSendOrder)
          Submits a cancel-replace order for the given OrderID with the given Order.
protected  DataFlowID createDataFlow(boolean inAppendDataSink, DataRequest... inRequests)
          Initiates a data flow request.
protected  void debug(String inMessage)
          Emits the given debug message to the strategy log output.
protected  void error(String inMessage)
          Emits the given error message to the strategy log output.
protected  Map<PositionKey<Future>,BigDecimal> getAllFuturePositionsAsOf(Date inDate)
          Gets all open Future positions at the given point in time.
protected  Map<PositionKey<Option>,BigDecimal> getAllOptionPositionsAsOf(Date inDate)
          Gets all open Option positions at the given point in time.
protected  Map<PositionKey<Equity>,BigDecimal> getAllPositionsAsOf(Date inDate)
          Gets all open Equity positions at the given point in time.
protected  BrokerStatus[] getBrokers()
          Returns the list of brokers known to the system.
protected  Deque<ReportBase> getExecutionReports(OrderID inOrderID)
          Gets the ReportBase values representing the order history of the given OrderID.
protected  BigDecimal getFuturePositionAsOf(Date inDate, String inUnderlyingSymbol, FutureExpirationMonth inExpirationMonth, int inExpirationYear)
          Gets the position in the given Future at the given point in time.
protected  Set<OrderID> getOpenOrderIDs()
          Returns the list of OrderID values for open orders created in this session in the order they were submitted.
protected  Collection<ExecutionReport> getOpenOrders()
          Gets the collection of open orders represented by the most recent ExecutionReport.
protected  BigDecimal getOptionPositionAsOf(Date inDate, String inOptionRoot, String inExpiry, BigDecimal inStrikePrice, OptionType inType)
          Gets the position in the given Option at the given point in time.
protected  Map<PositionKey<Option>,BigDecimal> getOptionPositionsAsOf(Date inDate, String... inOptionRoots)
          Gets open positions for the options specified by the given option roots at the given point in time.
protected  Collection<String> getOptionRoots(String inUnderlying)
          Gets the set of of known option roots for the given underlying symbol.
protected  OrderStatus getOrderStatus(OrderID inOrderID)
          Gets the OrderStatus for the given OrderID.
protected  String getParameter(String inName)
          Gets the parameter associated with the given name.
protected  BigDecimal getPositionAsOf(Date inDate, String inSymbol)
          Gets the position in the given Equity at the given point in time.
protected static String getProperty(String inKey)
          Gets the value associated with the given key.
protected  Date getReportHistoryOriginDate()
          Gets the report history origin date to use for the order history.
protected  Set<OrderID> getSubmittedOrderIDs()
          Returns the list of open order IDs created during this session in the order they were submitted.
protected  String getUnderlying(String inOptionRoot)
          Gets the underlying symbol for the given option root, if available.
protected  ModuleURN getURN()
          Gets the ModuleURN of this strategy.
protected  Properties getUserData()
          Gets the user data associated with the current user.
protected  void info(String inMessage)
          Emits the given info message to the strategy log output.
protected  void initializeReportHistoryManager()
          Initializes the report history manager.
protected  void requestCallbackAfter(long inDelay, Object inData)
          Requests a callback after a specified delay in milliseconds.
protected  void requestCallbackAt(Date inDate, Object inData)
          Requests a callback at a specific point in time.
protected  void requestCallbackEvery(long inDelay, long inPeriod, Object inData)
          Requests a callback periodically after a specified period in milliseconds.
protected  int requestCEPData(String[] inStatements, String inSource)
          Creates a complex event processor query.
protected  int requestMarketData(MarketDataRequest inRequest)
          Requests market data.
protected  int requestMarketData(String inRequest)
          Requests market data.
protected  int requestProcessedMarketData(MarketDataRequest inRequest, String[] inStatements, String inCepSource)
          Requests market data processed by the given complex event processor.
protected  int requestProcessedMarketData(String inRequest, String[] inStatements, String inCepSource)
          Requests market data processed by the given complex event processor from the given source.
protected  boolean send(Object inData)
          Sends an order to order subscribers.
protected  void sendEvent(Event inEvent)
          Sends the given event to the appropriate subscribers.
protected  void sendEventToCEP(Event inEvent, String inProvider)
          Sends the given event to the CEP module indicated by the provider.
protected  void sendMessage(Message inMessage, BrokerID inBroker)
          Sends a FIX message.
protected  void sendNotification(Notification inNotification)
          Sends the given notification to the appropriate subscribers.
protected static void setProperty(String inKey, String inValue)
          Sets the given key to the given value.
protected  void setUserData(Properties inUserData)
          Sets the user data associated with the current user.
protected  void suggestTrade(OrderSingle inOrder, BigDecimal inScore, String inIdentifier)
          Suggests a trade.
 String toString()
           
protected  void warn(String inMessage)
          Emits the given warn message to the strategy log output.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.marketcetera.strategy.RunningStrategy
onAsk, onBid, onCallback, onCancelReject, onDividend, onExecutionReport, onMarketstat, onOther, onStart, onStop, onTrade
 

Constructor Detail

AbstractRunningStrategy

public AbstractRunningStrategy()
Method Detail

toString

public final String toString()
Overrides:
toString in class Object

getSubmittedOrderIDs

protected final Set<OrderID> getSubmittedOrderIDs()
Returns the list of open order IDs created during this session in the order they were submitted.

Returns all order IDs regardless of their state.

Returns:
a Set<OrderID> value

getOpenOrderIDs

protected final Set<OrderID> getOpenOrderIDs()
Returns the list of OrderID values for open orders created in this session in the order they were submitted.

Returns IDs of open orders only. Orders that were canceled, replaced, filled, or otherwise are no longer open will not be returned. For orders submitted via cancelReplace(OrderID, OrderSingle, boolean), the ID of the OrderReplace value sent to the broker is returned, not the OrderSingle value used to create the OrderReplace.

Returns:
a Set<OrderID> value

getOpenOrders

protected final Collection<ExecutionReport> getOpenOrders()
Gets the collection of open orders represented by the most recent ExecutionReport.

Returns:
a Collection<ExecutionReport> value

getOrderStatus

protected final OrderStatus getOrderStatus(OrderID inOrderID)
Gets the OrderStatus for the given OrderID.

The given OrderID may be any part of the order chain. For example, if an order is replaced, either the original OrderID or the current OrderID will return the same value, although only the current OrderID is open.

Parameters:
inOrderID - an OrderID value or null if the given order cannot be found
Returns:
an OrderStatus value

setProperty

protected static void setProperty(String inKey,
                                  String inValue)
Sets the given key to the given value.

All running strategies have access to this properties store.

Parameters:
inKey - a String value
inValue - a String value

getProperty

protected static String getProperty(String inKey)
Gets the value associated with the given key.

All running strategies have access to this properties store.

Parameters:
inKey - a String value

getReportHistoryOriginDate

protected Date getReportHistoryOriginDate()
Gets the report history origin date to use for the order history.

Strategies may override this method to return a date. For performance reasons, it is best to use the most recent date possible. The default is to return the first second of the current day.

All strategies in the same strategy agent share the same order history manager. The report history origin date can be set only by the first strategy to run.

Returns:
a Date value

getParameter

protected final String getParameter(String inName)
Gets the parameter associated with the given name.

Parameters:
inName - a String value containing the key of a parameter key/value value
Returns:
a String value or null if no parameter is associated with the given name

requestMarketData

protected final int requestMarketData(MarketDataRequest inRequest)
Requests market data.

Parameters:
inRequest - a MarketDataRequest value containing the request to execute
Returns:
an int value containing the handle of the request or 0 if the request failed

requestMarketData

protected final int requestMarketData(String inRequest)
Requests market data.

Parameters:
inRequest - a String value containing the representation of a MarketDataRequest to execute
Returns:
an int value containing the handle of the request or 0 if the request failed

requestProcessedMarketData

protected final int requestProcessedMarketData(MarketDataRequest inRequest,
                                               String[] inStatements,
                                               String inCepSource)
Requests market data processed by the given complex event processor.

Parameters:
inRequest - a MarketDataRequest value containing the request to execute
inStatements - a String[] value containing the statements to pass to the complex event processor. The meaning of the statements varies according to the actual event processor that handles them.
inCepSource - a String value containing the name of the complex event processor to which to send the query request
Returns:
an int value containing the handle of the request or 0 if the request failed

requestProcessedMarketData

protected final int requestProcessedMarketData(String inRequest,
                                               String[] inStatements,
                                               String inCepSource)
Requests market data processed by the given complex event processor from the given source.

Parameters:
inRequest - a String value containing the representation of a MarketDataRequest to execute
inStatements - a String[] value containing the statements to pass to the complex event processor. The meaning of the statements varies according to the actual event processor that handles them.
inCepSource - a String value containing the name of the complex event processor to which to send the query request
Returns:
an int value containing the handle of the request or 0 if the request failed

cancelDataRequest

protected final void cancelDataRequest(int inRequestID)
Cancels the given data request.

Parameters:
inRequestID - an int value containing the identifier of the data request to cancel

cancelAllDataRequests

protected final void cancelAllDataRequests()
Cancels all data requests from this strategy.


requestCEPData

protected final int requestCEPData(String[] inStatements,
                                   String inSource)
Creates a complex event processor query.

Parameters:
inStatements - a String[] value containing the statements to pass to the complex event processor. The meaning of the statements varies according to the actual event processor that handles them.
inSource - a String value containing the name of the complex event processor to which to send the query request
Returns:
an int value containing the identifier of this request or 0 if the request failed

getExecutionReports

protected final Deque<ReportBase> getExecutionReports(OrderID inOrderID)
Gets the ReportBase values representing the order history of the given OrderID.

The ReportBase objects returned by this call represent the history of the order represented by the given OrderID. if there is no order history for the given OrderID, this operation will return an empty collection.

The values returned by this operation are sorted from newest to oldest: the order's current status is represented by the first element in the collection.

The collection returned by this operation will be updated as the underlying report history changes. The collection itself may not be modified.

The contents of the returned collection are limited by the value returned by getReportHistoryOriginDate(). The default value is all reports. No reports with a sending time before the origin date will be returned.

Parameters:
inOrderID - an OrderID value corresponding to an existing order, either open or closed
Returns:
a Deque<ReportBase> value containing the ReportBase objects

suggestTrade

protected final void suggestTrade(OrderSingle inOrder,
                                  BigDecimal inScore,
                                  String inIdentifier)
Suggests a trade.

Parameters:
inOrder - an OrderSingle value containing the trade to suggest
inScore - a BigDecimal value containing the score of this suggestion. this value is determined by the user but is recommended to fit in the interval [0..1]
inIdentifier - a String value containing a user-specified string to identify the suggestion

send

protected boolean send(Object inData)
Sends an order to order subscribers.

OrderSingle objects passed to this method will be added to the list of submitted orders but other object types will not. In order to track, for example, OrderReplace and OrderCancel objects, they must have first been created via cancelReplace(OrderID, OrderSingle, boolean) and cancelOrder(OrderID, boolean) respectively.

Parameters:
inData - an Object value
Returns:
a boolean value indicating whether the object was successfully transmitted or not

cancelOrder

protected final OrderCancel cancelOrder(OrderID inOrderID,
                                        boolean inSendOrder)
Submits a request to cancel the OrderSingle with the given OrderID.

The order must currently be open or this operation will fail. Note that the strategy's concept of open orders is based on its report history origin date as specified.

Parameters:
inOrderID - an OrderID value containing the ID of the open order to cancel
inSendOrder - a boolean value indicating whether the OrderCancel should be submitted or just returned to the caller If false, it is the caller's responsibility to submit the OrderCancel with send(Object).
Returns:
an OrderCancel value containing the cancel order or null if the OrderCancel could not be constructed

cancelAllOrders

protected final int cancelAllOrders()
Submits cancel requests for all OrderSingle open orders owned by the strategy's owner.

This method will make a best-effort attempt to cancel all orders. If an attempt to cancel one order fails, that order will be skipped and the others will still be attempted in their turn.

Returns:
an int value containing the number of orders for which cancels were submitted

cancelReplace

protected final OrderReplace cancelReplace(OrderID inOrderID,
                                           OrderSingle inNewOrder,
                                           boolean inSendOrder)
Submits a cancel-replace order for the given OrderID with the given Order.

The order must be open or this call will have no effect.

If inSendOrder is false, it is the caller's responsibility to submit the OrderReplace.

Parameters:
inOrderID - an OrderID value containing the order to cancel
inNewOrder - an OrderSingle value containing the order with which to replace the existing order
inSendOrder - a boolean value indicating whether the OrderReplace should be submitted or just returned to the caller. If false, it is the caller's responsibility to submit the OrderReplace with send(Object).
Returns:
an OrderReplace value containing the new order or null if the old order could not be canceled and the new one could not be sent

sendMessage

protected final void sendMessage(Message inMessage,
                                 BrokerID inBroker)
Sends a FIX message.

Parameters:
inMessage - a Message value
inBroker - a BrokerID value

sendEventToCEP

protected final void sendEventToCEP(Event inEvent,
                                    String inProvider)
Sends the given event to the CEP module indicated by the provider.

The corresponding CEP module must already exist or the message will not be sent.

Parameters:
inEvent - an Event value containing the event to be sent
inProvider - a String value containing the name of a CEP provider

sendEvent

protected final void sendEvent(Event inEvent)
Sends the given event to the appropriate subscribers.

Parameters:
inEvent - an Event value

sendNotification

protected final void sendNotification(Notification inNotification)
Sends the given notification to the appropriate subscribers.

Parameters:
inNotification - a Notification value

requestCallbackAfter

protected final void requestCallbackAfter(long inDelay,
                                          Object inData)
Requests a callback after a specified delay in milliseconds.

The callback will be executed as close to the specified millisecond as possible. There is no guarantee that the timing will be exact. If more than one callback is requested by the same RunningStrategy for the same millisecond, the requests will be processed serially in FIFO order. This implies that a long-running callback request may delay other callbacks from the same RunningStrategy unless the caller takes steps to mitigate the bottleneck.

Parameters:
inDelay - a long value indicating how many milliseconds to wait before executing the callback. A value <= 0 will be interpreted as a request for an immediate callback.
inData - an Object value to deliver along with the callback, may be null

requestCallbackEvery

protected final void requestCallbackEvery(long inDelay,
                                          long inPeriod,
                                          Object inData)
Requests a callback periodically after a specified period in milliseconds.

The callback will be executed as close to the specified millisecond as possible. There is no guarantee that the timing will be exact. If more than one callback is requested by the same RunningStrategy for the same millisecond, the requests will be processed serially in FIFO order. This implies that a long-running callback request may delay other callbacks from the same RunningStrategy unless the caller takes steps to mitigate the bottleneck.

Parameters:
inDelay - a long value indicating how many milliseconds to wait before executing the first callback. A value <= 0 will be interpreted as a request for an immediate callback.
inPeriod - a long value indicating how many milliseconds to wait before executing the second callback, and thereafter repeatedly The value must be > 0.
inData - an Object value to deliver along with the callback, may be null

requestCallbackAt

protected final void requestCallbackAt(Date inDate,
                                       Object inData)
Requests a callback at a specific point in time.

The callback will be executed as close to the specified millisecond as possible. There is no guarantee that the timing will be exact. If more than one callback is requested by the same RunningStrategy for the same millisecond, the requests will be processed serially in FIFO order. This implies that a long-running callback request may delay other callbacks from the same RunningStrategy unless the caller takes steps to mitigate the bottleneck.

Parameters:
inDate - a Date value at which to execute the callback. A date value earlier than the present will be interpreted as a request for an immediate callback.
inData - an Object value to deliver with the callback or null

getBrokers

protected final BrokerStatus[] getBrokers()
Returns the list of brokers known to the system.

These values can be used to create and send orders with sendMessage(Message, BrokerID) or send(Object).

Returns:
a BrokerStatus[] value

getPositionAsOf

protected final BigDecimal getPositionAsOf(Date inDate,
                                           String inSymbol)
Gets the position in the given Equity at the given point in time.

Note that this method will not retrieve Option positions. To retrieve Option positions, use getOptionPositionAsOf(Date, String, String, BigDecimal, OptionType).

Parameters:
inDate - a Date value indicating the point in time for which to search
inSymbol - a String value containing the Equity symbol
Returns:
a BigDecimal value or null if no position could be found

getAllPositionsAsOf

protected final Map<PositionKey<Equity>,BigDecimal> getAllPositionsAsOf(Date inDate)
Gets all open Equity positions at the given point in time.

Parameters:
inDate - a Date value indicating the point in time for which to search
Returns:
a Map<PositionKey<Equity>,BigDecimal> value

getFuturePositionAsOf

protected final BigDecimal getFuturePositionAsOf(Date inDate,
                                                 String inUnderlyingSymbol,
                                                 FutureExpirationMonth inExpirationMonth,
                                                 int inExpirationYear)
Gets the position in the given Future at the given point in time.

Note that this method will not retrieve Option or Equity positions. To retrieve Option positions, use getOptionPositionAsOf(Date, String, String, BigDecimal, OptionType). To retrieve Equity positions, use getPositionAsOf(Date, String).

Parameters:
inDate - a Date value indicating the point in time for which to search
inUnderlyingSymbol - a String value containing the underlying Future symbol
inExpirationMonth - a FutureExpirationMonth value
inExpirationYear - an int value
Returns:
a BigDecimal value or null if no position could be found

getAllFuturePositionsAsOf

protected final Map<PositionKey<Future>,BigDecimal> getAllFuturePositionsAsOf(Date inDate)
Gets all open Future positions at the given point in time.

Parameters:
inDate - a Date value indicating the point in time for which to search
Returns:
a Map<PositionKey<Equity>,BigDecimal> value

getOptionPositionAsOf

protected final BigDecimal getOptionPositionAsOf(Date inDate,
                                                 String inOptionRoot,
                                                 String inExpiry,
                                                 BigDecimal inStrikePrice,
                                                 OptionType inType)
Gets the position in the given Option at the given point in time.

Parameters:
inDate - a Date value indicating the point in time for which to search
inOptionRoot - a String value
inExpiry - a String value
inStrikePrice - a BigDecimal value
inType - an OptionType value
Returns:
a BigDecimal value or null if no position could be found

getAllOptionPositionsAsOf

protected final Map<PositionKey<Option>,BigDecimal> getAllOptionPositionsAsOf(Date inDate)
Gets all open Option positions at the given point in time.

Parameters:
inDate - a Date value indicating the point in time for which to search
Returns:
a Map<PositionKey<Option>,BigDecimal> value

getOptionPositionsAsOf

protected final Map<PositionKey<Option>,BigDecimal> getOptionPositionsAsOf(Date inDate,
                                                                           String... inOptionRoots)
Gets open positions for the options specified by the given option roots at the given point in time.

Parameters:
inDate - a Date value indicating the point in time for which to search
inOptionRoots - a String[] value containing the specific option roots for which to search
Returns:
a Map<PositionKey<Option>,BigDecimal> value

getUnderlying

protected final String getUnderlying(String inOptionRoot)
Gets the underlying symbol for the given option root, if available.

Parameters:
inOptionRoot - a String value containing an option root
Returns:
a String value containing the symbol for the underlying instrument or null if no underlying instrument could be found

getOptionRoots

protected final Collection<String> getOptionRoots(String inUnderlying)
Gets the set of of known option roots for the given underlying symbol.

Parameters:
inUnderlying - a String value containing the symbol of an underlying instrument
Returns:
a Collection<String> value sorted lexicographically by option root or null if no option roots could be found

createDataFlow

protected final DataFlowID createDataFlow(boolean inAppendDataSink,
                                          DataRequest... inRequests)
Initiates a data flow request.

See DataFlowSupport.createDataFlow(DataRequest[], boolean).

Parameters:
inAppendDataSink - a boolean value indicating if the sink module should be appended to the data pipeline, if it's not already requested as the last module and the last module is capable of emitting data.
inRequests - a DataRequest... value containing the ordered list of requests. Each instance identifies a stage of the data pipeline. The data from the first stage is piped to the next.
Returns:
a DataFlowID value containing a unique ID identifying the data flow. The ID can be used to cancel the data flow request and get more details on it. Returns null if the request could not be created.

cancelDataFlow

protected final void cancelDataFlow(DataFlowID inDataFlowID)
Cancels a data flow identified by the supplied data flow ID.

See DataFlowSupport.cancel(DataFlowID).

Parameters:
inDataFlowID - a DataFlowID value containing the request handle that was returned from a prior call to createDataFlow(boolean, DataRequest[])

getURN

protected final ModuleURN getURN()
Gets the ModuleURN of this strategy.

Returns:
a ModuleURN value

getUserData

protected final Properties getUserData()
Gets the user data associated with the current user.

Returns:
a Properties value

setUserData

protected final void setUserData(Properties inUserData)
Sets the user data associated with the current user.

Parameters:
inUserData - a Properties value

debug

protected void debug(String inMessage)
Emits the given debug message to the strategy log output.

Parameters:
inMessage - a String value

info

protected void info(String inMessage)
Emits the given info message to the strategy log output.

Parameters:
inMessage - a String value

warn

protected void warn(String inMessage)
Emits the given warn message to the strategy log output.

Parameters:
inMessage - a String value

error

protected void error(String inMessage)
Emits the given error message to the strategy log output.

Parameters:
inMessage - a String value

initializeReportHistoryManager

protected final void initializeReportHistoryManager()
Initializes the report history manager.

This can be a very expensive call depending on the value returned by getReportHistoryOriginDate() and the number of execution reports in the database.



Copyright © 2012. All Rights Reserved.