First Steps with Quantiacs: Code a Trend-Following System in Python

import numpy

def myTradingSystem(DATE, OPEN, HIGH, LOW, CLOSE, VOL, exposure, equity, settings):
'''This system uses trend following techniques to allocate capital into the desired equities.'''

nMarkets= CLOSE.shape[1]
    perL= 200
perS= 40
    smaLong  = numpy.nansum(CLOSE[-perL:, :], axis=0)/perL
smaRecent= numpy.nansum(CLOSE[-perS:, :], axis=0)/perS
    longEquity  = smaRecent > smaLong
shortEquity = ~longEquity
    pos= numpy.zeros(nMarkets)
pos[longEquity] = 1
pos[shortEquity]= -1
weights= pos/numpy.nansum(abs(pos))

return weights, settings
def mySettings():
'''Define your trading system settings here.'''

settings= {}

# selected Futures contracts
settings['markets']= ['CASH','F_AD', 'F_BO', 'F_BP', 'F_C']
settings['beginInSample']= '20120506'
settings['endInSample'] = '20150506'
settings['lookback']= 504
settings['budget'] = 10**6
settings['slippage']= 0.05

return settings
if __name__ = "__main__":
import quantiacsToolbox
results = runts(__file__)
import numpy

def myTradingSystem(DATE, OPEN, HIGH, LOW, CLOSE, VOL, exposure, equity, settings):
'''This system uses trend following techniques to allocate capital into the desired equities.'''

nMarkets= CLOSE.shape[1]

perL= 200
perS= 40

smaLong = numpy.nansum(CLOSE[-perL:, :], axis=0)/perL
smaRecent= numpy.nansum(CLOSE[-perS:, :], axis=0)/perS

longEquity= smaRecent > smaLong
shortEquity= ~longEquity

pos= numpy.zeros(nMarkets)
pos[longEquity]= 1
pos[shortEquity]= -1

weights = pos/numpy.nansum(abs(pos))

return weights, settings

def mySettings():
'''Define your trading system settings here.'''

settings= {}

# selected Futures contracts
settings['markets']= ['CASH', 'F_AD', 'F_BO', 'F_BP', 'F_C']
settings['beginInSample']= '20120506'
settings['endInSample'] = '20150506'
settings['lookback']= 504
settings['budget'] = 10**6
settings['slippage']= 0.05

return settings

if __name__ == "__main__":
import quantiacsToolbox
results = quantiacsToolbox.runts(__file__)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store