# -*- coding: utf-8 -*-
"""
Created on Mon Feb 11 08:11:25 2019

@author: cpace
"""
import numpy as np
from Modules.dataseries import dataseries
from Modules.indicators import bolinger
from Modules.indicators import rsi
from Modules.indicators import macd
from Modules.contractmonths import contractmonths as months
from Modules.indicators import adx

crop = 'soybeans'
month = 'May'
year = 2018

out = [] # [$win,$loss, ctwin, ctloss, totcount]
days = 4

a = dataseries(crop, month, year)

#    0 - Date
#    1 - Open
#    2 - High
#    3 - Low 
#    4 - Close
#    5 - ADX
#    6 - MACD Difference

adxthresh = 25

def findfirst():
    for i in range(1,len(npdf)):
        if npdf[i,5] >= adxthresh and zerodiff(i):
            return i
        elif npdf[i,5] < adxthresh and abs(npdf[i,6] - npdf[i-1,6] < 0):
            return i
    return 'now'

def zerodiff(i):
    if np.sign(npdf[i,6]) != np.sign(npdf[i-1,6]) or npdf[i,6] ==0:
        return True
    else: return False
        
def isdirectionchanged(i, series):
    if abs(series[i] - series[i-1] < 0):
        return True
    else:
        return False
    

def findADXtrend(i):
    for i in range(i,len(npdf)):
        if npdf[i,5]>=25:
            return i
        
def findnextzero(i):
    
    for i in range(i,len(npdf)-days):
        if np.isnan(npdf[i-1,5]) == False:
            if np.sign(npdf[i,5]) != np.sign(npdf[i-1,5]) or npdf[i,5] ==0:
                return i
            

#for crop in ['Wheat']:
#    for year in range(2010,2018):
#        contractmonths = months(crop)
#        monthlist = contractmonths.months
#        for month in monthlist:
yeartotal = 0
trades = 0
a = dataseries(crop, month, year)
df = a.dataseries
df = adx(df,14).adx
df = macd(df).difference


    
npdf = np.asarray(df)

i = findfirst()
if i > 0:
    print(npdf[i,0])
    

#priceentry = npdf[j,4]
#dateentry = npdf[j,0]
#look = True
            
#            while look:
#                i, position = findpullback(i,npdf,'last')    
#                positionexit = npdf[i,4] 
#                exitdate = npdf[i,0]  
#                if position == 'long':
#                    profit = positionexit - positionentry
#                else:
#                    profit = positionentry - positionexit
#                
#                out.append([position, entrydate, exitdate, profit, npdf[i,5]])
##                print(position + ' Position between ' + str(entrydate) + ' and ' + str(exitdate) + ' for ' + str(profit) + ' ADX = '+ str(npdf[i,5]))
#                positionentry = positionexit
#                entrydate = exitdate
#                
#                
#                if i == len(npdf)-1:
#                    look = False
#            
#            final = [0,0,0]        
#            for i in range(len(out)):
#                if  out[i][0] != 'complete':
#                    if out[i][3] > 0:
#                        win = 1
#                        loss = 0
#                    else:
#                        win = 0
#                        loss = 1
#                    final = [final[0]+out[i][3], final[1] + win, final[2] + loss]
#                    final.append(final[1]/(final[1]+final[2])*100)
    

a.closeconn()          