# -*- coding: utf-8 -*-
"""
Created on Mon Feb 11 08:11:25 2019

@author: cpace

This has a 56% success rate for wheat from 2000 to 2019.  29W 22L for $9.56/bu total
"""
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
from Modules.indicators import stochastics
from Modules.pnp import pnp

#crop = 'corn'
#month = 'jul'
#year = 2013

finalout = [] # [$win,$loss, ctwin, ctloss, totcount]
finalout1 = []
days = 4

#a = dataseries(crop, month, year)

final = []
total = []

def endofrise(row, col, thresh=70):
    if npdf[row][col] <= npdf[row-1][col] and npdf[row][col] >= thresh:
        return True
    elif npdf[row][col] > 85 and npdf[row-1][col] < thresh:
        return True
    else:
        return False
    
def endoffall(row, col, thresh=30):
    if npdf[row][col] >= npdf[row-1][col] and npdf[row][col] <= thresh:
        return True
    else:
        return False
    
def istrending(row, col, thresh=30):
    if npdf[row][col] >= thresh:
        return True
    elif trending and npdf[row][col] > 21:
        return True
    else:
        return False
    
def beginrise(row, col, thresh=30):
    if npdf[row][col] > thresh and npdf[row-1][col] < thresh and npdf[row-1][col] != 0 and npdf[row][5] != 0:
        return True
    else:
        return False
    
def beginfall(row, col, thresh=70):
    if npdf[row][col] < thresh and npdf[row-1][col] > thresh and npdf[row-1][col] != 0 and npdf[row][5] != 0:
        return True
    else:
        return False

def macdswitch(row, col):
    if abs(npdf[row-1][col] - npdf[row][col]) > 1:
        if npdf[row-1][col] < 0 and npdf[row][col] > 0:
            switch = 'Up'
        elif npdf[row-1][col] > 0 and npdf[row][col] < 0:
            switch = 'Down'
        else:
            switch = False
    else: 
        switch = False
    return switch


upper = 70
lower = 30
trend = 30   
i = 0   

for crop in ['Wheat']:
    for year in range(2000,2020):
        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 = stochastics(df).stochastics
            df = macd(df).difference   
            npdf = np.asarray(df)
            
            i = 0 
            look = True
            long = False
            short = False
            trending = False
            switch = False
            position = ''
            indcol = 7
            
            while i < len(npdf)-2:
                while look and i < len(npdf):
                    trending = istrending(i,5)
                    switch = macdswitch(i,9)
                    if switch == 'Up' and trending:
                        positionentry = npdf[i,4]
                        entrydate = npdf[i,0]
                        look = False
                        short = False
                        long = True
                        break
                    elif switch == 'Down' and trending:
                        positionentry = npdf[i,4]
                        entrydate = npdf[i,0]
                        look = False
                        short = True
                        long = False
                        break
                    else:
                        i += 1
                    i+=1
                                   
                while short and i < len(npdf):
                    trending = istrending(i,5)
                    switch = macdswitch(i,9)
                    if not trending:
                        positionexit = npdf[i,4]
                        exitdate = npdf[i,0]
                        look = True
#                        short = False
                        long = False
                        break
                    i += 1
                while long and i < len(npdf):
                    trending = istrending(i,5)
                    switch = macdswitch(i,9)
                    if not trending:
                        positionexit = npdf[i,4]
                        exitdate = npdf[i,0]
                        look = True
#                        short = False
                        long = False
                        break
                    i += 1
                i+=1
                
                if i > npdf.shape[0]-3:
                    positionexit = npdf[i-3,4]
                    exitdate = npdf[i-3,0]
                
                if i == len(npdf)-3:
                    positionexit = npdf[i,4]
                    exitdate = npdf[i,0]
                if long:
                    profit = positionexit - positionentry
                    long = False
                    position = 'Long'
                elif short:
                    profit = positionentry - positionexit
                    short = False
                    position = 'Short'
                else:
                    position = ''
                
                if position != '':
                    finalout.append([position, entrydate, exitdate, profit, positionentry, positionexit, month, year])
                    look = True
                    long = False
                    short = False
                    position = ''
            
            final = [0,0,0]        
            for i in range(len(finalout)):
                if  finalout[i][0] != 'complete':
                    if finalout[i][3] > 0:
                        win = 1
                        loss = 0
                    else:
                        win = 0
                        loss = 1
                    final = [final[0]+finalout[i][3], final[1] + win, final[2] + loss]
                    final.append(final[1]/(final[1]+final[2])*100)
                    
                    
    

a.closeconn()          