#!/usr/bin/env python ''' Created on Apr 26, 2013 @author: rtw ''' import sys from hedgeit.common.logger import getLogger import getopt from datetime import datetime import json from hedgeit.control.controller import Controller from hedgeit.feeds.db import InstrumentDb Log = getLogger(__name__) def usage(): print ''' usage: backtest.py <manifest> <sector-map> <feedStart> <tradeStart> <tradeEnd> Options: -h : show usage -c <number> : set the starting (per sector) equity (default = 1000000) -p <parms> : model parameters. Formatted as comma separated list of key=value pairs. ex. atrPeriod=100,period=7 -t <model> : model type ('breakout', 'macross', 'rsireversal', 'split7s', 'connorsrsi', default = 'breakout') -g : no compounding of equity --tssb <name>: write out two files for tssb consumption - <name>_long.csv and <name>_short.csv containing long and short trades
''' hedgeit.feeds.instrument Contains: class InstrumentDb ''' import csv from instrument import Instrument from hedgeit.common.logger import getLogger from hedgeit.common.singleton import Singleton import os logger = getLogger("feeds.db") @Singleton class InstrumentDb(object): def __init__(self): ''' Constructor. ''' self._db = {} def load(self, manifest): ''' Loads the instrument Database using a manifest file. :param str manifest: file containing the list of instruments to load File Format: The file must be a .CSV file containing a header row with at least the
Contains: class MultiSymFuturesBaseStrategy ''' from hedgeit.feeds.db import InstrumentDb from hedgeit.strategy.strategy import Strategy from hedgeit.broker.brokers import BacktestingFuturesBroker from hedgeit.feeds.indicators.atr import ATR from hedgeit.common.logger import getLogger from hedgeit.broker.commissions import FuturesCommission from hedgeit.broker.orders import Order import numpy import datetime logger = getLogger("strategy.msymfut") class MultiSymFuturesBaseStrategy(Strategy): def __init__(self, barFeed, symbols = None, broker = None, cash = 1000000,\ compounding = True, parms = None): self._parms = self.defaultParms() if parms: self._parms.update(parms) if broker is None: broker = BacktestingFuturesBroker( cash, barFeed, commission=FuturesCommission(2.50)) Strategy.__init__(self, barFeed, cash, broker) if symbols is None: self._symbols = barFeed.symbols()
""" hedgeit.feeds.instrument Contains: class InstrumentDb """ import csv from instrument import Instrument from hedgeit.common.logger import getLogger from hedgeit.common.singleton import Singleton import os logger = getLogger("feeds.db") @Singleton class InstrumentDb(object): def __init__(self): """ Constructor. """ self._db = {} def load(self, manifest): """ Loads the instrument Database using a manifest file. :param str manifest: file containing the list of instruments to load File Format: The file must be a .CSV file containing a header row with at least the
''' hedgeit.strategy.countertrends Contains: class RSIReversalStrategy class ConnorsRSIStrategy class Split7sStrategy ''' from msymfut import MultiSymFuturesBaseStrategy from hedgeit.feeds.indicators import talibfunc from hedgeit.feeds.indicators.cum import CUM from hedgeit.common.logger import getLogger logger = getLogger("strategy.macross") class RSIReversalStrategy(MultiSymFuturesBaseStrategy): def __init__(self, barFeed, symbols = None, broker = None, cash = 1000000,\ compounding = True, parms = None): MultiSymFuturesBaseStrategy.__init__(self, barFeed, symbols=symbols, broker=broker, cash=cash, compounding=compounding, parms=parms) self._lastRSI = {} self._tripped = {} def defaultParms(self):
class ClenowController ''' from hedgeit.analyzer.istrategy import InstrumentedStrategy from hedgeit.feeds.feed import Feed from hedgeit.feeds.db import InstrumentDb from hedgeit.feeds.multifeed import MultiFeed from hedgeit.strategy.factory import StrategyFactory from hedgeit.analyzer.drawdown import DrawDown from hedgeit.broker.brokers import BacktestingFuturesBroker from hedgeit.broker.commissions import FuturesCommission from hedgeit.broker.orders import Order from alert import Alert import numpy from hedgeit.common.logger import getLogger logger = getLogger("broker.backtesting") class Controller(object): def __init__(self, sectorMap, modelType=None, cash = 1000000, tradeStart=None, compounding = True, positionsFile=None, equityFile=None, returnsFile=None, summaryFile=None, parms = None ): self._runGroups = {} self._startingCash = cash self._db = InstrumentDb.Instance() self._feed = MultiFeed() self._broker = BacktestingFuturesBroker(cash, self._feed, commission=FuturesCommission(2.50)) show = True
''' hedgeit.feeds.instrument Contains: class Instrument ''' import csv from csvparser import PremiumDataParser from hedgeit.common.logger import getLogger import os logger = getLogger("hedgeit.feeds") class Instrument(object): ''' Instrument associates a symbol with bar data for a tradable instrument. At present time, Instrument is hard-coded to support the PremiumData feed format input via .csv file. In the future Instrument may become an abstract base class to support different historical and/or real-time data feeds ''' def __init__(self, symbol, datafile, pointValue=1, currency='USD', exchange='', \ initialMargin=0, maintMargin=0, sector='',description=''): ''' Constructor :param str symbol: symbol name :param str datafile: csv file containing historical bar data :param number pointValue: multiplier for one unit of movement in price quote
import traceback import getopt import os import shutil import sys import time from hedgeit.common.sendmail import sendmail from tssbutil.runtssb import get_process_list,run_tssb,kill_tssb from hedgeit.control.controller import Controller from hedgeit.feeds.db import InstrumentDb import json from hedgeit.common.logger import getLogger from tssbutil.pdb import DbParser from tssbutil.paudit import AuditParser Log = getLogger(__name__) class UpdateMain(object): def __init__(self): self._tradeappdir = 'c:/Trading Applications' self._dataconvdir = 'c:/Program Files (x86)/Premium Data Converter' # we know this script is in hedgeit/bin. We need to create the # path to hedgeit/data for the data update self._bindir = os.path.dirname(os.path.abspath(__file__)) self._basedir = os.path.split(self._bindir)[0] self._datadir = os.path.join(self._basedir,'data') self._alerts = [] self._exitOrders = [] def main(self,argv=None):
Contains: class MultiSymFuturesBaseStrategy ''' from hedgeit.feeds.db import InstrumentDb from hedgeit.strategy.strategy import Strategy from hedgeit.broker.brokers import BacktestingFuturesBroker from hedgeit.feeds.indicators.atr import ATR from hedgeit.common.logger import getLogger from hedgeit.broker.commissions import FuturesCommission from hedgeit.broker.orders import Order import numpy import datetime logger = getLogger("strategy.msymfut") class MultiSymFuturesBaseStrategy(Strategy): def __init__(self, barFeed, symbols = None, broker = None, cash = 1000000,\ compounding = True, parms = None): self._parms = self.defaultParms() if parms: self._parms.update(parms) if broker is None: broker = BacktestingFuturesBroker(cash, barFeed, commission=FuturesCommission(2.50)) Strategy.__init__(self, barFeed, cash, broker) if symbols is None: self._symbols = barFeed.symbols() else: self._symbols = symbols
# distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. ###################################################################### ## Broker import broker from commissions import * from fillstrategy import * import orders from hedgeit.common.logger import getLogger from hedgeit.feeds.db import InstrumentDb import copy logger = getLogger("broker.backtesting") ###################################################################### ## These are backtest-specific orders derived from the base classes ## in the orders module. class BacktestingOrder: def __init__(self): pass def checkCanceled(self, broker, bars): # This check is only for accepted orders that are not GTC. if self.getGoodTillCanceled() or not self.isAccepted(): return # TODO - for now every bar is a session close since we are only
''' hedgeit.feeds.instrument Contains: class Instrument ''' import csv from csvparser import PremiumDataParser from hedgeit.common.logger import getLogger import os logger = getLogger("hedgeit.feeds") class Instrument(object): ''' Instrument associates a symbol with bar data for a tradable instrument. At present time, Instrument is hard-coded to support the PremiumData feed format input via .csv file. In the future Instrument may become an abstract base class to support different historical and/or real-time data feeds ''' def __init__(self, symbol, datafile, pointValue=1, currency='USD', exchange='', \ initialMargin=0, maintMargin=0, sector='',description=''): ''' Constructor :param str symbol: symbol name :param str datafile: csv file containing historical bar data :param number pointValue: multiplier for one unit of movement in price quote :param str currency: currency that the instrument is settled in
''' hedgeit.strategy.countertrends Contains: class RSIReversalStrategy class ConnorsRSIStrategy class Split7sStrategy ''' from msymfut import MultiSymFuturesBaseStrategy from hedgeit.feeds.indicators import talibfunc from hedgeit.feeds.indicators.cum import CUM from hedgeit.common.logger import getLogger logger = getLogger("strategy.macross") class RSIReversalStrategy(MultiSymFuturesBaseStrategy): def __init__(self, barFeed, symbols = None, broker = None, cash = 1000000,\ compounding = True, parms = None): MultiSymFuturesBaseStrategy.__init__(self, barFeed, symbols = symbols, broker = broker, cash = cash, compounding = compounding, parms = parms ) self._lastRSI = {} self._tripped = {} def defaultParms(self):