Strategy Runner API
C++ Programming Reference

Callbacks

void onInitParameters(XPathWrapper &xpath)
The purpose of this callback is to retrieve the parameters of the strategy from the XML file. It is called once in order to set strategy parameters and constants when Strategy Runner framework initializes the strategy. Its default implementation reads and/or initializes a number of standard parameters. You have to include explicit call of the base class default implementation in your overridden implementation. You should sets values of those strategy data members that stay constant throughout the trading day in onInitParameters.
Parameters:

void onRegisterBars( XPathWrapper &xpath )
This callback is called once when Strategy Runner framework initializes the strategy. This function should include registerBar function calls for each particular bar.
Parameters:

bool onAddIndicators( XPathWrapper &xpath )
This callback is called once when Strategy Runner framework initializes the strategy. Use addIndicator function to add each particular indicator.
Parameters:

Return value: true if all indicators were added successfully

bool onRestoreOvernight(XPathWrapper& xpath)
This callback is called when Strategy Runner framework starts the strategy. The callback initializes those strategy data items that were saved by the end of the previous day in order to restore the strategy state on the next day. Strategy Runner framework calls this function only if strategy was defined as overnight in its XML definitions file.
Note: Special care should be taken in case the strategy is executed for the first time and still has no overnight data.
Parameters:

Return value: boolean (unused)

bool onInit(const TransactionDataWrapper& data)
This callback is called at the strategy's Start Time or when the strategy is started manually through the Console. You should set initial values for those strategy variable data members that may change during the trading day in onInit.
Parameters:

Return value: boolean (unused)

bool onStart(const TransactionDataWrapper& data)
This callback is called when the Start Time is reached. Its default implementation consists of the call to onEnterPositions below. Should it be overridden, it is not necessary to explicitly call onStart of the base class.
Parameters:

Return value: boolean (unused)

bool onEnterPositions(const TransactionDataWrapper& data)
This callback is called either immediately after the onInit function or each time the strategy wants to enter a new position (after onClosePositions). Within this function you should implement the way in which the strategy enters positions (send order, set alerts, etc).
Parameters:

Return value: boolean (unused)

bool onOpenPositions(const ActionDataWrapper& actionWrapper)
This callback is called when an order has been filled, and the strategy has open positions.
Parameters:

Return value: boolean (unused)

bool onClosePositions(const ActionDataWrapper& actionWrapper)
This function is called when an order has been filled, and the strategy is flat (there is no open positions).
Parameters:

Return value: boolean (unused)

bool onAlert(const ActionDataWrapper& actionWrapper)
This function is called after requested alert is triggered. Available alerts: Time alert, Time Transaction alert, Indicator alert, Price alert.
Parameters:

Return value: boolean (unused)

bool onBar(const BarId &barId, const BarDataWrapper& bar)
This function is called after a bar is closed (end-of-bar event).
Parameters:

Return value: boolean (unused)

bool onWaitAfterEntrance(const ActionDataWrapper& actionWrapper)
This callback is called when strategy has changed the number of open positions. This callback is followed by timeout before calling to onOpenPositions.
Parameters:

Return value: boolean (unused)

bool onWaitAfterWin(const ActionDataWrapper& actionWrapper)
This callback is called when strategy has closed its positions, and the last trading cycle was completed with profit. This callback is followed by timeout before calling to onEnterPositions.
Parameters:

Return value: boolean (unused)

bool onWaitAfterLoss(const ActionDataWrapper& actionWrapper)
This callback is called when strategy has closed its positions, and the last trading cycle was completed with loss. This callback is followed by timeout before calling to onEnterPositions.
Parameters:

Return value: boolean (unused)

char* onSaveOvernight()
This callback is called at the stop time. Its purpose is to save those of strategy state variables that would be restored on the next day. Strategy Runner framework calls this function only if strategy was defined as overnight in its XML definitions file.
Parameters:

bool onStop()
This callback is called at the stop time.

Trading Functions

void registerBar(XPathWrapper &xpath, const std::string &name, long contractId, BarId &barId)
This function registers the strategy to receive end-of-bar events for the bar.
Parameters:

long addIndicator(const char* name, XPathWrapper &xpath)
This function registers calculation of the specified indicator to the strategy.
Note: This function only registers indicator. Call startIndicator to start indicator calculations.
Parameters:

Return value: long - indicator id

void startIndicator(long id)
This function starts continuous indicator calculation as defined by given id. It should be called at exact moment when indicator calculation should commence. Usually, calls to startIndicator are performed in onInit or onStart which coincide with strategy start time.

long sendOrder( ActionDataWrapper::ACTION_TYPE action_type, double price, double price2, long count, long packageId );
This function sends an order.
Parameters:

Return value: long - sent order id > 0 on success, -1 otherwise

long sendSmartOrder(ActionDataWrapper::ACTION_TYPE action_type, double price, double price2, long count, long packageId, double slippage = 0.0, long timeout = -1)
This function sends a smart order.
Parameters:

Return value: long - sent order id > 0 on success, -1 otherwise

long modifyOrder (long orderId, double price, double price2, long count)
This function modifies the working order.
Parameters:

Return value: long - order id > 0 on success, -1 otherwise

long cancelOrder(long orderId)
This function cancels a specific order.
Parameters:

Return value: long - order id

void cancelPackage(long packageId)
This function cancels all orders and alerts in the package.
Parameters:

void sendEnterLimitPositions(double price, double epsilon, const SmartParams smart = NON_SMART_ORDER, long lots = 0, long packageId = -1)
This function sends two limit orders to enter new positions.
Parameters:

void sendEnterLimitPositionsPts(double price, double buyOffsetPts, double sellOffsetPts, const SmartParams smart = NON_SMART_ORDER, long lots = 0, long packageId = -1)
This function sends two limit orders to enter new positions.
Parameters:

void sendEnterStopPositions(double price, double epsilon, const SmartParams smart = NON_SMART_ORDER, long lots = 0, long packageId = -1)
This function sends two stop orders to enter new positions.
Parameters:

void sendEnterStopPositionsPts(double price, double buyOffsetPts, double sellOffsetPts, const SmartParams smart = NON_SMART_ORDER, long lots = 0, long packageId = -1)
This function sends two stop orders to enter new positions.
Parameters:

void sendClosePositions(double price, double limit, double stop, const SmartParams smart = NON_SMART_ORDER, long lots = 0, long packageId = -1 )
This function sends orders to close the already open positions in accordance with stop loss and profit limit values (in percents).
Parameters:

void sendClosePositionsPts(double price, double limitOffsetPts, double stopOffsetPts, const SmartParams smart = NON_SMART_ORDER, long lots = 0, long packageId = -1 )
This function sends orders to close the already open positions in accordance with stop loss and profit limit offset values (in points).
Parameters:

long sendCloseLimitPositions(double price, double limit, const SmartParams smart = NON_SMART_ORDER, long lots = 0, long packageId = -1)
This function sends a limit order to achieve profit target.
Parameters:

Return value: long - limit order id

long sendCloseLimitPositionsPts(double price, double limitOffsetPts, const SmartParams smart = NON_SMART_ORDER, long lots = 0, long packageId = -1)
This function sends a limit order to achieve profit target (in points).
Parameters:

Return value: long - limit order id

long sendCloseStopPositions(double price, double stop, const SmartParams smart = NON_SMART_ORDER, long lots = 0, long packageId = -1)
This function sends a stop order in accordance with stop loss value.
Parameters:

Return value: long - stop order id

long sendCloseStopPositionsPts(double price, double stopOffsetPts, const SmartParams smart = NON_SMART_ORDER, long lots = 0, long packageId = -1)
This function sends a stop order in accordance with stop loss offset (in points).
Parameters:

Return value: long - stop order id

long closeAllPositions()
This function closes all strategy open positions.

void cancelAllActions()
This function cancels all alerts and orders.

void cancelAllOrders()
This function cancels all strategy orders.

void stopTrader()
This function closes all positions, cancels all actions, and stops the strategy.

Alert Functions

long setTimeAlert(FATS::Time& time, long& id, long contractId = -1, long packageId = SYSTEM_PACKAGE_ID)
This function sets a time alert. This alert is triggered when the specified time is reached.
Parameters: Return value: long - alert id

long setTimeOffsetAlert(long offset, long& id, long contractId = -1, long packageId = SYSTEM_PACKAGE_ID )
This function sets an offset time alert (in seconds). Alert is triggered after the given time (offset) has passed since the moment when this function was called.
Parameters:

Return value: long - alert id

long setTimeTransactionAlert(FATS::Time &timeAlert, long& id, long contractId = -1, long packageId = SYSTEM_PACKAGE_ID)
This function sets a transaction time alert. This alert is triggered when the system receives the first transaction after the specified time.
Parameters:

Return value: long - alert id

long setPriceAlert(Predicate* p, long contractId = -1, long packageId = SYSTEM_PACKAGE_ID )
This function sets a price alert. When the predicate condition is satisfied, this alert is triggered.
Parameters:

Return value: long - alert id

long setPnLAlert(Predicate* p, long userData = 0, long packageId = SYSTEM_PACKAGE_ID)
This function sets a P&L alert. When the predicate condition on P&L is satisfied, this alert is triggered.
Parameters:

Return value: long - alert id

long setIndicatorAlert(long indicator_id, Predicate* p, long lUserData = 0, long contractId = -1, long packageId = SYSTEM_PACKAGE_ID)
This function sets an alert on value of specified indicator. When the predicate condition is satisfied, this alert is triggered.
Parameters:

Return value: long - alert id

void cancelTimeAlert(long id)
This function cancels specified time alert.

void cancelPriceAlert(long id)
This function cancels specified price alert.

void cancelPnLAlert(long id)
This function cancels specified PnL alert.

long cancelIndicatorAlert(long id_action)
This function cancels specified indicator alert.

Query Functions

FATS::Time getStartTime()
This function returns the starting time of the strategy.

FATS::Time getEndTime()
This function returns the end time of the strategy.

double getLimit()
This function returns the limit parameter.

double getStop()
This function returns the stop parameter.

long getOpenPositions()
This function returns the number of currently opened positions. The sign of the number refers either to long (positive) or to short (negative) positions.

long getWaitAfterEntrance()
This function returns strategy timeout (in seconds) after the strategy entered position (the trading cycle's entry order has been filled) and before calling onOpenPositions callback.

long getWaitAfterWin()
This function returns strategy timeout (in seconds) after the strategy closed position with profit (the trading cycle's profit limit order has been filled) and before calling onEnterPositions callback.

long getWaitAfterLoss()
This function returns strategy timeout (in seconds) after the strategy closed position with loss (the trading cycle's stop loss order has been filled) and before calling onEnterPositions callback.

long getContractId()
This function returns the working contract id of the strategy.

long getContractCountParam()
This function returns the default number of lots for order sending. Set the number through the Console.

MARKET_DIRECTION getMarketDirection()
This function returns the market direction (bullish, bearish or none). Set the type through the Console.

double getCurrentPnL()
This function returns the current P&L (profit and loss) in points.

double getCurrentPnLPrice()
This function returns the current P&L in currency.

FATS::Time getCurrentTime(long contractId = -1)
This function returns the current time for the specified contract. If a contract is not specified, the working contract is used.

FATS::Date getCurrentDate(long contractId = -1)
This function returns the current date for the specified contract. If a contract is not specified, the working contract is used.

double getContractPointPrice(long contractId)
This function returns the price of a one point for the specified contract.

double getContractTick(long contractId)
This function returns the minimal part of point (tick) for the specified contract.

double getDenominator(long contractId)
This function returns the denominator for the given contract.

double getFloatValue(double showValue, double denominator)
This function converts the price from displayed representation to actual value.

double getShowValue(double floatValue, double denominator)
This function converts the price from actual value to displayed representation.

double round(double price)
This function rounds the price according to the contract tick.

long nextPackageId()
This function returns a unique package id.

bool isTurnWon()
This function returns true if last transaction round trip (trading cycle) was with profit, and false otherwise.

TransactionDataWrapper getLastTransaction(long contractId)
This function returns the most recently occurred transaction for specified contract.

double getIndicatorValue(long contractId, long indicatorId, long time)
This function returns the value of the specified indicator at given time.

ActionDataWrapper getWorkingOrder(long orderId)
This function returns a working order according to its id.

const BarDataWrapper* getBar(const BarId &barId, long since)
This function returns the previous bar.
Parameters:

Return value: const BarDataWrapper* - pointer to a bar object. If NULL - bar doesn't exist.

int getStrategyBarIndex(const BarId & barId)
This function returns the index of the given bar since strategy started.

Messages and tracing

Strategy may send messages to the Console, or write traces and logs to file/database.

void sendMessage(const char* msg) sends the given message to the Console.

BaseStrategyClass::_pTracer member variable of type Tracer allows you to insert an arbitrary message into strategy trace file or strategy trace data base.

Appendix A: Terminology

Appendix B: Helper Classes

Appendix C: A Sample Strategy XML File

Strategy XML files are divided into several sections, as shown in the following example:

<VTrader
	description='My description'	: strategy description
	packageName='MyStrategies'	: strategy DLL name (without .dll)
	templateName='MyStrategyClass'	: strategy C++ class name
	overnight='yes'>                : strategy keeps overnight data/positions
	
	<Counts				: this part describes how many round turns (trading cycles)
		wins='1'		: and of which type
		total='1'		: the strategy is allowed to do
		losses='1'>
	</Counts>

	<Time
		startTime='09:00:00'	: start the strategy
		enterEnd='09:05:00'	: do not enter position after this time
		endTime='11:30:00'>	: stop the strategy
	</Time>

	<ClosePosition
		stop='8.0'		: stop loss value
		limit='10.0'>		: profit target value
	</ClosePosition>

	<Wait
		afterEntrance='0'	: timeout after entering position
		afterWin='0'		: timeout after closing position with win, before starting next trading cycle
		afterLoss='0'>		: timeout after closing position with loss, before starting next trading cycle
		
	</Wait>

	: strategy section
	<Strategy	

		name='StrategyName'	: strategy name, will be displayed in Console
		traceToFile='yes'	: write strategy log file
		
		...			: other strategy related variables 

		>

		<Bars>	: bar section
			<Bar
				name='mybar'	: bar name
				type='time' 	: bar type. Options: daily, time, transaction, volume
				period='600'	: bar period (for all except daily bars)
						: for time bars - in seconds
						: for transaction bars - in ticks
						: for volume bars - in volume
									
				: previous days sub-section - mandatory for daily bars, optional for other
				numOfPrevBars='200'
				startTimeOfPrevDays='08:30:00' 
				endTimeOfPrevDays='15:15:00'/>
		</Bars>

		<Indicators>	: indicator section
			<Indicator 
				name='MyIndicator1' 
				packageName='SampleIndicators' 
				templateName='MovingAverage'
				period='30' 
				show='1'>
			</Indicator>
		</Indicators>

	</Strategy>

</VTrader>