Exemplo n.º 1
0
def collectStatistics(testDf, startDeposit=300, lotSize=0.1, lotCoef=100000, spread=18, spreadCoef=0.00001 ):
    openerPriceDiffGenerator = W2VDiffGenerator(featureList=priceFeatList).loadGenerator("./w2vPriceDiffGen.pkl")
    buyerPriceDiffGenerator = W2VDiffGenerator(featureList=priceFeatList).loadGenerator("./w2vPriceDiffGen.pkl")
    sellerPriceDiffGenerator = W2VDiffGenerator(featureList=priceFeatList).loadGenerator("./w2vPriceDiffGen.pkl")

    openerVolumeDiffGenerator = W2VDiffGenerator(featureList=volumeFeatList).loadGenerator("./w2vVolumeDiffGen.pkl")
    buyerVolumeDiffGenerator = W2VDiffGenerator(featureList=volumeFeatList).loadGenerator("./w2vVolumeDiffGen.pkl")
    sellerVolumeDiffGenerator = W2VDiffGenerator(featureList=volumeFeatList).loadGenerator("./w2vVolumeDiffGen.pkl")

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

    openerVolumeDiffGenerator.setFitMode(False)
    buyerVolumeDiffGenerator.setFitMode(False)
    sellerVolumeDiffGenerator.setFitMode(False)

    openerCompositeGenerator = W2VCompositeGenerator( [openerPriceDiffGenerator,openerVolumeDiffGenerator], flatStack=False)
    buyerCompositeGenerator = W2VCompositeGenerator( [buyerPriceDiffGenerator,buyerVolumeDiffGenerator], flatStack=False)
    sellerCompositeGenerator = W2VCompositeGenerator( [sellerPriceDiffGenerator,sellerVolumeDiffGenerator], flatStack=False)

    testEnv = CompositeEnv(testDf, openerCompositeGenerator, buyerCompositeGenerator, sellerCompositeGenerator,
                        startDeposit=startDeposit, lotSize=lotSize, lotCoef=lotCoef, spread = spread, spreadCoef = spreadCoef, renderFlag=True)
    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("./", "best_composite")
    print("start using agent")
    dealsStatistics = agent.use_agent(testEnv)
    return dealsStatistics
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 evaluateAgent(backTestDf, genPath, agentDir, agentName):

    openerPriceDiffGenerator = MultiScalerDiffGenerator(
        featureList=hkFeatList).loadGenerator("{}".format(genPath))
    buyerPriceDiffGenerator = MultiScalerDiffGenerator(
        featureList=hkFeatList).loadGenerator("{}".format(genPath))
    sellerPriceDiffGenerator = MultiScalerDiffGenerator(
        featureList=hkFeatList).loadGenerator("{}".format(genPath))

    testEnv = CompositeEnv(backTestDf,
                           openerPriceDiffGenerator,
                           buyerPriceDiffGenerator,
                           sellerPriceDiffGenerator,
                           startDeposit=300,
                           lotSize=0.1,
                           lotCoef=100000,
                           spread=18,
                           spreadCoef=0.00001,
                           stoplossPuncts=100,
                           takeprofitPuncts=300,
                           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(agentDir, agentName)
    # agent  = agent.load_agent("./", "checkpoint_composite")
    print("start using agent")
    dealsStatistics = agent.use_agent(testEnv)
    sumRew = 0
    for i in range(len(dealsStatistics)):
        sumRew += dealsStatistics[i]
    return sumRew
Exemplo n.º 5
0
def collectStatistics(testDf,
                      startDeposit=300,
                      lotSize=0.1,
                      lotCoef=100000,
                      spread=18,
                      spreadCoef=0.00001):
    openerPriceDiffGenerator = MultiScalerDiffGenerator(
        featureList=priceFeatList).loadGenerator("./MSDiffGen.pkl")
    buyerPriceDiffGenerator = MultiScalerDiffGenerator(
        featureList=priceFeatList).loadGenerator("./MSDiffGen.pkl")
    sellerPriceDiffGenerator = MultiScalerDiffGenerator(
        featureList=priceFeatList).loadGenerator("./MSDiffGen.pkl")

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

    testEnv = CompositeEnv(testDf,
                           openerPriceDiffGenerator,
                           buyerPriceDiffGenerator,
                           sellerPriceDiffGenerator,
                           startDeposit=startDeposit,
                           lotSize=lotSize,
                           lotCoef=lotCoef,
                           spread=spread,
                           spreadCoef=spreadCoef,
                           renderFlag=True)
    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("./", "best_composite")
    agent = agent.load_agent("./", "checkpoint_composite")
    print("start using agent")
    dealsStatistics = agent.use_agent(testEnv)
    return dealsStatistics
Exemplo n.º 6
0
def evaluateAgent(backTestDf):

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

    testEnv = CompositeEnv(backTestDf,
                           openerPriceDiffGenerator,
                           buyerPriceDiffGenerator,
                           sellerPriceDiffGenerator,
                           startDeposit=300,
                           lotSize=0.1,
                           lotCoef=100000,
                           spread=18,
                           spreadCoef=0.00001,
                           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("./", "best_composite")
    # agent  = agent.load_agent("./", "checkpoint_composite")
    print("start using agent")
    dealsStatistics = agent.use_agent(testEnv)
    sumRew = 0
    for i in range(len(dealsStatistics)):
        sumRew += dealsStatistics[i]
    return sumRew
Exemplo n.º 7
0
#testDf = modDf.tail(6032).tail(1032)

openerPriceDiffGenerator = CompositeGenerator().loadGenerator("./CompositeMSDiffGen.pkl")
buyerPriceDiffGenerator = CompositeGenerator().loadGenerator("./CompositeMSDiffGen.pkl")
sellerPriceDiffGenerator = CompositeGenerator().loadGenerator("./CompositeMSDiffGen.pkl")

#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)

# 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("./", "best_composite", dropSupportModel=True)
#agent  = agent.load_agent("./", "checkpoint_composite")
print("start using agent")

dealsStatistics = agent.use_agent(testEnv)
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.º 8
0
openerCompositeGenerator = W2VCompositeGenerator(
    [openerEnergyScalerGenerator, openerEnergyDiffGenerator], flatStack=False)
buyerCompositeGenerator = W2VCompositeGenerator(
    [buyerEnergyScalerGenerator, buyerEnergyDiffGenerator], flatStack=False)
sellerCompositeGenerator = W2VCompositeGenerator(
    [sellerEnergyScalerGenerator, sellerEnergyDiffGenerator], flatStack=False)

testEnv = CompositeEnv(testDf,
                       openerCompositeGenerator,
                       buyerCompositeGenerator,
                       sellerCompositeGenerator,
                       startDeposit=300,
                       lotSize=0.01,
                       lotCoef=100000,
                       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("./", "best_composite")
print("start using agent")
agent.use_agent(testEnv)
"""realEnv = RealEnv( symbol=symbol, timeframe=timeframe,
                   terminal=terminal, dataUpdater=dataUpdater, dataManager=dataManager, featureFactory=featureFactory,
                   obsFeatList=obsFeatList)
agent.use_agent(realEnv)"""