Example #1
0
 def run(self):
     pair = self.pair
     interval = self.interval
     df = self.df
     strat = Strategy(df, pair)
     strat.run() #run test the entire day and live goes live
     strat.output(True)
Example #2
0
    def run(self):
        player = self.process_client.login(3)
        map_graph = self.process_client.read_map()
        objects = self.process_client.read_objects()
        strategy = Strategy(player, map_graph, objects)
        if self.is_gui:
            self.gui = GUI(player, map_graph, objects)

        while player.is_alive:
            if self.is_gui:
                self.gui.turn()
                if ((not self.gui.paused) or self.gui.onestep):
                    moves = strategy.get_moves()
                    if moves:
                        for move in moves:
                            self.process_client.move(move)
                    self.process_client.turn()
            else:
                moves = strategy.get_moves()
                if moves:
                    for move in moves:
                        self.process_client.move(move)
                self.process_client.turn()
                sleep(0.5)

        return player.is_alive  # for testing
Example #3
0
    def create_strategy(self, dict_arguments):
        # 形参{'trader_id': '1601', 'user_id': '800658', 'strategy_id': '01', 'OrderAlgorithm':'01', 'list_instrument_id': ['cu1611', 'cu1610']}
        # 判断数据库中是否存在trader_id
        if self.__DBM.get_trader(dict_arguments['trader_id']) is None:
            print("MultiUserTradeSys.create_strategy()数据库中不存在该交易员")
            return False
        # 判断数据库中是否存在user_id
        if self.__DBM.get_user(dict_arguments['user_id']) is None:
            print("MultiUserTradeSys.create_strategy()数据库中不存在该期货账户")
            return False
        # strategy_id格式必须为两位阿拉伯数字的字符串,判断数据库中是否已经存在该strategy_id
        if len(dict_arguments['strategy_id']) != 2:
            print("MultiUserTradeSys.create_strategy()策略编码数据长度不为二", len(dict_arguments['strategy_id']))
            return False

        print('===========================')
        print("CTPManager.create_strategy()创建策略实例", dict_arguments)
        for i in self.__list_user:
            if i.get_user_id().decode('utf-8') == dict_arguments['user_id']:
                obj_strategy = Strategy(dict_arguments, i, self.__DBM)    # 创建策略实例,user实例和数据库连接实例设置为strategy的属性
                i.add_strategy(obj_strategy)               # 将obj_strategy添加到user实例中的list_strategy
                # obj_strategy.set_DBM(self.__DBM)           # 将数据库连接实例设置为strategy的属性
                # obj_strategy.set_user(i)                   # 将user设置为strategy的属性
                self.__list_strategy.append(obj_strategy)  # 将策略实例添加到ctp_manager对象的__list_strategy属性

        # 字符串转码为二进制字符串
        list_instrument_id = list()
        for i in dict_arguments['list_instrument_id']:
            list_instrument_id.append(i.encode())
        # 订阅行情
        self.__md.sub_market(list_instrument_id, dict_arguments['user_id'], dict_arguments['strategy_id'])
Example #4
0
def bootstrap_sim(ticker, start, n_samples):
    raw_data = get_boto_client('s3').get_object(Bucket='stockdatalambda',
                                                Key=ticker + '_5.pickle')
    data = pickle.loads(raw_data['Body'].read())
    strat = Strategy(data, start_offset=start, n_samples=n_samples)
    calls = strat.run()
    return calls
Example #5
0
    def __init__(self):
        # Config set up. Environment overrides app.yaml
        with open("cfg/app.yaml", "r") as f:
            config = yaml.safe_load(f)
            config.update(os.environ)

        # Logger set up
        self._init_logger(config["log_dir"])
        self._logger.info("Initializing the App")

        # Quik connector
        self._connector = WebQuikConnector(conn=config["conn"],
                                           passwd=config["passwd"],
                                           account=config["account"])

        # Feed2Csv just receive price and level2 for single configured asset and write to data folder
        web_quik_feed = WebQuikFeed(self._connector,
                                    rabbit_host=config["rabbit_host"])
        # self._feed = Feed2Csv(web_quik_feed, config.sec_class, config.sec_code)

        # Broker is not implemented, just a stub.
        web_quik_broker = WebQuikBroker(connector=self._connector,
                                        client_code=config["client_code"],
                                        trade_account=config["trade_account"],
                                        rabbit_host=config["rabbit_host"])

        # Create feed, subscribe events
        # Todo: support making orders
        self._strategy = Strategy(web_quik_feed, web_quik_broker,
                                  config["sec_class"], config["sec_code"])
Example #6
0
def opt_wrapper(params):
    """
        optimizing for periods
    """
    # assign parameters
    periods_bol = int(params[0])  # space from optimizer returns floats
    periods_adx = int(params[1])
    periods_rsi = int(params[2])
    adx_value = int(params[3])

    ###### Define Simulations ######
    data = Data(start_date="20-03-01")  # historical data interfal: hours
    df = data.load()
    strategy = Strategy(df=df,
                        periods_bol=periods_bol,
                        periods_adx=periods_adx,
                        periods_rsi=periods_rsi,
                        adx_value=adx_value)

    account = Account(balance={"euro": 1000, "btc": 0}, av_balance=0.8)

    sim = Sim(strategy=strategy,
              account=account,
              stake_amount=50,
              stop_loss=0.02)
    sim_result = sim.start()

    # negate as optimization looks for a minimum
    sim_result = -sim_result["account"].balance["euro"]

    return sim_result
def createStrategyCombinations(historicalNodeCount):
    strategylist = list()
    for placementstrategy in ['druidcostbased', 'bestfit']:
        for routingstrategy in ['chooseleastloaded']:
            strategylist.append(
                Strategy(historicalNodeCount, placementstrategy,
                         routingstrategy))

    return strategylist
Example #8
0
    def traverse_ESMCCFR(self, state, player):

        if state.is_terminal():
            return state.get_utility(player)

        #default to chance player
        other_player = 3 - player
        player_turn = state.get_players_turn()
        possible_bets = self.available_bets.get_bets_as_numbers(
            state._my_contrib(player_turn), state._other_contrib(player_turn),
            self.abstracted)
        # Determine the strategy at this infoset
        infoset = state.get_infoset(player_turn)
        if infoset in self.infoset_strategy_map.keys():
            strategy = self.infoset_strategy_map[infoset]
        else:
            strategy = Strategy(len(possible_bets))
            self.infoset_strategy_map[infoset] = strategy

        player_strategy = strategy.calculate_strategy()

        if player_turn == player:
            # initialize expected value
            # value of a node h is the value player i expects to achieve if all players play according to given strategy, having reached h
            value = 0
            value_bet = [0] * len(player_strategy)
            for bet_index, bet in enumerate(possible_bets):
                # need to define adding an bet to a bets, make bet class
                memento = state.update(bet)

                # Traverse each bet (per iteration of loop) (each bet changes the bets)
                va = self.traverse_ESMCCFR(state, player)
                state.reverse_update(memento)

                value_bet[bet_index] = va

                # Update the expected value
                value += player_strategy[bet_index] * va
            for bet_index in range(len(possible_bets)):
                # Update the cumulative regret of each bet
                strategy.regret_sum[bet_index] += value_bet[bet_index] - value

            return value

        elif player_turn == other_player:
            # Sample one bet and increment bet counter
            bet_index = self.get_random_bet(player_strategy)
            bet = possible_bets[bet_index]
            strategy.count[bet_index] += 1

            memento = state.update(bet)
            val = self.traverse_ESMCCFR(state, player)
            state.reverse_update(memento)
            return val
        else:
            raise Exception('How did we get here? There are no other players')
Example #9
0
def test_defult():

    strategy = Strategy(start_data)
    next_move = strategy.move(world, game)

    print(next_move)
    test_move = Move(12, 1, 0)
    assert next_move.line_idx == test_move.line_idx
    assert next_move.speed == test_move.speed
    assert next_move.train_idx == test_move.train_idx
Example #10
0
    def __init__(self, symbol, sym, timeFrames, QCAlgorithm):

        self.tick = symbol
        self.algo = QCAlgorithm
        self.symbol = QCAlgorithm.Portfolio[sym]

        self.params = pd.Series({
            'is_long': False,
            'is_short': False,
            'is_sideways': False,
            'sideways': np.uint8(0),
            'sidewaysInit': None,
            ' trade': False,
            'pause': False,
            'strength': np.uint8(0),
            'tolerance': 1.01
        })

        self.inds = pd.Series({
            'AL':
            ALMA(self.symbol, 12, 26, 6, .15, tol=self.params.tolerance),
            'ATR':
            ATR(self.symbol, 40, 15),
            'AR':
            AROON(self.symbol, 50)
        })

        self.Strats = pd.Series({
            'm':
            Strategy(self.symbol, symbol, timeFrames['m'], QCAlgorithm),
            'h':
            Strategy(self.symbol, symbol, timeFrames['h'], QCAlgorithm)
        })
        self.secData = Data()
        self.data = pd.DataFrame({
            'openTime': [],
            'open': [],
            'high': [],
            'low': [],
            'close': []
        }).set_index('openTime')

        self.correlation = pd.Series({'pos': {}, 'neg': {}})
Example #11
0
 def developStrategy(self, stratfiles):
     stop = False
     count = 0
     while (not stop):
         strat = Strategy(self.__binary, self.__param, self.__directory,
                          self.__dumpJsonProfileFile, self.__outputFile,
                          self.__onlyApplyingStrat, self.__onlyGenStrat,
                          stratfiles, count, self.__execAllStrat)
         yield strat
         stop = strat.isLast()
         count += 1
Example #12
0
    def BT(self):
        self.data = DataImport('spx_vols.txt')
        tempData = self.data.PushData()
        AllDates = sorted(set(tempData['Date']))
        LocalStrategy = Strategy(self.data, self.portfolio)
        rate = self.portfolio.PrintRate()
        lable = [
            'Date', 'Delta', 'Gamma', 'Vega', 'Theta', '#Call', '#Put',
            '#CashSpot', '$P&L', '$Notional'
        ]
        self.output.append(lable)

        for OneDate in AllDates:
            LocalStrategy.Straddles(str(OneDate), True, 'W-FRI',
                                    self.MaxNotionalRatio, self.DaysToMaturity)
            DataForOneDate = self.data.singleDataColumn('Date', int(OneDate))
            self.portfolio.PortfolioDelta(rate, DataForOneDate)
            self.portfolio.PortfolioGamma(rate, DataForOneDate)
            self.portfolio.PortfolioVega(rate, DataForOneDate)
            self.portfolio.PortfolioTheta(rate, DataForOneDate)
            self.portfolio.PortfolioHedging(self.portfolio.PrintPortDelta(),
                                            DataForOneDate)
            self.portfolio.ExciseOptions(OneDate, DataForOneDate)
            self.portfolio.PortfolioPnL(DataForOneDate)
            self.portfolio.AddPortfolioPnLToNational()
            if self.portfolio.PrintNotional() > 0.0:
                temp = [
                    OneDate,
                    self.portfolio.PrintPortDelta(),
                    self.portfolio.PrintPortGamma(),
                    self.portfolio.PrintPortVega(),
                    self.portfolio.PrintPortTheta(),
                    self.portfolio.VolumeOfCalls(),
                    self.portfolio.VolumeOfPuts(),
                    self.portfolio.VolumeOfCashSpot(),
                    self.portfolio.TotalPortfolioPnL(),
                    self.portfolio.PrintNotional()
                ]
                self.output.append(temp)
            else:
                temp = [
                    OneDate,
                    self.portfolio.PrintPortDelta(),
                    self.portfolio.PrintPortGamma(),
                    self.portfolio.PrintPortVega(),
                    self.portfolio.PrintPortTheta(),
                    self.portfolio.VolumeOfCalls(),
                    self.portfolio.VolumeOfPuts(),
                    self.portfolio.VolumeOfCashSpot(),
                    self.portfolio.TotalPortfolioPnL(),
                    self.portfolio.PrintNotional()
                ]
                self.output.append(temp)
                break
Example #13
0
    def create_strategy(self, dict_arguments):
        # 不允许重复创建策略实例
        if len(self.__list_strategy) > 0:
            for i in self.__list_strategy:
                if i.get_strategy_id(
                ) == dict_arguments['strategy_id'] and i.get_user_id(
                ) == dict_arguments['user_id']:
                    print("CTPManager.create_strategy() 不能重复创建,已经存在user_id=",
                          dict_arguments['user_id'], "strategy_id=",
                          dict_arguments['strategy_id'])
                    return False

        # 遍历user对象列表,找到将要创建策略所属的user对象
        for i_user in self.__list_user:
            if i_user.get_user_id().decode(
            ) == dict_arguments['user_id']:  # 找到策略所属的user实例
                # 创建策略
                obj_strategy = Strategy(
                    dict_arguments,
                    i_user)  # 创建策略实例,user实例和数据库连接实例设置为strategy的属性
                # obj_strategy.set_DBManager(self.__DBManager)
                i_user.add_strategy(obj_strategy)  # 将策略实例添加到user的策略列表
                self.__list_strategy.append(
                    obj_strategy)  # 将策略实例添加到CTP_Manager的策略列表
                print("CTPManager.create_strategy() 创建策略,user_id=",
                      dict_arguments['user_id'], "strategy_id=",
                      dict_arguments['strategy_id'])
                # 为策略订阅行情
                list_instrument_id = list()
                for i in dict_arguments['list_instrument_id']:
                    list_instrument_id.append(i.encode())  # 将合约代码转码为二进制字符串
                self.__MarketManager.sub_market(list_instrument_id,
                                                dict_arguments['user_id'],
                                                dict_arguments['strategy_id'])

        # 判断内核是否初始化完成(所有策略是否初始化完成)
        if self.__init_finished:  # 内核初始化完成、程序运行中新添加策略,在界面策略列表框内添加一行
            print("CTPManager.create_strategy() 程序运行中添加策略,user_id =",
                  dict_arguments['user_id'], 'strategy_id =',
                  dict_arguments['strategy_id'])
            self.signal_insert_strategy.emit(obj_strategy)  # 内核向界面插入策略
            self.signal_hide_QNewStrategy.emit()  # 隐藏创建策略的小弹窗
        else:  # 内核初始化未完成
            # 最后一个策略初始化完成,将内核初始化完成标志设置为True
            # if self.__list_strategy_info[-1]['user_id'] == dict_arguments['user_id'] and \
            #                 self.__list_strategy_info[-1]['strategy_id'] == dict_arguments['strategy_id']:
            #     self.__init_finished = True  # CTPManager初始化完成
            pass
Example #14
0
def normal_sim():
    data = Data(start_date="19-12-01")  # historical data interfal: hours
    df = data.load()
    strategy = Strategy(df=df,
                        periods_bol=760,
                        periods_adx=56,
                        periods_rsi=10,
                        adx_value=20)
    account = Account(balance={"euro": 1000, "btc": 0}, av_balance=0.8)

    sim = Sim(strategy=strategy,
              account=account,
              stake_amount=50,
              stop_loss=0.02)
    sim_result = sim.start()

    return sim_result
Example #15
0
 def __init__(self):
     self.data = Strategy()
     self.ranks = {
         '2': 2,
         '3': 3,
         '4': 4,
         '5': 5,
         '6': 6,
         '7': 7,
         '8': 8,
         '9': 9,
         '10': 10,
         'J': 10,
         'Q': 10,
         'K': 10,
         'A': 'A'
     }
Example #16
0
    def run(self):
        status, start_data = self.remote_process_client.login(self.name)
        try:
            map_graph = self.remote_process_client.read_map()
            objects = self.remote_process_client.read_objects()
            strategy = Strategy(start_data)
            while strategy.in_progress:
                self.remote_process_client.update_objects(objects)
                moves = strategy.get_moves(objects, map_graph)
                if moves:
                    for move in moves:
                        self.remote_process_client.move(move)
                self.remote_process_client.turn()

        finally:
            self.remote_process_client.logout()
            self.remote_process_client.close()
Example #17
0
def main():
    logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.WARNING)
    first = timer()
    historical_data = utility.import_data(config.csv_file, config.hdf_file, config.path)
    load = timer()
    print("Data import time: " + '{0:0.1f} seconds'.format(load - first))

    # Create strategies for backtesting
    strategies = []
    for i in np.arange(config.required_profit[0], config.required_profit[1], config.required_profit[2]):
        for j in np.arange(config.required_pct_change_max[0], config.required_pct_change_max[1],
                           config.required_pct_change_max[2]):
            for k in np.arange(config.required_volume[0], config.required_volume[1], config.required_volume[2]):
                strategies.append(Strategy(i, 0, j, k))


    strategy_count = len(strategies)
    i = 1
    for ticker in config.tickers:
        mask = np.in1d(historical_data['ticker'].values, [ticker])
        historical_data_trim = historical_data[mask]
        if len(historical_data_trim) == 0:
            logging.warning("Ticker " + ticker + " not in historical data.")
            continue

        for strategy in strategies:
            print("Strategy(" + str(strategy.required_profit) + ", " + str(strategy.required_pct_change_min) + ", " +
                  str(strategy.required_pct_change_max) + ", " + str(strategy.required_volume) + ")" + " "
                  + str(i) + " of " + str(strategy_count) + " of total strategies")

            result = Result(ticker, strategy, historical_data_trim)
            if config.write_results:
                performance = utility.init_performance_table()
                utility.init_transactions_table(performance)
                utility.result_to_db(result)
            else:
                utility.plot_price(result.data, ticker)
            i = i + 1

    complete = timer()
    print("Runtime: " + '{0:0.1f} seconds'.format(complete - load))
def _read_row(row):
    hole = int(row[0])
    board = row[1]
    if board == 0:
        board = ()
    else:
        board = ((int(board), ), )
    bets_0 = _comma_split_int(row[2])
    bets_1 = _comma_split_int(row[3])

    infoset = InfoSet((hole, ), board, (bets_0, bets_1))

    strategy = Strategy(0)
    strategy.average_strategy = [
        float(x) / 100000 for x in _comma_split_int(row[4])
    ]
    sum_strategy = sum(strategy.average_strategy)
    for s in strategy.average_strategy:
        s /= sum_strategy

    return infoset, strategy
Example #19
0
 def __init__(self,
              playerNumber,
              action='C',
              reward=0.0,
              fitness=0.0,
              strategyType='random'):
     """ Create a new Player with action, reward, fitness """
     self.__action = action
     self.__strInstance = Strategy(playerNumber)
     self.__strInstance.set_strategyType(strategyType)
     self.__strInstance.set_currentAction(action)
     self.__reward = reward
     self.__rounds = 0
     self.__fitness = fitness
     self.__nextAction = None
     self.__strategyType = strategyType
     # while init, a temperoy next action will be generate.
     # it is not good for pupular one
     #self.set_nextAction()
     #self.__strategy = Strategy(strategyType, None)
     # set unique instance count (class variable used)
     type(self).counter += 1
     self.__uniqueId = type(self).counter
Example #20
0
from Curler import Curler
from Strategy import Strategy, StockActions
from Transaction import Transaction

apiAcess = "https://www.alphavantage.co/query?" \
           "function=TIME_SERIES_INTRADAY&" \
           "symbol=AAPL&" \
           "interval=5min&" \
           "outputsize=compact&" \
           "datatype=json&" \
           "apikey=IMA31P07MG9SPXRA";

intervallSEC = (60/5)
stocks = _collections.deque(maxlen=50)

strategys = [Strategy()]
investHistory = {}
wealth = 1000.0


######################################
# Transactions - Methods
######################################


def recordedTransaction(strategy: Strategy):
    """
    :param strategy: Our strategy we wanna check
    :type strategy:
    :return: True or false
    :rtype: bool
Example #21
0
elif player == 2:
    print("You are the small blind, you post", State.p2_contrib)
    print("Your private card is", Card.int_to_str(State.p2_card))

while not State.is_terminal():
    players_turn = State.get_players_turn()
    possible_actions = State.get_possible_actions(players_turn)
    infoset = State.get_infoset(players_turn)
    if players_turn == player:
        print("The current community card is",
              [Card.int_to_str(c) for c in infoset.flop_card])
        print("You can bet the following amounts:", possible_actions)
        action = int(input('Your action: '))
        State.update(players_turn, action)
    else:
        strategy = Strategy(len(possible_actions))
        if infoset in infoset_strategy_map.keys():
            strategy = infoset_strategy_map[infoset]
            print(
                "This strategy has seen this infoset before, and will play the following actions with the following probabilities"
            )
            print(possible_actions, strategy.get_average_strategy())
        else:
            print(
                "This strategy has never seen the following infoset before, and will therefore play randomly"
            )
            print(infoset)
        action = utility_methods.get_random_action(
            strategy.get_average_strategy())
        print("Opponent has decided to take action", possible_actions[action])
        State.update(players_turn, possible_actions[action])
Example #22
0
 def short_high_risk(self, params):
     strat = Strategy(6, params[0], params[1], params[2], params[3])
     return strat
Example #23
0
    def testAddingPlayer(self):
        self.assertEqual(self.game.playerCount(), 0, "no player at start")

        self.game.addPlayer(Player("Red", Strategy()))
        self.assertEqual(self.game.playerCount(), 1, "one player added")
Example #24
0
 def __init__(self):
     self.currentKey = None  # 存储的当前行
     self.currentKeyList = []  # 当前行的所有val值
     self.stragegy = Strategy()
Example #25
0
 def send_data(self):
     self.Strategy = Strategy.Strategy(self.data, self.coin_number,
                                       self.principal)
     return self.Strategy.analysis()
Example #26
0
def Enviro(stockList, Investment, numSpecies, genCount, shareCount, trainStart,
           trainEnd, testStart, testEnd, SaveNew, geneTest, evolutionPlot):
    avg = []
    mx = []
    graph = {}
    g = []
    TOPV = 0
    TOP = 0
    qg = []

    Environment = Strategy(stockList)
    pInvest = Investment
    startDay = trainStart
    DAYS = trainEnd
    inputList = {}
    startPriceList = {}
    endPriceList = {}

    #Make Portfolios

    for individual in range(numSpecies):
        Environment.addPortfolio("p" + str(individual), pInvest)

#Set inputList
    try:
        inputList = pickle.load(open("inputListA.p", "rb"))
        startPriceList = pickle.load(open("startPriceListA.p", "rb"))
        endPriceList = pickle.load(open("endPriceListA.p", "rb"))
    except (IOError):
        print "Writing New Pickle"
    for d in range(startDay, DAYS):
        if d in inputList:
            pass
        else:
            print "Adding Day", d
            Environment.setInputs("LUVBBF3", d, d + 1)
            inputList[d] = (Environment.getInputsF().copy())
            startPriceList[d] = Environment.getRecentPrice()
            endPriceList[d] = Environment.getEndPrice()
            pickle.dump(inputList, open("inputListA.p", "wb"))
            pickle.dump(startPriceList, open("startPriceListA.p", "wb"))
            pickle.dump(endPriceList, open("endPriceListA.p", "wb"))

#Make lists for randomization of initial population

    inputs = inputList[startDay]
    variables = inputs.keys()
    right = [[], []]
    weight = []
    fitness = {}
    delimeters = ["<", ">"]

    for valTop in np.arange(-1, 1.1, .1):
        right[0].append(round(valTop, 2))
    for y in variables:
        right[1].append(y)
    for z in np.arange(0, 1, .01):
        weight.append(round(z, 3))

#Construct initial population

    for portfolio in Environment.getPortfolios().keys():
        p = Environment.getPortfolios()[portfolio]
        p.addSpecies(inputs)
        for variable in Environment.getPortfolios()[portfolio].getSpecies(
        ).getGenes().keys():
            a = random.randint(0, 1)
            b = random.randint(0, len(right[a]) - 1)
            c = delimeters[random.randint(0, 1)]
            d = weight[random.randint(0, 99)]
            Environment.getPortfolios()[portfolio].getSpecies().addGene(
                variable, right[a][b], c, d)
        Environment.getPortfolios()[portfolio].getSpecies().setThresh(
            random.randrange(
                -len(Environment.getPortfolios()
                     [portfolio].getSpecies().getGenes()), 1) + 0.0,
            random.randrange(
                0,
                len(Environment.getPortfolios()
                    [portfolio].getSpecies().getGenes()) + 1) + 0.0)
        Environment.getPortfolios()[portfolio].getSpecies().setShareFactor(
            (random.randrange(1, 100)))
# Start loop of days and generations
    manager = mp.Manager()
    portDict = manager.dict()
    for gen in range(genCount):
        portDict.clear()
        t1 = time.time()

        print "Generation : ", gen + 1
        pList = []
        for portfolio in Environment.getPortfolios().keys():
            proc = mp.Process(target=MultiProcess1,
                              args=(portDict, Environment, startDay, DAYS,
                                    inputList, startPriceList, endPriceList,
                                    stockList, portfolio))
            pList.append(proc)
            proc.start()

        for proc in pList:
            if proc.is_alive():
                proc.join()

        for port in portDict.keys():
            Environment.getPortfolios()[port] = portDict[port]

#Get fitness of species
        for portfolio in Environment.getPortfolios().keys():
            #fitness[portfolio]=(Environment.getPortfolios()[portfolio].getCorrectList().count(1))*1.0/len(Environment.getPortfolios()[portfolio].getCorrectList())
            fitness[portfolio] = (Environment.getPortfolios(
            )[portfolio].getCorrectList().count(1)) * 1.0 / len(
                Environment.getPortfolios()[portfolio].getCorrectList())
            # Environment.getPortfolios()[portfolio].balanceList[-1]
#Find top 50%

        sorted_fitness = sorted(fitness.items(), key=operator.itemgetter(1))
        if (sorted_fitness[-1][-1] > TOPV):
            TOP = sorted_fitness[-1][0]
            TOPV = sorted_fitness[-1][1]

#Get bottom 50%

        bottom = sorted_fitness[0:(numSpecies / 2)]

        #Choose 50 pairs of top 50

        top = sorted_fitness[numSpecies / 2:numSpecies]
        topPair = []
        # for valTop in range(len(top)):
        #     pair=valTop
        #     while top[pair][0]==top[valTop][0]:
        #         pair=random.randint(0,len(top)-1)
        #     topPair.append((top[valTop][0],top[pair][0]))
        pairs = []
        for valTop in range(len(top)):
            for val2 in range(valTop + 1, len(top)):
                pairs.append((top[valTop][0], top[val2][0]))
        np.random.shuffle(pairs)
        topPair = pairs[:numSpecies]

        #For portfolio in 50 worst, set it equal to the breeding of a pair of top 50

        for valBot in range(len(bottom)):
            a = Environment.getPortfolios()[topPair[valBot]
                                            [0]].getSpecies().copy()
            b = Environment.getPortfolios()[topPair[valBot]
                                            [1]].getSpecies().copy()
            Environment.getPortfolios()[bottom[valBot][0]].resetAll(pInvest)
            Environment.getPortfolios()[bottom[valBot][0]].updateSpecies(
                a.breed(b).copy())

        q = []
        for portfolio in range(len(Environment.getPortfolios().keys())):
            for port2 in range(portfolio + 1,
                               len(Environment.getPortfolios().keys())):
                if (portfolio != port2):
                    q.append(
                        Environment.getPortfolios()[Environment.getPortfolios(
                        ).keys()[portfolio]].getSpecies().numEquals(
                            Environment.getPortfolios()[
                                Environment.getPortfolios().keys()
                                [port2]].getSpecies()))
        print np.round(
            100.0 * average(q) /
            len(Environment.getPortfolios()[Environment.getPortfolios().keys()
                                            [0]].getSpecies().getGenes()),
            2), "%"
        qg.append(100.0 * average(q) / len(Environment.getPortfolios()[
            Environment.getPortfolios().keys()[0]].getSpecies().getGenes()))
        print np.round(time.time() - t1, 2), "seconds"
        print
        print

        if (gen != genCount - 1):
            for valTop in range(len(top)):
                Environment.getPortfolios()[top[valTop][0]].reset(pInvest)

    print TOPV, "Fitness"
    if (len(Environment.getPortfolios()[TOP].getCorrectList()) == 0):
        print "bad things happend there was nothing there, uh hello"
    else:
        print "TRAIN CORRECT   : ", (
            Environment.getPortfolios()[TOP].getCorrectList().count(1) *
            1.0) / len(Environment.getPortfolios()[TOP].getCorrectList())
        print "TRAIN INCORRECT : ", (
            Environment.getPortfolios()[TOP].getCorrectList().count(-1) *
            1.0) / len(Environment.getPortfolios()[TOP].getCorrectList())
        print "TRAIN HELD      : ", (
            Environment.getPortfolios()[TOP].getCorrectList().count(0) *
            1.0) / len(Environment.getPortfolios()[TOP].getCorrectList())
        print "TRAIN GUESSED   : ", (
            Environment.getPortfolios()[TOP].getCorrectList().count(1) +
            Environment.getPortfolios()[TOP].getCorrectList().count(-1) *
            1.0) / len(Environment.getPortfolios()[TOP].getCorrectList())
    print "TRAIN BALANCE   : ", Environment.getPortfolios(
    )[TOP].getBalanceList()[-1]

    pToO = []

    for day in range(startDay, DAYS):
        inputs = inputList[day]
        startPrice = startPriceList[day]
        endPrice = endPriceList[day]

        # Get boolean statements for the day
        for gene in Environment.getPortfolios()[TOP].getSpecies().getGenes(
        ).keys():
            Environment.getPortfolios()[TOP].getSpecies().getGenes(
            )[gene].makeBoolean(inputs)


# Get species output and add corresponding behavior
# print Environment.getPortfolios()[portfolio].getBalance()/(0.0+startPrice),Environment.getPortfolios()[portfolio].getShareFactor()
        Environment.getPortfolios()[TOP].addBehavior(
            Environment.getPortfolios()[TOP].getSpecies().getOutput() >
            Environment.getPortfolios()[TOP].getSpecies().getRightThresh(),
            ("Buy", "LUV",
             int(Environment.getPortfolios()[TOP].getShareFactor() *
                 (Environment.getPortfolios()[TOP].getBalance() /
                  (0.0 + startPrice))), startPrice))
        Environment.getPortfolios()[TOP].addBehavior(
            Environment.getPortfolios()[TOP].getSpecies().getOutput() <
            Environment.getPortfolios()[TOP].getSpecies().getLeftThresh(),
            ("Short", "LUV",
             int(Environment.getPortfolios()[TOP].getShareFactor() *
                 (Environment.getPortfolios()[TOP].getBalance() /
                  (0.0 + startPrice))), startPrice))
        Environment.getPortfolios()[TOP].addGeneCorrect(endPrice - startPrice)
        #Go through with actions, sellBack at end of day and add balance to balanceList

        Environment.getPortfolios()[TOP].makeActions(startPrice)
        Environment.getPortfolios()[TOP].sellBack(stockList, endPrice)
        Environment.getPortfolios()[TOP].addBalance(
            Environment.getPortfolios()[TOP].balance)
        pToO.append((Environment.getPortfolios()[TOP].getSpecies().getOutput(),
                     (Environment.getPortfolios()[TOP].balanceList[-1] -
                      Environment.getPortfolios()[TOP].balanceList[-2]) / abs(
                          (Environment.getPortfolios()[TOP].balanceList[-1] -
                           Environment.getPortfolios()[TOP].balanceList[-2]))))
    np.savetxt("pCorrectToOutput.csv", pToO, delimiter=",")

    # if evolutionPlot == True:
    # plt.plot(Environment.getPortfolios()[TOP].getBalanceList())
    # plt.show()

    if geneTest == True:
        for gene in Environment.getPortfolios()[TOP].getGeneCorrect():
            Environment.getPortfolios()[TOP].getGeneCorrect()[gene] = average(
                Environment.getPortfolios()[TOP].getGeneCorrect()[gene])
        # plt.bar(range(len(Environment.getPortfolios()[TOP].getGeneCorrect())), Environment.getPortfolios()[TOP].getGeneCorrect().values(), align='center')
        # plt.xticks(range(len(Environment.getPortfolios()[TOP].getGeneCorrect())), Environment.getPortfolios()[TOP].getGeneCorrect().keys())
        # plt.show()
        lsta = []
        lstb = []
        for gene in Environment.getPortfolios()[TOP].getGeneCorrect():
            lsta.append(
                Environment.getPortfolios()[TOP].getGeneCorrect()[gene])
            lstb.append(Environment.getPortfolios()
                        [TOP].getSpecies().genes[gene].getWeight())
        # plt.plot(lsta,lstb,"go")
        print(np.corrcoef(lsta, lstb))
        # plt.show()
    #plt.plot(Environment.getPortfolios()[TOP].getShareCountList(),Environment.getPortfolios()[TOP].getOutputList(),"bo")
    # return IndividualTester.Test(Environment.getPortfolios()[TOP].getSpecies().getGenes(), testStart, testEnd, shareCount, Investment),(Environment.getPortfolios()[TOP].getCorrectList().count(1)*1.0)/len(Environment.getPortfolios()[TOP].getCorrectList())
    return IndividualTester.Test(Environment.getPortfolios()[TOP], testStart,
                                 testEnd)
Example #27
0
def Test(TopSpecies, testStart, testEnd):
    for x in range(1):
        try:
            with open('bList.csv', 'rb') as csvfile:
                balance = []
                for row in csvfile:
                    balance.append(float(row))
            Investment = balance[-1]
        except(IndexError):
            print "yikes"
            Investment = 100000

        stockList=["LUV"]
        avg=[]
        mx=[]
        graph={}
        g=[]
        TOPV=0
        Graph = []
    
                
        Environment = Strategy(stockList)
        numSpecies=1
        pInvest=Investment
        genCount=1
        startDay= testStart
        DAYS= testEnd
        inputList={}
        startPriceList={}
        endPriceList={}
        
    #Make Portfolios
    
        Environment.addPortfolio("p0",pInvest)
        Environment.getPortfolios()["p0"] = TopSpecies
        Environment.getPortfolios()["p0"].reset(Investment)
        
    
    #Set inputList
    
        try: 
            inputList = pickle.load( open( "inputListA.p", "rb" ) )
            startPriceList = pickle.load( open( "startPriceListA.p", "rb" ) )
            endPriceList = pickle.load( open( "endPriceListA.p", "rb" ) )
        except(IOError):
            print "Writing New Pickle"
        for d in range(startDay,DAYS):
            if d in inputList:
                print "Day", d, "Ready"
            else:
                print "Adding Day", d
                Environment.setInputs("LUVBBF3", d, d+1)
                inputList[d]=(Environment.getInputsF().copy())
                startPriceList[d]=Environment.getRecentPrice()
                endPriceList[d]=Environment.getEndPrice()
                pickle.dump( inputList, open( "inputListA.p", "wb" ) )
                pickle.dump( startPriceList, open( "startPriceListA.p", "wb" ) )
                pickle.dump( endPriceList, open( "endPriceListA.p", "wb" ) )
        
        
    #Make lists for randomization of initial population
        inputs=inputList[startDay]
        variables=inputs.keys()
        right=[[],[]]
        weight=[]
        fitness={}
        delimeters=["<",">"]
    
        for valTop in np.arange(-1,1.1,.1):
            right[0].append(round(valTop,2))
        for y in variables:
            right[1].append(y)    
        for z in np.arange(0,1,.01):
            weight.append(round(z,3))
             
    #Construct test population
        # for portfolio in Environment.getPortfolios().keys():
        #     p = Environment.getPortfolios()[portfolio]
        #     p.addSpecies(inputs)
            
        # for gene in TopSpecies:
        #     Environment.getPortfolios()['p0'].getSpecies().addGene(TopSpecies[gene].left,TopSpecies[gene].right, TopSpecies[gene].delimeter, TopSpecies[gene].weight)
                
    # Start loop of days and generations     
        
        for day in range(startDay, DAYS):
            inputs=inputList[day]  
            startPrice=startPriceList[day]
            endPrice=endPriceList[day] 
            
# Get boolean statements for the day   
     
            for portfolio in Environment.getPortfolios().keys():
                for gene in Environment.getPortfolios()[portfolio].getSpecies().getGenes().keys():
                    Environment.getPortfolios()[portfolio].getSpecies().getGenes()[gene].makeBoolean(inputs)    
                    
# Get species output and add corresponding behavior
        portfolio = "p0"
        print Environment.getPortfolios()[portfolio].getSpecies().getRightThresh(),"right"
        Environment.getPortfolios()[portfolio].addBehavior(Environment.getPortfolios()[portfolio].getSpecies().getOutput() > Environment.getPortfolios()[portfolio].getSpecies().getRightThresh() , ("Buy", "LUV",int(Environment.getPortfolios()[portfolio].getShareFactor()*(Environment.getPortfolios()[portfolio].getBalance()/(0.0+startPrice))),startPrice))
        Environment.getPortfolios()[portfolio].addBehavior(Environment.getPortfolios()[portfolio].getSpecies().getOutput() < Environment.getPortfolios()[portfolio].getSpecies().getLeftThresh(), ("Short", "LUV",int(Environment.getPortfolios()[portfolio].getShareFactor()*(Environment.getPortfolios()[portfolio].getBalance()/(0.0+startPrice))),startPrice))
        Environment.getPortfolios()[portfolio].addGeneCorrect(endPrice-startPrice)
#Go through with actions, sellBack at end of day and add balance to balanceList
        Environment.getPortfolios()[portfolio].makeActions(startPrice)
        Environment.getPortfolios()[portfolio].sellBack(stockList,endPrice)
        Environment.getPortfolios()[portfolio].addBalance(Environment.getPortfolios()[portfolio].balance)  

            # for gene in Environment.getPortfolios()["p0"].getGeneCorrect():
            #     Environment.getPortfolios()["p0"].getGeneCorrect()[gene]=average(Environment.getPortfolios()["p0"].getGeneCorrect()[gene])
            # plt.bar(range(len(Environment.getPortfolios()["p0"].getGeneCorrect())), Environment.getPortfolios()["p0"].getGeneCorrect().values(), align='center')
            # plt.xticks(range(len(Environment.getPortfolios()["p0"].getGeneCorrect())), Environment.getPortfolios()["p0"].getGeneCorrect().keys())
            # plt.show()
            # lsta=[]
            # lstb=[]
            # for gene in Environment.getPortfolios()["p0"].getGeneCorrect():
            #     lsta.append(Environment.getPortfolios()["p0"].getGeneCorrect()[gene])
            #     lstb.append(Environment.getPortfolios()["p0"].getSpecies().genes[gene].getWeight())
            # plt.plot(lsta,lstb,"go")
            # print(np.corrcoef(lsta,lstb))
            # plt.show()
    #Add balanceList to graph
        

        for portfolio in Environment.getPortfolios().keys():
            fitness[portfolio]=Environment.getPortfolios()[portfolio].balanceList[-1]
                     
        sorted_fitness = sorted(fitness.items(), key=operator.itemgetter(1))
   
        if(sorted_fitness[-1][-1]>TOPV):
            TOP=sorted_fitness[-1][0]
            TOPV=sorted_fitness[-1][1]
                
        print "TEST DAY        : ", DAYS
        print "TEST CORRECT    : ", (Environment.getPortfolios()["p0"].getCorrectList().count(1)*1.0)/len(Environment.getPortfolios()["p0"].getCorrectList())
        print "TEST INCORRECT  : ", (Environment.getPortfolios()["p0"].getCorrectList().count(-1)*1.0)/len(Environment.getPortfolios()["p0"].getCorrectList())
        print "TEST HELD       : ", (Environment.getPortfolios()["p0"].getCorrectList().count(0)*1.0)/len(Environment.getPortfolios()["p0"].getCorrectList())
        print "TEST GUESSED    : ", (Environment.getPortfolios()["p0"].getCorrectList().count(1)+Environment.getPortfolios()["p0"].getCorrectList().count(-1)*1.0)/len(Environment.getPortfolios()["p0"].getCorrectList())
        print "LEFT THRESH     : ", Environment.getPortfolios()[portfolio].getSpecies().getLeftThresh()
        print "RIGHT THRESH    : ", Environment.getPortfolios()[portfolio].getSpecies().getRightThresh()                   
        print "SHARE FACTOR    : ", Environment.getPortfolios()[portfolio].getShareFactor()
        print "OUTPUT          : ", Environment.getPortfolios()[portfolio].getSpecies().getOutput()
        print "INVESTMENT      : ", Investment
        print "TEST BALANCE    : ", Environment.getPortfolios()["p0"].getBalanceList()[-1]
        print "DAY CHANGE      : ", endPrice-startPrice
        print "DAY PROFIT      : ", Environment.getPortfolios()["p0"].getBalanceList()[-1]-Investment

        return  [(Environment.getPortfolios()["p0"].getCorrectList().count(1)*1.0)/len(Environment.getPortfolios()["p0"].getCorrectList()), Environment.getPortfolios()["p0"].getBalanceList()[-1]]
Example #28
0
 def __init__(self, name, sName=None):
     Player.__init__(self, name)
     self.strat = Strategy(sName)
     self.strategy = self.strat.strategyName
Example #29
0
 def medium_high_risk(self, params):
     strat = Strategy(7, params[0], params[1], params[2], params[3])
     return strat
Example #30
0
 def long_high_risk(self, params):
     strat = Strategy(8, params[0], params[1], params[2], params[3])
     return strat