#!/usr/bin/env python
# -*- coding: utf-8 -*-
import talib
import cross_order as order
import time


def TDSequential(close):
    tdlist = [0, 0, 0, 0]  # tdlist是存储TD结果用的,前4个无效用0占位置.因为TD定义是下标第n和n-4比较
    top = 0  # 初始化上标
    bot = 0  # 初始化下标
    for i in range(4, len(close)):  # 为何会从4开始,因为要用index n与index n-4 作比较,比他大才算TD上标1
        if close.iloc[i] > close.iloc[i - 4]:  # 由于i本身就是从4开始, 所以就是由4与0比较,i的最后一个循环到列表的最后一个数字,能够遍历完
            top += 1
            bot *= 0
            if top <= 13:
                tdlist.append(top)
        else:
            top *= 0
            bot += 1
            if top >= -13:
                tdlist.append(-bot)
    return tdlist


def main():
    print("任务开始时间:", time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))

    for symbol in order.symbol_pool:
        # 设置杠杆倍数
        order.set_leverage(symbol=symbol, leverage='25')
        # 获取标的的最新价
        df = order.get_candlesticks(symbol=symbol, interval='15m', limit=str(300))
        tdsequential = TDSequential(df['close'])
        if (tdsequential[-2] == -8) and (tdsequential[-1] == -9):
            order.up_cross(symbol, 'td(-9) 策略做多')
            print('td(-9) 策略做多: ' + symbol)
        elif (tdsequential[-2] == 8) and (tdsequential[-1] == 9):
            order.down_cross(symbol, 'td(9) 策略做空')
            print('td(9) 策略做空: ' + symbol)
        if (tdsequential[-2] == -12) and (tdsequential[-1] == -13):
            order.up_cross(symbol, 'td(-13) 策略做多')
            print('td(-13) 策略做多: ' + symbol)
        elif (tdsequential[-2] == 12) and (tdsequential[-1] == 13):
            order.down_cross(symbol, 'td(13) 策略做空')
            print('td(13) 策略做空: ' + symbol)

    time.sleep(5)

    print("任务结束时间:", time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))


if __name__ == '__main__':
    main()