from avera.feature_generators.CompositeGenerator import CompositeGenerator from avera.datamanagement.SymbolDataManager import SymbolDataManager from avera.datamanagement.SymbolDataUpdater import SymbolDataUpdater symbol = "EURUSD_i" timeframe = "H1" obsFeatList = ["open", "close"] #terminal = MT5Terminal() #dataUpdater = SymbolDataUpdater() dataManager = SymbolDataManager() #dataUpdater.fullUpdate(terminal, symbol, timeframe, startDate="2015-01-01 00:00:00") df = SymbolDataManager().getData( symbol, timeframe) #, normalizeNames=True, normalizeDateTime=True) df = df.tail(1000) openerScaleGen = ScalerGenerator(featureList=obsFeatList, nPoints=50, flatStack=True, fitOnStep=False) openerDiffGen = DiffGenerator(featureList=obsFeatList, nDiffs=1, nPoints=50, flatStack=True, fitOnStep=False) openerFeatureFactory = CompositeGenerator( genList=[openerScaleGen, openerDiffGen]) buyerScaleGen = ScalerGenerator(featureList=obsFeatList, nPoints=50, flatStack=True,
from avera.feature_generators.DiffGenerator import DiffGenerator from avera.feature_generators.FeatureScaler import FeatureScaler from avera.datamanagement.SymbolDataManager import SymbolDataManager from matplotlib import pyplot as plt from dateutil import parser featureList = ["open", "close"] df = SymbolDataManager().getData("TEST", "H1", normalizeDateTime=True, normalizeNames=True) df.set_index("datetime", drop=True, inplace=True) featureScaler = DiffGenerator(featureList=featureList, nDiffs=1, nPoints=4, flatStack=True) featureScaler = featureScaler.globalFit(df) print(df.tail(1)) lastDt = list(df.tail(1).index)[0] print(lastDt) print(df.loc[lastDt]) print(featureScaler.getFeatByDatetime(lastDt, df)) featureScaler = FeatureScaler() df = featureScaler.extractFeature(df, featureList) print(df.loc[lastDt])
startDeposit=300, lotSize=0.01, lotCoef=100000, renderFlag=True) # get size of state and action from environment openerAgent = DQNAgent(trainEnv.observation_space["opener"], trainEnv.action_space["opener"].n, memorySize=500, batch_size=100, train_start=200, epsilon_min=0.01, epsilon=1, epsilon_decay=0.9993) buyerAgent = DQNAgent(trainEnv.observation_space["buyer"], trainEnv.action_space["buyer"].n, memorySize=1000, batch_size=200, train_start=300, epsilon_min=0.01, epsilon=1, epsilon_decay=0.9999) sellerAgent = DQNAgent(trainEnv.observation_space["seller"], trainEnv.action_space["seller"].n, memorySize=1000, batch_size=200, train_start=300, epsilon_min=0.01, epsilon=1, epsilon_decay=0.9999) agent = CompositeAgent(openerAgent, buyerAgent, sellerAgent) agent.fit_agent(env=trainEnv, nEpisodes=20, nWarmUp = 1, uniformEps = False, plotScores=True, saveBest=True, saveFreq=2)""" ############################### # use agent ############################### #testDf = df.copy()#[int(len(df)*0.9):] testDf = df.tail(222) #check scaling at test env #testDf = testDf[int(len(testDf)*0.8):] openerEnergyScalerGenerator = W2VScaleGenerator( featureList=energyFeatList).loadGenerator("./w2vEnergyScaleGen.pkl") buyerEnergyScalerGenerator = W2VScaleGenerator( featureList=energyFeatList).loadGenerator("./w2vEnergyScaleGen.pkl") sellerEnergyScalerGenerator = W2VScaleGenerator( featureList=energyFeatList).loadGenerator("./w2vEnergyScaleGen.pkl") openerEnergyDiffGenerator = W2VDiffGenerator( featureList=energyFeatList).loadGenerator("./w2vEnergyDiffGen.pkl") buyerEnergyDiffGenerator = W2VDiffGenerator( featureList=energyFeatList).loadGenerator("./w2vEnergyDiffGen.pkl") sellerEnergyDiffGenerator = W2VDiffGenerator( featureList=energyFeatList).loadGenerator("./w2vEnergyDiffGen.pkl")
startDeposit=300, lotSize=0.01, lotCoef=100000, renderFlag=True) # get size of state and action from environment openerAgent = DQNAgent(trainEnv.observation_space["opener"], trainEnv.action_space["opener"].n, memorySize=500, batch_size=100, train_start=200, epsilon_min=0.01, epsilon=1, epsilon_decay=1) buyerAgent = DQNAgent(trainEnv.observation_space["buyer"], trainEnv.action_space["buyer"].n, memorySize=1000, batch_size=200, train_start=300, epsilon_min=0.01, epsilon=1, epsilon_decay=1) sellerAgent = DQNAgent(trainEnv.observation_space["seller"], trainEnv.action_space["seller"].n, memorySize=1000, batch_size=200, train_start=300, epsilon_min=0.01, epsilon=1, epsilon_decay=1) agent = CompositeAgent(openerAgent, buyerAgent, sellerAgent) agent.fit_agent(env=trainEnv, nEpisodes=50, nWarmUp = 0, uniformEps = True, plotScores=True, saveBest=True, saveFreq=2)""" ############################### # use agent ############################### #testDf = df.copy()#[int(len(df)*0.9):] testDf = df.tail(5000) #check scaling at test env #testDf = testDf[int(len(testDf)*0.8):] openerPriceScalerGenerator = W2VScaleGenerator( featureList=priceFeatList).loadGenerator("./w2vPriceScaleGen.pkl") buyerPriceScalerGenerator = W2VScaleGenerator( featureList=priceFeatList).loadGenerator("./w2vPriceScaleGen.pkl") sellerPriceScalerGenerator = W2VScaleGenerator( featureList=priceFeatList).loadGenerator("./w2vPriceScaleGen.pkl") openerPriceDiffGenerator = W2VDiffGenerator( featureList=priceFeatList).loadGenerator("./w2vPriceDiffGen.pkl") buyerPriceDiffGenerator = W2VDiffGenerator( featureList=priceFeatList).loadGenerator("./w2vPriceDiffGen.pkl") sellerPriceDiffGenerator = W2VDiffGenerator( featureList=priceFeatList).loadGenerator("./w2vPriceDiffGen.pkl")
from avera.datamanagement.SymbolDataUpdater import SymbolDataUpdater from avera.terminal.MT5Terminal import MT5Terminal from avera.mods.HeikenAshiMod import HeikenAshiMod symbol = "EURUSD_i" timeframe = "M15" hkFeatList = ["open", "close", "low", "high"] volumeFeatList = ["tick_volume"] #terminal = MT5Terminal() #dataUpdater = SymbolDataUpdater("../../data/raw/") dataManager = SymbolDataManager("../../data/raw/") #dataUpdater.fullUpdate(terminal, symbol, timeframe, startDate="2015-01-01 00:00:00") df = SymbolDataManager().getData(symbol, timeframe) df = df.tail(10000) ######## #df = df.tail(10000) #print("applying heiken ashi mod...") #enMod = HeikenAshiMod() #df = enMod.modHistory(df) #enMod.checkQuality(df) ######## """priceDiffGenerator = W2VRelativeDiffGenerator(featureList=hkFeatList, nDiffs=1, nPoints = 21, flatStack = False, fitOnStep = False, nIntervals = 10000, w2vSize=32, window=3, iter=100, min_count=0, sample=0.0, sg=0) volumeDiffGenerator = W2VRelativeDiffGenerator(featureList=volumeFeatList, nDiffs=1, nPoints = 21, flatStack = False, fitOnStep = False, nIntervals = 10000, w2vSize=32, window=3, iter=100, min_count=0, sample=0.0, sg=0) priceDiffGenerator.setFitMode(True) priceDiffGenerator = priceDiffGenerator.globalFit(df)
# train agent ################################ openerPriceDiffGenerator = W2VDiffGenerator(featureList=hkFeatList).loadGenerator("./w2vPriceDiffGen.pkl") buyerPriceDiffGenerator = W2VDiffGenerator(featureList=hkFeatList).loadGenerator("./w2vPriceDiffGen.pkl") sellerPriceDiffGenerator = W2VDiffGenerator(featureList=hkFeatList).loadGenerator("./w2vPriceDiffGen.pkl") openerVolumeDiffGenerator = W2VDiffGenerator(featureList=volumeFeatList).loadGenerator("./w2vVolumeDiffGen.pkl") buyerVolumeDiffGenerator = W2VDiffGenerator(featureList=volumeFeatList).loadGenerator("./w2vVolumeDiffGen.pkl") sellerVolumeDiffGenerator = W2VDiffGenerator(featureList=volumeFeatList).loadGenerator("./w2vVolumeDiffGen.pkl") openerCompositeGenerator = W2VCompositeGenerator( [openerPriceDiffGenerator, openerVolumeDiffGenerator], flatStack=False) buyerCompositeGenerator = W2VCompositeGenerator( [buyerPriceDiffGenerator, buyerVolumeDiffGenerator], flatStack=False) sellerCompositeGenerator = W2VCompositeGenerator( [sellerPriceDiffGenerator, sellerVolumeDiffGenerator], flatStack=False) #trainDf = df[:int(len(df)*0.9)] trainDf = df.tail(2044).head(1022) #trainDf = trainDf[:int(len(trainDf)*0.5)] trainEnv = CompositeEnv(trainDf, openerCompositeGenerator, buyerCompositeGenerator, sellerCompositeGenerator, startDeposit=300, lotSize=0.1, lotCoef=100000, renderFlag=True) # get size of state and action from environment openerAgent = DQNAgent(trainEnv.observation_space["opener"], trainEnv.action_space["opener"].n, memorySize=500, batch_size=100, train_start=200, epsilon_min=0.05, epsilon=1, epsilon_decay=0.9994) buyerAgent = DQNAgent(trainEnv.observation_space["buyer"], trainEnv.action_space["buyer"].n, memorySize=1000, batch_size=200, train_start=300, epsilon_min=0.05, epsilon=1, epsilon_decay=0.9999) sellerAgent = DQNAgent(trainEnv.observation_space["seller"], trainEnv.action_space["seller"].n, memorySize=1000, batch_size=200, train_start=300, epsilon_min=0.05, epsilon=1, epsilon_decay=0.9999) agent = CompositeAgent(openerAgent, buyerAgent, sellerAgent) agent.fit_agent(env=trainEnv, nEpisodes=15, nWarmUp = 0, uniformEps = False, plotScores=True, saveBest=True, saveFreq=2) ############################### # use agent
sellerVolumeDiffGenerator = W2VDiffGenerator( featureList=volumeFeatList).loadGenerator("./w2vVolumeDiffGen.pkl") openerCompositeGenerator = W2VCompositeGenerator( [openerPriceDiffGenerator, openerVolumeDiffGenerator], flatStack=False) buyerCompositeGenerator = W2VCompositeGenerator( [buyerPriceDiffGenerator, buyerVolumeDiffGenerator], flatStack=False) sellerCompositeGenerator = W2VCompositeGenerator( [sellerPriceDiffGenerator, sellerVolumeDiffGenerator], flatStack=False) print("data updated") print("start train") ################################ # train agent ################################ trainDf = df.tail(6200).head(5200) trainEnv = CompositeEnv(trainDf, openerCompositeGenerator, buyerCompositeGenerator, sellerCompositeGenerator, startDeposit=300, lotSize=0.1, lotCoef=100000, spread=18, spreadCoef=0.00001, renderFlag=True) backTestDf = df.tail(7200).head(1200) backTestEnv = CompositeEnv(backTestDf, openerCompositeGenerator, buyerCompositeGenerator, sellerCompositeGenerator,