예제 #1
0
파일: gekko.py 프로젝트: GO1984/japonicus
def getCandles(DateRange, size=100):
    globalconf = getSettings('Global')
    base = random.choice(globalconf.GekkoURLs)

    URL = base + "/api/getCandles"
    CONFIG = {
        "watch": {
            "exchange": "poloniex",
            "currency": "BTC",
            "asset": "ETH"
            },
        "daterange": DateRange,
        "adapter": "sqlite",
        "sqlite": {
            "path": "plugins/sqlite",

            "dataDirectory": "history",
            "version": 0.1,

            "dependencies": [{
                "module": "sqlite3",
                "version": "3.1.4"
                }]
            },
        "candleSize": size
    }

    RESULT = httpPost(URL, CONFIG)
    return RESULT
예제 #2
0
def logInfo(message, filename="evolution_gen.log"):
    gsettings = getSettings()['Global']
    filename = os.path.join(gsettings['save_dir'], filename)
    F=open(filename, 'a+')
    F.write(message)
    print(message)
    F.close()
예제 #3
0
def getCandles(DateRange, size=100):
    globalconf = getSettings('Global')
    base = random.choice(globalconf.GekkoURLs)

    URL = base + "/api/getCandles"
    CONFIG = {
        "watch": {
            "exchange": "poloniex",
            "currency": "BTC",
            "asset": "ETH"
        },
        "daterange": DateRange,
        "adapter": "sqlite",
        "sqlite": {
            "path": "plugins/sqlite",
            "dataDirectory": "history",
            "version": 0.1,
            "dependencies": [{
                "module": "sqlite3",
                "version": "3.1.4"
            }]
        },
        "candleSize": size
    }

    RESULT = httpPost(URL, CONFIG)
    return RESULT
예제 #4
0
def logInfo(message, filename="evolution_gen.log"):
    gsettings = getSettings()['Global']
    filename = os.path.join(gsettings['save_dir'], filename)
    F = open(filename, 'a+')
    F.write(message)
    print(message)
    F.close()
예제 #5
0
def getAllScanset():
    globalconf = getSettings('Global')
    base = random.choice(globalconf.GekkoURLs)
    URL = base + '/api/scansets'

    RESP = httpPost(URL)

    return RESP['datasets']
예제 #6
0
파일: gekko.py 프로젝트: GO1984/japonicus
def getAllScanset():
    globalconf = getSettings('Global')
    base = random.choice(globalconf.GekkoURLs)
    URL = base + '/api/scansets'

    RESP = httpPost(URL)

    return RESP['datasets']
예제 #7
0
def gekko_bayesian(strategy):
    print("")
    global Strategy
    Strategy = strategy
    TargetParameters = getSettings()["strategies"][Strategy]
    TargetParameters = promoterz.parameterOperations.parameterValuesToRangeOfValues(
        TargetParameters, bayesconf.parameter_spread
    )
    print("Starting search %s parameters" % Strategy)
    bo = BayesianOptimization(gekko_search, copy.deepcopy(TargetParameters))
    # 1st Evaluate
    print("")
    print("Step 1: BayesianOptimization parameter search")
    bo.maximize(init_points=settings['init_points'], n_iter=settings['num_iter'])
    max_val = bo.res['max']['max_val']
    index = all_val.index(max_val)
    s1 = stats[index]
    # 2nd Evaluate
    print("")
    print("Step 2: testing searched parameters on random date")
    max_params = bo.res['max']['max_params'].copy()
    # max_params["persistence"] = 1
    print("Starting Second Evaluation")
    gekko_search(**max_params)
    s2 = stats[-1]
    # 3rd Evaluate
    print("")
    print("Step 3: testing searched parameters on new date")
    watch = settings["watch"]
    print(max_params)
    result = Evaluate(Strategy, max_params)
    resultjson = expandGekkoStrategyParameters(max_params, Strategy)  # [Strategy]
    s3 = result
    # config.js like output
    percentiles = np.array([0.25, 0.5, 0.75])
    formatted_percentiles = [str(int(round(x * 100))) + "%" for x in percentiles]
    stats_index = (['count', 'mean', 'std', 'min'] + formatted_percentiles + ['max'])
    print("")
    print("// " + '-' * 50)
    print("// " + Strategy + ' Settings')
    print("// " + '-' * 50)
    print("// 1st Evaluate: %.3f" % s1[1])
    for i in range(len(s1)):
        print('// %s: %.3f' % (stats_index[i], s1[i]))
    print("// " + '-' * 50)
    print("// 2nd Evaluate: %.3f" % s2[1])
    for i in range(len(s2)):
        print('// %s: %.3f' % (stats_index[i], s2[i]))
    print("// " + '-' * 50)
    print("// 3rd Evaluted: %f" % s3)
    print("// " + '-' * 50)
    print("config.%s = {%s};" % (Strategy, json.dumps(resultjson, indent=2)[1:-1]))
    print('\n\n')
    print(resultInterface.parametersToTOML(resultjson))
    print("// " + '-' * 50)
    return max_params
예제 #8
0
def buildJaponicusOptions(optionparser):
    settings = getSettings()

    # PARSE GENCONF & DATASET COMMANDLINE ARGUMENTS;
    settingSubsets = ['generations', 'dataset', 'backtest', 'evalbreak']
    for settingSubset in settingSubsets:
        parser = promoterz.metaPromoterz.generateCommandLineArguments(
            optionparser,
            settings[settingSubset])

    options, args = parser.parse_args()
    for settingSubset in settingSubsets:
        settings[settingSubset] = promoterz.metaPromoterz.applyCommandLineOptionsToSettings(
            options,
            settings[settingSubset]
        )

    return settings, options
예제 #9
0
def getCandles(DateRange, Dataset, size=100):
    globalconf = getSettings('Global')
    base = random.choice(globalconf.GekkoURLs)
    URL = base + "/api/getCandles"
    CONFIG = {
        "watch": Dataset.specifications,
        "daterange": DateRange,
        "adapter": "sqlite",
        "sqlite": {
            "path": "plugins/sqlite",
            "dataDirectory": "history",
            "version": 0.1,
            "dependencies": [{
                "module": "sqlite3",
                "version": "3.1.4"
            }],
        },
        "candleSize": size,
    }
    RESULT = httpPost(URL, CONFIG)
    return RESULT
예제 #10
0
def showTitleDisclaimer():
    TITLE = """\tGEKKO
        ██╗ █████╗ ██████╗  ██████╗ ███╗   ██╗██╗ ██████╗██╗   ██╗███████╗
        ██║██╔══██╗██╔══██╗██╔═══██╗████╗  ██║██║██╔════╝██║   ██║██╔════╝
        ██║███████║██████╔╝██║   ██║██╔██╗ ██║██║██║     ██║   ██║███████╗
   ██   ██║██╔══██║██╔═══╝ ██║   ██║██║╚██╗██║██║██║     ██║   ██║╚════██║
   ╚█████╔╝██║  ██║██║     ╚██████╔╝██║ ╚████║██║╚██████╗╚██████╔╝███████║
    ╚════╝ ╚═╝  ╚═╝╚═╝      ╚═════╝ ╚═╝  ╚═══╝╚═╝ ╚═════╝ ╚═════╝ ╚══════╝"""
    try:
        print(TITLE)
    except UnicodeEncodeError or SyntaxError:
        print("\nJAPONICUS\n")
    print('\t' * 8 + 'v%.2f' % VERSION)
    print()

    profitDisclaimer = "The profits reported here depends on backtest interpreter function;"
    interpreterFuncName = getSettings('generations').interpreteBacktestProfit
    interpreterInfo = evaluation.gekko.backtest.getInterpreterBacktestInfo(
        interpreterFuncName)

    print("%s \n\t%s" % (profitDisclaimer, interpreterInfo))
예제 #11
0
import os
import numpy as np
import pandas as pd
from gekkoWrapper import getAvailableDataset, runBacktest
#from plotInfo import plotEvolutionSummary
from bayes_opt import BayesianOptimization
from multiprocessing import Pool
import multiprocessing as mp

from coreFunctions import getRandomDateRange, getDateRange
from Settings import getSettings
import gekkoWrapper
import chart

dict_merge = lambda a, b: a.update(b) or a
settings = getSettings()['bayesian']
Strategy = settings["Strategy"]
params = settings[Strategy]
percentiles = np.array([0.25, 0.5, 0.75])
all_val = []
stats = []
candleSize = 0
historySize = 0


def EvaluateRaw(watch, DateRange, Individual, Strategy):
    config = compressing_flatten_dict(Individual, Strategy)
    config["watch"] = watch
    gekko_config = gekkoWrapper.createConfig(config, DateRange)
    url = gekkoWrapper.getURL('/api/backtest')
    if candleSize > 0:
예제 #12
0
import numpy as np
import pandas as pd
import copy
#from plotInfo import plotEvolutionSummary
from bayes_opt import BayesianOptimization
from multiprocessing import Pool
import multiprocessing as mp

from promoterz.statistics import write_evolution_logs
import promoterz
from Settings import getSettings
import promoterz.evaluation.gekko as gekkoWrapper
import chart

dict_merge = lambda a, b: a.update(b) or a
gsettings = getSettings()['Global']
settings = getSettings()['bayesian']

Strategy = settings["Strategy"]
StratConfig = getSettings()["strategies"][Strategy]

percentiles = np.array([0.25, 0.5, 0.75])
all_val = []
stats = []
candleSize = 0
historySize = 0


def EvaluateRaw(watch, DateRange, Individual, Strategy):
    config = expandGekkoStrategyParameters(Individual, Strategy)
    config["watch"] = watch
def gekko_generations(Strategy, NBEPOCH=300, POP_SIZE=30):
    # SETTINGS;############################
    settings=getSettings()['generations']

    DRP = 10 # Date range persistence; Number of subsequent rounds
             # until another time range in dataset is selected;
    _lambda  = 5 # size of offspring generated per epoch;
    cxpb, mutpb = 0.2, 0.8 # Probabilty of crossover and mutation respectively;
    deltaDays=21 # time window of dataset for evaluation
    n_ParallelBacktests = 5
    #######################################
    parallel = Pool(n_ParallelBacktests)
    
    toolbox = base.Toolbox()
    creator.create("FitnessMax", base.Fitness, weights=(1.0,))
    creator.create("Criterion", list,
                   fitness=creator.FitnessMax, Strategy=Strategy)
    toolbox.register("newCriterion", initInd, creator.Criterion)
    
    toolbox.register("population", tools.initRepeat, list,  toolbox.newCriterion)

    toolbox.register("map", progrBarMap)
    
    toolbox.register("mate", tools.cxTwoPoint)
    toolbox.register("mutate", tools.mutUniformInt, low=10, up=10, indpb=0.2)
    
    POP = toolbox.population(n=POP_SIZE)
    W=0
    availableDataRange = getAvailableDataset()
    print("using candlestick dataset %s" % availableDataRange)
    print("%s strategy;" % Strategy)
    
    InfoData={}
    
    #firePaperTrader(reconstructTradeSettings(POP[0], POP[0].Strategy), "poloniex",
    #                "USDT", "BTC")
    stats = tools.Statistics(lambda ind: ind.fitness.values)
    stats.register("avg", np.mean)
    stats.register("std", np.std)
    stats.register("min", np.min)
    stats.register("max", np.max)

    InitialBestScores, FinalBestScores = [], []
    FirstEpochOfDataset = False
    Stats = None
    settings_debug_min = reconstructTradeSettings([0 for x in range(10)], 'MIN_ VALUES')
    settings_debug_max = reconstructTradeSettings([100 for x in range(10)], 'MAX_ VALUES')
    
    print("DEBUG %s" % json.dumps(settings_debug_min, indent=2))
    print("DEBUG %s" % json.dumps(settings_debug_max, indent=2))
          
    while W < NBEPOCH: 
        HallOfFame = tools.HallOfFame(30)
        bestScore = 0
        Deviation = 0
        
        Z = not W % DRP and bestScore > 0.3 and not Deviation
        K = not W % (DRP*3)
        if Z or K: # SELECT NEW DATERANGE;
            if W:# SEND BEST IND TO HoF;
                BestSetting = tools.selBest(POP, 1)[0]
                HallOfFame.insert(BestSetting)
                #print(InfoData)
                #plotEvolutionSummary(InfoData,
                #                     "evolution_%s"% (Strategy))
                FinalBestScores.append(Stats['max'])
            DateRange = getRandomDateRange(availableDataRange, deltaDays)
            print("Loading new date range;")
            print("\t%s to %s" % (DateRange['from'], DateRange['to']))
            for I in range(len(POP)):
                del POP[I].fitness.values
            toolbox.register("evaluate", Evaluate, DateRange)
            FirstEpochOfDataset = True
            
        if random.random() < 0.2 and HallOfFame.items:
            # casually insert individuals from HallOfFame on population;
            for Q in range(1):
                CHP = deepcopy(random.choice(HallOfFame))
                del CHP.fitness.values
                POP += [CHP]

        if random.random() < 0.5:
            # should have built the wall;
            nb = random.randint(1,9)
            POP += toolbox.population(nb)
            
        individues_to_simulate = [ind for ind in POP if not ind.fitness.valid]
        #fitnesses = toolbox.map(toolbox.evaluate, individues_to_simulate)
        to_simulation = [ (x[:], x.Strategy) for x in individues_to_simulate ]
        fitnesses = parallel.starmap(toolbox.evaluate, to_simulation)
        
        for ind, fit in zip(individues_to_simulate, fitnesses):
            ind.fitness.values = fit

        # get proper evolution statistics; #TBD
        Stats=stats.compile(POP)


            

        # show information;
        print("EPOCH %i/%i" % (W, NBEPOCH)) 
        print("Average profit %.3f%%\tDeviation %.3f" % (Stats['avg'],Stats['std']))
        print("Maximum profit %.3f%%\tMinimum profit %.3f%%" % (Stats['max'],Stats['min']))
        print("")

        # log statistcs;
        InfoData[W] = Stats
        if FirstEpochOfDataset:
            InitialBestScores.append(Stats['max'])
            FirstEpochOfDataset = False
            
        bestScore = Stats['max']
        Deviation = Stats['std']
        # generate and append offspring in population;
        offspring = algorithms.varOr(POP, toolbox, settings['_lambda'],
                                     settings['cxpb'], settings['mutpb'])

        POP[:] = tools.selBest(POP+offspring, POP_SIZE)

        #print("POPSIZE %i" % len(POP))
        W+=1

    # RUN ENDS. SELECT INDIVIDUE, LOG AND PRINT STUFF;
    FinalBestScores.append(Stats['max'])
    FinalIndividue = tools.selBest(POP, 1)[0]
    FinalIndividueSettings = reconstructTradeSettings(FinalIndividue,
                                                      FinalIndividue.Strategy)
    Show = json.dumps(FinalIndividueSettings, indent=2)
    logInfo("~" * 18)
    for S in range(len(FinalBestScores)):
        logInfo("Candlestick Set %i: \n\n" % (S+1)+\
                "EPOCH ONE BEST PROFIT: %.3f\n" % InitialBestScores[S] +\
                "FINAL EPOCH BEST PROFIT: %.3f\n" % FinalBestScores[S])

        
    print("Settings for Gekko config.js:")
    print(Show)
    print("Settings for Gekko --ui webpage")
    logInfo(pasteSettingsToUI(FinalIndividueSettings))

    print("\nRemember to check MAX and MIN values for each parameter.")
    print("\tresults may improve with extended ranges.")
    
    print("Testing Strategy:\n")
    Vv=stratSettingsProofOfViability(FinalIndividueSettings, availableDataRange)
    Vv = "GOOD STRAT" if Vv else "SEEMS BAD"
    logInfo(Vv)
    print("\t\t.RUN ENDS.")
예제 #14
0
from time import sleep
from random import choice, randrange
from subprocess import Popen, PIPE
from threading import Thread
from Settings import getSettings
from evolution_generations import gekko_generations

import datetime
from os import chdir, path, listdir
chdir(path.dirname(path.realpath(__file__)))

from japonicus_options import options, args
import web
import promoterz
from version import VERSION
settings = getSettings()
#from evolution_bayes import gekko_bayesian

gekko_server = None
web_server = None
TITLE = """\tGEKKO
     ██╗ █████╗ ██████╗  ██████╗ ███╗   ██╗██╗ ██████╗██╗   ██╗███████╗
     ██║██╔══██╗██╔══██╗██╔═══██╗████╗  ██║██║██╔════╝██║   ██║██╔════╝
     ██║███████║██████╔╝██║   ██║██╔██╗ ██║██║██║     ██║   ██║███████╗
██   ██║██╔══██║██╔═══╝ ██║   ██║██║╚██╗██║██║██║     ██║   ██║╚════██║
╚█████╔╝██║  ██║██║     ╚██████╔╝██║ ╚████║██║╚██████╗╚██████╔╝███████║
 ╚════╝ ╚═╝  ╚═╝╚═╝      ╚═════╝ ╚═╝  ╚═══╝╚═╝ ╚═════╝ ╚═════╝ ╚══════╝"""

if options.spawn_gekko:
    if options.genetic_algorithm or options.bayesian_optimization:
        gekko_args = [
예제 #15
0
def gekko_generations(TargetParameters, GenerationMethod,
                      EvaluationMode, NB_LOCALE=2, web=None):

    Logger = promoterz.logger.Logger()
    GenerationMethod = promoterz.functions.selectRepresentationMethod(GenerationMethod)

    genconf=getSettings('generations')
    globalconf = getSettings('Global')
    datasetconf = getSettings('dataset')
    indicatorconf = getSettings()['indicators']
    if EvaluationMode == 'indicator':
        #global StrategyFileManager
        StrategyFileManager = stratego.gekko_strategy.StrategyFileManager(
            globalconf.gekkoPath, indicatorconf)
        Evaluate = partial(aEvaluate, StrategyFileManager)
        Strategy = None

    # --for standard methods;
    else:
        Evaluate = bEvaluate
        Strategy = EvaluationMode

    Logger.log("Evolving %s strategy;\n" % Strategy)

    print("evaluated parameters ranges:")

    TargetParameters = promoterz.parameterOperations.flattenParameters(TargetParameters)
    TargetParameters = promoterz.parameterOperations.parameterValuesToRangeOfValues(
        TargetParameters,
        genconf.parameter_spread)

    GlobalTools = GenerationMethod.getToolbox(Strategy, genconf, TargetParameters)

    RemoteHosts = promoterz.evaluation.gekko.loadHostsFile(globalconf.RemoteAWS)
    globalconf.GekkoURLs += RemoteHosts

    if RemoteHosts:
        print("Connected Remote Hosts:\n%s" % ('\n').join(RemoteHosts))
        if EvaluationMode == 'indicator':
            exit('Indicator mode is yet not compatible with multiple hosts.')

    for k in TargetParameters.keys():
        Logger.log( "%s%s%s" % (k, " " * (30-len(k)), TargetParameters[k]) )

    # --GRAB PRIMARY (EVOLUTION) DATASET
    D = promoterz.evaluation.gekko.selectCandlestickData(
            exchange_source=datasetconf.dataset_source)
    evolutionDataset = CandlestickDataset(*D)
    evolutionDataset.restrain(datasetconf.dataset_span)
    # --GRAB SECONDARY (EVALUATION) DATASET
    try:
        D = promoterz.evaluation.gekko.selectCandlestickData(
        exchange_source = datasetconf.eval_dataset_source,
        avoidCurrency = evolutionDataset.specifications['asset'] )
        evaluationDataset = CandlestickDataset(*D)
        evaluationDataset.restrain(datasetconf.eval_dataset_span)
    except RuntimeError:
        evaluationDataset = None
        print("Evaluation dataset not found.")



    # --SHOW DATASET INFO;
    interface.showDatasetInfo("evolution",
                              evolutionDataset)

    if evaluationDataset:
        interface.showDatasetInfo("evaluation",
                                  evaluationDataset)

    # --INITIALIZE WORLD WITH CANDLESTICK DATASET INFO;
    GlobalTools.register('Evaluate', Evaluate,
                         GlobalTools.constructPhenotype, genconf )


    loops = [ promoterz.sequence.standard_loop.standard_loop ]
    World = promoterz.world.World(GlobalTools, loops,
                                  genconf, globalconf, TargetParameters, NB_LOCALE,
                                  EnvironmentParameters=[ evolutionDataset,
                                                          evaluationDataset ], web=web)
    World.logger = Logger
    # --RUN EPOCHES;
    while World.EPOCH < World.genconf.NBEPOCH:
        World.runEPOCH()
        if genconf.evaluateSettingsPeriodically:
            if not World.EPOCH % genconf.evaluateSettingsPeriodically:
                resultInterface.showResults(World)

    # RUN ENDS. SELECT INDIVIDUE, LOG AND PRINT STUFF;
    # FinalBestScores.append(Stats['max'])
    print(World.EnvironmentParameters)
    # After running EPOCHs, select best candidates;
    resultInterface.showResults(World)



    print("")
    print("\t\t.RUN ENDS.")
예제 #16
0
import copy
#from plotInfo import plotEvolutionSummary
from bayes_opt import BayesianOptimization
from multiprocessing import Pool
import multiprocessing as mp

from promoterz.statistics import write_evolution_logs
import promoterz
from Settings import getSettings
import promoterz.evaluation.gekko as gekkoWrapper
import chart

from japonicus_options import options, args
dict_merge = lambda a, b: a.update(b) or a

gsettings = getSettings()['Global']

# Fix the shit below!
settings = getSettings()['bayesian']
bayesconf = getSettings('bayesian')

Strategy = None
percentiles = np.array([0.25, 0.5, 0.75])
all_val = []
stats = []
candleSize = 0
historySize = 0

watch = settings["watch"]

watch, DatasetRange = gekkoWrapper.selectCandlestickData(watch)
예제 #17
0
def gekko_generations(TargetParameters,
                      GenerationMethod,
                      EvaluationMode,
                      NB_LOCALE=2,
                      web=None):

    GenerationMethod = promoterz.functions.selectRepresentationMethod(
        GenerationMethod)

    genconf = getSettings('generations')
    globalconf = getSettings('Global')

    if EvaluationMode == 'indicator':
        #global StrategyFileManager
        StrategyFileManager = stratego.gekko_strategy.StrategyFileManager(
            globalconf.gekkoPath)
        Evaluate = partial(aEvaluate, StrategyFileManager)
        Strategy = None
    else:
        Evaluate = bEvaluate
        Strategy = EvaluationMode

    print("Evolving %s strategy;\n" % Strategy)

    print("evaluated parameters ranges:")

    TargetParameters = promoterz.utils.flattenParameters(TargetParameters)

    GlobalTools = GenerationMethod.getToolbox(Strategy, genconf,
                                              TargetParameters)

    RemoteHosts = promoterz.evaluation.gekko.loadHostsFile(
        globalconf.RemoteAWS)
    globalconf.GekkoURLs += RemoteHosts
    if RemoteHosts:
        print("Connected Remote Hosts:\n%s" % ('\n').join(RemoteHosts))
        if EvaluationMode == 'indicator':
            exit('Indicator mode is yet not compatible with multiple hosts.')

    for k in TargetParameters.keys():
        print("%s%s%s" % (k, " " * (30 - len(k)), TargetParameters[k]))

    datasetSpecifications, availableDataRange = promoterz.evaluation.gekko.getAvailableDataset(
        exchange_source=genconf.dataset_source)

    GlobalTools.register('Evaluate', Evaluate, GlobalTools.constructPhenotype,
                         genconf, datasetSpecifications)




    showdatadaterange = [ promoterz.evaluation.gekko.epochToString(availableDataRange[x])\
                    for x in ['from', 'to'] ]

    print()
    print("using candlestick dataset %s to %s" %
          (showdatadaterange[0], showdatadaterange[1]))

    print()

    loops = [promoterz.sequence.standard_loop.standard_loop]
    World = promoterz.world.World(GlobalTools,
                                  loops,
                                  genconf,
                                  globalconf,
                                  TargetParameters,
                                  NB_LOCALE,
                                  EnvironmentParameters=availableDataRange,
                                  web=web)

    while World.EPOCH < World.genconf.NBEPOCH:
        World.runEPOCH()
        if genconf.evaluateSettingsPeriodically:
            if not World.EPOCH % genconf.evaluateSettingsPeriodically:
                resultInterface.showResults(World)

    # RUN ENDS. SELECT INDIVIDUE, LOG ANDo PRINT STUFF;
    # FinalBestScores.append(Stats['max'])
    print(World.EnvironmentParameters)
    # After running EPOCHs, select best candidates;
    resultInterface.showResults(World)

    print("")
    print("\t\t.RUN ENDS.")
예제 #18
0
def gekko_generations(TargetParameters,
                      GenerationMethod,
                      EvaluationMode,
                      NB_LOCALE=2,
                      web=None):

    GenerationMethod = promoterz.functions.selectRepresentationMethod(
        GenerationMethod)

    genconf = getSettings('generations')
    globalconf = getSettings('Global')

    if EvaluationMode == 'indicator':
        #global StrategyFileManager
        StrategyFileManager = stratego.gekko_strategy.StrategyFileManager(
            globalconf.gekkoPath)
        Evaluate = partial(aEvaluate, StrategyFileManager)
        Strategy = None
    else:
        Evaluate = bEvaluate
        Strategy = EvaluationMode

    print("Evolving %s strategy;\n" % Strategy)

    print("evaluated parameters ranges:")

    TargetParameters = promoterz.utils.flattenParameters(TargetParameters)

    GlobalTools = GenerationMethod.getToolbox(Strategy, genconf,
                                              TargetParameters)

    RemoteHosts = promoterz.evaluation.gekko.loadHostsFile(
        globalconf.RemoteAWS)
    globalconf.GekkoURLs += RemoteHosts
    if RemoteHosts:
        print("Connected Remote Hosts:\n%s" % ('\n').join(RemoteHosts))
        if EvaluationMode == 'indicator':
            exit('Indicator mode is yet not compatible with multiple hosts.')

    for k in TargetParameters.keys():
        print("%s%s%s" % (k, " " * (30 - len(k)), TargetParameters[k]))

    datasetSpecifications, availableDataRange = promoterz.evaluation.gekko.getAvailableDataset(
        exchange_source=genconf.dataset_source)

    GlobalTools.register('Evaluate', Evaluate, GlobalTools.constructPhenotype,
                         genconf.candleSize, datasetSpecifications)




    showdatadaterange = [ promoterz.evaluation.gekko.epochToString(availableDataRange[x])\
                    for x in ['from', 'to'] ]

    print()
    print("using candlestick dataset %s to %s" %
          (showdatadaterange[0], showdatadaterange[1]))

    print()

    loops = [promoterz.sequence.standard_loop.standard_loop]
    World = promoterz.world.World(GlobalTools,
                                  loops,
                                  genconf,
                                  globalconf,
                                  TargetParameters,
                                  NB_LOCALE,
                                  EnvironmentParameters=availableDataRange,
                                  web=web)

    while World.EPOCH < World.genconf.NBEPOCH:
        World.runEPOCH()
    # RUN ENDS. SELECT INDIVIDUE, LOG AND PRINT STUFF;
    #FinalBestScores.append(Stats['max'])
    print(World.EnvironmentParameters)
    ValidationDataset =\
        promoterz.evaluation.gekko.globalEvaluationDataset(World.EnvironmentParameters,
                                                           genconf.deltaDays, 12)
    # After running EPOCHs, select best candidates;
    for LOCALE in World.locales:
        LOCALE.population = [
            ind for ind in LOCALE.population if ind.fitness.valid
        ]
        B = genconf.finaltest['NBBESTINDS']
        BestIndividues = tools.selBest(LOCALE.population, B)

        Z = genconf.finaltest['NBADDITIONALINDS']
        print("Selecting %i+%i individues, random test;" % (B, Z))
        AdditionalIndividues = promoterz.evolutionHooks.Tournament(
            LOCALE.population, Z, Z * 2)

        print("%i selected;" % len(AdditionalIndividues))
        AdditionalIndividues = [ x for x in AdditionalIndividues\
                                 if x not in BestIndividues ]

        FinalIndividues = BestIndividues + AdditionalIndividues

        print("%i selected;" % len(FinalIndividues))
        for FinalIndividue in FinalIndividues:
            proof = resultInterface.stratSettingsProofOfViability
            AssertFitness, FinalProfit = proof(World, FinalIndividue,
                                               ValidationDataset)
            print("Testing Strategy:\n")
            if AssertFitness or FinalProfit > 50:
                FinalIndividueSettings = GlobalTools.constructPhenotype(
                    FinalIndividue)

                Show = json.dumps(FinalIndividueSettings, indent=2)
                resultInterface.logInfo("~" * 18)

                resultInterface.logInfo(" %.3f final profit ~~~~" %
                                        FinalProfit)
                print("Settings for Gekko config.js:")
                print(Show)
                print("Settings for Gekko --ui webpage")
                resultInterface.logInfo(
                    resultInterface.pasteSettingsToUI(FinalIndividueSettings))

                print(
                    "\nRemember to check MAX and MIN values for each parameter."
                )
                print("\tresults may improve with extended ranges.")

            else:
                print("Strategy Fails.")

            print("")
    print("\t\t.RUN ENDS.")
예제 #19
0
import numpy as np
import pandas as pd
import copy
#from plotInfo import plotEvolutionSummary
from bayes_opt import BayesianOptimization
from multiprocessing import Pool
import multiprocessing as mp

from promoterz.statistics import write_evolution_logs
import promoterz
from Settings import getSettings
import promoterz.evaluation.gekko as gekkoWrapper
import chart

dict_merge = lambda a,b: a.update(b) or a
gsettings = getSettings()['Global']
# Fix the shit below!
settings = getSettings()['bayesian']
bayesSettings = getSettings('bayesian')

Strategy = settings["Strategy"]
StratConfig = getSettings()["strategies"][Strategy]

percentiles = np.array([0.25, 0.5, 0.75])
all_val = []
stats = []
candleSize = 0
historySize = 0

watch = settings["watch"]
예제 #20
0
def gekko_generations(TargetParameters, GenerationMethod,
                      EvaluationMode, NB_LOCALE=2, web=None):

    GenerationMethod = promoterz.functions.selectRepresentationMethod(GenerationMethod)

    genconf=getSettings('generations')
    globalconf = getSettings('Global')

    if EvaluationMode == 'indicator':
        #global StrategyFileManager
        StrategyFileManager = stratego.gekko_strategy.StrategyFileManager(globalconf.gekkoPath)
        Evaluate = partial(aEvaluate, StrategyFileManager)
        Strategy = None
    else:
        Evaluate = bEvaluate
        Strategy = EvaluationMode

    print("Evolving %s strategy;\n" % Strategy)

    print("evaluated parameters ranges:")

    TargetParameters = promoterz.utils.flattenParameters(TargetParameters)

    GlobalTools = GenerationMethod.getToolbox(Strategy, genconf, TargetParameters)


    RemoteHosts = promoterz.evaluation.gekko.loadHostsFile(globalconf.RemoteAWS)
    globalconf.GekkoURLs += RemoteHosts
    if RemoteHosts:
        print("Connected Remote Hosts:\n%s" % ('\n').join(RemoteHosts))
        if EvaluationMode == 'indicator':
            exit('Indicator mode is yet not compatible with multiple hosts.')

    for k in TargetParameters.keys():
        print( "%s%s%s" % (k, " " * (30-len(k)), TargetParameters[k]) )

    datasetSpecifications, availableDataRange = promoterz.evaluation.gekko.getAvailableDataset(
            exchange_source=genconf.dataset_source)




    GlobalTools.register('Evaluate', Evaluate,
                         GlobalTools.constructPhenotype, genconf, datasetSpecifications)




    showdatadaterange = [ promoterz.evaluation.gekko.epochToString(availableDataRange[x])\
                    for x in ['from', 'to'] ]

    print()
    print("using candlestick dataset %s to %s" %     (showdatadaterange[0],
                                                      showdatadaterange[1]))

    print()

    loops = [ promoterz.sequence.standard_loop.standard_loop ]
    World = promoterz.world.World(GlobalTools, loops,
                                  genconf, globalconf,  TargetParameters, NB_LOCALE,
                                  EnvironmentParameters=availableDataRange, web=web)

    while World.EPOCH < World.genconf.NBEPOCH:
        World.runEPOCH()
        if genconf.evaluateSettingsPeriodically:
            if not World.EPOCH % genconf.evaluateSettingsPeriodically:
                resultInterface.showResults(World)

    # RUN ENDS. SELECT INDIVIDUE, LOG ANDo PRINT STUFF;
    # FinalBestScores.append(Stats['max'])
    print(World.EnvironmentParameters)
    # After running EPOCHs, select best candidates;
    resultInterface.showResults(World)




    print("")
    print("\t\t.RUN ENDS.")
예제 #21
0
def gekko_generations(RuntimeDefinedStrategy, GenerationMethod, NB_LOCALE=2):

    GenerationMethod = promoterz.functions.selectRepresentationMethod(
        GenerationMethod)
    EvaluationMethod = promoterz.evaluation.gekko.Evaluate

    genconf = getSettings('generations')
    globalconf = getSettings('Global')
    genconf.Strategy = RuntimeDefinedStrategy if RuntimeDefinedStrategy else genconf.Strategy
    TargetParameters = getSettings()['strategies'][genconf.Strategy]
    GlobalTools = GenerationMethod.getToolbox(genconf, TargetParameters)

    RemoteHosts = promoterz.evaluation.gekko.loadHostsFile(
        globalconf.RemoteAWS)
    globalconf.GekkoURLs += RemoteHosts
    if RemoteHosts:
        print("Connected Remote Hosts:\n%s" % ('\n').join(RemoteHosts))

    print("Evolving %s strategy;\n" % genconf.Strategy)

    print("evaluated parameters ranges:")

    Params = promoterz.utils.flattenParameters(TargetParameters)

    for k in Params.keys():
        print("%s%s%s" % (k, " " * (30 - len(k)), Params[k]))

    GlobalTools.register('Evaluate', EvaluationMethod,
                         GlobalTools.constructPhenotype, genconf.candleSize)

    availableDataRange = promoterz.evaluation.gekko.getAvailableDataset(
        exchange_source=genconf.dataset_source)

    showdatadaterange = [ promoterz.evaluation.gekko.epochToString(availableDataRange[x])\
                    for x in ['from', 'to'] ]

    print("using candlestick dataset %s to %s" %
          (showdatadaterange[0], showdatadaterange[1]))

    loops = [promoterz.sequence.standard_loop.standard_loop]
    World = promoterz.world.World(GlobalTools,
                                  loops,
                                  genconf,
                                  globalconf,
                                  TargetParameters,
                                  NB_LOCALE,
                                  EnvironmentParameters=availableDataRange)

    while World.EPOCH < World.genconf.NBEPOCH:
        World.runEPOCH()
    # RUN ENDS. SELECT INDIVIDUE, LOG AND PRINT STUFF;
    #FinalBestScores.append(Stats['max'])
    print(World.EnvironmentParameters)
    ValidationDataset =\
        promoterz.evaluation.gekko.globalEvaluationDataset(World.EnvironmentParameters,
                                                           genconf.deltaDays, 12)

    for LOCALE in World.locales:
        BestIndividues = tools.selBest(LOCALE.population,
                                       genconf.finaltest['NBBESTINDS'])

        Z = genconf.finaltest['NBADDITIONALINDS']
        AdditionalIndividues = tools.selTournament(LOCALE.population, Z, Z * 2)
        AdditionalIndividues = [
            x for x in AdditionalIndividues if x not in BestIndividues
        ]
        FinalIndividues = BestIndividues + AdditionalIndividues

        for FinalIndividue in FinalIndividues:

            AssertFitness = coreFunctions.stratSettingsProofOfViability(
                World, FinalIndividue, ValidationDataset)
            print("Testing Strategy:\n")
            if AssertFitness[0] or AssertFitness[1] > 50:
                FinalIndividueSettings = GlobalTools.constructPhenotype(
                    FinalIndividue)

                Show = json.dumps(FinalIndividueSettings, indent=2)
                coreFunctions.logInfo("~" * 18)

                print("Settings for Gekko config.js:")
                print(Show)
                print("Settings for Gekko --ui webpage")
                coreFunctions.logInfo(
                    coreFunctions.pasteSettingsToUI(FinalIndividueSettings))

                print(
                    "\nRemember to check MAX and MIN values for each parameter."
                )
                print("\tresults may improve with extended ranges.")

            else:
                print("Strategy Fails.")

            print("")
    print("\t\t.RUN ENDS.")
예제 #22
0
def gekko_generations(TargetParameters,
                      GenerationMethod,
                      EvaluationMode,
                      NB_LOCALE=2,
                      web=None):

    # --LOAD SETTINGS;
    genconf = getSettings('generations')
    globalconf = getSettings('Global')
    datasetconf = getSettings('dataset')
    indicatorconf = getSettings()['indicators']

    # --APPLY COMMAND LINE GENCONF SETTINGS;
    for parameter in genconf.__dict__.keys():
        if parameter in options.__dict__.keys():
            if options.__dict__[parameter] != None:
                genconf.__dict__[parameter] = options.__dict__[parameter]

    GenerationMethod = promoterz.functions.selectRepresentationMethod(
        GenerationMethod)
    if EvaluationMode == 'indicator':
        #global StrategyFileManager
        StrategyFileManager = stratego.gekko_strategy.StrategyFileManager(
            globalconf.gekkoPath, indicatorconf)
        Evaluate = partial(aEvaluate, StrategyFileManager)
        Strategy = options.skeleton

    # --for standard methods;
    else:
        Evaluate = bEvaluate
        Strategy = EvaluationMode

    TargetParameters = promoterz.parameterOperations.flattenParameters(
        TargetParameters)
    TargetParameters = promoterz.parameterOperations.parameterValuesToRangeOfValues(
        TargetParameters, genconf.parameter_spread)

    GlobalTools = GenerationMethod.getToolbox(Strategy, genconf,
                                              TargetParameters)

    RemoteHosts = evaluation.gekko.API.loadHostsFile(globalconf.RemoteAWS)
    globalconf.GekkoURLs += RemoteHosts

    if RemoteHosts:
        print("Connected Remote Hosts:\n%s" % ('\n').join(RemoteHosts))
        if EvaluationMode == 'indicator':
            exit('Indicator mode is yet not compatible with multiple hosts.')

    # --GRAB PRIMARY (EVOLUTION) DATASET
    D = evaluation.gekko.dataset.selectCandlestickData(
        exchange_source=datasetconf.dataset_source)
    evolutionDataset = CandlestickDataset(*D)
    evolutionDataset.restrain(datasetconf.dataset_span)

    # --GRAB SECONDARY (EVALUATION) DATASET
    try:
        D = evaluation.gekko.dataset.selectCandlestickData(
            exchange_source=datasetconf.eval_dataset_source,
            avoidCurrency=evolutionDataset.specifications['asset'])
        evaluationDataset = CandlestickDataset(*D)
        evaluationDataset.restrain(datasetconf.eval_dataset_span)
    except RuntimeError:
        evaluationDataset = None
        print("Evaluation dataset not found.")

    # --INITIALIZE LOGGER;
    ds_specs = evolutionDataset.specifications

    logfilename = "%s-%s-%s-%s-%s" % (Strategy, ds_specs['exchange'],
                                      ds_specs['currency'], ds_specs['asset'],
                                      str(datetime.datetime.now())[-6:])

    Logger = promoterz.logger.Logger(logfilename)

    # --PRINT RUNTIME ARGS TO LOG HEADER;
    ARGS = '$python ' + ' '.join(sys.argv)
    Logger.log(ARGS, target='Header')

    # --SHOW PARAMETER INFO;
    if Strategy:
        Logger.log("Evolving %s strategy;\n" % Strategy)

    Logger.log("evaluated parameters ranges:", target="Header")

    for k in TargetParameters.keys():
        Logger.log("%s%s%s\n" % (k, " " * (30 - len(k)), TargetParameters[k]),
                   target="Header")

    # --LOG CONFIG INFO;
    configInfo = json.dumps(genconf.__dict__, indent=4)
    Logger.log(configInfo, target="Header", show=False)

    # --SHOW DATASET INFO;
    Logger.log(interface.parseDatasetInfo("evolution", evolutionDataset),
               target="Header")

    if evaluationDataset:
        Logger.log(interface.parseDatasetInfo("evaluation", evaluationDataset),
                   target="Header")

    # --INITIALIZE WORLD WITH CANDLESTICK DATASET INFO; HERE THE GA KICKS IN;
    GlobalTools.register('Evaluate', Evaluate, GlobalTools.constructPhenotype,
                         genconf)

    # --THIS LOADS A DATERANGE FOR A LOCALE;
    def onInitLocale(World, locale):
        locale.DateRange = getLocaleDateRange(World, locale)

    loops = [promoterz.sequence.standard_loop.standard_loop]
    World = promoterz.world.World(
        GlobalTools,
        loops,
        genconf,
        globalconf,
        TargetParameters,
        NB_LOCALE,
        EnvironmentParameters=[evolutionDataset, evaluationDataset],
        onInitLocale=onInitLocale,
        web=web)
    World.logger = Logger
    World.EvaluationStatistics = []
    World.logger.updateFile()

    # --RUN EPOCHES;
    while World.EPOCH < World.genconf.NBEPOCH:
        World.runEPOCH()
        if genconf.evaluateSettingsPeriodically:
            if not World.EPOCH % genconf.evaluateSettingsPeriodically:
                resultInterface.showResults(World)

    # RUN ENDS. SELECT INDIVIDUE, LOG AND PRINT STUFF;
    # FinalBestScores.append(Stats['max'])
    print(World.EnvironmentParameters)
    # After running EPOCHs, select best candidates;
    resultInterface.showResults(World)

    print("")
    print("\t\t.RUN ENDS.")
예제 #23
0
def launchJaponicus(parser):

    settings = getSettings()

    # PARSE GENCONF & DATASET COMMANDLINE ARGUMENTS;
    settingSubsets = ['generations', 'dataset', 'backtest', 'evalbreak']
    for settingSubset in settingSubsets:
        parser = promoterz.metaPromoterz.generateCommandLineArguments(
            parser, settings[settingSubset])

    options, args = parser.parse_args()
    for settingSubset in settingSubsets:
        settings[
            settingSubset] = promoterz.metaPromoterz.applyCommandLineOptionsToSettings(
                options, settings[settingSubset])

    # ABORT WHEN ILLEGAL OPTIONS ARE SET;
    if not options.genetic_algorithm and not options.bayesian_optimization:
        exit("Aborted: No operation specified.")
    if not os.path.isfile(settings['Global']['gekkoPath'] + '/gekko.js'):
        exit("Aborted: gekko.js not found on path specified @Settings.py;")

    # ADDITIONAL MODES;
    gekko_server = launchGekkoChildProcess() if options.spawn_gekko else None
    web_server = launchWebEvolutionaryInfo() if options.spawn_web else None
    sleep(1)
    markzero_time = datetime.datetime.now()
    showTitleDisclaimer(settings['backtest'])

    # --SELECT STRATEGY;
    if options.random_strategy:
        Strategy = ""
        GekkoStrategyFolder = listdir(settings['Global']['gekkoPath'] +
                                      '/strategies')
        while Strategy + '.js' not in GekkoStrategyFolder:
            if Strategy:
                print(
                    "Strategy %s descripted on settings but not found on strat folder."
                    % Strategy)
            Strategy = choice(list(settings['strategies'].keys()))
            print("> %s" % Strategy)
    elif options.strategy:
        Strategy = options.strategy
    elif not options.skeleton:
        exit("No strategy specified! Use --strat or go --help")

    # --LAUNCH GENETIC ALGORITHM;
    if options.genetic_algorithm:
        GenerationMethod = 'chromosome' if options.chromosome_mode else 'oldschool'
        if options.skeleton:
            EvaluationMode = 'indicator'
            AllIndicators = getSettings()['indicators']
            TargetParameters = getSettings()['skeletons'][options.skeleton]
            for K in AllIndicators.keys():
                if type(AllIndicators[K]) != dict:
                    TargetParameters[K] = AllIndicators[K]
                elif AllIndicators[K]['active']:
                    TargetParameters[K] = AllIndicators[K]
                    TargetParameters[K]['active'] = (0, 1)
            if not TargetParameters:
                exit("Bad configIndicators!")
        else:
            EvaluationMode = Strategy

            # READ STRATEGY PARAMETER RANGES FROM TOML;
            try:
                TOMLData = TOMLutils.preprocessTOMLFile(
                    "strategy_parameters/%s.toml" % Strategy)
            except FileNotFoundError:
                print("Failure to find strategy parameter rules for" %
                      (Strategy) + "%s at ./strategy_parameters" % Strategy)
                gekkoParameterPath = "%s/config/strategies/%s.toml" %\
                                     (settings['global']['GekkoDir'], Strategy)
                print("Trying to locate gekko parameters at %s" %
                      gekkoParameterPath)
                TOMLData = TOMLutils.preprocessTOMLFile(gekkoParameterPath)

            TargetParameters = TOMLutils.TOMLToParameters(TOMLData)
        # RUN ONE EQUAL INSTANCE PER REPEATER NUMBER SETTINGS, SEQUENTIALLY;
        for s in range(options.repeater):
            gekko_generations(TargetParameters,
                              GenerationMethod,
                              EvaluationMode,
                              settings,
                              options,
                              web=web_server)
    # --LAUNCH BAYESIAN OPTIMIZATION;
    elif options.bayesian_optimization:
        import evolution_bayes

        for s in range(options.repeater):
            evolution_bayes.gekko_bayesian(Strategy)
    deltatime = datetime.datetime.now() - markzero_time
    print("Run took %i seconds." % deltatime.seconds)
    if options.spawn_web:
        print('Statistics info server still runs...')
예제 #24
0
from time import sleep
from random import choice, randrange
from subprocess import Popen, PIPE
from threading import Thread
from Settings import getSettings
from evolution_generations import gekko_generations

import datetime
from os import chdir, path, listdir
chdir(path.dirname(path.realpath(__file__)))

from japonicus_options import options, args
import web
import promoterz
from version import VERSION
settings = getSettings()
#from evolution_bayes import gekko_bayesian

gekko_server = None
web_server = None

TITLE = """\tGEKKO
     ██╗ █████╗ ██████╗  ██████╗ ███╗   ██╗██╗ ██████╗██╗   ██╗███████╗
     ██║██╔══██╗██╔══██╗██╔═══██╗████╗  ██║██║██╔════╝██║   ██║██╔════╝
     ██║███████║██████╔╝██║   ██║██╔██╗ ██║██║██║     ██║   ██║███████╗
██   ██║██╔══██║██╔═══╝ ██║   ██║██║╚██╗██║██║██║     ██║   ██║╚════██║
╚█████╔╝██║  ██║██║     ╚██████╔╝██║ ╚████║██║╚██████╗╚██████╔╝███████║
 ╚════╝ ╚═╝  ╚═╝╚═╝      ╚═════╝ ╚═╝  ╚═══╝╚═╝ ╚═════╝ ╚═════╝ ╚══════╝"""

if options.spawn_gekko:
    if options.genetic_algorithm or options.bayesian_optimization:
예제 #25
0
from time import sleep
from random import choice, randrange
from subprocess import Popen, PIPE
from threading import Thread
from Settings import getSettings
from evolution_generations import gekko_generations

import datetime
from os import chdir, path, listdir
chdir(path.dirname(path.realpath(__file__)))

from japonicus_options import options, args
import web
import promoterz
from version import VERSION
settings = getSettings()
#from evolution_bayes import gekko_bayesian

gekko_server = None
web_server = None
TITLE ="""\tGEKKO
     ██╗ █████╗ ██████╗  ██████╗ ███╗   ██╗██╗ ██████╗██╗   ██╗███████╗
     ██║██╔══██╗██╔══██╗██╔═══██╗████╗  ██║██║██╔════╝██║   ██║██╔════╝
     ██║███████║██████╔╝██║   ██║██╔██╗ ██║██║██║     ██║   ██║███████╗
██   ██║██╔══██║██╔═══╝ ██║   ██║██║╚██╗██║██║██║     ██║   ██║╚════██║
╚█████╔╝██║  ██║██║     ╚██████╔╝██║ ╚████║██║╚██████╗╚██████╔╝███████║
 ╚════╝ ╚═╝  ╚═╝╚═╝      ╚═════╝ ╚═╝  ╚═══╝╚═╝ ╚═════╝ ╚═════╝ ╚══════╝"""

if options.spawn_gekko:
   if options.genetic_algorithm or options.bayesian_optimization:
        gekko_args = ['node',
예제 #26
0
from evolution_generations import gekko_generations

import TOMLutils

import datetime
from os import chdir, path, listdir

chdir(path.dirname(path.realpath(__file__)))

from japonicus_options import options, args
import web
import promoterz
from version import VERSION
import os
import evaluation
settings = getSettings()


# from evolution_bayes import gekko_bayesian
def showTitleDisclaimer():
    TITLE = """\tGEKKO
        ██╗ █████╗ ██████╗  ██████╗ ███╗   ██╗██╗ ██████╗██╗   ██╗███████╗
        ██║██╔══██╗██╔══██╗██╔═══██╗████╗  ██║██║██╔════╝██║   ██║██╔════╝
        ██║███████║██████╔╝██║   ██║██╔██╗ ██║██║██║     ██║   ██║███████╗
   ██   ██║██╔══██║██╔═══╝ ██║   ██║██║╚██╗██║██║██║     ██║   ██║╚════██║
   ╚█████╔╝██║  ██║██║     ╚██████╔╝██║ ╚████║██║╚██████╗╚██████╔╝███████║
    ╚════╝ ╚═╝  ╚═╝╚═╝      ╚═════╝ ╚═╝  ╚═══╝╚═╝ ╚═════╝ ╚═════╝ ╚══════╝"""
    try:
        print(TITLE)
    except UnicodeEncodeError or SyntaxError:
        print("\nJAPONICUS\n")
예제 #27
0
def launchJaponicus(settings, options):
    # ABORT WHEN ILLEGAL OPTIONS ARE SET;
    if not options.genetic_algorithm and not options.bayesian_optimization:
        exit("Aborted: No operation specified.")
    if not os.path.isfile(settings['global']['gekkoPath'] + '/gekko.js'):
        exit("Aborted: gekko.js not found on path specified @Settings.py;")

    # ADDITIONAL MODES;
    gekko_server = launchGekkoChildProcess(settings)\
        if options.spawn_gekko else None
    web_server = launchWebEvolutionaryInfo()\
        if options.spawn_web else None
    sleep(1)
    markzero_time = datetime.datetime.now()
    showTitleDisclaimer(settings['backtest'])

    print()

    # LOCATE & VALIDATE RUNNING GEKKO INSTANCES FROM CONFIG URLs;
    possibleInstances = settings['global']['GekkoURLs']
    validatedInstances = []
    for instance in possibleInstances:
        Response = evaluation.gekko.API.checkInstance(instance)
        if Response:
            validatedInstances.append(instance)
            print("found gekko @ %s" % instance)
        else:
            print("unable to locate %s" % instance)

    if validatedInstances:
        settings['global']['GekkoURLs'] = validatedInstances
    else:
        exit("Aborted: No running gekko instances found.")

    # --SELECT STRATEGY;
    if options.random_strategy:
        Strategy = ""
        GekkoStrategyFolder = os.listdir(settings['global']['gekkoPath'] + '/strategies')
        while Strategy + '.js' not in GekkoStrategyFolder:
            if Strategy:
                print(
                    "Strategy %s descripted on settings but not found on strat folder." %
                    Strategy
                )
            Strategy = choice(list(settings['strategies'].keys()))
            print("> %s" % Strategy)
    elif options.strategy:
        Strategy = options.strategy
    elif not options.skeleton:
        exit("No strategy specified! Use --strat or go --help")

    # --LAUNCH GENETIC ALGORITHM;
    if options.genetic_algorithm:
        GenerationMethod = 'chromosome' if options.chromosome_mode else 'oldschool'
        if options.skeleton:
            EvaluationMode = 'indicator'
            AllIndicators = getSettings()['indicators']
            TargetParameters = getSettings()['skeletons'][options.skeleton]
            for K in AllIndicators.keys():
                if type(AllIndicators[K]) != dict:
                    TargetParameters[K] = AllIndicators[K]
                elif AllIndicators[K]['active']:
                    TargetParameters[K] = AllIndicators[K]
                    TargetParameters[K]['active'] = (0, 1)
            if not TargetParameters:
                exit("Bad configIndicators!")
        else:
            EvaluationMode = Strategy

            # READ STRATEGY PARAMETER RANGES FROM TOML;
            try:
                TOMLData = TOMLutils.preprocessTOMLFile(
                    "strategy_parameters/%s.toml" % Strategy
                )
            except FileNotFoundError:
                print("Failure to find strategy parameter rules for " +
                      "%s at ./strategy_parameters" % Strategy)
                gekkoParameterPath = "%s/config/strategies/%s.toml" %\
                                     (settings['global']['GekkoDir'], Strategy)
                print("Trying to locate gekko parameters at %s" %
                      gekkoParameterPath)
                TOMLData = TOMLutils.preprocessTOMLFile(gekkoParameterPath)

            TargetParameters = TOMLutils.TOMLToParameters(TOMLData)
        # RUN ONE EQUAL INSTANCE PER REPEATER NUMBER SETTINGS, SEQUENTIALLY;
        for s in range(options.repeater):
            gekko_generations(
                TargetParameters, GenerationMethod,
                EvaluationMode, settings, options, web=web_server
            )

    # --LAUNCH BAYESIAN OPTIMIZATION;
    elif options.bayesian_optimization:
        import evolution_bayes

        for s in range(options.repeater):
            evolution_bayes.gekko_bayesian(Strategy)
    deltatime = datetime.datetime.now() - markzero_time
    print("Run took %i seconds." % deltatime.seconds)
    if options.spawn_web:
        print('Statistics info server still runs...')
예제 #28
0
def launchJaponicus():
    if not options.genetic_algorithm and not options.bayesian_optimization:
        exit("Aborted: No operation specified.")
    if not os.path.isfile(settings['Global']['gekkoPath'] + '/gekko.js'):
        exit("Aborted: gekko.js not found on path specified @Settings.py;")

    gekko_server = launchGekkoChildProcess() if options.spawn_gekko else None
    web_server = launchWebEvolutionaryInfo() if options.spawn_web else None
    sleep(1)
    markzero_time = datetime.datetime.now()
    showTitleDisclaimer()
    # --SELECT STRATEGY;
    if options.random_strategy:
        Strategy = ""
        GekkoStrategyFolder = listdir(settings['Global']['gekkoPath'] +
                                      '/strategies')
        while Strategy + '.js' not in GekkoStrategyFolder:
            if Strategy:
                print(
                    "Strategy %s descripted on settings but not found on strat folder."
                    % Strategy)
            Strategy = choice(list(settings['strategies'].keys()))
            print("> %s" % Strategy)
    elif options.strategy:
        Strategy = options.strategy
    elif not options.skeleton:
        exit("No strategy specified! Use --strat or go --help")
    # --LAUNCH GENETIC ALGORITHM;
    if options.genetic_algorithm:
        GenerationMethod = 'chromosome' if options.chromosome_mode else 'oldschool'
        if options.skeleton:
            EvaluationMode = 'indicator'
            AllIndicators = getSettings()['indicators']
            TargetParameters = getSettings()['skeletons'][options.skeleton]
            for K in AllIndicators.keys():
                if type(AllIndicators[K]) != dict:
                    TargetParameters[K] = AllIndicators[K]
                elif AllIndicators[K]['active']:
                    TargetParameters[K] = AllIndicators[K]
                    TargetParameters[K]['active'] = (0, 1)
            if not TargetParameters:
                exit("Bad configIndicators!")
        else:
            EvaluationMode = Strategy
            try:
                TargetParameters = getSettings()['strategies'][Strategy]
            # -- Yeah, nested exceptions;
            except KeyError:
                try:
                    TOMLData = TOMLutils.preprocessTOMLFile(
                        "strategy_parameters/%s.toml" % Strategy)
                except FileNotFoundError:
                    TOMLData = TOMLutils.preprocessTOMLFile(
                        "%s/config/strategies/%s.toml" % (GekkoDir, Strategy))
                TargetParameters = TOMLutils.TOMLToParameters(TOMLData)
        for s in range(options.repeater):
            gekko_generations(TargetParameters,
                              GenerationMethod,
                              EvaluationMode,
                              web=web_server)
    # --LAUNCH BAYESIAN OPTIMIZATION;
    elif options.bayesian_optimization:
        import evolution_bayes

        for s in range(options.repeater):
            evolution_bayes.gekko_bayesian(Strategy)
    deltatime = datetime.datetime.now() - markzero_time
    print("Run took %i seconds." % deltatime.seconds)
    if options.spawn_web:
        print('Statistics info server still runs...')
예제 #29
0
import promoterz
import evaluation

from Settings import getSettings

import evaluation

import TOMLutils

from evaluation.gekko.datasetOperations import CandlestickDataset
from japonicus_options import parser
options, args = parser.parse_args()

dict_merge = lambda a, b: a.update(b) or a
gsettings = getSettings()['global']
# Fix the shit below!
settings = getSettings()['bayesian']
bayesconf = getSettings('bayesian')
datasetconf = getSettings('dataset')
Strategy = None
percentiles = np.array([0.25, 0.5, 0.75])
all_val = []
stats = []
candleSize = 0
historySize = 0
watch = settings["watch"]
watch, DatasetRange = evaluation.gekko.dataset.selectCandlestickData(watch)


def expandGekkoStrategyParameters(IND, Strategy):