Marketlib

Marketlib is a library of functions already imported by default on our application. There are some ready made valuation functions which you can use for your model to start off with. It also has some helper functions which you can use to help you write your own valuation functions.

Helper Functions

These can be used within your valuation function as short hands.

Valuation Functions

These are a variety of simple to more complex valuation functions you can apply to the model. These you could also use within your valuation function to build off of the result.

All Functions

askPrice

(Helper Functions)

marketlib.askPrice(q, i)

‘q’ is either a stock symbol in the form of a string, or an already created quote node

‘i’ is an int between 0 and 4

returns the ask price of the quote at level i from the inside of the book

Call askPrice with either a security name as a string or use it within a valuation function where q is already an initialized Quote and pass this function as a short hand to output the ask price.

askQty

(Helper Functions)

marketlib.askQty(q, i)

‘q’ is either a stock symbol in the form of a string, or an already created quote node

‘i’ is an int between 0 and 4

returns the ask quantity of the quote at level i from the inside of the book

Call askQty with either a security name as a string or use it within a valuation function where q is already an initialized Quote and pass this function as a short hand to output the ask quantity.

bidPrice

(Helper Functions)

marketlib.bidPrice(q, i)

‘q’ is either a stock symbol in the form of a string, or an already created quote node

‘i’ is an int between 0 and 4

returns the bid price of the quote at level i from the inside of the book

call bidPrice with either a security name as a string or use it within a valuation function where q is already an initialized Quote and pass this function as a short hand to output the bid price.

bidQty

(Helper Functions)

marketlib.bidQty(q, i)

‘q’ is either a security name in the form of a string, or an already created quote node

‘i’ is an int between 0 and 4

returns the bid quantity of the quote at level i from the inside of the book

Call bidQty with either a security name as a string or use it within a valuation function where q is already an initialized Quote and pass this function as a short hand to output the bid quanitity.

CT

(Valuation Functions)

marketlib.CT(q_theo=None, ref_theo=None, vm=0.5, cxa=5.0)

q_theo is a quote node, ref_theo is reference node

vm is a volatility multiplier

IN DEVELOPMENT

comp theo, returns Xbar * (Y/ybar)^vm where Xbar is and ema of X, y is the driver signal, ybar is an ema of the driver signal and vm is a volatility multiplier

EMA

(Valuation Functions)

marketlib.EMA(node, tau, initialValue=0.0)

node is either a quote or trade event

Tau is the decay factor in microseconds

initial values is starting point of calculation returns exponentialy moving average of node

returns the exponentially weighted moving average, tau is the decay factor. Use either Quote, Trade or Any. (EMA uses WithInitial where initial value is the initial value of a lambda function, normally leave as 0.0)

ExpWeightAv

(Valuation Functions)

marketlib.ExpWeightAv(q, exp=1.0)

‘q’ is either a security name in the form of a string, or an already created quote node, ‘exp’ is the exponent

returns exponentially weighted average of inside prices

Exponentially Weighted Average: call ExpWeightAv with either a security name as a string or use it within a valuation function where q is already an initialized Quote and pass this function as a short hand to output the Weighted average.

Uses the exponent value to add weight to the bid and ask quantities, this function will then calculate the weighted average by cross multiplying bid price with weighted ask quantity and adding it to the ask price times the weighted bid quantity. Then dividing all that with the sum of the weighted bid and ask quantity.

(Can be used as both a helper function within a valuation function in order to manipulate the output further, or as a valuation function in and of itself)

InsideAsk

(Helper Functions)

marketlib.InsideAsk(q)

‘q’ is either a security name in the form of a string, or an already created quote node

returns the inside ask price of q

Call InsideAsk with either a security name as a string or use it within a valuation function where q is already an initialized Quote and pass this function as a short hand to output the inside asking price.

InsideBid

(Helper Functions)

marketlib.InsideBid(q)

‘q’ is either a security symbol in the form of a string, or an already created quote node

returns the inside bid price of q

Call InsideBid with either a security name as a string or use it within a valuation function where q is already an initialized Quote and pass this function as a short hand to output the inside bid price.

MP2

(Valuation Functions)

marketlib.MP2(symbol, exp=1.0, trade_weight=1, trade_weight_exp=1.0, trade_exp=1.0, tau_halflife=1000.0)

returns an EMA of weighted trades based off the security symbol ‘symbol’

Returns the exponential moving average of trading of weighted trades. Go to Example Valuation Function MP2 for a detailed look at MP2.

Midpt

(Valuation Functions)

marketlib.Midpt(q)

‘q’ is either a security name in the form of a string, or an already created quote node

returns the midpoint between q’s inside asking price and inside biding price

call spread with either a security name as a string or with an initiated Quote from the Alpha Trading Labs DSL library

PriceMultPremium

(Valuation Functions)

marketlib.PriceMultPremium(referenceNode, priceMult, premium)

‘referenceNode’ should be a trade price, bid price, or ask price, ‘priceMult’ and ‘premium’ both should be integers

returns (price * ‘priceMult’) + ‘premium’

Simple linear calculation, should normally be used with a some form of price, and returns that price times the multiplier and added to the premium.

SizeWa

(Valuation Functions)

marketlib.SizeWa(q, size=1000, depth=3)

q is a is a security symbol or already created Quote node

‘size’ is the denominator in the average calculation

‘depth’ is how many leves into the book to use for the calculation of the average (0 - 4)

returns the weighted average

Size Weighted Average: call SizeWa with either a security name as a string or use it within a valuation function where q is already an initialized Quote and pass this function as a short hand to output the Size Weighted Average.

call it with a security symbol in the form of a string, set the size and depth you want. This will calculated a weighted bid and ask price for each bid in the levels 0 through ‘depth’ in the book and adding them together and dividing them by 2 * ‘size’ to output the size weighted average.

(Can be used as both a helper function within a valuation function in order to manipulate the output further, or as a valuation function in and of itself)

Spread

(Valuation Functions)

marketlib.Spread(q)

‘q’ is either a security name in the form of a string, or an already created quote node

returns the difference between q’s inside asking price and inside bid price

call spread with either a security name as a string or use it within a valuation function where q is already an initialized Quote and pass this function as a short hand to output the spread between the inside asking price and inside bid price.