예제 #1
0
    def calculateStats( self, p_market_list, p_exchange, p_interval ):
        market_stats = [] #pd.DataFrame()
        error_list   = []
        
        market_count = 0
        total_markets = len(p_market_list)
        print("Market count: "+str(total_markets))
        for market_name in p_market_list:
            market_count += 1
            mkt = Market(market_name, p_exchange, p_interval)
            

            try:
                print(str(market_count)+" of "+str(total_markets)+" "+mkt.getMarketName() +", " + mkt.getInterval() +" on " + mkt.getExchange())
                mkt_data = mkt.readMarketDataCSV()       # self.readMarketDataCSV()
            except IOError:
#                 print("ERROR: Portfolio::calculateStats   " + )
                self.printException(inspect.currentframe().f_lineno, market_name + " does not exist. "+str(sys.exc_info()[0]))
                error_list = ["NO FILE: "+market_name] + error_list
                print(); print(); print()
            except:
                self.printException(inspect.currentframe().f_lineno,"ERROR: unknown - "+str(sys.exc_info()[0]))
#                 print(sys.exc_info()[0])
                error_list = ["ERROR: "+market_name] + error_list
                print(); print(); print()
            else:
                mstats = MarketStats()
                mstats.calculateStats( market_name, mkt_data )
                market_stats.append(mstats.toDict()) 
#                 print(mstats.toDataFrame()) 
                 
        #convert to dataframe
        return pd.DataFrame.from_records(market_stats, index="market_name")
예제 #2
0
class Trader:
    def __init__(self, player):
        self.market = Market("Trader")
        self.success = True
        self.player = player

    def negotiate_prices(self):
        chance_of_success = random.randint(30, 70) * (self.player.barterer / 3)
        if chance_of_success >= 40:
            pass
        else:
            pass

    def rob(self):
        chance_of_success = random.randint(30,
                                           70) * (self.player.cannoneer / 3)
        if chance_of_success >= 60:
            for i in self.market.items.keys():
                self.player.add_to_inv(i)
        else:
            self.player.ship.health -= 50

    def buy(self, item):
        self.market.remove_from_market

    def sell(self, item):
        self.market.sell(item, self.player)

    def negotiate(self):
        print("for later")
예제 #3
0
class TestWorker(object):
    def __init__(self, name, globalAC):
        self.env = Market(filename, size, train_size, test_size, valid_size,
                          CATEGORY)
        self.name = name
        self.AC = ACNet(name, globalAC)
        self.AC.pull_global()

    def reset(self):
        self.AC.pull_global()

    def work(self, flag="test"):
        total_step = 1
        buffer_s, buffer_a, buffer_r = [], [], []
        s = self.env.reset_for_test(flag)
        while True:
            a = self.AC.choose_action(s, test=True)
            s_, r, done, info = self.env.step(a)
            print("a:", a, "r:", r, "time:", self.env.time, "len:",
                  len(self.env.observation_space))
            buffer_s.append(s[-1])
            buffer_a.append(a)
            buffer_r.append(r)

            s = s_
            total_step += 1
            if done:
                prob = np.array(buffer_r) + 1
                print("prof:", prob.prod(), "len", len(prob))

                with open(LOG_DIR + "/state_detail.txt", 'w') as f:
                    f.write(str(buffer_s))
                break
        return prob
예제 #4
0
class Worker(object):
    def __init__(self, name, globalAC, step=60):
        self.env = Market(filename,size,train_size,test_size,valid_size,CATEGORY)
        self.name = name
        self.AC = ACNet(name, globalAC)
        self.step = step

    def set_scope(self,scope):
        return self.env.set_env(scope[0],scope[1])


    def work(self):
        global GLOBAL_RUNNING_R, GLOBAL_EP
        total_step = 1
        buffer_s, buffer_a, buffer_r = [], [], []
        while not COORD.should_stop() and GLOBAL_EP < MAX_GLOBAL_EP:
            s = self.env.reset()
            R = [1]
            while True:
                a = self.AC.choose_action(s)
                s_, r, done, info = self.env.step(a)
                #print("a:",a,"r:",r,"time:",self.env.time,"len:",len(self.env.observation_space))
                buffer_s.append(s)
                buffer_a.append(a)
                buffer_r.append(r)
                R.append((r+1)*R[-1])

                if total_step % self.step == 0 or done:   # update global and assign to local net
                    if done:
                        v_s_ = 0   # terminal
                    else:
                        v_s_ = SESS.run(self.AC.v, {self.AC.s: s_[np.newaxis, :]})[0, 0]
                    buffer_v_target = []
                    for r in buffer_r[::-1]:    # reverse buffer r
                        v_s_ = r + GAMMA * v_s_
                        buffer_v_target.append(v_s_)
                    buffer_v_target.reverse()

                    buffer_s, buffer_a, buffer_v_target = np.vstack(buffer_s), np.array(buffer_a), np.vstack(buffer_v_target)
                    feed_dict = {
                        self.AC.s: buffer_s,
                        self.AC.a_his: buffer_a,
                        self.AC.v_target: buffer_v_target,
                    }
                    self.AC.update_global(feed_dict)

                    buffer_s, buffer_a, buffer_r = [], [], []
                    self.AC.pull_global()

                s = s_
                total_step += 1   
                if done:
                    GLOBAL_RUNNING_R[self.name].append(R[-1])
                    GLOBAL_EP += 1
                    print(self.name,"Ep:", GLOBAL_EP, "prof:",R[-1],"len",len(R))  
                    #for temp in R:
                        #print(temp+1)                                                 

                    break
예제 #5
0
def main(random):
    """
    Buy on the dips
        run <num_runs> simulations
        tracking output over 20 years
        plot a return distribution
    """

    # parameters specific to this continuous purchase model
    title = ("Random" if random else "Real sequence") + " simulations of "
    monthly = True     # monthly simulations

    # mappings from parameters into point formats
    colors = { 0.10:"r", 0.15:"y", 0.20:"g", 0.25:"c" }
    symbols = [ "x", ".", "o", "+", "*" ]

    legends = []
    simulator = Market(monthly=monthly)
    # for a range of plausible dip thresholds
    for max_dip in (0.10, 0.15, 0.20, 0.25):
        # are we willing to buy part on smaller dips
        for buy_points in [1, 2, 3]:
            results = []
            # a statistically interesting number of runs
            for runs in range(num_runs * 2 if random else num_runs):
                sequence = simulator.rates(length=num_years*12, random=random)
                results.append( strat_dips(sequence, max_dip, buy_points, monthly) )

            # summarize the results
            mean = sum(results) / len(results)
            sigma = statistics.stdev(results)
            report = "{}({}%/{}) over {} years: mean={:.2f}, sigma={:.2f}"
            print(report.format(my_name, int(100*max_dip), buy_points, num_years, mean, sigma))

            # bucketize and display the results
            granularity = bucketwidth(results)
            buckets = bucketize(results, granularity)
            (x_values, y_values) = distribution(buckets, granularity)

            plt.plot(x_values, y_values, colors[max_dip] + symbols[buy_points])
            legends.append(str(int(max_dip*100)) + "% dip/" + str(buy_points))

        print("")   # blank line between changes in threshold

    # put up the title, axes, and data
    plt.title(title + my_name)
    plt.xlabel(str(num_years) + "-year return")
    plt.ylabel("probability")
    plt.legend(legends)
    if output is None:
        plt.show()
    else:
        print("saving distribution plot as " + output)
        plt.savefig(output)
        plt.close()
예제 #6
0
    def __init__(self, instrumentos, test_days):

        market_train = Market(test_days, instrumentos, tipo="train")
        market_test = Market(test_days, instrumentos, tipo="test")

        self.test_results = 0
        self.test_results_list = []

        self.player_train = Player(market_train, 0)
        self.player_test = Player(market_test, 0)
        self.Q = Qlearn()
예제 #7
0
    def __init__(self, p_market, p_strategy, p_start_date, p_end_date):
        '''  Constructor '''
        self.market = Market(p_market)
        self.result = pd.DataFrame
        self.start_date = p_start_date
        self.end_date = p_end_date

        # dynamically create the strategy name
        # https://stackoverflow.com/questions/4821104/python-dynamic-instantiation-from-string-name-of-a-class-in-dynamically-imported
        module = __import__(p_strategy)
        class_ = getattr(module, p_strategy)
        self.strategy = class_(p_start_date, p_end_date)
예제 #8
0
def main(random):
    """
    For all-in and all-out
        run <num_runs> simulations
        tracking output over 20 years
        plot a return distribution
    """

    # parameters specific to this continuous purchase model
    title = ("Random" if random else "Real sequence") + " simulations of "
    monthly = False  # annual simulations

    legends = []
    simulator = Market(monthly=monthly)
    # purchases spread out over 1-5 years
    for in_cds in [True, False]:
        results = []
        # a statistically interesting number of runs
        for runs in range(num_runs * 2 if random else num_runs):
            sequence = simulator.rates(length=num_years, random=random)
            results.append(strat_all(sequence, in_cds, monthly))

        # summarize the results
        mean = sum(results) / len(results)
        sigma = statistics.stdev(results)
        report = "{} {}, {} years: mean={:.2f}, sigma={:.2f}"
        print(
            report.format(my_name, "CDs" if in_cds else "market", num_years,
                          mean, sigma))

        # bucketize and display the results
        granularity = bucketwidth(results)
        buckets = bucketize(results, granularity)
        (x_values, y_values) = distribution(buckets, granularity)

        plt.plot(x_values, y_values, "go" if in_cds else "b*")
        legends.append("CDs" if in_cds else "market")

    # put up the title, axes, and data
    plt.title(title + my_name)
    plt.xlabel(str(num_years) + "-year return")
    plt.ylabel("probability")
    plt.legend(legends)
    if output is None:
        plt.show()
    else:
        print("saving distribution plot as " + output)
        plt.savefig(output)
        plt.close()
예제 #9
0
파일: net.py 프로젝트: Tsangares/market
def viewMarket():
    marketName = request.args.get('name')
    market = Market.load(marketName)
    print(market)
    return render_template('market.html',
                           market=market.data(),
                           businesses=market.businesses)
예제 #10
0
    def create_mmbot(self, symbol):
        print("CREATE MMBOT %s FOR %s WITH %d$ QUOTA TO RUN LIVE:%d" %
              (self.botname, symbol, self.quota, self.live))
        mylogger = MyLogger(symbol, self.live)

        if self.brokertype == "ibkr":
            print("CREATE IBKR BROKER")
            broker = IBBroker(self.budget, self.quota, mylogger)
        else:
            print("CREATE SIM MARKET")
            broker = Broker(self.budget, self.quota, mylogger)

        if self.marketdata == "ibkr":
            print("CREATE IBKR MARKET DATA")
            market = IBMarket(self.rth)
        else:
            print("CREATE ALPHA VANTAGE MARKER DATA")
            market = Market()

        # create and add bot to list of bots
        self.bots.append(
            MMBot(budget=self.quota,
                  symbol=symbol,
                  period=self.period,
                  live=self.live,
                  debug=self.debug,
                  email=self.email,
                  daily=self.daily,
                  broker=broker,
                  market=market,
                  mylogger=mylogger,
                  since=self.since,
                  liquidate=self.liquidate))
예제 #11
0
 def __init__(self, argv):
     self.markets = []
     marketFileDefined = 0
     try:
         opts, args = getopt.getopt(argv, "hv", ["help", "verbose", "markets=", "testmode="])
     except getopt.GetoptError:
         self.ExitUsage(1, "Bad arguments.")
     for opt, arg in opts:
         if opt in ("-h", "--help"):
             self.ExitUsage()
         elif opt in ("-v", "--verbose"):
             Globales.verbose = 1
         elif opt == "--markets":
             marketFileDefined = 1
             self.marketFile = arg
             try:
                 f = open(self.marketFile, "r")
                 for line in f:
                     infos = line.split()
                     if infos[0][0] != '#':
                         self.markets.append(Market(self, infos[0], infos[1], infos[2]))
             except IOError as e:
                 self.ExitUsage(1, "Bad arguments. Failed to open the markets description file.")
         elif opt == "--testmode":
             Globales.testMode = 1
             Globales.testModeFile = arg
     if marketFileDefined == 0:
         self.ExitUsage(1, "Bad arguments. Please define a markets description file.")
     self.interface = Interface(self)
예제 #12
0
def main(random):
    """
    Only buy in at lows
        run <num_runs> simulations
        tracking output over 20 years
        plot a return distribution
    """

    # parameters specific to this continuous purchase model
    title = ("Random" if random else "Real sequence") + " simulations of "
    monthly = True  # monthly simulations
    formats = ["w.", "r.", "y+", "g*", "co"]

    legends = []
    simulator = Market(monthly=monthly)
    for fractions in [1, 2, 3, 4]:
        results = []
        # a statistically interesting number of runs
        for runs in range(num_runs * 2 if random else num_runs):
            sequence = simulator.rates(length=num_years * 12, random=random)
            results.append(strat_bottom(sequence, fractions, monthly))

        # summarize the results
        mean = sum(results) / len(results)
        sigma = statistics.stdev(results)
        report = "{} over {} years in {} pieces: mean={:.2f}, sigma={:.2f}"
        print(report.format(my_name, num_years, fractions, mean, sigma))

        # bucketize and display the results
        granularity = bucketwidth(results)
        buckets = bucketize(results, granularity)
        (x_values, y_values) = distribution(buckets, granularity)

        plt.plot(x_values, y_values, formats[fractions])
        legends.append("fractions=" + str(fractions))

    # put up the title, axes, and data
    plt.title(title + my_name)
    plt.xlabel(str(num_years) + "-year return")
    plt.ylabel("probability")
    plt.legend(legends)
    if output is None:
        plt.show()
    else:
        print("saving distribution plot as " + output)
        plt.savefig(output)
        plt.close()
예제 #13
0
 def addMarkets( self, p_markets, p_exchange, p_interval ):
     ''' INPUT     list of market names as strings
         OUTPUT    nil
         FUNCTION  creates market objects for each market listed and adds to the internal list
     '''
     for market in p_markets:
         mkt_obj = Market(market, p_exchange, p_interval)
         self.portfolio_market_strategies.append(mkt_obj)
예제 #14
0
 def create_market(obfuscation_dist=None):
     return Market(
         tau=config['tau'],
         N_firms=config['N_firms'],
         price_dist=create_distribution(config['price_dist']),
         obfuscation_dist=create_distribution(config['obfuscation_dist'])
         if obfuscation_dist is None else obfuscation_dist,
         demand_curve=create_curve(config['demand']))
예제 #15
0
 def __init__(self, share_name, sma_20_periods, ema_1_periods,
              ema_2_periods, q_learn):
     self.sma_20_periods = sma_20_periods
     self.ema_1_periods = ema_1_periods
     self.ema_2_periods = ema_2_periods
     self.data = Data(share_name, 50, self.sma_20_periods,
                      self.ema_1_periods, self.ema_2_periods)
     self.market = Market(self.data)
     self.player = Player(market=self.market, balance=0)
     self.Q = q_learn
예제 #16
0
def test():
    '''
	Unit test for Simulator
	'''
    from Simulator import Simulator
    from Strategy import SimpleStrategy
    from Market import Market
    from User import User
    # from Gui import Gui
    from PriceSource import PriceSource
    import datetime
    from PyQt4 import QtGui
    app = QtGui.QApplication([])

    strategy = SimpleStrategy()

    name = 'Weichao Qiu'
    initial_money = 10000
    user = User(name, initial_money, strategy)

    # sz01 = PriceSource('000001.SZ')

    market = Market()
    # market.AddPriceSource(sz01)  # Done by ctor of Market class
    market.AddUser(user)

    simulator = Simulator(market)
    # simulator.gui = Gui()

    simulator.start_date = datetime.datetime(2011, 1, 10)
    simulator.end_date = datetime.datetime(2011, 3, 15)
    # simulator.end_date = simulator.start_date + datetime.timedelta(180)
    # simulate for 6 months
    # simulator.end_date = datetime.datetime.now()
    simulator.Simulate()  # Begin simulation

    print 'end of simulation'

    app.exec_()
예제 #17
0
def download(package,
             email,
             password,
             country,
             operator,
             device,
             path='/tmp',
             devname='passion',
             sdklevel=19):
    try:
        if not package:
            raise ValueError('No package')
        market = Market(email, password)
        market.login()
        operator = Operator(country, operator)
        request = AssetRequest(package, market.token, device, operator,
                               devname, sdklevel)
        (url, market_da) = market.get_asset(request.encode())
        Util.download_apk(package, url, market_da, path)
        return True
    except:
        return False
예제 #18
0
class MarketStrategy(object):
    '''
    classdocs
    '''
    def __init__(self, p_market, p_strategy, p_start_date, p_end_date):
        '''  Constructor '''
        self.market = Market(p_market)
        self.result = pd.DataFrame
        self.start_date = p_start_date
        self.end_date = p_end_date

        # dynamically create the strategy name
        # https://stackoverflow.com/questions/4821104/python-dynamic-instantiation-from-string-name-of-a-class-in-dynamically-imported
        module = __import__(p_strategy)
        class_ = getattr(module, p_strategy)
        self.strategy = class_(p_start_date, p_end_date)

    def getMarket(self):
        '''  Getter '''
        return self.market

    def getStrategy(self):
        '''  Getter '''
        return self.strategy

    def getResult(self):
        '''  Getter '''
        return self.result

    def getMarketName(self):
        ''' Getter '''
        return self.market.getMarketName()

    def toPrint(self):
        ''' Printer '''
        return "Market: " + self.market.getMarketName(
        ) + ", Strategy: " + self.strategy.strategy_name + ", Start: " + self.start_date.strftime(
            "%B %d, %Y") + " End: " + self.end_date.strftime("%B %d, %Y")
예제 #19
0
    def parseData(input):
        """Parse input and return a dict keyed by hour containing a sorted dict keyed by day
           Since we only compare data points from same hr there is not benefit in sorting first dict"""
        hourToDayMtkData = {}

        for marketData in input:
            marketData = Market(*marketData.split(","))
            hour = marketData.hour
            if marketData.hour not in hourToDayMtkData.keys():
                hourToDayMtkData[hour] = {marketData.dayOfMonth: marketData}
            else:
                if marketData.dayOfMonth not in hourToDayMtkData[hour].keys():
                    hourToDayMtkData[hour].update(
                        {marketData.dayOfMonth: marketData})
                # It is possible to have 0, 1, or more than 1 row of input for a given combination of hour of day and day of month. If there is more than 1 row,
                #  the net (ie. total) value of the assets for that hour and day should be calculated.
                else:
                    currentAssetVal = hourToDayMtkData[hour][
                        marketData.dayOfMonth].marketValue
                    netAstVal = marketData.marketValue + currentAssetVal
                    hourToDayMtkData[hour][marketData.dayOfMonth] = Market(
                        netAstVal, hour, marketData.dayOfMonth)
        return hourToDayMtkData
예제 #20
0
    def test(self):

        result = True
        self.assertEqual(result, True, "OH NO!")

        # Get the data
        #         mkt = Market("Test-bitcoin-SuperTrend")
        mkt = Market.fromTesting(test_file)
        mkt_data = mkt.readMarketDataCSV(p_testing=True)

        # Create the Indicator
        p_wma_period = 90
        p_wma_column = "Close"
        p_fast_st_multiplier = 3
        p_fast_st_period = 15
        p_slow_st_multiplier = 3.5
        p_slow_st_period = p_fast_st_period
        stc = SuperTrendCross(p_wma_period, p_wma_column, p_fast_st_multiplier,
                              p_fast_st_period, p_slow_st_multiplier,
                              p_slow_st_period)

        print()
        print()
        print()
        print("=== Super Trend Cross Calc === === === === === ")
        print()
        print()
        print()
        print("=== Original market ================")
        print(mkt_data.head(0))
        print(mkt_data.tail(5))

        # Calculate the indicator
        stc.calculate(mkt_data)

        # Print Results
        print()
        print()
        print()
        print("=== Super Trend Cross Calc === === === === === ")
        print(mkt_data.head(5))
        print(mkt_data.tail(10))

        #         xlApp=win32com.client.Dispatch("Excel.Application")
        #         wb = xlApp.Workbooks.Open(Filename="C:\Full Location\To\excelsheet.xlsm")

        #         # WRITE IN DATA FRAME TO SHEET 5
        writer = ExcelWriter(settings.testing_file_path + 'Test Results.xlsx')
        mkt_data.to_excel(writer, 'Results')
        writer.save()
예제 #21
0
 def play(self, market: Market):
     remaining_firms = copy(market.firms)
     # average obfuscation level
     t_mean = market.compute_mean_obfuscation_level()
     search_cost = self.compute_keep_search_cost(tau=market.tau,
                                                 t_mean=t_mean)
     while self.should_keep_searching(market=market,
                                      search_cost=search_cost):
         firm = remaining_firms[np.random.choice(len(remaining_firms))]
         remaining_firms.remove(firm)
         if self.p_0_firm is None\
                 or self.p_0_firm.price > firm.price:
             self.p_0_firm = firm
         self.t_0 += (market.tau + firm.obfuscation_level)
         self.search_count += 1
         if len(remaining_firms) == 0:
             break
     # buy from firm offering lowerest price
     if self.p_0_firm is not None:
         self.p_0_firm.sell()
예제 #22
0
 def _create_market(self, funds):
     p = Portfolio(10000)
     m = Market(funds, p, self._logger)
     return m
예제 #23
0
@author: Carl
"""
from Stocks import Stocks
from Learner import Learner
from Market import Market
from Twitter import Twitter

import datetime

start = datetime.date(2010, 1, 1)
end = datetime.date(2017, 3, 31)

stocks = Stocks(start, end, ['GOOG', 'AAPL', 'SPY'])
learner = Learner(stocks)
market = Market(stocks, 1000000, 1)
twitter = Twitter('./PickledTweets')

ticker = stocks.get_tickers()[0]
features = [
    feature for feature in stocks.get_features()
    if feature not in ['Open', 'High', 'Low', 'Close']
]
target = 'Daily Log Return Direction'
test_size = 0.1
scale_data = True

learner.prepare_data(ticker, features, target, test_size, scale_data)
print("---Classifiers---")
learner.train_classifiers()
예제 #24
0
sys.path.append('../src/trade/')
import os
import time

from Market import Market
from Trade import Trade

if len(sys.argv) == 1:
    print '''启动/停止系统:./ctpService start/stop
启动/停止队列:./ctpService startQ/stopQ
查看状态:./ctpService status'''
    sys.exit()

cmd = sys.argv[1]

market = Market()
trade = Trade()

if cmd == 'start':
    appKey = sys.argv[2]
    trade.start(appKey)
    market.start(appKey)
elif cmd == 'stop':
    market.stop()
    trade.stop()
elif cmd == 'status':
    appKey = sys.argv[2] if len(sys.argv) == 3 else ''
    os.system('./find.sh %s' % appKey)
elif cmd == 'startQ':
    os.system('./startQ.sh')
elif cmd == 'stopQ':
예제 #25
0
        for pattern, handler in self.handlers.items():
            matches = re.match(pattern, self.path)
            if matches:
                handler(*matches.groups())
                return

        # No handler found
        self.send_response(404)
        self.end_headers()


if __name__ == "__main__":
    try:
        with open('config.json', 'rb') as f:
            config = json.loads(f.read().decode('utf-8'))
    except:
        print('Unable to load config file')
        sys.exit(1)

    market = Market(config['email'], config['password'])
    market.login()

    operator = Operator(config['country'], config['operator'])

    try:
        server = HTTPServer(('', 5000), ApkServerHandler)
        server.serve_forever()
    except KeyboardInterrupt:
        print('^C received, shutting down the web server')
        server.socket.close()
예제 #26
0
    def __init__(self,
                 db,
                 initial_liquidity,
                 log_broker,
                 log_broker_ref,
                 log_port,
                 min_date=datetime(2006, 1, 3),
                 max_date=datetime(2016, 11, 4),
                 min_value=0,
                 max_value=float("inf"),
                 fig=None):
        """
        Create Broker
        :param db: connexion in to DB
        :type db: Manager_DB.DbConnection.DbConnection
        :param initial_liquidity: value to portfolio to begin
        :type initial_liquidity: int
        :param log_broker: path file to write log of Broker
        :type log_broker: str
        :param log_broker_ref: path file to write log of Broker ref_curve
        :type log_broker_ref: str
        :param log_port: path file to write log of Portfolio
        :type log_port: str
        :param min_date: date to start simulation
        :type min_date: datetime.datetime
        :param max_date: date included when the simulation stops
        :type max_date: datetime.datetime
        :param min_value: min stock value to do transaction
        :type min_value: int
        :param max_value: max stock value to invest in a company
        :type max_value: int
        :param fig: figure for update plot QT
        :type fig: matplotlib.figure.Figure
        """
        # The Market where the broker is buying and selling stocks
        self._market = Market(min_date, max_date, db)

        self.log_broker = open(log_broker, 'w')
        self.log_broker.write(
            "date;cash;stocks_value;bill;value_with_ref_curve\n")

        # The portfolio contains all the stocks bought for the current day and time and
        # the cash money that can be used instantly by the broker to buy stocks.
        self._portfolio = Portfolio(initial_liquidity, min_value, max_value,
                                    log_port)

        # The bill is how much the broker has charged for its services.
        self._bill = 0.0

        # Dictionary that keeps track of the value of our portfolio at any given time since the start of the simulation.
        # Key: Date ; Value: Total of our assets for any given
        self._hist_market_value = {}
        self._simulation_port_value = []  # keep in memory value in order dates

        # A commission is how much the broker is asking to get paid for a given transaction.
        # Initially, there is no commission. It must be added with a setter.
        self._commission = self._Commission()

        # Filters are functions used to select which companies should be in our portfolio at any given time.
        self._sell_filters = []
        self._buy_filters = []

        self._fig = fig
        self._data_ref_curve = HelperFunctionQt.read_reference_curve(
            log_broker_ref, True)
        self._ref_curve_value = []
예제 #27
0
        r = self.__2array(self.trend_index.find({'date': {
            "$lte": self.__now()
        }}).sort([("date", -1)]).limit(W))
        r.reverse()
        return r


if __name__ == '__main__':
    from pymongo import MongoClient
    from datetime import datetime
    from Market import Market
    import matplotlib
    matplotlib.use('Agg')
    import matplotlib.pyplot as plt
    client = MongoClient("mongodb://115.28.4.59:27017")
    market = Market(client.trans.cnbtc, client.depths.cnbtc, client.trans_stat.cnbtc_min, client.trans_stat.cnbtc_hr, client.trans_stat.cnbtc_index)
    now = datetime.now()
    index_list = market.getMarketTrendIndexInLastN(60 * 24)
    index_480 = [x['index_480'] for x in index_list]
    index_240 = [x['index_240'] for x in index_list]
    index_120 = [x['index_120'] for x in index_list]
    index_60 = [x['index_60'] for x in index_list]
    t = [x['date'] for x in index_list]
    plt.figure(1, figsize=(15, 15))

    price_list = market.getMALastNMin(60 * 24, 10)

    plt.subplot(511)
    plt.plot(t, index_60, '-')
    plt.title("Market Trend with N = 60 min")
    plt.grid()
예제 #28
0
from Market import Market
from TestingAlgorithms import BuyInTheMorning, TwoAverages
from CSVForexTicksHandler import CSVForexTicksHandler
from TickBroker import TickBroker
from Portfolio import Portfolio
import logging as log
from datetime import datetime

log.basicConfig(level=log.DEBUG, format=
	"%(asctime)s || %(levelname)s: %(filename)s:%(lineno)d - "
	"%(funcName)s: %(message)s")

datahandler = CSVForexTicksHandler(
    '/home/ioan/Dokumente/MarketMaker/marketmaker/histdata/',
    ['EURUSD'])

market = Market(TwoAverages, datahandler, TickBroker, Portfolio, 'test')
market.run(10)
예제 #29
0
# -*- coding: utf-8 -*-
"""
Created on Tue May 15 22:07:40 2018

@author: Administrator
"""

from Market import Market

start = '2018-01-01'
end = '2018-02-01'
market = Market()
market.init(start, end)
market.init_data()
market.CreateAccount()

market.account.buy("600232.SH", 7.62, 1000)
market.account.buy("000001.SZ", 12.0, 1000, -1)
market.account.ShowPosition()

market.tomorrow()
market.account.ShowPosition()

market.tomorrow()
market.account.ShowPosition()
del market
예제 #30
0
    def test(self):

        # Test Parameters
        output_info = True
        target_file = "Results-bitcoin-SuperTrend"
        test_file = "bitcoin_TEST"
        target_columns = [
            "Date", "H-L", "H-PC", "L-PC", "True_Range", "ATR_15", "basic_ub",
            "basic_lb", "final_ub", "final_lb", "Super_Trend"
        ]
        test_columns = [
            "Date", "H-L", "H-PC", "L-PC", "True_Range", "ATR_15", "basic_ub",
            "basic_lb", "final_ub", "final_lb", "Super_Trend"
        ]
        # ["Date","H-L","H-PC","L-PC","TR","ATR","BUB","BLB","FUB","FLB","ST"]
        st_period = 15
        st_multiplier = 3
        st_name_extension = ""

        # Output Headings
        print("This is a test of SUPERTREND")
        print("Test Data: " + test_file + "    Target Data: " + target_file)
        print("Columns: " + str(test_columns))
        print("Parameters: period: " + str(st_period) + "   Multiplier: " +
              str(st_multiplier))

        # Get the data
        mkt = Market.fromTesting(test_file)
        mkt_data = mkt.readMarketDataCSV(p_testing=True)

        # Calcualte the Indicators
        supertrend = SuperTrend(st_period, st_multiplier, st_name_extension)
        mkt_data = supertrend.calculate(mkt_data)

        # Read in the target results
        target_data = self.readTargetResults(target_file, target_columns)

        # Format boolean columns
        # None                       # convert to boolean

        # Output the Testing Results
        if output_info:
            print()
            print()
            print()
            print("=== Target_data . head  === === === === === ")
            print(target_data.head(16))
            print()
            print("=== Target_data . tail === === === === === ")
            print(target_data.tail(5))

            print()
            print()
            print()
            print("=== Market data Results . head === === === === === ")
            print(mkt_data.head(16))  #[test_columns]
            print()
            print("=== Market data Results . tail === === === === === ")
            print(mkt_data.tail(5))  # [test_columns]

        # Assertions
        if output_info:
            print()
            print("=== Series Equal DATE === === === === === ")
            #             assert_series_equal( mkt_data["Date"], target_data["Date"])
            print("PASS")
def main():
        print("\n\tGooglePlay Downloader - Directly download apks from GooglePlay to your PC.\n" +
              "\tCopyleft Simone Margaritelli <*****@*****.**>\n" +
              "\thttp://www.evilsocket.net\n\n")

        defaults = {
            'email': None,
            'password': None,
            'package': None,
            'country': None,
            'operator': None,
            'device': None,
            'sdklevel': '19',
            'devname': 'hammerhead',
            'dry_run': False
        }

        usage = """usage: %prog [options]

EXAMPLE:
        %prog --email [email protected] --password your-password --name com.arttech.xbugsfree --country "Italy" --operator "3" --device your-device-id
"""
        parser = ExtendedOptionParser(defaults=defaults, usage=usage)

        parser.add_option_with_default("-e", "--email", action="store", dest="email", help="Your android account email.")
        parser.add_option_with_default("-p", "--password", action="store", dest="password", help="Your android account password.")
        parser.add_option_with_default("-n", "--name", action="store", dest="package", help="Package identifier (com.something.name).")
        parser.add_option_with_default("-c", "--country", action="store", dest="country", help="Your country.")
        parser.add_option_with_default("-o", "--operator", action="store", dest="operator", help="Your phone operator.")
        parser.add_option_with_default("-d", "--device", action="store", dest="device", help="Your device ID (can be obtained with this app https://play.google.com/store/apps/details?id=com.redphx.deviceid) .")
        parser.add_option_with_default("-s", "--sdklevel", action="store", dest="sdklevel", help="Android SDK API level (default is 19 like Android 4.4).")
        parser.add_option_with_default("-m", "--devname", action="store", dest="devname", help="Device name (default 'passion' like HTC Passion aka Google Nexus One.")
        parser.add_option_with_default("-t", "--dry-run", action="store_true", dest="dry_run", help="Test only, a.k.a. dry run")
        parser.add_option_with_default("--proxy", action="store", dest="proxy", default=None, help="Proxy server to use. Use the form user:pass@host:port")
        parser.add_option_with_default("--proxy-auth-digest", action="store_true", default=None, dest="proxy_auth_digest", help="Use digest authentication in proxies. Default is basic authentication")
        parser.add_option("-f", "--config", action="store", dest="config", default=None, help="Load additional settings from the specified config file. Parameters in this file always overwrite command line settings.")

        (o, args) = parser.parse_args()

        option_pool = {}

        for key in defaults:
            option_pool[key] = getattr(o, key)

        if o.config is not None:
            config = json.loads(open(o.config, 'rb').read().decode('utf-8'))
            for key in config:
                # in Python 2.x, json results are unicode
                option_pool[key] = str(config[key])

        if option_pool['email'] is None:
            print("No email specified.")

        elif option_pool['password'] is None:
            print("No password specified.")

        elif option_pool['package'] is None:
            print("No package specified.")

        elif option_pool['country'] is None or option_pool['country'] not in Operator.OPERATORS:
            print("Empty or invalid country specified, choose from: \n\n" + ", ".join(Operator.OPERATORS.keys()))

        elif option_pool['operator'] is None or option_pool['operator'] not in Operator.OPERATORS[option_pool['country']]:
            print("Empty or invalid operator specified, choose from: \n\n" + ", ".join(Operator.OPERATORS[option_pool['country']].keys()))

        elif option_pool['device'] is None:
            print("No device id specified.")

        elif int(option_pool['sdklevel']) < 2:
            print("The SDK API level cannot be less than 2.")

        else:
            if option_pool['proxy']:
                set_default_proxy(
                    option_pool['proxy'],
                    'digest' if option_pool['proxy_auth_digest'] else 'basic')
            print("@ Logging in ...")

            market = Market(option_pool['email'], option_pool['password'])
            market.login()

            print("@ Requesting package ...")

            operator = Operator(option_pool['country'], option_pool['operator'])

            request = AssetRequest(option_pool['package'], market.token, option_pool['device'], operator, option_pool['devname'], int(option_pool['sdklevel']))
            (url, market_da) = market.get_asset(request.encode())

            if not option_pool['dry_run']:
                print("@ Downloading...\n")

                Util.download_apk(option_pool['package'], url, market_da)
예제 #32
0
파일: main.py 프로젝트: KateKayasheva/ABM
def run(test_number):
    agents = generate_agents(params, nrt=50, nmm=40, nhft=10)

    agents_dict = agents_dictionary(agents)
    market = Market()
    market.create_database()

    for day in range(0, 150):

        print('DAY:', day)
        for a in agents:
            if a.money < 0 or a.stocks < 0: print('N------------N')
            print(a)
            print(a.wealth())
            if a.money < 0 or a.stocks < 0: print('N------------N')

            # if a.money < 0 or a.stocks < 0:
            #     market.export_db('test_early_exit_{}.csv'.format(test_number))
            #     return 0

            orders = a.order(day=day, market=market)
            time = datetime.datetime.now().timestamp()  # time in seconds
            # print("time:", time)
            # print("\n")
            """
            Need to iterate since market makers can have two orders stored as a list of dictionaries
            """
            # print(orders)
            if type(orders) == list:
                for order in orders:
                    market.add_order(order, time)
            elif type(orders) == dict:
                market.add_order(orders, time)
            else:
                try:
                    market.add_order(orders, time)
                finally:
                    pass
                    # print("Cannot add order, Order type:",  type(orders))

        print("BUY:", market.buybook)
        print("SELL:", market.sellbook)

        print('PRICES DAY BEFORE:', market.preprices)

        market.match_orders(agents_dict)
        # print("BUY:", market.buybook)
        # print("SELL:", market.sellbook)
        market.clear_books()
        print('--------------------------------------------')
        print(market.data)

    market.export_db('test_{}.csv'.format(test_number))
예제 #33
0
class Broker:
    def __init__(self,
                 db,
                 initial_liquidity,
                 log_broker,
                 log_broker_ref,
                 log_port,
                 min_date=datetime(2006, 1, 3),
                 max_date=datetime(2016, 11, 4),
                 min_value=0,
                 max_value=float("inf"),
                 fig=None):
        """
        Create Broker
        :param db: connexion in to DB
        :type db: Manager_DB.DbConnection.DbConnection
        :param initial_liquidity: value to portfolio to begin
        :type initial_liquidity: int
        :param log_broker: path file to write log of Broker
        :type log_broker: str
        :param log_broker_ref: path file to write log of Broker ref_curve
        :type log_broker_ref: str
        :param log_port: path file to write log of Portfolio
        :type log_port: str
        :param min_date: date to start simulation
        :type min_date: datetime.datetime
        :param max_date: date included when the simulation stops
        :type max_date: datetime.datetime
        :param min_value: min stock value to do transaction
        :type min_value: int
        :param max_value: max stock value to invest in a company
        :type max_value: int
        :param fig: figure for update plot QT
        :type fig: matplotlib.figure.Figure
        """
        # The Market where the broker is buying and selling stocks
        self._market = Market(min_date, max_date, db)

        self.log_broker = open(log_broker, 'w')
        self.log_broker.write(
            "date;cash;stocks_value;bill;value_with_ref_curve\n")

        # The portfolio contains all the stocks bought for the current day and time and
        # the cash money that can be used instantly by the broker to buy stocks.
        self._portfolio = Portfolio(initial_liquidity, min_value, max_value,
                                    log_port)

        # The bill is how much the broker has charged for its services.
        self._bill = 0.0

        # Dictionary that keeps track of the value of our portfolio at any given time since the start of the simulation.
        # Key: Date ; Value: Total of our assets for any given
        self._hist_market_value = {}
        self._simulation_port_value = []  # keep in memory value in order dates

        # A commission is how much the broker is asking to get paid for a given transaction.
        # Initially, there is no commission. It must be added with a setter.
        self._commission = self._Commission()

        # Filters are functions used to select which companies should be in our portfolio at any given time.
        self._sell_filters = []
        self._buy_filters = []

        self._fig = fig
        self._data_ref_curve = HelperFunctionQt.read_reference_curve(
            log_broker_ref, True)
        self._ref_curve_value = []

    #######################################################################################################
    #                                      Commission
    #######################################################################################################

    class _Commission:
        def __init__(self, type_commission="no_commission", commission_val=0):
            """Represent a commission charged by the broker to execute a transaction.

            An instance of _Commission should always be created by one of the two functions
            set_flat_fee_commission() and set_percent_commission().

            :param type_commission: "percent", "flat_fee" or "no_commission". Default is "no_commission"
            :type type_commission: str
            :param commission_val: The value of the commission. In % or $ depending of its type.
            :type commission_val: float | int
            :return: None
            """
            self.value = commission_val
            self.type = type_commission

        def calculate(self, transaction_value):
            """Calculate the commission that the broker should charge to the client for the transaction.

            :param transaction_value: The value in $ of the transaction made (buy or sell)
            :type transaction_value: float | int
            :return: The commission that should be charged by the broker for this transaction.
            :rtype: float | int
            """
            return {
                "percent": transaction_value * self.value,
                "flat_fee": self.value,
                "no_commission": 0.0
            }[self.type]

    #######################################################################################################
    #                                         Setters
    #######################################################################################################
    def set_flat_fee_commission(self, value):
        """Set the commission of the broker to a flat fee for any transaction done.

        :param value: The flat fee that will be charged for any transaction. Must be > 0.
                      If == 0, put to no_commission.
        :type value: float | int
        :return: None
        """
        if value > 0:
            self._commission = self._Commission("flat_fee", value)
        else:
            self._commission = self._Commission()

    def set_percent_commission(self, value):
        """Set the commission of the broker to a percentage of any transaction done.

        :param value: Percentage of the transaction that will be charged. Must be > 0 and < 100.
                      If == 0, put to no_commission.
        :type value: float | int
        :return: None
        """
        if value > 0:
            self._commission = self._Commission("percent", value)
        else:
            self._commission = self._Commission()

    def add_sell_filters(self, *filters):
        """Add one or many filters to reduce the list of companies in our portfolio.

        Functions are regrouped in "Filters.py". They must take as parameters a list of strings, an
        object "Market" and an object "Portfolio".
        The list contains all symbols of the companies in our portfolio that will be sold.
        The "Market" is an instance of the class "Market"; ditto for "Portfolio".

        The filter function must return the same type of list as the one in the parameters. It should be
        reduced to remove all companies in our portfolio that we want to keep because they satisfy a
        specific criterion (or many criteria).

        :param filters: One or many functions as described above.
        :return: None
        """
        for f in filters:
            self._sell_filters.append(f)

    def add_buy_filters(self, *filters):
        """Add one or many filters to reduce the list of companies where we should invest.

        Functions are regrouped in "Filters.py". They must take as parameters a list of strings, an
        object "Market" and an object "Portfolio".
        The list contains all symbols of the companies in our portfolio that will be sold.
        The "Market" is an instance of the class "Market"; ditto for "Portfolio".

        The function must return the same type of list as the one in the parameters. It should be reduced
        to remove all companies where we don't want to invest because they don't satisfy a specific
        criterion (or many criteria).

        :param filters: One or many functions as described above.
        :return: None
        """
        for f in filters:
            self._buy_filters.append(f)

    def add_max_nb_of_stocks_to_buy(self, nb_stocks):
        self._portfolio.set_max_number_of_stocks_to_buy(nb_stocks)

    def calculate_global_ranking(self, calculate_glob_ranking, dict_params):
        """
        Set variable to say if we compute global ranking
        :param calculate_glob_ranking: boolean to say if we need calculate global ranking
        :type calculate_glob_ranking: bool
        :param dict_params: dict criteria for compute global ranking
        :type dict_params: dict{dict}
        :return: None
        """
        self._market._calculate_global_ranking = calculate_glob_ranking
        self._market._dict_criteria_glob_ranking = dict_params
        self._market.compute_global_ranking()

    #######################################################################################################
    #                                Run the simulation
    #######################################################################################################
    def _sell(self):
        """
        Sell company
        :return: None
        """
        # Get the list of all companies that we can sell (those we have in our portfolio);
        lst = self._portfolio.get_companies()

        # Filter the list to only keep the companies that should be sold;
        for f in self._sell_filters:
            lst = f.run(lst, self._market, self._portfolio)

        # For all companies that we should sell, sell 'em;
        for symbol in lst:
            # Instruct the portfolio to sell all stocks it has for this company
            gain = self._portfolio.sell_all_stocks(
                symbol, self._market.get_price(symbol),
                self._market.get_current_date())

            # If it succeeded, calculate the commission
            if gain > 0:
                commission = self._commission.calculate(gain)
                self._bill += commission

    def _buy(self):
        """
        Buy company
        :return: None
        """
        # Get the list of all companies that we can buy (those trading that day in the market);
        lst = self._market.get_trading_stocks()

        # Filter the list to only keep the companies that we should buy;
        # TODO : Remove those that we already have and which are maxed out.
        for f in self._buy_filters:
            lst = f.run(lst, self._market, self._portfolio)

        # For all companies that we should buy, as long as we have money, buy 'em (random order)
        random.shuffle(lst)
        for symbol in lst:
            # Attempt to buy as many stocks as possible for this company
            # TODO : Allow to choose a different algorithm for buying
            cost = self._portfolio.buy_stocks(symbol,
                                              self._market.get_price(symbol),
                                              self._market.get_current_date())

            # If it succeeded, calculate the commission
            if cost > 0:
                commission = self._commission.calculate(cost)
                self._bill += commission

            # Validate if we can continue to buy
            if not self._portfolio.can_buy():
                break

    def get_value(self, d):
        return d

    def _tracking(self, mode_debug=False):
        """
        Log transaction of day
        :param mode_debug: if want print the logs
        :type mode_debug: bool
        :return: None
        """
        # Calculate how much we have
        self._simulation_port_value.append(
            self._portfolio.get_cash_money() +
            self._portfolio.get_value_of_portfolio(self._market) - self._bill)
        self._hist_market_value[
            self._market.get_current_date()] = self._simulation_port_value[-1]
        self._ref_curve_value.append(
            float(
                self._data_ref_curve.get(str(self._market.get_current_date()),
                                         [0])[-1]) *
            self._simulation_port_value[-1])
        if mode_debug:
            print("{}\t\t{}".format(
                self._market.get_current_date(),
                self._hist_market_value[self._market.get_current_date()]))
        self.log_broker.write("{};{};{};{};{}\n".format(
            self._market.get_current_date(), self._portfolio.get_cash_money(),
            self._portfolio.get_value_of_portfolio(self._market), self._bill,
            self._ref_curve_value[-1]))

    def run_simulation(self, mode_debug=False):
        """
        Start simulation
        :param mode_debug: if want print the logs
        :type mode_debug: bool
        :return: None
        """
        # As long as there is a new business day in our simulation;
        trading = True
        while trading:
            # Sell companies in our portfolio that satisfy our criteria for selling;
            self._sell()

            # If we have enough money, buy companies trading this day that satisfy our criteria for buying;
            if self._portfolio.can_buy():
                self._buy()

            # Keep track of our assets
            self._tracking(mode_debug=mode_debug)

            # Go to the next trading day.
            trading = self._market.next()

        # Update plot QT
        if self._fig:
            # Calculate portfolio_value with ref_curve for ref_curve optimal
            HelperFunctionQt.update_plot(
                self._fig, sorted(self._hist_market_value.keys()),
                self._simulation_port_value, self._ref_curve_value)
            self._fig.savefig(
                self.log_broker.name.replace('log_brok', 'simulation')[:-3] +
                'png',
                format='png')
        if mode_debug:
            self._portfolio.print_portfolio()