User Tools

Site Tools


useful_functions

Useful Functions

All mathematical functions defined in the “math” module of Python are directly available within TSL code. You can read about these here.

Some other functions are also provided in order to make it easier to create trading systems and technical indicators:

Name Sample call Return value
crossOver crossOver(SMA(14), Close) Returns True if the first parameter crossed over the second parameter on the current period, False otherwise.

Sample implementation:

def crossOver(Indic, Ref):
    try: float(Ref)
    except: return Indic[1] <= Ref[1] and Indic[0] > Ref[0]
    else:   return Indic[1] <= Ref and Indic[0] > Ref
crossBelow crossBelow(SMA(7), 25.1) Returns True if the first parameter crossed below the second parameter on the current period, False otherwise.

Sample implementation:

def crossBelow(Indic, Ref):
    try: float(Ref)
    except: return Indic[1] >= Ref[1] and Indic[0] < Ref[0]
    else:   return Indic[1] >= Ref and Indic[0] < Ref
average average(Volume) Returns the average value of the parameter.

Sample implementation:

def average(P):
    return float(sum(P)) / len(P)
averageDev averageDev(Price) Returns the average deviation of the parameter.

Sample implementation:

def averageDev(P):
    MA = average(P)
    Sum = 0
    for V in P:
        Sum += abs(V - MA)
    return Sum / len(P)
stdDev stdDev(TypicalPrice) Returns the standard deviation of the parameter.

Sample implementation:

def stdDev(P):
    MA = average(P)
    SquareSum = 0
    for V in P:
        Deviation = V - MA
        SquareSum += Deviation * Deviation
    return sqrt(SquareSum / len(P))
useful_functions.txt · Last modified: 2014/08/02 06:29 (external edit)