main.cpp
#include <iostream>
#include <math.h>
#include "DAY.h"
#include "WEEK.h"
#include "MONTH.h"
#include "ENUM.h"
#include "READ_FILE.h"
#include "SHOW.h"
using namespace std;
const int MAX=10000;
int days,weeks,months;
int FOMCs,NFPs,URs,PCEs,CPIs,ADPs,GDPs,Powells;
int ASML_reports,ECBs;
int yyyymmdd[MAX];
float opening[MAX];
float closing[MAX];
float highest[MAX];
float lowest[MAX];
int FOMC[MAX];
int NFP[MAX];
int UR[MAX];
int PCE[MAX];
int CPI[MAX];
int ADP[MAX];
int GDP[MAX];
int Powell[MAX];
int ASML_report[MAX];
int ECB[MAX];
extern void READ_DATA(OBJECT_NAME object);
extern void SET_DAY(DAY (*dayPtr));
extern void SET_WEEK(WEEK (*weekPtr),DAY (*dayPtr));
extern void SET_MONTH(MONTH (*monthPtr),DAY (*dayPtr));
extern inline bool SEARCH(int search[],int length,int data);
extern inline int DISTANCE(int yyyymmdd1,int yyyymmdd2);
extern inline int TOMORROW(int yyyymmdd);
int main()
{
READ_DATA(OBJECT_NAME::_6Emain);
DAY day[days];
DAY (*dayPtr)[days];
dayPtr=&day;
WEEK week[weeks];
WEEK (*weekPtr)[weeks];
weekPtr=&week;
MONTH month[months];
MONTH (*monthPtr)[months];
monthPtr=&month;
SET_DAY(*dayPtr);
SET_WEEK(*weekPtr,*dayPtr);
SET_MONTH(*monthPtr,*dayPtr);
//SHOW_BASIC_DATA(week,weeks);
SHOW_EVENT(week,EVENT_NAME::ECB,1,3);
//cout<<endl<<"days="<<days<<'\t'<<"weeks="<<weeks<<'\t'<<"months="<<months<<endl;
return 0;
}
void READ_DATA(OBJECT_NAME object)
{
switch(object)
{
case OBJECT_NAME::ESmain:
days=READ_FILE(yyyymmdd,"data/ESmain/yyyymmdd.txt");
READ_FILE(opening,"data/ESmain/opening.txt");
READ_FILE(closing,"data/ESmain/closing.txt");
READ_FILE(highest,"data/ESmain/highest.txt");
READ_FILE(lowest,"data/ESmain/lowest.txt");
break;
case OBJECT_NAME::CLmain:
days=READ_FILE(yyyymmdd,"data/CLmain/yyyymmdd.txt");
READ_FILE(opening,"data/CLmain/opening.txt");
READ_FILE(closing,"data/CLmain/closing.txt");
READ_FILE(highest,"data/CLmain/highest.txt");
READ_FILE(lowest,"data/CLmain/lowest.txt");
break;
case OBJECT_NAME::_6Emain:
days=READ_FILE(yyyymmdd,"data/6Emain/yyyymmdd.txt");
READ_FILE(opening,"data/6Emain/opening.txt");
READ_FILE(closing,"data/6Emain/closing.txt");
READ_FILE(highest,"data/6Emain/highest.txt");
READ_FILE(lowest,"data/6Emain/lowest.txt");
break;
case OBJECT_NAME::SPY:
days=READ_FILE(yyyymmdd,"data/SPY/yyyymmdd.txt");
READ_FILE(opening,"data/SPY/opening.txt");
READ_FILE(closing,"data/SPY/closing.txt");
READ_FILE(highest,"data/SPY/highest.txt");
READ_FILE(lowest,"data/SPY/lowest.txt");
break;
case OBJECT_NAME::QQQ:
days=READ_FILE(yyyymmdd,"data/QQQ/yyyymmdd.txt");
READ_FILE(opening,"data/QQQ/opening.txt");
READ_FILE(closing,"data/QQQ/closing.txt");
READ_FILE(highest,"data/QQQ/highest.txt");
READ_FILE(lowest,"data/QQQ/lowest.txt");
break;
case OBJECT_NAME::ASML:
days=READ_FILE(yyyymmdd,"data/ASML/yyyymmdd.txt");
READ_FILE(opening,"data/ASML/opening.txt");
READ_FILE(closing,"data/ASML/closing.txt");
READ_FILE(highest,"data/ASML/highest.txt");
READ_FILE(lowest,"data/ASML/lowest.txt");
break;
}
weeks=days/4.7;
months=days/20;
FOMCs=READ_FILE(FOMC,"data/event/FOMC.txt");
NFPs=READ_FILE(NFP,"data/event/NFP.txt");
URs=READ_FILE(UR,"data/event/UR.txt");
PCEs=READ_FILE(PCE,"data/event/PCE.txt");
CPIs=READ_FILE(CPI,"data/event/CPI.txt");
ADPs=READ_FILE(ADP,"data/event/ADP.txt");
GDPs=READ_FILE(GDP,"data/event/GDP.txt");
Powells=READ_FILE(Powell,"data/event/Powell.txt");
ASML_reports=READ_FILE(Powell,"data/event/ASML_report.txt");
ECBs=READ_FILE(ECB,"data/event/ECB.txt");
}
void SET_DAY(DAY (*dayPtr))
{
if(dayPtr==nullptr)
{
return;
}
int count=0;
for(int i=0;i<days;i++)
{
dayPtr[i].date.set(yyyymmdd[i]);
dayPtr[i].set_opening(opening[i]);
dayPtr[i].set_closing(closing[i]);
dayPtr[i].set_highest(highest[i]);
dayPtr[i].set_lowest(lowest[i]);
if(SEARCH(FOMC,FOMCs,yyyymmdd[i])==true)
{
dayPtr[i].set_event(EVENT_NAME::FOMC,count);
count++;
}
if(SEARCH(NFP,NFPs,yyyymmdd[i])==true)
{
dayPtr[i].set_event(EVENT_NAME::NFP,count);
count++;
}
if(SEARCH(UR,URs,yyyymmdd[i])==true)
{
dayPtr[i].set_event(EVENT_NAME::UR,count);
count++;
}
if(SEARCH(PCE,PCEs,yyyymmdd[i])==true)
{
dayPtr[i].set_event(EVENT_NAME::PCE,count);
count++;
}
if(SEARCH(CPI,CPIs,yyyymmdd[i])==true)
{
dayPtr[i].set_event(EVENT_NAME::CPI,count);
count++;
}
if(SEARCH(ADP,ADPs,yyyymmdd[i])==true)
{
dayPtr[i].set_event(EVENT_NAME::ADP,count);
count++;
}
if(SEARCH(GDP,GDPs,yyyymmdd[i])==true)
{
dayPtr[i].set_event(EVENT_NAME::GDP,count);
count++;
}
if(SEARCH(ECB,ECBs,yyyymmdd[i])==true)
{
dayPtr[i].set_event(EVENT_NAME::ECB,count);
count++;
}
/*if(SEARCH(Powell,Powells,yyyymmdd[i])==true)
{
dayPtr[i].set_event(EVENT_NAME::Powell,count);
count++;
}*/
/*if(SEARCH(ASML_report,ASML_reports,yyyymmdd[i])==true)
{
dayPtr[i].set_event(EVENT_NAME::ASML_report,count);
count++;
}*/
dayPtr[i].set(count);
count=0;
}
}
void SET_WEEK(WEEK (*weekPtr),DAY (*dayPtr))
{
if(weekPtr==nullptr||dayPtr==nullptr)
{
return;
}
int count=0,j=0;
for(int i=0;i<days;i++)
{
weekPtr[count].weekday[j]=dayPtr[i];
//明天不休市
if(DISTANCE(yyyymmdd[i],yyyymmdd[i+1])==1)
{
j++;
continue;
}
//现在是星期五,本周数据统计结束,下周一不休市
if(DISTANCE(yyyymmdd[i],yyyymmdd[i+1])==3)
{
weekPtr[count].set(count+1);
count++;
j=0;
continue;
}
//本周五或者下周一休市
if(DISTANCE(yyyymmdd[i],yyyymmdd[i+1])==4)
{
//现在是星期四,星期五休市,本周数据统计结束
if(weekPtr[count].weekday[j].date.get_weekday()==4)
{
weekPtr[count].weekday[j+1].date.set(TOMORROW(yyyymmdd[i]));
weekPtr[count].set(count+1);
count++;
j=0;
}
//现在是星期五,本周数据统计结束,下周一休市
else
{
weekPtr[count].set(count+1);
count++;
j=0;
weekPtr[count].weekday[j].date.set(TOMORROW(TOMORROW(TOMORROW(yyyymmdd[i]))));
j++;
}
continue;
}
//明天(周二/周三/周四)休市
if(DISTANCE(yyyymmdd[i],yyyymmdd[i+1])==2)
{
weekPtr[count].weekday[j+1].date.set(TOMORROW(yyyymmdd[i]));
j+=2;
continue;
}
//现在是统计数据中的最后一天
weekPtr[count].set(count+1);
}
weeks=count+1;
}
void SET_MONTH(MONTH (*monthPtr),DAY (*dayPtr))
{
if(monthPtr==nullptr||dayPtr==nullptr)
{
return;
}
int count=0,j=0,m1,m2;
for(int i=0;i<days;i++)
{
monthPtr[count].day[j]=dayPtr[i];
/*monthPtr[count].day[j].date.set(yyyymmdd[i]);
monthPtr[count].day[j].set_opening(opening[i]);
monthPtr[count].day[j].set_closing(closing[i]);
monthPtr[count].day[j].set_highest(highest[i]);
monthPtr[count].day[j].set_lowest(lowest[i]);
monthPtr[count].day[j].set(0);/*/
m1=(yyyymmdd[i]-yyyymmdd[i]/10000*10000)/100;
m2=(yyyymmdd[i+1]-yyyymmdd[i+1]/10000*10000)/100;
if(m1!=m2)
{
monthPtr[count].set(j+1);
count++;
j=0;
continue;
}
j++;
}
months=count;
}
inline bool SEARCH(int search[],int length,int data)
{
int low=0,high=length-1;
while(low<=high)
{
int mid=low+(high-low)/2;
if(search[mid]==data)
{
return true;
}
else
{
if(search[mid]<data)
{
low=mid+1;
}
else
{
high=mid-1;
}
}
}
return false;
}
inline int DISTANCE(int yyyymmdd1,int yyyymmdd2)
{
int y1,m1,d1,m2,d2,distance,x=0;
int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
y1=yyyymmdd1/10000;
m1=(yyyymmdd1-yyyymmdd1/10000*10000)/100;
d1=yyyymmdd1-yyyymmdd1/10000*10000-(yyyymmdd1-yyyymmdd1/10000*10000)/100*100;
m2=(yyyymmdd2-yyyymmdd2/10000*10000)/100;
d2=yyyymmdd2-yyyymmdd2/10000*10000-(yyyymmdd2-yyyymmdd2/10000*10000)/100*100;
if(m1!=m2)
{
x=days[m1-1];
if(y1%4==0&&m1==2)
x++;
}
distance=d2-d1+x;
return distance;
}
inline int TOMORROW(int yyyymmdd)
{
int tomorrow;
int y1,m1,d1,y2,m2,d2;
int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
y1=yyyymmdd/10000;
m1=(yyyymmdd-yyyymmdd/10000*10000)/100;
d1=yyyymmdd-yyyymmdd/10000*10000-(yyyymmdd-yyyymmdd/10000*10000)/100*100;
if(d1<days[m1-1])
{
y2=y1;
m2=m1;
d2=d1+1;
}
else
{
if(m1==12)
{
y2=y1+1;
m2=1;
d2=1;
}
else
{
y2=y1;
m2=m1+1;
d2=1;
}
}
tomorrow=y2*10000+m2*100+d2;
return tomorrow;
}
DATE.h
#ifndef _DATE_H_
#define _DATE_H_
#include <iostream>
using namespace std;
extern int WEEKDAY(int year,int month,int day);
class DATE
{
public:
void set(int yyyymmdd);
int get_yyyymmdd()const;
int get_year()const;
int get_month()const;
int get_day()const;
int get_weekday()const;
void show()const;
private:
int yyyymmdd;
int year;
int month;
int day;
int weekday;
};
void DATE::set(int yyyymmdd)
{
this->yyyymmdd=yyyymmdd;
year=yyyymmdd/10000;
month=(yyyymmdd-year*10000)/100;
day=yyyymmdd-year*10000-month*100;
weekday=(yyyymmdd==0?0:WEEKDAY(year,month,day));
}
int DATE::get_yyyymmdd()const
{
return yyyymmdd;
}
int DATE::get_year()const
{
return year;
}
int DATE::get_month()const
{
return month;
}
int DATE::get_day()const
{
return day;
}
int DATE::get_weekday()const
{
return weekday;
}
void DATE::show()const
{
cout<<year<<"年"<<month<<"月"<<day<<"日";
}
int WEEKDAY(int year,int month,int day)
{
if(month==1||month==2)
{
month+=12;
year--;
}
int weekday=(day+2*month+3*(month+1)/5+year+year/4-year/100+year/400)%7+1;
return weekday;
}
#endif
DAY.h
#ifndef _DAY_H_
#define _DAY_H_
#include <iostream>
#include <math.h>
#include "DATE.h"
#include "EVENT.h"
using namespace std;
class DAY
{
public:
DATE date;
DAY()
{
opening=0;
closing=0;
highest=0;
lowest=0;
max_up_amp=0;
max_down_amp=0;
max_amp=0;
events=0;
}
void set_opening(float opening);
void set_closing(float closing);
void set_highest(float highest);
void set_lowest(float lowest);
void set_event(EVENT_NAME name,int number);
void set(int events);
bool exist();
float get_opening()const;
float get_closing()const;
float get_highest()const;
float get_lowest()const;
float get_max_amp()const;
bool is_up_or_down()const;
int get_events()const;
EVENT_NAME get_event_name(int number)const;
void show()const;
private:
float opening;
float closing;
float highest;
float lowest;
float max_up_amp;
float max_down_amp;
float max_amp;
bool up_or_down;
int events;
EVENT event[5];
};
void DAY::set_opening(float opening)
{
this->opening=opening;
}
void DAY::set_closing(float closing)
{
this->closing=closing;
}
void DAY::set_highest(float highest)
{
this->highest=highest;
}
void DAY::set_lowest(float lowest)
{
this->lowest=lowest;
}
void DAY::set_event(EVENT_NAME name,int number)
{
event[number].set_name(name);
}
void DAY::set(int events)
{
this->events=events;
max_up_amp=(highest-opening)/opening;
max_down_amp=(opening-lowest)/opening;
if(max_up_amp>max_down_amp)
{
max_amp=max_up_amp;
up_or_down=true;
}
else
{
max_amp=max_down_amp;
up_or_down=false;
}
}
bool DAY::exist()
{
if(opening==0)
{
return false;
}
else
{
return true;
}
}
float DAY::get_opening()const
{
return opening;
}
float DAY::get_closing()const
{
return closing;
}
float DAY::get_highest()const
{
return highest;
}
float DAY::get_lowest()const
{
return lowest;
}
float DAY::get_max_amp()const
{
return max_amp;
}
bool DAY::is_up_or_down()const
{
return up_or_down;
}
int DAY::get_events()const
{
return events;
}
EVENT_NAME DAY::get_event_name(int number)const
{
return event[number].get_name();
}
void DAY::show()const
{
cout<<"______________________"<<endl
<<"日期:";
date.show();
cout<<endl
<<"星期"<<date.get_weekday()<<endl
<<endl
<<"开盘价:"<<opening<<endl
<<"收盘价:"<<closing<<endl
<<"最高价:"<<highest<<endl
<<"最低价:"<<lowest<<endl
<<"最大单边波幅:"<<round(max_amp*10000)/100.0<<"%"<<endl
<<"最大涨幅:"<<round(max_up_amp*10000)/100.0<<"%"<<endl
<<"最大跌幅:"<<round(max_down_amp*10000)/100.0<<"%"<<endl
<<"事件数:"<<events<<endl
<<"事件:";
if(events>0)
{
for(int i=0;i<events;i++)
{
switch(event[i].get_name())
{
case EVENT_NAME::FOMC:
cout<<"FOMC ";
break;
case EVENT_NAME::NFP:
cout<<"NFP ";
break;
case EVENT_NAME::UR:
cout<<"UR ";
break;
case EVENT_NAME::PCE:
cout<<"PCE ";
break;
case EVENT_NAME::CPI:
cout<<"CPI ";
break;
case EVENT_NAME::ADP:
cout<<"ADP ";
break;
case EVENT_NAME::GDP:
cout<<"GDP ";
break;
case EVENT_NAME::Powell:
cout<<"Powell ";
break;
case EVENT_NAME::ASML_report:
cout<<"ASML_report ";
break;
case EVENT_NAME::ECB:
cout<<"ECB ";
break;
}
}
}
else
{
cout<<"无 ";
}
cout<<endl;
}
#endif
WEEK.h
#ifndef _WEEK_H_
#define _WEEK_H_
#include <math.h>
#include "DATE.h"
#include "EVENT.h"
#include "DAY.h"
class WEEK
{
public:
DAY weekday[5];
DAY trading_day[5];
WEEK()
{
number=0;
trading_days=0;
opening=0;
closing=0;
highest=0;
lowest=0;
max_up_amp=0;
max_down_amp=0;
max_amp=0;
events=0;
}
void set(int number);
int get_number()const;
int get_trading_days()const;
float get_opening()const;
float get_opening(int trading_day)const;
float get_closing()const;
float get_closing(int trading_day)const;
float get_highest()const;
float get_highest(int trading_day1,int trading_day2)const;
float get_lowest()const;
float get_lowest(int trading_day1,int trading_day2)const;
float get_max_amp()const;
float get_max_amp(int trading_day1,int trading_day2)const;
bool is_up_or_down()const;
int get_highest_time()const;
int get_lowest_time()const;
int get_max_amp_time()const;
int get_events()const;
EVENT_NAME get_event_name(int number)const;
int get_event_yyyymmdd(int number)const;
void show()const;
private:
int number;
int trading_days;
float opening;
float closing;
float highest;
float lowest;
float max_up_amp;
float max_down_amp;
float max_amp;
bool up_or_down;
int highest_time;
int lowest_time;
int max_amp_time;
int events;
EVENT event[10];
};
void WEEK::set(int number)
{
this->number=number;
for(int i=0;i<5;i++)
{
if(weekday[i].exist()==true)
{
trading_day[trading_days]=weekday[i];
trading_days++;
if(weekday[i].get_events()>0)
{
for(int j=0;j<weekday[i].get_events();j++)
{
event[events].set_name(weekday[i].get_event_name(j));
event[events].set_yyyymmdd(weekday[i].date.get_yyyymmdd());
events++;
}
}
}
}
opening=trading_day[0].get_opening();
if(trading_days==5)
{
closing=trading_day[4].get_closing();
}
else
{
closing=trading_day[3].get_closing();
}
float max=0,min=10000;
for(int i=0;i<trading_days;i++)
{
if(trading_day[i].get_highest()>max)
{
max=trading_day[i].get_highest();
highest_time=i+1;
}
if(trading_day[i].get_lowest()<min)
{
min=trading_day[i].get_lowest();
lowest_time=i+1;
}
}
highest=max;
lowest=min;
max_up_amp=(highest-opening)/opening;
max_down_amp=(opening-lowest)/opening;
if(max_up_amp>max_down_amp)
{
max_amp=max_up_amp;
up_or_down=true;
max_amp_time=get_highest_time();
}
else
{
max_amp=max_down_amp;
up_or_down=false;
max_amp_time=get_lowest_time();
}
}
int WEEK::get_number()const
{
return number;
}
int WEEK::get_trading_days()const
{
return trading_days;
}
float WEEK::get_opening()const
{
return opening;
}
float WEEK::get_opening(int trading_day)const
{
if(trading_day<=trading_days)
{
return this->trading_day[trading_day-1].get_opening();
}
else
{
return this->trading_day[trading_day-2].get_opening();
}
}
float WEEK::get_closing()const
{
return closing;
}
float WEEK::get_closing(int trading_day)const
{
if(trading_day<=trading_days)
{
return this->trading_day[trading_day-1].get_closing();
}
else
{
return this->trading_day[trading_day-2].get_closing();
}
}
float WEEK::get_highest()const
{
return highest;
}
float WEEK::get_highest(int trading_day1,int trading_day2)const
{
float max=0;
for(int i=trading_day1-1;i<=trading_day2-1;i++)
{
if(trading_day[i].get_highest()>max)
{
max=trading_day[i].get_highest();
}
}
return max;
}
float WEEK::get_lowest()const
{
return lowest;
}
float WEEK::get_lowest(int trading_day1,int trading_day2)const
{
float min=10000;
for(int i=trading_day1-1;i<=trading_day2-1;i++)
{
if(trading_day[i].get_lowest()<min)
{
min=trading_day[i].get_lowest();
}
}
return min;
}
float WEEK::get_max_amp()const
{
return max_amp;
}
float WEEK::get_max_amp(int trading_day1,int trading_day2)const
{
float up_amp=(get_highest(trading_day1,trading_day2)-get_opening(trading_day1))/get_opening(trading_day1);
float down_amp=(get_opening(trading_day1)-get_lowest(trading_day1,trading_day2))/get_opening(trading_day1);
float amp=max(up_amp,down_amp);
return amp;
}
bool WEEK::is_up_or_down()const
{
return up_or_down;
}
int WEEK::get_highest_time()const
{
return highest_time;
}
int WEEK::get_lowest_time()const
{
return lowest_time;
}
int WEEK::get_max_amp_time()const
{
return max_amp_time;
}
int WEEK::get_events()const
{
return events;
}
EVENT_NAME WEEK::get_event_name(int number)const
{
if(number<0||number>events-1)
{
return EVENT_NAME::None;
}
else
{
return event[number].get_name();
}
}
int WEEK::get_event_yyyymmdd(int number)const
{
if(number<0||number>events-1)
{
return 0;
}
else
{
return event[number].get_yyyymmdd();
}
}
void WEEK::show()const
{
cout<<"______________________"<<endl
<<"第"<<number<<"周:"<<endl
<<endl
<<"交易日个数:"<<trading_days<<endl
<<"开盘价:"<<opening<<endl
<<"收盘价:"<<closing<<endl
<<"最高价:"<<highest<<endl
<<"最低价:"<<lowest<<endl
<<"最大单边波幅时间:星期"<<max_amp_time<<endl
<<"最大单边波幅是涨是跌:";
if(up_or_down==true)
{
cout<<"涨"<<endl;
}
else
{
cout<<"跌"<<endl;
}
cout<<"最大单边波幅:"<<round(max_amp*10000)/100.0<<"%"<<endl
<<"最高价时间:星期"<<highest_time<<endl
<<"最大涨幅:"<<round(max_up_amp*10000)/100.0<<"%"<<endl
<<"最低价时间:星期"<<lowest_time<<endl
<<"最大跌幅:"<<round(max_down_amp*10000)/100.0<<"%"<<endl
<<"事件数:"<<events<<endl
<<"事件:";
if(events>0)
{
for(int i=0;i<events;i++)
{
switch(event[i].get_name())
{
case EVENT_NAME::FOMC:
cout<<"FOMC ";
break;
case EVENT_NAME::NFP:
cout<<"NFP ";
break;
case EVENT_NAME::UR:
cout<<"UR ";
break;
case EVENT_NAME::PCE:
cout<<"PCE ";
break;
case EVENT_NAME::CPI:
cout<<"CPI ";
break;
case EVENT_NAME::ADP:
cout<<"ADP ";
break;
case EVENT_NAME::GDP:
cout<<"GDP ";
break;
case EVENT_NAME::Powell:
cout<<"Powell ";
break;
case EVENT_NAME::ASML_report:
cout<<"ASML_report ";
break;
case EVENT_NAME::ECB:
cout<<"ECB ";
break;
}
}
}
else
{
cout<<"无 ";
}
cout<<endl;
for(int i=0;i<5;i++)
{
cout<<endl
<<"星期"<<weekday[i].date.get_weekday()<<":"<<endl;
if(weekday[i].get_opening()!=0)
{
cout<<"开盘价:"<<weekday[i].get_opening()<<endl
<<"收盘价:"<<weekday[i].get_closing()<<endl
<<"最高价:"<<weekday[i].get_highest()<<endl
<<"最低价:"<<weekday[i].get_lowest()<<endl
<<"最大单边波幅:"<<round(weekday[i].get_max_amp()*10000)/100.0<<"%"<<endl;
}
else
{
cout<<"休市"<<endl;
}
}
}
#endif
MONTH.h
#ifndef _MONTH_H_
#define _MONTH_H_
#include <iostream>
#include <math.h>
#include "DATE.h"
#include "EVENT.h"
#include "DAY.h"
using namespace std;
class MONTH
{
public:
DAY day[23];
void set(int number);
int get_days()const;
float get_opening()const;
float get_closing()const;
float get_highest()const;
float get_lowest()const;
float get_max_amp()const;
bool is_up_or_down()const;
int get_highest_time()const;
int get_lowest_time()const;
int get_max_amp_time()const;
void show()const;
private:
int days;
float opening;
float closing;
float highest;
float lowest;
float max_up_amp;
float max_down_amp;
float max_amp;
bool up_or_down;
int highest_time;
int lowest_time;
int max_amp_time;
EVENT event[30];
};
void MONTH::set(int number)
{
days=number;
opening=day[0].get_opening();
closing=day[days-1].get_closing();
float max=0,min=10000;
for(int i=0;i<days;i++)
{
if(day[i].get_highest()>max)
{
max=day[i].get_highest();
highest_time=i+1;
}
if(day[i].get_lowest()<min)
{
min=day[i].get_lowest();
lowest_time=i+1;
}
}
highest=max;
lowest=min;
max_up_amp=(highest-opening)/opening;
max_down_amp=(opening-lowest)/opening;
if(max_up_amp>max_down_amp)
{
max_amp=max_up_amp;
up_or_down=true;
max_amp_time=highest_time;
}
else
{
max_amp=max_down_amp;
up_or_down=false;
max_amp_time=lowest_time;
}
}
int MONTH::get_days()const
{
return days;
}
float MONTH::get_opening()const
{
return opening;
}
float MONTH::get_closing()const
{
return closing;
}
float MONTH::get_highest()const
{
return highest;
}
float MONTH::get_lowest()const
{
return lowest;
}
float MONTH::get_max_amp()const
{
return max_amp;
}
bool MONTH::is_up_or_down()const
{
return up_or_down;
}
int MONTH::get_highest_time()const
{
return highest_time;
}
int MONTH::get_lowest_time()const
{
return lowest_time;
}
int MONTH::get_max_amp_time()const
{
return max_amp_time;
}
void MONTH::show()const
{
cout<<endl<<"______________________"<<endl
<<day[0].date.get_year()<<"年"<<day[0].date.get_month()<<"月:"<<endl<<endl
<<"最大单边波幅:"<<round(max_amp*10000)/100.0<<"%"<<endl
<<"最大单边波幅时间:第"<<max_amp_time<<"个交易日"<<endl
<<"最大涨幅"<<round(max_up_amp*10000)/100.0<<"%"<<endl
<<"最高价时间:第"<<highest_time<<"个交易日"<<endl
<<"最大跌幅"<<round(max_down_amp*10000)/100.0<<"%"<<endl
<<"最低价时间:第"<<lowest_time<<"个交易日"<<endl;
for(int i=0;i<days;i++)
{
cout<<endl<<"日期:"<<day[i].date.get_month()<<"月"<<day[i].date.get_day()<<"日"<<endl
<<"开盘价:"<<day[i].get_opening()<<endl
<<"收盘价:"<<day[i].get_closing()<<endl
<<"最高价:"<<day[i].get_highest()<<endl
<<"最低价:"<<day[i].get_lowest()<<endl
<<"最大单边波幅:"<<round(day[i].get_max_amp()*10000)/100.0<<"%"<<endl;
}
}
#endif
EVENT.h
#ifndef _EVENT_H_
#define _EVENT_H_
#include "ENUM.h"
class EVENT
{
public:
EVENT()
{
name=EVENT_NAME::None;
yyyymmdd=0;
importance=0;
}
void set_name(EVENT_NAME name);
void set_yyyymmdd(int yyyymmdd);
bool exist()const;
EVENT_NAME get_name()const;
int get_yyyymmdd()const;
int get_importance()const;
private:
EVENT_NAME name;
int yyyymmdd;
int importance;
};
void EVENT::set_name(EVENT_NAME name)
{
this->name=name;
switch(name)
{
case EVENT_NAME::FOMC:
importance=5;
break;
case EVENT_NAME::NFP:
importance=5;
break;
case EVENT_NAME::UR:
importance=5;
break;
case EVENT_NAME::PCE:
importance=4;
break;
case EVENT_NAME::CPI:
importance=4;
break;
case EVENT_NAME::ADP:
importance=4;
break;
case EVENT_NAME::GDP:
importance=4;
break;
case EVENT_NAME::Powell:
importance=5;
break;
case EVENT_NAME::ASML_report:
importance=2;
break;
case EVENT_NAME::ECB:
importance=4;
break;
case EVENT_NAME::None:
importance=0;
break;
}
}
void EVENT::set_yyyymmdd(int yyyymmdd)
{
this->yyyymmdd=yyyymmdd;
}
bool EVENT::exist()const
{
if(importance==0)
return false;
else
return true;
}
EVENT_NAME EVENT::get_name()const
{
return name;
}
int EVENT::get_yyyymmdd()const
{
return yyyymmdd;
}
int EVENT::get_importance()const
{
return importance;
}
#endif
ENUM.h
#ifndef _ENUM_H_
#define _ENUM_H_
#include <iostream>
using namespace std;
enum class OBJECT_NAME
{
ESmain,//标普500主连
CLmain,//原油主连
_6Emain,//欧元主连
SPY,//标普500ETF
QQQ,//纳指100ETF
ASML,//阿斯麦
TSLA//特斯拉
};
enum class EVENT_NAME
{
FOMC,//美联储利率决议
NFP,//美国季调后非农就业人口(万人)
UR,//美国失业率(%)
PCE,//美国核心PCE物价指数年率(%)
CPI,//美国未季调CPI年率(%)
ADP,//美国ADP就业人数(万人)
GDP,//美国实际GDP年化率终值(%)
Powell,//鲍威尔讲话
ASML_report,//阿斯麦财报
ECB,//欧洲央行利率决议
None//无重大事件
};
ostream& operator<<(ostream& os,OBJECT_NAME object)
{
switch(object)
{
case OBJECT_NAME::ESmain:
os<<"ESmain";
break;
case OBJECT_NAME::CLmain:
os<<"CLmain";
break;
case OBJECT_NAME::SPY:
os<<"SPY";
break;
case OBJECT_NAME::QQQ:
os<<"QQQ";
break;
case OBJECT_NAME::ASML:
os<<"ASML";
break;
case OBJECT_NAME::TSLA:
os<<"TSLA";
break;
default:
os<<"Unknown object";
break;
}
return os;
}
ostream& operator<<(ostream& os,EVENT_NAME event)
{
switch(event)
{
case EVENT_NAME::FOMC:
os<<"FOMC";
break;
case EVENT_NAME::NFP:
os<<"NFP";
break;
case EVENT_NAME::UR:
os<<"UR";
break;
case EVENT_NAME::PCE:
os<<"PCE";
break;
case EVENT_NAME::CPI:
os<<"CPI";
break;
case EVENT_NAME::ADP:
os<<"ADP";
break;
case EVENT_NAME::GDP:
os<<"GDP";
break;
case EVENT_NAME::Powell:
os<<"Powell";
break;
case EVENT_NAME::ASML_report:
os<<"ASML_report";
break;
case EVENT_NAME::ECB:
os<<"ECB";
break;
case EVENT_NAME::None:
os<<"None";
break;
default:
os<<"Unknown event";
break;
}
return os;
}
#endif
SHOW.h
#ifndef _SHOW_H_
#define _SHOW_H_
#include <iostream>
#include <math.h>
#include "DAY.h"
#include "WEEK.h"
#include "MONTH.h"
using namespace std;
extern int days,weeks,months;
extern int FOMCs,NFPs,URs,PCEs,CPIs,ADPs,GDPs,Powells;
extern int ASML_reports;
extern int FOMC[];
extern int NFP[];
extern int UR[];
extern int PCE[];
extern int CPI[];
extern int ADP[];
extern int GDP[];
extern int Powell[];
extern int ASML_report[];
extern void SHOW_PROBABILITY(const float (*ampPtr),int length);
template <typename T>
void SHOW_BASIC_DATA(T (*tPtr),int length)
{
if(tPtr==nullptr)
{
return;
}
float amp[length];
for(int i=0;i<length;i++)
{
amp[i]=tPtr[i].get_max_amp();
tPtr[i].show();
}
SHOW_PROBABILITY(amp,length);
}
void SHOW_EVENT(DAY (*dayPtr),EVENT_NAME event)
{
cout<<event<<"日期"<<'\t'<<'\t'
<<"最大单边波幅"<<'\t'
<<"最大单边波幅是涨是跌"<<'\t'
<<"叠加事件"<<endl
<<endl;
int events=0,count=0;
bool flag=false;
float max_amp[days/10];
for(int i=0;i<days;i++)
{
events=dayPtr[i].get_events();
if(events>0)
{
for(int j=0;j<events;j++)
{
if(dayPtr[i].get_event_name(j)==event)
{
flag=true;
max_amp[count]=dayPtr[i].get_max_amp();
count++;
cout<<dayPtr[i].date.get_yyyymmdd()<<'\t'
<<round(dayPtr[i].get_max_amp()*10000)/100.0<<"%"<<'\t'<<'\t';
if(dayPtr[i].is_up_or_down()==true)
{
cout<<"↑"<<'\t'<<'\t'<<'\t';
}
else
{
cout<<"↓"<<'\t'<<'\t'<<'\t';
}
break;
}
}
if(flag==true)
{
if(events==1)
{
flag=false;
}
else
{
flag=false;
for(int j=0;j<events;j++)
{
if(dayPtr[i].get_event_name(j)==event)
{
continue;
}
cout<<dayPtr[i].get_event_name(j)<<" ";
}
}
cout<<endl;
}
}
}
SHOW_PROBABILITY(max_amp,count);
cout<<endl<<"样本数量:"<<count;
}
void SHOW_EVENT(WEEK (*weekPtr),EVENT_NAME event,int trading_day1=1,int trading_day2=5)
{
cout<<" "<<'\t'<<'\t'<<" 最大"<<'\t'<<'\t'<<" 最大"<<'\t'<<"最高价"<<'\t'<<"最低价"<<'\t'<<" "<<'\t'<<"第"<<trading_day1<<"-"<<trading_day2<<"个交易日"<<endl
<<event<<"日期"<<'\t'<<'\t'<<"单边波幅"<<'\t'<<"单边波幅时间"<<'\t'<<" 时间"<<'\t'<<" 时间"<<'\t'<<"涨/跌"<<'\t'<<"最大单边波幅"<<'\t'<<"叠加事件"<<endl
<<endl;
int events=0,count=0;
bool flag=false;
float max_amp[weeks/2];
float range_max_amp[weeks/2];
for(int i=0;i<weeks;i++)
{
events=weekPtr[i].get_events();
if(events>0)
{
for(int j=0;j<events;j++)
{
if(weekPtr[i].get_event_name(j)==event)
{
flag=true;
max_amp[count]=weekPtr[i].get_max_amp();
range_max_amp[count]=weekPtr[i].get_max_amp(trading_day1,trading_day2);
count++;
cout<<weekPtr[i].get_event_yyyymmdd(j)<<'\t'
<<" "<<round(weekPtr[i].get_max_amp()*10000)/100.0<<"%"<<'\t'<<'\t'
<<" 周"<<weekPtr[i].get_max_amp_time()<<'\t'<<'\t'
<<" 周"<<weekPtr[i].get_highest_time()<<'\t'
<<" 周"<<weekPtr[i].get_lowest_time()<<'\t';
if(weekPtr[i].is_up_or_down()==true)
{
cout<<" ↑"<<'\t';
}
else
{
cout<<" ↓"<<'\t';
}
cout<<" "<<round(weekPtr[i].get_max_amp(trading_day1,trading_day2)*10000)/100<<"%"<<'\t'<<'\t';
break;
}
}
if(flag==true)
{
if(events==1)
{
flag=false;
}
else
{
flag=false;
for(int j=0;j<events;j++)
{
if(weekPtr[i].get_event_name(j)==event)
{
continue;
}
cout<<weekPtr[i].get_event_name(j)<<" ";
}
}
cout<<endl;
}
}
}
SHOW_PROBABILITY(max_amp,count);
SHOW_PROBABILITY(range_max_amp,count);
cout<<endl<<"样本数量:"<<count;
}
void SHOW_PROBABILITY(const float (*ampPtr),int length)
{
int amp[length];
int max_amp=0;
for(int i=0;i<length;i++)
{
amp[i]=round(ampPtr[i]*10000);
if(amp[i]>max_amp)
{
max_amp=amp[i];
}
}
int sample_size=max_amp+2;
int X[sample_size];
int PX[sample_size];
int count=0;
for(int i=0;i<sample_size;i++)
{
X[i]=i;
for(int j=0;j<length;j++)
{
if(amp[j]>=X[i])
{
count++;
}
}
PX[i]=round(float(count)/float(length)*10000);
count=0;
}
cout<<endl
<<"止盈振幅-盈利概率表:"<<endl<<endl
<<"止盈振幅"<<'\t'<<"盈利概率"<<endl;
for(int i=0;i<sample_size;i++)
{
if(PX[i+1]==PX[i])
{
continue;
}
cout<<float(X[i])/100<<"%"<<'\t'<<'\t'<<float(PX[i])/100<<"%"<<endl;
}
}
#endif
READ_FILE.h
#ifndef _READ_FILE_H_
#define _READ_FILE_H_
#include <iostream>
#include <fstream>
#include <stdexcept>
using namespace std;
template <typename T>
int READ_FILE(T data[],string file_name)
{
try
{
ifstream file;
file.open(file_name);
if(!file)
{
throw runtime_error(file_name+"文件打开失败!");
}
int datalen=0;
while(!file.eof())
{
file>>data[datalen++];
}
file.close();
return datalen;
}
catch (const exception& e)
{
cerr<<"Error: "<<e.what()<<endl;
}
}
#endif