#!/usr/bin/env python
# coding: utf-8

# In[5]:


import os

import pandas as pd
import datetime
import numpy as np

from tqdm import tqdm

from indicators_v2 import *
from signals_v2 import *
from dealManager import *
from trandeVoter import *
from riskManager import *
import pickle


# In[6]:


df_candle = pd.read_csv("../data/EURUSD_price_candlestick.csv")
df_candle.rename(columns={'timestamp': 'date'}, inplace=True)
df_candle


# In[7]:


df_candle['close']


# In[8]:


class decsionManager:
    '''
sigAgrReq = {
    'sig_BB':{
        'className':sig_BB,
        'params':{'source':'close','target':'close'},
        'indicators':{
            'ind_BB':{
                'className':ind_BB,
                'params':{'MeanType':'SMA','window':30,'valueType':'close','kDev':2.5}
            }            
        }
    },
    'sig_BB_2':{
        'className':sig_BB,
        'params':{'source':'close','target':'close'},
        'indicators':{
            'ind_BB':{
                'className':ind_BB,
                'params':{'MeanType':'SMA','window':30,'valueType':'close','kDev':2}
            }            
        }
    }
}

sigAgrData = {
    'sig_BB':{
        'signalData': df_candle[990:1000],
        'indicatorData' :{'ind_BB': df_candle[:1000]}
    },
    'sig_BB_2':{
        'signalData': df_candle[990:1000],
        'indicatorData' :{'ind_BB': df_candle[:1000]}
    }    
}


sigAgrRetroTemplate = {
    'sig_BB':{
        'signalData': None,
        'indicatorData' :{'ind_BB': None}
    },
    'sig_BB_2':{
        'signalData': None,
        'indicatorData' :{'ind_BB': None}
    }     
}    
    
    
    
    
    
    
    '''
    
    
    
    
    def __init__(self,name, sigDict: dict):
        self.RM = riskManager()
        self.DM = DealManager()
        self.TV = trandeVoter(name)
        self.SA = signalsAgrigator(sigDict)
        self.sigDict = sigDict
        
        
    def getOnlineAns(self, signalsAns: dict, price: float) -> dict:
        probabilityDecsion = self.TV.getDecisionBySignals(self.getSignalsAns(signalsAns))
        RMD = self.RM.getDecision(probabilityDecision=probabilityDecsion, price=price, deals = self.DM.deals)
        return RMD
        
    def getSignalsAns(self, signalsDataDict: dict) -> dict:
        return self.SA.getAns(signalsDataDict)
    
    def getRightAns(self,value_1, value_2):
        
        ans=''
        
        if value_1 > value_2:
            ans = 'down'
        elif value_1 < value_2:
            ans = 'up'
        else:
            ans = 'none'
        
        return ans
        
    def getRetroTrendAns(self, retroTemplateDict: dict, data: pd.DataFrame(), window: int) -> list:
        
        reqSig={} 
        ans = {
            'signalsAns':[],
            'rightAns':[]
            
        }
        target = ''
        
        
        for k in tqdm(range(data.shape[0]-window-1)):
            for i in retroTemplateDict.keys():
                reqSig[i] = {'signalData': data[k:k+window], 'indicatorData':{}}
                target = self.SA.signals[i].params['target']
                for j in retroTemplateDict[i]['indicatorData'].keys():
                    reqSig[i]['indicatorData'][j] = data[k:k+window]
                    
            sigAns = self.getSignalsAns(reqSig)
            rightAns = self.getRightAns(data[target][k], data[target][k+1])

            ans['signalsAns'].append(sigAns)
            ans['rightAns'].append(rightAns)

        return ans
 

    def generateMatrixProbabilityFromDict(self, dictSignals: dict) -> dict:
        self.TV.createMatrixAmounts(dictSignals['signalsAns'][0].keys())
        for i in range(len(dictSignals['signalsAns'])):
            self.TV.setDecisionBySignals(signalDecisions = dictSignals['signalsAns'][i],
                                        trande = dictSignals['rightAns'][i])
        self.TV.generateMatrixProbability()
        
    def createDump(self,postfix='') -> str:
        dataDict = {
            'RM':self.RM,
            'DM':self.DM,
            'TV':self.TV,
            'SA':self.SA,
            'sigDict':self.sigDict
        }
        fileName='data_'+postfix+'.pickle'
        with open(fileName, 'wb') as f:
            pickle.dump(dataDict, f)
            
        return os.path.abspath(fileName)
    
    def loadDump(self,path: str) -> None:
        
        with open(path, 'rb') as f:
            dataDict = pickle.load(f)
        
        self.RM = dataDict['RM']
        self.DM = dataDict['DM']
        self.TV = dataDict['TV']
        self.SA = dataDict['SA']
        self.sigDict = dataDict['sigDict']


# In[9]:


sigAgrReq = {
    'sig_BB':{
        'className':sig_BB,
        'params':{'source':'close','target':'close'},
        'indicators':{
            'ind_BB':{
                'className':ind_BB,
                'params':{'MeanType':'SMA','window':30,'valueType':'close','kDev':2.5}
            }            
        }
    },
    'sig_BB_2':{
        'className':sig_BB,
        'params':{'source':'close','target':'close'},
        'indicators':{
            'ind_BB':{
                'className':ind_BB,
                'params':{'MeanType':'SMA','window':30,'valueType':'close','kDev':2}
            }            
        }
    }
}

sigAgrData = {
    'sig_BB':{
        'signalData': df_candle[990:1000],
        'indicatorData' :{'ind_BB': df_candle[:1000]}
    },
    'sig_BB_2':{
        'signalData': df_candle[990:1000],
        'indicatorData' :{'ind_BB': df_candle[:1000]}
    }    
}


sigAgrRetroTemplate = {
    'sig_BB':{
        'signalData': None,
        'indicatorData' :{'ind_BB': None}
    },
    'sig_BB_2':{
        'signalData': None,
        'indicatorData' :{'ind_BB': None}
    }     
}


# In[10]:


test = decsionManager('Pipa', sigAgrReq)


# In[11]:


test.__dict__


# In[12]:


test.TV.__dict__


# In[13]:


test.SA.signals['sig_BB'].params['target']


# In[14]:


test.getSignalsAns(sigAgrData)


# In[15]:


#test.loadDump('C:\\Users\\Redsandy\\PyProj\\Trade\\MVP\\data_pupa.pickle')


# In[16]:


uuu = test.getRetroTrendAns(sigAgrRetroTemplate,df_candle[:5000],40)
uuu


# In[17]:


test.generateMatrixProbabilityFromDict(uuu)


# In[18]:


test.TV.__dict__


# In[19]:


test.getOnlineAns(sigAgrData, 0.0)


# In[20]:


(test.DM.deals).shape


# In[21]:


test.createDump('pupa')


# In[ ]:





# In[22]:


with open('C:\\Users\\Redsandy\\PyProj\\Trade\\MVP\\data_pupa.pickle', 'rb') as f:
    data_new = pickle.load(f)
data_new


# In[ ]:





# In[ ]:





# In[ ]:





# In[ ]:





# In[ ]:





# In[ ]:





# In[ ]:





# In[ ]:





# In[ ]:





# In[ ]:




