Exemplo n.º 1
0
def cycleTrain(saveDir, genPath, saveAgentName):
    while True:

        preprocDf = preprocData(df)
        createGenerator(preprocDf,
                        featureList=hkFeatList,
                        saveDir=saveDir,
                        saveName=genName)

        trainDf = preprocDf.tail(1232).head(1032)
        backTestDf = preprocDf.tail(1432).head(232)

        lastSaveEp = trainAgent(trainDf=trainDf,
                                backTestDf=backTestDf,
                                genPath=genPath,
                                saveAgentDir=saveDir,
                                saveAgentName=saveAgentName)

        #####
        # break
        #####

        sumReward = evaluateAgent(backTestDf=backTestDf,
                                  genPath=genPath,
                                  agentDir=saveDir,
                                  agentName=saveAgentName)
        if lastSaveEp > 0 and sumReward > 0:
            break
        else:
            print("Backtest failed. Retrain.")

        reset_keras()
Exemplo n.º 2
0
def useAgent(symbol, timeframe, terminal, dataUpdater, dataManager,
             hkFeatList, saveDir, genPath, agentName, timeConstraint):
    ###############################
    # use agent on test
    ###############################

    useCpu(nThreads=8, nCores=8)

    openerPriceDiffGenerator = MultiScalerDiffGenerator(featureList=hkFeatList).loadGenerator(genPath)
    buyerPriceDiffGenerator = MultiScalerDiffGenerator(featureList=hkFeatList).loadGenerator(genPath)
    sellerPriceDiffGenerator = MultiScalerDiffGenerator(featureList=hkFeatList).loadGenerator(genPath)

    openerPriceDiffGenerator.setFitMode(False)
    buyerPriceDiffGenerator.setFitMode(False)
    sellerPriceDiffGenerator.setFitMode(False)

    testEnv = RealCompositeEnv(symbol, timeframe, terminal, dataUpdater, dataManager,
                               openerPriceDiffGenerator, buyerPriceDiffGenerator, sellerPriceDiffGenerator,
                               startDeposit=300, lotSize=0.1, lotCoef=100000,
                               stoplossPuncts=60, takeprofitPuncts=120, renderFlag=True)

    # get size of state and action from environment
    openerAgent = DQNAgent(testEnv.observation_space["opener"], testEnv.action_space["opener"].n)
    buyerAgent = DQNAgent(testEnv.observation_space["buyer"], testEnv.action_space["buyer"].n)
    sellerAgent = DQNAgent(testEnv.observation_space["seller"], testEnv.action_space["seller"].n)
    agent = CompositeAgent(openerAgent, buyerAgent, sellerAgent)
    agent = agent.load_agent(saveDir, agentName, dropSupportModel=True)
    # agent  = agent.load_agent("./", "checkpoint_composite")
    print("start using agent")

    startTime = datetime.now()
    agent.use_agent(testEnv, timeConstraint=timeConstraint)
    endTime = datetime.now()
    print("Use time: {}".format(endTime - startTime))
    reset_keras()
Exemplo n.º 3
0
def useAgent(preprocDf, hkFeatList, saveDir, genPath, agentName,
             timeConstraint):
    ###############################
    # use agent on test
    ###############################

    useCpu(nThreads=8, nCores=8)

    testDf = preprocDf.tail(232)

    openerPriceDiffGenerator = MultiScalerDiffGenerator(
        featureList=hkFeatList).loadGenerator(genPath)
    buyerPriceDiffGenerator = MultiScalerDiffGenerator(
        featureList=hkFeatList).loadGenerator(genPath)
    sellerPriceDiffGenerator = MultiScalerDiffGenerator(
        featureList=hkFeatList).loadGenerator(genPath)

    #openerPriceDiffGenerator.setFitMode(False)
    #buyerPriceDiffGenerator.setFitMode(False)
    #sellerPriceDiffGenerator.setFitMode(False)

    testEnv = CompositeEnv(testDf,
                           openerPriceDiffGenerator,
                           buyerPriceDiffGenerator,
                           sellerPriceDiffGenerator,
                           startDeposit=300,
                           lotSize=0.1,
                           lotCoef=100000,
                           spread=18,
                           spreadCoef=0.00001,
                           renderFlag=True,
                           renderDir=saveDir,
                           renderName="testDealsPlot")

    # get size of state and action from environment
    openerAgent = DQNAgent(testEnv.observation_space["opener"],
                           testEnv.action_space["opener"].n)
    buyerAgent = DQNAgent(testEnv.observation_space["buyer"],
                          testEnv.action_space["buyer"].n)
    sellerAgent = DQNAgent(testEnv.observation_space["seller"],
                           testEnv.action_space["seller"].n)
    agent = CompositeAgent(openerAgent, buyerAgent, sellerAgent)
    agent = agent.load_agent(saveDir, agentName, dropSupportModel=True)
    # agent  = agent.load_agent("./", "checkpoint_composite")
    print("start using agent")

    startTime = datetime.now()
    dealsStatistics = agent.use_agent(testEnv, timeConstraint=timeConstraint)
    endTime = datetime.now()
    print("Use time: {}".format(endTime - startTime))
    reset_keras()

    sumRew = 0
    cumulativeReward = []
    for i in range(len(dealsStatistics)):
        sumRew += dealsStatistics[i]
        cumulativeReward.append(sumRew)
    plt.plot([x for x in range(len(cumulativeReward))], cumulativeReward)
    plt.show()
Exemplo n.º 4
0
def cycleTrain(symbol, timeframe, dataUpdater, saveDir, genPath,
               saveAgentName):
    while True:
        dataUpdater.partialUpdate(terminal, symbol, timeframe)
        df = SymbolDataManager().getData(symbol, timeframe)
        preprocDf = preprocData(df)
        createGenerator(preprocDf,
                        featureList=hkFeatList,
                        saveDir=saveDir,
                        saveName=genName)

        trainDf = preprocDf.tail(2032).tail(1032)
        backTestDf = preprocDf.tail(2032).head(1032)

        lastSaveEp = trainAgent(trainDf=trainDf,
                                backTestDf=backTestDf,
                                genPath=genPath,
                                saveAgentDir=saveDir,
                                saveAgentName=saveAgentName)
        ##############
        reset_keras()
        ##############

        #####
        # break
        #####

        sumReward = evaluateAgent(backTestDf=backTestDf,
                                  genPath=genPath,
                                  agentDir=saveDir,
                                  agentName=saveAgentName)
        if lastSaveEp > 0 and sumReward > -110:
            print("Backtest's succesfull. Stop Training.")
            break
        else:
            print("Backtest failed. Retrain.")

        #############
        #weighted train/back reward
        """backReward = evaluateAgent(backTestDf=backTestDf, genPath=genPath, agentDir=saveDir, agentName=saveAgentName)
        trainReward = evaluateAgent(backTestDf=trainDf, genPath=genPath, agentDir=saveDir, agentName=saveAgentName)
        backW = 0.8 #1.0 - (len(backTestDf) / (len(backTestDf) + len(trainDf)))
        trainW = 0.2 #1.0 - (len(trainDf) / (len(backTestDf) + len(trainDf)))
        weightedReward = (backW * backReward + trainW * trainReward) / 2
        if lastSaveEp > 0 and weightedReward > 0:
            print("Backtest's succesfull. Stop Training.")
            break
        else:
            print("Backtest failed. Retrain.")"""
        #############

        reset_keras()
Exemplo n.º 5
0
def collectStatistics(symbol, nExperiment, trainDf, backTestDf, testDf, startDeposit=300, lotSize=0.1, lotCoef=100000, spread=18, spreadCoef=0.00001):
    reset_keras()
    openerPriceDiffGenerator = MultiScalerDiffGenerator(featureList=priceFeatList).loadGenerator("./MSDiffGen.pkl")
    buyerPriceDiffGenerator = MultiScalerDiffGenerator(featureList=priceFeatList).loadGenerator("./MSDiffGen.pkl")
    sellerPriceDiffGenerator = MultiScalerDiffGenerator(featureList=priceFeatList).loadGenerator("./MSDiffGen.pkl")

    trainEnv = CompositeEnv(trainDf, openerPriceDiffGenerator, buyerPriceDiffGenerator, sellerPriceDiffGenerator,
                            startDeposit=startDeposit, lotSize=lotSize, lotCoef=lotCoef, spread=spread, spreadCoef=spreadCoef, renderFlag=True)
    backTestEnv = CompositeEnv(backTestDf, openerPriceDiffGenerator, buyerPriceDiffGenerator, sellerPriceDiffGenerator,
                               startDeposit=startDeposit, lotSize=lotSize, lotCoef=lotCoef, spread=spread, spreadCoef=spreadCoef, renderFlag=True)
    forwardTestEnv = CompositeEnv(testDf, openerPriceDiffGenerator, buyerPriceDiffGenerator, sellerPriceDiffGenerator,
                               startDeposit=startDeposit, lotSize=lotSize, lotCoef=lotCoef, spread=spread, spreadCoef=spreadCoef, renderFlag=True)
    # get size of state and action from environment
    openerAgent = DQNAgent(trainEnv.observation_space["opener"], trainEnv.action_space["opener"].n,
                           memorySize=2500, batch_size=100, train_start=200, epsilon_min=0.05, epsilon=1,
                           epsilon_decay=0.9995, learning_rate=0.0005)
    buyerAgent = DQNAgent(trainEnv.observation_space["buyer"], trainEnv.action_space["buyer"].n,
                          memorySize=5000, batch_size=200, train_start=300, epsilon_min=0.05, epsilon=1,
                          epsilon_decay=1.0, learning_rate=0.0005)
    sellerAgent = DQNAgent(trainEnv.observation_space["seller"], trainEnv.action_space["seller"].n,
                           memorySize=5000, batch_size=200, train_start=300, epsilon_min=0.05, epsilon=1,
                           epsilon_decay=1.0, learning_rate=0.0005)
    agent = CompositeAgent(openerAgent, buyerAgent, sellerAgent)

    continueFit = False
    trainDealsStatistics = []
    testDealsStatistics = []
    backDealsStatistics = []
    for i in range(20):
        agent.fit_agent(env=trainEnv, backTestEnv=None, nEpisodes=1, nWarmUp=0,
                        uniformEps=False, synEps=True, plotScores=True, saveBest=False, saveFreq=111, continueFit=continueFit)
        continueFit = True

        trainDealsStatistics.append( agent.use_agent(trainEnv) )
        testDealsStatistics.append( agent.use_agent(forwardTestEnv) )
        backDealsStatistics.append( agent.use_agent(backTestEnv) )

        with open("./" + "trainDealsStatistics_{}_{}.pkl".format(symbol, nExperiment), mode="wb") as dealsFile:
            joblib.dump(trainDealsStatistics, dealsFile)
        with open("./" + "testDealsStatistics_{}_{}.pkl".format(symbol, nExperiment), mode="wb") as dealsFile:
            joblib.dump(testDealsStatistics, dealsFile)
        with open("./" + "backDealsStatistics_{}_{}.pkl".format(symbol, nExperiment), mode="wb") as dealsFile:
            joblib.dump(backDealsStatistics, dealsFile)
    pass
Exemplo n.º 6
0
                             nWarmUp=0,
                             uniformEps=False,
                             synEps=False,
                             plotScores=False,
                             saveBest=True,
                             saveFreq=1)

endTime = datetime.now()
print("Training finished. Total time: {}".format(endTime - startTime))

###############################
# use agent
###############################
from avera.utils.keras_utils import reset_keras

reset_keras()

#testDf = modDf.tail(12000).head(2000)
#testDf = modDf.tail(10000).head(8000)
testDf = modDf.tail(35000).head(5000)
#testDf = modDf.tail(110000).head(10000) #back

openerPriceDiffGenerator = MultiScalerDiffGenerator(
    featureList=hkFeatList).loadGenerator("./MSDiffGen.pkl")
buyerPriceDiffGenerator = MultiScalerDiffGenerator(
    featureList=hkFeatList).loadGenerator("./MSDiffGen.pkl")
sellerPriceDiffGenerator = MultiScalerDiffGenerator(
    featureList=hkFeatList).loadGenerator("./MSDiffGen.pkl")

#openerPriceDiffGenerator.setFitMode(False)
#buyerPriceDiffGenerator.setFitMode(False)