def __init__(self, currency, actorNumber, resource, min_init_amountToSell, max_init_amountToSell, min_init_priceDivergence, max_init_priceDivergence, trade, converging_value): #Setting base values self.currency = currency self.actorNumber = actorNumber self.resource = resource self.trade = trade #If there is no productivity convergence then this should be -1 self.converging_value = converging_value self.min_init_amountToSell = min_init_amountToSell self.max_init_amountToSell = max_init_amountToSell self.min_init_priceDivergence = min_init_priceDivergence self.max_init_priceDivergence = max_init_priceDivergence #Start initaion of enviroment self.dayNum = 0 if currency == True: self.market = Market.NiceCurrencyMarket( self.resource.amountOfUniqueResources, self.resource, self.resource.GetCurrency()) else: self.market = Market.NiceMarket( self.resource.amountOfUniqueResources, self.resource) self.CreateActors(self.actorNumber)
def sweep_variations(funds_and_expense_ratios, leverage_ratio, num_samples, num_trajectories_to_save_as_figures, outfilepath): for scenario in LEV_ETF_SCENARIOS.keys(): dir = path.join(outfilepath, LEV_ETF_SCENARIOS[scenario]) if not os.path.isdir(dir): os.mkdir(dir) tax_rate = 0 funds_and_expense_ratios_to_use = copy.copy(funds_and_expense_ratios) leverage_ratio_to_use = leverage_ratio if "Match theory" in scenario: investor = Investor.Investor(monthly_probability_of_layoff=0, only_paid_in_first_month_of_sim=True, initial_emergency_savings=0) market = Market.Market(inflation_rate=0,medium_black_swan_prob=0, large_black_swan_prob=0) elif "Default" in scenario: market = Market.Market() investor = Investor.Investor() else: raise Exception("scenario type not supported") if "3X" in scenario: leverage_ratio_to_use = 3.0 if "no expense ratios" in scenario: for key in funds_and_expense_ratios.keys(): funds_and_expense_ratios_to_use[key] = 0 if "taxes" in scenario: tax_rates = TaxRates.TaxRates() if "moderate taxes" in scenario: tax_rate = MODERATE_ANNUAL_FRACTION_OF_SHORT_TERM_CAP_GAINS * tax_rates.short_term_cap_gains_rate_plus_state() elif "high taxes" in scenario: tax_rate = HIGH_ANNUAL_FRACTION_OF_SHORT_TERM_CAP_GAINS * tax_rates.short_term_cap_gains_rate_plus_state() print "\n==Scenario: %s==" % scenario many_runs(funds_and_expense_ratios_to_use, tax_rate, leverage_ratio_to_use, num_samples, investor, market, path.join(dir,""), num_trajectories_to_save_as_figures)
def TEST_resource_trading_multiple_trades_with_one(): number_of_resources = 2 number_of_actors = 3 resource = Resource(0, 0, 0, 0, number_of_resources, number_of_actors) actor1 = 0 actor2 = 1 actor3 = 2 resource1 = 0 resource2 = 1 resource1_amount = 20 resource2_amount = 20 resource23_amount = 10 resource.resourceArray[actor1][0][resource2] = resource1_amount resource.resourceArray[actor2][0][resource1] = resource2_amount resource.resourceArray[actor3][0][resource2] = resource23_amount resource_before = resource.GetTotalResources() bartermark = Market.NiceMarket(number_of_resources, resource) bartermark.PostResource(resource1, resource2, 10, 20, actor1) bartermark.PostResource(resource2, resource1, 40, 20, actor2) bartermark.PostResource(resource1, resource2, 5, 10, actor3) bartermark.MatchTrades() if resource_before == resource.GetTotalResources() and resource.resourceArray[actor2][0][resource2] == 30 and resource.resourceArray[actor2][0][resource1] == 5 and resource.resourceArray[actor1][0][resource2] == 0 and resource.resourceArray[actor1][0][resource1] == 10 and resource.resourceArray[actor3][0][resource2] == 0 and resource.resourceArray[actor3][0][resource1] == 5: print("TEST_resource_trading_multiple_trades_with_one successful") else: print("TEST_resource_trading_multiple_trades_with_one failed")
def create_data_2(nb_mois_test): df_start=ap.load_data("airbnb_data.csv") df_start=df_start.loc[(df_start["room_type"]=="Entire home/apt") & (df_start["price"]>=100) & (df_start["price"]<=200)] df_final=da.review_data(df_start,"new-york-city") nombre_de_mois_test = nb_mois_test df_final=df_final.reset_index(drop=True) df_final=df_final.loc[df_final.shape[0]-nombre_de_mois_test:df_final.shape[0]-1] df_glob_sales = pd.DataFrame(columns= ["Prix","Achat_Tot","Date","Part_Strat"]) #parcours de prix for n in range (300): print(n, "% effectués") #parcours de proportion for i in range(100): #on créé le marché market=mkt.Market(100,200,30,1-(i/100),0.85,0.15,df_start,df_final) p_trace=[rd.randrange(100,200)] p = rd.randrange(100,200) #parcours de date for k in range(df_final.shape[0]): choice = rd.random() #prix random if choice <0.25 : p = rd.randrange(100,200) if choice >= 0.25 and choice < 0.5 : up = rd.randrange(1,10) p = p + up if choice >= 0.5 and choice < 0.75 : down = rd.randrange(1,10) p = p + down if choice >= 0.75 : p = p p_trace.append(p) achat_naiv, achat_strat = market.updates(p,p_trace,k) df_glob_sales = df_glob_sales.append({"Prix":p,"Achat_Tot":(achat_naiv+achat_strat),"Date":k+1,"Part_Strat":(i/100)},ignore_index=True) print(df_glob_sales) df_glob_sales.to_csv("Data_Model_2.csv",index=False)
def main(): number_of_resources = 4 number_of_actors = 10000 resource = Resource(0, 100, -0.2, 0.2, number_of_resources, number_of_actors) print(resource.GetTotalResources()) bartermark = Market.NiceMarket(number_of_resources, resource) print(resource.GetTotalResources()) for _ in range(2): resource.Produce() print(resource.GetTotalResources())
def CreateMarket(self, name): #Market별로 상이한 API를 생성하고 Market instance를 생성한다. api= self.CreateAPI(name) if api == None: print(name+": API creation failed!!!") return None else: print(name+": API createion successful") return Market(name, api)
def __init__(self, player_list, verbose=False, interest_rate=0.10): """ @param player_list List of Player objects @param interest_rate is in range [0,1] """ self.verbose = verbose map_width = 200 # Dimensions of map map_height = 100 resolution_x = 2 # Resolution to render the map at resolution_y = 3 # Game using the simple small map. # node_list = ["Academy", "City", "Gallery", "Junkyard", "Office", "Park", "Stadium", "Tree", "Weather Station"] # self.map = Map(node_list, map_width, map_height, resolution_x, resolution_y, seed=2354) # Game using a medium map. node_list = list(string.ascii_uppercase) self.map = Map(node_list, map_width, map_height, resolution_x, resolution_y, seed=23624) # Game using a large map. #node_list = list(string.ascii_uppercase) + list(string.ascii_lowercase) #self.map = Map(node_list, map_width, map_height, resolution_x, resolution_y, seed=2360) random.seed(time.time()) self.markets = {node: Market() for node in self.map.get_node_names() } # need to randomise markets params BUG! self.have_researched = { node: [] for node in self.map.get_node_names() } # list of player ids that hace researched this node self.turn_num = 0 self.num_players = 0 # next player id is this + 1 self.players = { } # key=integer index into player_list value=tuple indexed by INFO_* self.interest = interest_rate for p in player_list: self.add_player(p)
def graph_margin_vs_regular_trajectories(regular_wealths, margin_wealths, outfilepath, iter_num): num_days = len(regular_wealths) assert len(margin_wealths) == num_days, "Input arrays not the same length." default_market = Market.Market() x_axis = [ float(day) / margin_leverage.DAYS_PER_YEAR for day in xrange(num_days) ] pyplot.plot(x_axis, regular_wealths, label="regular") pyplot.plot(x_axis, margin_wealths, label="margin") pyplot.title( "Trajectories of regular vs. margin investing for one simulation run") pyplot.xlabel("Years since beginning") pyplot.ylabel("Nominal asset value ($)") pyplot.legend() pyplot.savefig(outfilepath + "_regularvsmar_iter%i" % iter_num) pyplot.close()
def graph_lev_ETF_and_underlying_trajectories(regular_ETF, lev_ETF, outfilepath, iter_num): num_days = len(regular_ETF) assert len(lev_ETF) == num_days, "Input arrays not the same length." default_market = Market.Market() x_axis = [ float(day) / default_market.trading_days_per_year for day in xrange(num_days) ] pyplot.plot(x_axis, regular_ETF, label="regular ETF") pyplot.plot(x_axis, lev_ETF, label="leveraged ETF") pyplot.title( "Trajectories of regular vs. leveraged ETF for one simulation run") pyplot.xlabel("Years since beginning") pyplot.ylabel("Nominal asset value ($)") pyplot.legend() pyplot.savefig(outfilepath + "_regularvslev_iter%i" % iter_num) pyplot.close()
def register(self, marketName, updateTick=0.5, override=False): """ Create and add the market with marketName to the stack return a market """ marketName = marketName.upper().replace(" ", "") if hash(marketName) in self.Stack.keys(): if override is False: print( "\nNo market created,a market with the same name is already in the stack\n" ) return self.Stack[hash(marketName)] else: print( "\nMarket overriden,a market with the same name is already in the stack\n" ) self.Stack[hash(marketName)] = Market.Market(marketName, self.Exchange, updateTick) return self.Stack[hash(marketName)]
def __init__(self, params): advertisingBudget = params['AdvertisingBudget'] marketNPS = params['MarketNPS'] self.product = Product.Product(advertisingBudget) self.market = Market.Market(self.product, marketNPS) self.population = params['Population'] self.innovators = int(self.population * params['Innovators']) self.earlyAdopters = self.population - self.innovators purchaseProbability = params['PurchaseProbability'] coefAdvertise = params['CoefficiencyOfAdvertisement'] coefNPS = params['CoefficiencyOfNPS'] self.people = [ Person.Innovator(self.market, self.product, purchaseProbability) for i in range(self.innovators) ] + [ Person.EarlyAdopter(self.market, self.product, coefNPS, coefAdvertise) for i in range(self.earlyAdopters) ] self.market.people = self.people
def TEST_resource_trading_inverse_size(): number_of_resources = 2 number_of_actors = 2 resource = Resource(0, 0, 0, 0, number_of_resources, number_of_actors) actor1 = 0 actor2 = 1 resource1 = 0 resource2 = 1 resource1_amount = 100 resource2_amount = 60 resource.resourceArray[actor1][0][resource2] = resource1_amount resource.resourceArray[actor2][0][resource1] = resource2_amount resource_before = resource.GetTotalResources() bartermark = Market.NiceMarket(number_of_resources, resource) bartermark.PostResource(resource1, resource2, 50, 100, actor1) bartermark.PostResource(resource2, resource1, 20, 60, actor2) bartermark.MatchTrades() if resource_before == resource.GetTotalResources() and resource.resourceArray[actor2][0][resource2] == 100 and resource.resourceArray[actor2][0][resource1] == 10 and resource.resourceArray[actor1][0][resource2] == 0 and resource.resourceArray[actor1][0][resource1] == 50: print("TEST_resource_trading_inverse_size successful") else: print("TEST_resource_trading_inverse_size failed")
def TEST_resource_trading_not_even_ratio(): number_of_resources = 2 number_of_actors = 2 resource = Resource(0, 0, 0, 0, number_of_resources, number_of_actors) actor1 = 0 actor2 = 1 resource1 = 0 resource2 = 1 resource1_amount = 20 resource2_amount = 20 resource.resourceArray[actor1][0][resource2] = resource1_amount resource.resourceArray[actor2][0][resource1] = resource2_amount resource_before = resource.GetTotalResources() bartermark = Market.NiceMarket(number_of_resources, resource) bartermark.PostResource(resource1, resource2, 10, 20, actor1) bartermark.PostResource(resource2, resource1, 30, 20, actor2) bartermark.MatchTrades() if resource_before == resource.GetTotalResources() and resource.resourceArray[actor2][0][resource2] == 20 and resource.resourceArray[actor2][0][resource1] == 10 and resource.resourceArray[actor1][0][resource2] == 0 and resource.resourceArray[actor1][0][resource1] == 10: print("TEST_resource_trading_not_even_ratio successful") else: print("TEST_resource_trading_not_even_ratio failed")
''' Created on Mar 29, 2019 @author: shruthi ''' from Market import * from Shop import * market = Market() def main(): print("Welcome to Downtown Farmers Market!!!") # get choice from user to perform various operations flag = True while flag: choice = str( raw_input( "Please Enter your choice : \n1. Farmer \n2. Customer \n3. Quit \n" )) if choice == '1': print("Please check the below details and select your shop") print("The total area available for market is 15,000 square feet") print("Total number of shops in Market are 10") print("Each shop is of 1000 square feet") print('The number of shops available for registrat3ion -->', market.total_availability()) #get the details from user flag = False while not flag: try: inp = int( input(
# Market pipe main_conn_market, market_conn = multiprocessing.Pipe( ) #Création of the pipe between main process and Market Process. # houses pipe houses_pipes = [multiprocessing.Pipe() for i in range(numberOfHouses)] ###MAIN choice = input( "If you want to follow the evolution of all the houses during the simulation, type y.\n>" ) marketProcess = Market.Market(externalFactors, lockExternal, globalNeed, lockGlobalNeed, payableEnergyBank, lockPayable, clocker, weather, market_conn) print("Starting market") marketProcess.start() weatherProcess = Weather.Weather(weather, clocker, day) print("Starting weather") weatherProcess.start() houses = [ House.House(i, clocker, weather, lockHouse, houses_pipes[i][1]) for i in range(numberOfHouses) ] print("Starting every Houses") for k in houses: k.start()
import Market as market myMarket = market.Market(stock_value=1000, stock_volume=10000, traders_init_cash=10000, n_traders=10) myMarket.run()
import simpy import numpy as np from Market import * from players import * env = simpy.Environment() farmer_pop = 15#Numeric value buyer_pop = 15#Numeric value time_steps = 100000 #Simulation Span #logger = logger("MarketProfit", [ 'Market_Profit_per_trade' , 'Difference_in_mean_types']) logger = logger("Efficiency", [ 'efficiency']) market = Market(env, 'India', farmer_pop, buyer_pop, logger) '''Embedding the process in the Environment, cascaded constructor calls ensure individual agent processes are also embedded into the environment. See Market.py and players.py ''' env.process(market.Trading()) env.run(until=time_steps)
import Market Market.Market()
import Market """ Classe main qui gère le lancement du Market """ if __name__ == "__main__": market = Market.Market(10, 5, 0.25) market.run()
import requests import json import Market import stock_api_exceptions import matplotlib api_key = "pk_e4cd3161272b47369625df7d517b8714" market = Market.Market() class Stock: def __init__(self, ticker, amount_of_stocks=0, cost=-1, purchase_date="False"): self._amount_of_stocks = int(amount_of_stocks) self._ticker = str(ticker) self._cost = int(cost) if purchase_date == 'False': self._purchase_date = '-' else: self._purchase_date = str(purchase_date) # TODO Handle Exception try: api_request = requests.get( "https://cloud.iexapis.com/stable/stock/" + self._ticker + "/quote/?token=" + api_key) self.check_response_code(api_request) api = json.loads(api_request.content)
self.queue = JoinableQueue() self.signal = 0 #Metre le signal dans son queue def sendsignal(self): self.queue.put(self.signal) self.queue.put(self.signal) self.queue.put(self.signal) if __name__ == '__main__': """Lancer le projet ,envoyer le queue de tache pour les autres processus""" event = Event() clock = Clock() #Remettre le valeur pour les nouveaux testes mk.Marche("", 8000).reset() f = open("project_Energy.txt", 'w') f.write("") """Mettre une boucle for pour test en 5 fois , peut faire avec une boucle infine dans une periode du temps""" for i in range(5): clock.sendsignal() cl = clock.queue f = open("project_Energy.txt", 'a') f.write("Day " + str(i + 1) + "\n") f.write("********************" + "\n") thread1 = t.Thread(target=w.signa, args=(cl, event)) thread2 = t.Thread(target=h.signa, args=(cl, event)) thread3 = t.Thread(target=mk.signa, args=(cl, event)) thread1.start() thread2.start()
from Market import * print( " 1) Multi Item Logit \n 2) Multi Item Logit with Fixed Costs \n 3) Mixed Logit (best response dynamic)" ) print(" 4) Two dimensional Mixed Logit (Binary Search)") select = int(input(" Select one (1-4): ")) #fileaddress=input(" Input file address: ") fileaddress = "alg1_test_0.txt" if (select == 1): M = Market() M.read(fileaddress) M.Single_Logit_Multi_Items_Equilibrium() M.print() else: if (select == 2): M = Market() M.read_fcost(fileaddress) #("inputf.txt") M.Single_Logit_Multi_Items_Fixed_Cost_Equilibrium() M.print() else: if (select == 3): M = Market() M.read(fileaddress) M.Mixed_Logit_Equilibrium_Best_Response_Dynamic() M.print() else: if (select == 4): M = Market() M.read(fileaddress) M.Mixed_Logit_Equilibrium_Binary_Search()
# import jieba from futuquant.open_context import * import futu_tools as tools import Market import Stock if __name__ == '__main__': data_dir = "./data" tools.check_dir_exist(data_dir, create=True) # seg_list = jieba.cut("他来到了网易杭研大厦", cut_all=False) # print("Full Mode: " + "/ ".join(seg_list)) # quote_ctx = OpenQuoteContext(host='127.0.0.1', port=11111) hk_market = Market.Market(quote_ctx, "HK") tools.storeJson(hk_market.toJson(), data_dir + "/hk_market.json") #腾讯 tencent = Stock.Stock(quote_ctx, "HK.00700", hk_market) csv = tencent.getHistoryData(start_time="2015-01-01", csv=True) tools.storeString(csv, data_dir + "/tencent.csv") tencent.history, tencent.history_index, tencent.key_index = tencent.getHistoryData( start_time="2015-01-01") tools.storeJson(tencent.toJson(), data_dir + "/tencent.json") #国企指数 guoqi = Stock.Stock(quote_ctx, "HK.800100", hk_market) csv = guoqi.getHistoryData(start_time="2015-01-01", csv=True) tools.storeString(csv, data_dir + "/guoqi.csv") guoqi.history, guoqi.history_index, guoqi.key_index = guoqi.getHistoryData(
def expand(self): self.rate += 1 def downsize(self): self.rate = max(1, self.rate - 1) class Shopper(Agents.Consumer): def __init__(self, utility, mps=0.1): super().__init__(utility, mps) self.income = random.randint(50, 200) self.turns = {0: self.work, 3: self.shop, 4: self.consume} def work(self): self.cash += self.income def consume(self): for item in self.inventory.keys(): self.inventory[item] = 0 market = Market.RetailStore() world = World(5) world.add_market(market) world.make_agents(Shopper, 1000, Utility(UTILITY_FUNCTION)) world.make_agents(Shop, NUMBER_OF_FISH, "fish") world.make_agents(Shop, NUMBER_OF_CHIPS, "chips") world.run_sim(100) world.show_data()
# Initial Capital initialcapital = 10000 # Create portfolio portfolio = PortfolioClass.Portfolio(tickersymbolist, inputdate_init, inputdate_fin, initialcapital) #portfolio['MSFT'].stocks = 5 print(portfolio) # Simulation input date begin print(inputdate_init) # Simulation input date final print(inputdate_fin) # List of Ticker Symbols print(tickersymbolist) print() # Check Open Market days in input dates simdate_init = portfolio.pricedatadf[inputdate_init:].index[0] simdate_fin = portfolio.pricedatadf[inputdate_init:].index[-1] print('Simulation begin and finish:') print(simdate_init) print(simdate_fin) # Minimum Variance Portfolio weightsdf = MinVar.main(portfolio) Market.main(portfolio, weightsdf) exit()
self.expected_salary += 1 def lower_salary(self): self.expected_salary = max(1, self.expected_salary - 1) def do_nothing(self): pass def balance(self): self.round_score = self.inventory['cakes'] + self.inventory['brownies'] + (self.cash * 0.2) self.inventory['cakes'] = 0 self.inventory['brownies'] = 0 Agents.SmartAgent.balance(self) return Agents.Consumer.balance(self) terra = World(10) market = Market.RetailStore(debug=False) terra.make_agents(Plebian, 1000) for item in ["flour", "milk", "eggs"]: for i in range(20): agent = Farm(item) terra.add_agent(agent) for item in ["cakes", "brownies"]: for i in range(20): agent = Factory(item) terra.add_agent(agent) terra.add_market(market) terra.run_sim(10)
import Market as mk import time import GraphIt as gr import numpy as np from sets import * import matplotlib.pyplot as plt from matplotlib.ticker import FuncFormatter from decimal import * btc = mk.Market('BTC-USD') print(btc.getTrade()[0]) tradeID = Set() sum = 0 while True: tradeID.add print(tradeID) time.sleep(0.5)
def main(): global test_result, global_give_opside global stop_win, stop_lose import Analyse_price_walk as Analyse import DataSrc from ClassLine import Line import Market test_tmp = 0 stop_direction = 0 if global_source_file.find("IF") > 0: DataSrc.init(global_source_file, "IF") else: DataSrc.init(global_source_file, "MT4") i = 0 signar = 0 tmp_count = 0 crr_price = 0 price_walk = Line(Market.E_N_AVG, 3) price_ct_diff = Line(Market.E_N_AVG) shoot_diff = Line(Market.E_N_AVG) vol_ct_diff = Line(Market.E_N_AVG) vol_strong_win_diff = Line(Market.E_N_AVG) vol_weak_win_diff = Line(Market.E_N_AVG) vol_strong_lose_diff = Line(Market.E_N_AVG) vol_weak_lose_diff = Line(Market.E_N_AVG) vol_index_diff = Line(Market.E_N_AVG) vol_avg_diff = Line(Market.E_N_AVG) score = Line(20, 10) while (1): one = DataSrc.getLine() # if i < 120000: # i += 1 # continue # if i > 170000: # break # print "line: ", one if not one: break crr_price = one[5] market = Market.listen(one) check_info = global_order.tick(one[6], one[7], one[5]) if check_info: sendOrder("3", one[5], one[0] + " " + one[1], True, "") # if check_info['profit']< 0: # stop_direction = global_give_opside * check_info['direction'] tmpw = market["info"]["price_crr_walk"] tmp1 = price_ct_diff.add(market["info"]["price_up_ct"] - market["info"]["price_sell_ct"]) price_walk.add(market["info"]["price_crr_walk"]) tmp2 = int(abs(tmpw) > 1) # print price_walk.point tmp = int(tmpw * tmp1 > 0) * tmp2 tmpv1 = shoot_diff.add(market["info"]["vol_up_shoot"] - market["info"]["vol_sell_shoot"]) # print shoot_diff.info() # print tmpv1 tmpv2 = vol_ct_diff.add(market["info"]["vol_up_ct"] - market["info"]["vol_sell_ct"]) tmpv3 = vol_strong_win_diff.add(market["info"]["vol_up_strong_win"] - market["info"]["vol_sell_strong_win"]) tmpv4 = vol_weak_win_diff.add(market["info"]["vol_up_weak_win"] - market["info"]["vol_sell_weak_win"]) tmpv5 = vol_strong_lose_diff.add( market["info"]["vol_up_strong_lose"] - market["info"]["vol_sell_strong_lose"]) tmpv6 = vol_weak_lose_diff.add(market["info"]["vol_up_weak_lose"] - market["info"]["vol_sell_weak_lose"]) tmpv71 = market["info"]["vol_up_weak_win"] + market["info"][ "vol_up_weak_lose"] + market["info"]["vol_up_strong_win"] + market[ "info"]["vol_up_strong_lose"] tmpv72 = market["info"]["vol_sell_weak_win"] + market["info"][ "vol_sell_weak_lose"] + market["info"][ "vol_sell_strong_win"] + market["info"]["vol_sell_strong_lose"] tmpv7 = vol_index_diff.add(tmpv71 - tmpv72) tmpv8 = vol_avg_diff.add(market["info"]["vol_avg_diff"]) tmpv9 = market["info"]["vol_diff"] tmpv = int(tmpv1 * tmpv2 > 0) * int(tmpv1 * tmpv3 > 0) * int( tmpv1 * tmpv6 > 0) * int(tmpv1 * tmpv7 > 0) * int( tmpv1 * tmpv8 > 0) * int(tmpv1 * tmpv4 > 0) * int( tmpv1 * tmpv5 > 0) #* int(tmpv1 * tmpv9 > 0) signar = tmpv1 * tmp * tmpv * int(tmpv1 * tmpw > 0) # score_sum = price_ct_diff.walk_score + shoot_diff.walk_score + vol_ct_diff.walk_score # score_sum = vol_strong_win_diff.walk_score + vol_weak_win_diff.walk_score + vol_strong_lose_diff.walk_score + vol_weak_lose_diff.walk_score + vol_index_diff.walk_score + vol_avg_diff.walk_score # signar = score.add(score_sum) * int(tmpw * score.last_n > 0) # signar = tmpv8 # signar = random.randint(-1,1) if signar > 0: one_opt = "1" elif signar < 0: one_opt = "2" else: one_opt = "0" # if tmp9 == 0: # one_opt = "3" if stop_direction == OP_SELL: if one_opt == "2": one_opt = "3" # elif one_opt == "1": stop_direction = 0 elif stop_direction == OP_UP: if one_opt == "1": one_opt = "3" # elif one_opt == "2": stop_direction = 0 order_info = sendOrder(one_opt, one[5], one[0] + " " + one[1], False, "") i += 1 if i % 1440 == 0: print i, one[0] + " " + one[1] if global_order.dealOpen(): print global_order.deal[ "direction"], " deal(", signar, "): ", global_order.profit( global_order.deal, crr_price) else: print "no deal(", signar, ")" global_order.info() print "---------------------" DataSrc.done() res = global_order.info() order_info = sendOrder("3", crr_price, "last", True, "") if res: mkCsvFileWin("test_deals_info.csv", res) mkCsvFileWin("test_info.csv", test_result) print test_tmp print "done res file"
def main(): solar_data = [] solar_data += DataReader.get_daily_totals_for_file('Data/2005.csv') solar_data += DataReader.get_daily_totals_for_file('Data/2006.csv') solar_data += DataReader.get_daily_totals_for_file('Data/2007.csv') solar_data += DataReader.get_daily_totals_for_file('Data/2008.csv') solar_data += DataReader.get_daily_totals_for_file('Data/2009.csv') print("Imported Data") print("Running Simulation:") num_of_sims = 0 with open('Output/rs.csv', 'w') as csvfile: writer = csv.writer(csvfile, delimiter=',') writer.writerow(['Smart Agents Wealth', 'Smart Agents Trades', 'Smart Agents Wealth (all)', 'Smart Agents Trades (all)','Controlled Agents Wealth', 'Controlled Agents Trades', 'Controlled Agents Wealth (all)', 'Controlled Agents Trades (all)', 'All Agents Wealth', 'All Agents Trades', 'All Agents Wealth (all)', 'All Agents Trades (all)', 'Smart Agents Price Correlation', 'Controlled Agents Price Correlation', 'All Agents Price Correlation']) for i in range(num_of_sims): print("Simulation:" + str(i + 1)) market = Market(100, 10) for i in range(len(solar_data)): weather = solar_data[i] market.update(float(weather)/1000.0) price = market.price_history[-1] supply = market.asks[-1] demand = market.bids[-1] smart_agents = [agent for agent in market.agents if agent.use_brain == True] controlled_agents = market.agents[:10] all_agents = market.agents smart_wealth = sum([agent.wealth for agent in smart_agents if agent.wealth > 0])/len(smart_agents) smart_no_trades = sum([agent.no_trades for agent in smart_agents if agent.wealth > 0])/len(smart_agents) smart_wealth_a = sum([agent.wealth for agent in smart_agents])/len(smart_agents) smart_no_trades_a = sum([agent.no_trades for agent in smart_agents])/len(smart_agents) controlled_wealth = sum([agent.wealth for agent in controlled_agents if agent.wealth > 0])/len(controlled_agents) controlled_no_trades = sum([agent.no_trades for agent in controlled_agents if agent.wealth > 0])/len(controlled_agents) controlled_wealth_a = sum([agent.wealth for agent in controlled_agents])/len(controlled_agents) controlled_no_trades_a = sum([agent.no_trades for agent in controlled_agents])/len(controlled_agents) all_wealth = sum([agent.wealth for agent in all_agents if agent.wealth > 0])/len(all_agents) all_no_trades = sum([agent.no_trades for agent in all_agents if agent.wealth > 0])/len(all_agents) all_wealth_a = sum([agent.wealth for agent in all_agents])/len(all_agents) all_no_trades_a = sum([agent.no_trades for agent in all_agents])/len(all_agents) smart_price_history = [agent.price_history for agent in smart_agents] smart_price_history = [sum(col) / float(len(col)) for col in zip(*smart_price_history)] smart_prediction_accuracy = str(numpy.corrcoef(smart_price_history,market.price_history)[0][1]) controlled_price_history = [agent.price_history for agent in controlled_agents] controlled_price_history = [sum(col) / float(len(col)) for col in zip(*controlled_price_history)] controlled_prediction_accuracy = str(numpy.corrcoef(controlled_price_history,market.price_history)[0][1]) all_price_history = [agent.price_history for agent in all_agents] all_price_history = [sum(col) / float(len(col)) for col in zip(*all_price_history)] all_prediction_accuracy = str(numpy.corrcoef(all_price_history,market.price_history)[0][1]) writer.writerow([smart_wealth, smart_no_trades, smart_wealth_a, smart_no_trades_a, controlled_wealth, controlled_no_trades, controlled_wealth_a, controlled_no_trades_a,all_wealth, all_no_trades, all_wealth_a, all_no_trades_a,smart_prediction_accuracy,controlled_prediction_accuracy,all_prediction_accuracy]) print("Creating Graphs:") market = Market(100, 10) for i in range(len(solar_data)): if int(i % (len(solar_data) / 100)) == 0: print(str(int(i / len(solar_data) * 100)) + "%") weather = solar_data[i] market.update(float(weather)/1000.0) price = market.price_history[-1] supply = market.asks[-1] demand = market.bids[-1] solar_plot = pyplot.figure() a = solar_plot.add_subplot(111) a.plot(range(len(solar_data)), solar_data, label='Solar Radiance') a.legend() a.set_ylabel('kWh per m^2') a.set_xlabel('Day') solar_plot.savefig('Output/solar_radiance.png') aggregate_supply_demand_plot = pyplot.figure() b = aggregate_supply_demand_plot.add_subplot(111) b.plot(range(len(solar_data)), market.bids, label='Aggregate Demand') b.plot(range(len(solar_data)), market.asks, label='Aggregate Supply') b.legend() b.set_ylabel('Quantity') b.set_xlabel('Day') aggregate_supply_demand_plot.savefig('Output/supply_demand_history.png') price_history_plot = pyplot.figure() c = price_history_plot.add_subplot(111) c.plot(range(len(solar_data)), market.price_history, label='Average Trading Price') c.legend() c.set_ylabel('Price') c.set_xlabel('Day') c.set_ylim(12,22) price_history_plot.savefig('Output/price_history.png') #Plot average price expectation smart_agents = [agent for agent in market.agents if agent.use_brain == True] smart_price_history = [agent.price_history for agent in smart_agents] smart_price_history = [sum(col) / float(len(col)) for col in zip(*smart_price_history)] smart_price_prediction = pyplot.figure() d = smart_price_prediction.add_subplot(111) d.plot(range(len(solar_data)), market.price_history, label='Average Trading Price') d.plot(range(len(solar_data)), smart_price_history, label='Smart Agent Predicted Price') d.legend() d.set_ylabel('Price') d.set_xlabel('Day') d.set_ylim(12,22) smart_price_prediction.savefig('Output/smart_price_history.png') controlled_agents = market.agents[:10] controlled_price_history = [agent.price_history for agent in controlled_agents] controlled_price_history = [sum(col) / float(len(col)) for col in zip(*controlled_price_history)] controlled_price_prediction = pyplot.figure() e = controlled_price_prediction.add_subplot(111) e.axis('equal') e.scatter(market.price_history, controlled_price_history, label='Control Group Predicted Price', color = 'blue', alpha = 0.5, s=10) e.scatter(market.price_history, smart_price_history, label='Machine Learning Predicted Price', color = 'green', alpha = 0.5, s=10) e.legend() e.set_ylabel('Predicted Price') e.set_xlabel('Actual Price') e.set_ylim(12,20) e.set_xlim(12,20) controlled_price_prediction.savefig('Output/controlled_price_history.png') all_agents = market.agents all_price_history = [agent.price_history for agent in all_agents] all_price_history = [sum(col) / float(len(col)) for col in zip(*all_price_history)] all_price_prediction = pyplot.figure() f = all_price_prediction.add_subplot(111) f.plot(range(len(solar_data)), market.price_history, label='Average Trading Price') f.plot(range(len(solar_data)), all_price_history, label='All Group Predicted Price') f.legend() f.set_ylabel('Price') f.set_xlabel('Day') f.set_ylim(12,22) all_price_prediction.savefig('Output/all_price_history.png') #Sample Supply Demand Curve for agent in market.agents: agent.day_begin(5.0, market) buyers = [agent.price for agent in [agent for agent in market.agents if agent.demand > 0]] sellers = [agent.price for agent in [agent for agent in market.agents if agent.supply > 0]] buyers.sort(reverse=True) sellers.sort() while len(sellers) < len(buyers): buyers.pop() while len(buyers) < len(sellers): sellers.pop() supply_demand = pyplot.figure() g = supply_demand.add_subplot(111) g.plot(range(len(sellers)), sellers, label='Supply') g.plot(range(len(buyers)), buyers, label='Demand') g.legend() g.set_ylabel('Price') g.set_xlabel('Quantity') g.set_ylim(12,22) supply_demand.savefig('Output/supply_demand.png') print("Done")
def test(nb_episode, learn_rate,nb_part,batch_size): ################################### #chargement initiale des données qui serviront pour les clients df_start=ap.load_data("airbnb_data.csv") df_start=df_start.loc[(df_start["room_type"]=="Entire home/apt") & (df_start["price"]>=100) & (df_start["price"]<=200)] df_final=da.review_data(df_start,"new-york-city") #choix du nombre de mois de test nombre_de_mois_test = 12 #on sélectionne alors la bonne partie de la df (qui servira pour appliquer la demande observé aux client naifs) df_final=df_final.reset_index(drop=True) df_final=df_final.loc[df_final.shape[0]-nombre_de_mois_test:df_final.shape[0]-1] #calcul des subdivision de DQN number_of_part= nb_part steps=100/number_of_part #initialisation de la df de résultat df_result= pd.DataFrame(columns= ["Ite","Prix Moyen DQN","Prix_Min","Prix_Max","Liste_Prix","Nb_V_Naif","Nb_V_Strat","CA_Tot","Rev_Tot","Rev_Naif","Rev_Strat","CA_tampon","Prix_tampon", 'Rev_Naif_tampon', 'Rev_Strat_tampon',"CA_Market_DQN","CA_Market_Temoin"]) #pour chauqe partie for n in range(number_of_part): print("Part ",n) #on init le DQN dqn=mdqn.DQN("Data_Model_2.csv",0.9,learn_rate,0.83,(n*steps)/100,((n+1)*steps)/100,batch_size) #on l'entraine return_trace, p_trace = dqn.dqn_training(nb_episode) #plot de surveillance dqn.plot_result(return_trace, p_trace) #liste qui va retenir les séquence de prix du DQN list_p_trace=[] #boucle pour peut-etre multiplier les test avec un entrainement for j in range(1): #enregistrement temporaire des données df_temp= pd.DataFrame(columns= ["Ite","Prix Moyen DQN","Prix_Min","Prix_Max","Liste_Prix","Nb_V_Naif","Nb_V_Strat","CA_Tot","Rev_Tot","Rev_Naif","Rev_Strat","CA_tampon","Prix_tampon", 'Rev_Naif_tampon', 'Rev_Strat_tampon',"CA_Market_DQN","CA_Market_Temoin"]) # print("Ite",j) #pour chaque proportions comprise dans la part for i in range(int(n*steps),int((n+1)*steps)): #on créé le marché market=mkt.Market(100,200,30,1-(i/100),0.85,0.15,df_start,df_final) #on créé un marché temoin market_temoin = copy.deepcopy(market) #etat init state = dqn.env_initial_test_state(150,0,1)#init price #différent enregistrement reward_trace = [] p_trace = [state[0,0]] list_achat_naiv=[] list_achat_strat=[] vente_tot=[] #on parcours le nombre de mois for k in range(df_final.shape[0]): #arrivée d'un unique prix p, state= dqn.dqn_interaction(state) #on enregistre le prix p_trace.append(p) #on update le marché achat_naiv, achat_strat = market.updates(p,p_trace,k) #on enregistre list_achat_naiv.append(achat_naiv) list_achat_strat.append(achat_strat) vente_tot.append(achat_naiv+achat_strat) #on modifie le state state[0,1]=achat_strat + achat_naiv state[0,2]=k+1 reward=dqn.profit_t_d(state[0,0],state[0,1]) reward_trace.append(reward) #on ajoute la séquence de prix dans son enregistreur list_p_trace.append(p_trace) #on set le price temoin init price_temoin = 150 #2on l'enregistre p_trace_temoin = [price_temoin] #on créé les support d'enregistrement du témoin list_achat_naiv_temoin=[] list_achat_strat_temoin=[] vente_tot_temoin=[] #on réalise le marché temoin for k in range(df_final.shape[0]): #on set le prix aléatoire et on l'enregistre price_temoin =rd.randrange(130,170) p_trace_temoin.append(price_temoin) #on update et on enregistre achat_naiv_temoin, achat_strat_temoin = market_temoin.updates(price_temoin,p_trace_temoin,k) list_achat_naiv_temoin.append(achat_naiv_temoin) list_achat_strat_temoin.append(achat_strat_temoin) vente_tot_temoin.append(achat_naiv_temoin+achat_strat_temoin) #on enregistre les données de cette propostion dans la dataframe df_temp.loc[i] = [i, int(sum(p_trace)/len(p_trace)), min(p_trace), max(p_trace), p_trace, sum(list_achat_naiv), sum(list_achat_strat), sum([a*b for a,b in zip( p_trace, vente_tot)]), sum(reward_trace), sum([a*b for a,b in zip( p_trace, list_achat_naiv)]), sum([a*b for a,b in zip( p_trace, list_achat_strat)]), sum([a*b for a,b in zip( p_trace_temoin, vente_tot_temoin)]), price_temoin, sum([a*b for a,b in zip( p_trace_temoin, list_achat_naiv_temoin)]), sum([a*b for a,b in zip( p_trace_temoin, list_achat_strat_temoin)]), sum([a*b for a,b in zip( p_trace, df_final["mean_booked_30"].tolist())]), sum([a*b for a,b in zip( p_trace_temoin, df_final["mean_booked_30"].tolist())])] #on affiche les séquence de prix plot_ptrace(list_p_trace) #on ajoute dans la df de résultat df_result = pd.concat([df_result,df_temp]) print(df_result) #on sauvegarde le tout dans un excel df_result.to_excel("Evol_Strat.xlsx",index=False)
def main(): global test_result, global_test_data, global_data_index, global_error_index, global_give_opside, global_chg_per global global_true_order_direction, global_true_stop_direction global stop_win, stop_lose, stop_lose_true, stop_win_true import Analyse_price_walk as Analyse import DataSrc from ClassLine import Line import Market test_tmp = 0 stop_direction = 0 Analyse.initPriceWalk(global_chg_per) if global_source_file.find("IF") > 0: DataSrc.init(global_source_file, "IF") else: DataSrc.init(global_source_file, "MT4") i = 0 signar = 0 tmp_count = 0 crr_price = 0 price_walk = Line(Market.E_N_AVG, 3) price_ct_diff = Line(Market.E_N_AVG) shoot_diff = Line(Market.E_N_AVG) vol_ct_diff = Line(Market.E_N_AVG) vol_strong_win_diff = Line(Market.E_N_AVG) vol_weak_win_diff = Line(Market.E_N_AVG) vol_strong_lose_diff = Line(Market.E_N_AVG) vol_weak_lose_diff = Line(Market.E_N_AVG) vol_index_diff = Line(Market.E_N_AVG) vol_avg_diff = Line(Market.E_N_AVG) while (1): one = DataSrc.getLine() # print "line: ", one if not one: break crr_price = one[5] market = Market.listen(one) check_info_true = TrueOrder.checkTick(one[6], one[7], one[5]) check_info = Order.checkTick(one[6], one[7], one[5], 0) # check_info = Order.checkTick(one[6], one[7], one[5], stop_lose) if check_info: sendOrder("3", one[5], one[0] + " " + one[1], True, "") # if check_info['profit']< 0: # stop_direction = global_give_opside * check_info['direction'] tmpw = market["info"]["price_crr_walk"] tmp1 = price_ct_diff.add(market["info"]["price_up_ct"] - market["info"]["price_sell_ct"]) price_walk.add(market["info"]["price_crr_walk"]) tmp2 = int(abs(tmpw) > 1) # print price_walk.point tmp = int(tmpw * tmp1 > 0) * tmp2 tmpv1 = shoot_diff.add(market["info"]["vol_up_shoot"] - market["info"]["vol_sell_shoot"]) # print shoot_diff.info() # print tmpv1 tmpv2 = vol_ct_diff.add(market["info"]["vol_up_ct"] - market["info"]["vol_sell_ct"]) tmpv3 = vol_strong_win_diff.add(market["info"]["vol_up_strong_win"] - market["info"]["vol_sell_strong_win"]) tmpv4 = vol_weak_win_diff.add(market["info"]["vol_up_weak_win"] - market["info"]["vol_sell_weak_win"]) tmpv5 = vol_strong_lose_diff.add( market["info"]["vol_up_strong_lose"] - market["info"]["vol_sell_strong_lose"]) tmpv6 = vol_weak_lose_diff.add(market["info"]["vol_up_weak_lose"] - market["info"]["vol_sell_weak_lose"]) tmpv71 = market["info"]["vol_up_weak_win"] + market["info"][ "vol_up_weak_lose"] + market["info"]["vol_up_strong_win"] + market[ "info"]["vol_up_strong_lose"] tmpv72 = market["info"]["vol_sell_weak_win"] + market["info"][ "vol_sell_weak_lose"] + market["info"][ "vol_sell_strong_win"] + market["info"]["vol_sell_strong_lose"] tmpv7 = vol_index_diff.add(tmpv71 - tmpv72) tmpv8 = vol_avg_diff.add(market["info"]["vol_avg_diff"]) tmpv9 = market["info"]["vol_diff"] tmpv = int(tmpv1 * tmpv2 > 0) * int(tmpv1 * tmpv3 > 0) * int( tmpv1 * tmpv6 > 0) * int(tmpv1 * tmpv7 > 0) * int( tmpv1 * tmpv8 > 0) * int(tmpv1 * tmpv4 > 0) * int( tmpv1 * tmpv5 > 0) #* int(tmpv1 * tmpv9 > 0) signar = tmpv1 * tmp * tmpv * int(tmpv1 * tmpw > 0) # signar = random.randint(-1,1) if signar > 0: one_opt = "1" elif signar < 0: one_opt = "2" else: one_opt = "0" # if Order.orderProfit(Order.global_deal, one[5]) < -5: # one_opt = "3" # if tmp9 == 0: # one_opt = "3" if stop_direction == Order.OP_SELL: if one_opt == "2": one_opt = "3" # elif one_opt == "1": stop_direction = 0 elif stop_direction == Order.OP_UP: if one_opt == "1": one_opt = "3" # elif one_opt == "2": stop_direction = 0 order_info = sendOrder(one_opt, one[5], one[0] + " " + one[1], False, "") i += 1 if i % 1440 == 0: print i, one[0] + " " + one[1] if Order.isDealOpen(): print Order.global_deal[ "direction"], " deal(", signar, "): ", Order.orderProfit( Order.global_deal, crr_price) else: print "no deal(", signar, ")" Order.profitInfo() print "---------------------" # TrueOrder.profitInfo() # testPrint([Order.global_profit_sum]) testPrint([Order.global_profit_sum]) DataSrc.done() res = Order.profitInfo() res_real = TrueOrder.profitInfo() order_info = sendOrder("3", crr_price, "last", True, "") if res: mkCsvFileWin("test_deals_info.csv", res) if res_real: mkCsvFileWin("test_deals_info_real.csv", res_real) mkCsvFileWin("test_info.csv", test_result) print test_tmp print "done res file"