package link.at17.mid.tushare;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import link.at17.mid.tushare.dao.StockDailyDao;
import link.at17.mid.tushare.dao.ThsDailyDao;
import link.at17.mid.tushare.dao.ThsListDao;
import link.at17.mid.tushare.data.crawler.tushare.TushareCrawler;
import link.at17.mid.tushare.data.models.StockValue;
import link.at17.mid.tushare.data.models.ThsStockInfo;
import link.at17.mid.tushare.enums.ThsStockMarket;
import link.at17.mid.tushare.system.util.LocalDateTimeUtils;
import lombok.extern.slf4j.Slf4j;

/**
 * 均线多头排列测试
 * @author Barry
 *
 */
@SpringBootTest
@Slf4j
class VolIncreaseBk {

    @Autowired
    TushareCrawler tushareCrawler;
    
    @Autowired
    StockDailyDao stockDailyDao;
    
    @Autowired
    ThsListDao thsListDao;
    
    @Autowired
    ThsDailyDao thsDailyDao;
    
    @Test
    void test() throws ParseException {
        List<ThsStockInfo> stockInfos = thsListDao.listByExchange(ThsStockMarket.A);
        //stockInfos = new ArrayList<>();
        //stockInfos.add(stockInfoDao.selectOne(new QueryWrapper<StockInfo>().eq("ts_code", "601330.SH")));
        
        
        List<Double> profits = new ArrayList<>();
        int count = 5;
        stockInfos.parallelStream().forEach(stockInfo -> {
            List<StockValue> dailyK = thsDailyDao.getDailyBeforeTushare(stockInfo.getTsCode(), null, 200L);
            if (dailyK.size() == 0) {
                return;
            }
            
            // 测试均线多头策略的收益
            
            for (int i = count; i < dailyK.size(); i++) {
                StockValue current = dailyK.get(i);
                Double first = dailyK.get(i - count).getClose();
                Double last = dailyK.get(i - 1).getClose();
                boolean hugeVol = dailyK.subList(i - count, i).stream().mapToDouble(StockValue::getVol).average().orElse(current.getVol()) * 2 < current.getVol();
                hugeVol = hugeVol && (last - first) / first < 0.05;
                hugeVol = hugeVol && (current.getClose() > current.getOpen());
                
                if (hugeVol) {
                    String date = LocalDateTimeUtils.format(current.getDate(), "yyyy-MM-dd");
                        log.info("{} {} - {} 放巨量",
                                stockInfo.getTsCode(), 
                                stockInfo.getName(), 
                                date);
                }
            }
            
            /*
            bullishArrange.stream().forEach(index -> {
                log.info("{} {} - {} 出现多头排列",
                            stockInfo.getTsCode(), 
                            stockInfo.getName(), 
                            DateUtils.format(dailyK.get(index).getDate(), "yyyy-MM-dd"));
            });
            if (true) return;*/
        });
        System.out.println("总收益率 " + profits.stream().reduce(Double::sum).orElse(0d) / profits.size());
    }

}
