- // EA初始化、切换品种的时候运行一次
- int OnInit() {
-
- // 设置定时器, 每 2 秒执行一次OnTimer()
- // EventSetTimer(5);
-
- // 下面两句代码会开启对象的创建和删除的交互功能
- //--- enable object create events
- ChartSetInteger(ChartID(),CHART_EVENT_OBJECT_CREATE,true);
- //--- enable object delete events
- ChartSetInteger(ChartID(),CHART_EVENT_OBJECT_DELETE,true);
- Print("OnInit");
- return INIT_SUCCEEDED;
- }
- void OnTimer() {
-
- Print(TimeLocal());
-
- return;
- }
-
-
-
-
-
-
-
- #define KEY_LEFT 37
- #define KEY_UP 38
- #define KEY_RIGHT 39
- #define KEY_DOWN 40
- void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam) {
-
- // 判断当前事件的类型
- if(id == CHARTEVENT_KEYDOWN) {
- // click left button of keyboard
- if(lparam == KEY_LEFT) {
- changeToLeftPeriod();
- } else if(lparam == KEY_RIGHT) {
- changeToRightPeriod();
- }
- }
-
- if(id == CHARTEVENT_CLICK) {
- Print("The coordinates of the mouse click on the chart are: x = ", lparam, " y = ", dparam);
- }
-
- if(id == CHARTEVENT_OBJECT_CLICK) {
- Print("The mouse has been clicked on the object with name '", sparam,"'");
- }
-
- if(id == CHARTEVENT_OBJECT_DELETE) {
- Print("the object with name '", sparam, "' has been deleted");
- }
-
- if(id == CHARTEVENT_OBJECT_CREATE) {
- Print("the object with name '", sparam, "' has been created");
- }
-
- if(id == CHARTEVENT_OBJECT_DRAG) {
- Print("the object with name '", sparam, "' has been draged");
- }
-
- if(id == CHARTEVENT_OBJECT_ENDEDIT) {
- Print("the object with name '", sparam, "' has been edited");
- }
- }
-
-
- void changeToLeftPeriod() {
-
- ENUM_TIMEFRAMES nowPeriod = PERIOD_CURRENT;
-
- // set period at now
- switch(Period()) {
- case PERIOD_M1: {
- nowPeriod = PERIOD_MN1;
- break;
- }
- case PERIOD_M5: {
- nowPeriod = PERIOD_M1;
- break;
- }
- case PERIOD_M15: {
- nowPeriod = PERIOD_M5;
- break;
- }
- case PERIOD_M30: {
- nowPeriod = PERIOD_M15;
- break;
- }
- case PERIOD_H1: {
- nowPeriod = PERIOD_M30;
- break;
- }
- case PERIOD_H4: {
- nowPeriod = PERIOD_H1;
- break;
- }
- case PERIOD_D1: {
- nowPeriod = PERIOD_H4;
- break;
- }
- case PERIOD_W1: {
- nowPeriod = PERIOD_D1;
- break;
- }
- case PERIOD_MN1: {
- nowPeriod = PERIOD_W1;
- break;
- }
- default: break;
- }
-
- // change period
- if(nowPeriod != PERIOD_CURRENT) {
- ChartSetSymbolPeriod(0, Symbol(), nowPeriod);
- }
- }
-
-
- void changeToRightPeriod() {
-
- ENUM_TIMEFRAMES nowPeriod = PERIOD_CURRENT;
-
- // set period at now
- switch(Period()) {
- case PERIOD_M1: {
- nowPeriod = PERIOD_M5;
- break;
- }
- case PERIOD_M5: {
- nowPeriod = PERIOD_M15;
- break;
- }
- case PERIOD_M15: {
- nowPeriod = PERIOD_M30;
- break;
- }
- case PERIOD_M30: {
- nowPeriod = PERIOD_H1;
- break;
- }
- case PERIOD_H1: {
- nowPeriod = PERIOD_H4;
- break;
- }
- case PERIOD_H4: {
- nowPeriod = PERIOD_D1;
- break;
- }
- case PERIOD_D1: {
- nowPeriod = PERIOD_W1;
- break;
- }
- case PERIOD_W1: {
- nowPeriod = PERIOD_MN1;
- break;
- }
- case PERIOD_MN1: {
- nowPeriod = PERIOD_M1;
- break;
- }
- default: break;
- }
-
- // change period
- if(nowPeriod != PERIOD_CURRENT) {
- ChartSetSymbolPeriod(0, Symbol(), nowPeriod);
- }
- }
-
-
-
-
-
- // 盘面改变品种、切换时间周期、修改EA参数、关闭品种的盘面的时候会触发这个函数
- void OnDeinit(const int reason) {
-
- //--- The first way to get the uninitialization reason code
- //Print(__FUNCTION__,"_Uninitalization reason code = ",reason);
- //--- The second way to get the uninitialization reason code
- Print(__FUNCTION__, "_UninitReason = ", getUninitReasonText(_UninitReason));
-
- return;
- }
-
- // get reason text
- string getUninitReasonText(int reasonCode) {
-
- string text="";
-
- switch(reasonCode) {
- case REASON_ACCOUNT:{
- text="Account was changed";
- break;
- }
- case REASON_CHARTCHANGE: {
- text="Symbol or timeframe was changed";
- break;
- }
- case REASON_CHARTCLOSE:{
- text="Chart was closed";
- break;
- }
- case REASON_PARAMETERS:{
- text="Input-parameter was changed";
- break;
- }
- case REASON_RECOMPILE:{
- text="Program "+__FILE__+" was recompiled";
- break;
- }
- case REASON_REMOVE:{
- text="Program "+__FILE__+" was removed from chart";
- break;
- }
- case REASON_TEMPLATE:{
- text="New template was applied to chart";
- break;
- }
- default:{
- text="Another reason";
- }
- }
-
- return text;
- }
复制代码
|