OnStart

Start事件发生时在脚本和服务中调用这个函数。这个函数旨在一次性执行程序中实施的操作。有两种函数类型。

返回结果的版本

int  OnStart(void);

返回值

int类型的值显示在“日志”选项卡中。

脚本执行完成之后,在程序端日志中创建了“脚本script_name已移除(结果代码N)”条目。在这里,N是OnStart()函数的返回值。

服务执行完成之后,在程序端日志中创建了“服务service_name已停止(结果代码N)” 条目。在这里,N是OnStart()函数的返回值。

建议使用返回执行结果的OnStart()调用,因为它不仅允许脚本或服务执行,还可以返回错误代码或其他有用的数据来分析程序执行结果。

注意

OnStart()是处理脚本和服务事件的唯一函数。不会有其他事件被发送至这些程序。并且,Start事件不被传递到EA和自定义指标。

没有结果返回的版本只为与旧代码兼容而保留。不建议使用

void  OnStart(void);

注意

OnStart()是处理脚本和服务事件的唯一函数。不会有其他事件被发送至这些程序。并且,Start事件不被传递到EA和自定义指标。

脚本样例:

//--- 用于处理颜色的宏
#define XRGB(r,g,b)    (0xFF000000|(uchar(r)<<16)|(uchar(g)<<8)|uchar(b))
#define GETRGB(clr)    ((clr)&0xFFFFFF)
//+------------------------------------------------------------------+
//| 脚本程序起始函数                                                   |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- 设置一个下行的蜡烛图颜色
   Comment("Set a downward candle color"); 
   ChartSetInteger(0,CHART_COLOR_CANDLE_BEAR,GetRandomColor());
   ChartRedraw(); // 无需等候新报价,立即更新图表
   Sleep(1000);   // 暂停1秒,查看所有的变化
//--- 设置一个上行的蜡烛图颜色
   Comment("Set an upward candle color"); 
   ChartSetInteger(0,CHART_COLOR_CANDLE_BULL,GetRandomColor());
   ChartRedraw(); 
   Sleep(1000);   
//--- 设置背景颜色
   Comment("Set the background color"); 
   ChartSetInteger(0,CHART_COLOR_BACKGROUND,GetRandomColor());
   ChartRedraw(); 
   Sleep(1000);   
//--- 设置买价线的颜色
   Comment("Set color of Ask line"); 
   ChartSetInteger(0,CHART_COLOR_ASK,GetRandomColor());
   ChartRedraw(); 
   Sleep(1000);   
//--- 设置卖价线的颜色
   Comment("Set color of Bid line"); 
   ChartSetInteger(0,CHART_COLOR_BID,GetRandomColor());
   ChartRedraw(); 
   Sleep(1000);    
//--- 设置下行柱形图和下行蜡烛图框架的颜色
   Comment("Set color of a downward bar and a downward candle frame"); 
   ChartSetInteger(0,CHART_COLOR_CHART_DOWN,GetRandomColor());
   ChartRedraw(); 
   Sleep(1000);   
//--- 设置图表线和Doji蜡烛图的颜色
   Comment("Set color of a chart line and Doji candlesticks"); 
   ChartSetInteger(0,CHART_COLOR_CHART_LINE,GetRandomColor());
   ChartRedraw(); 
   Sleep(1000);   
//--- 设置上行柱形图和上行蜡烛图框架的颜色  
   Comment("Set color of an upward bar and an upward candle frame"); 
   ChartSetInteger(0,CHART_COLOR_CHART_UP,GetRandomColor());
   ChartRedraw(); 
   Sleep(1000);   
//--- 设置坐标轴、比例和OHLC线的颜色
   Comment("Set color of axes, scale and OHLC line"); 
   ChartSetInteger(0,CHART_COLOR_FOREGROUND,GetRandomColor());
   ChartRedraw(); 
   Sleep(1000);   
//--- 设置一个网格颜色
   Comment("Set a grid color"); 
   ChartSetInteger(0,CHART_COLOR_GRID,GetRandomColor());
   ChartRedraw(); 
   Sleep(1000);   
//--- 设置最后价格线
   Comment("Set Last price color"); 
   ChartSetInteger(0,CHART_COLOR_LAST,GetRandomColor());
   ChartRedraw(); 
   Sleep(1000);   
//--- 设置止损和获利水平的颜色
   Comment("Set color of Stop Loss and Take Profit order levels"); 
   ChartSetInteger(0,CHART_COLOR_STOP_LEVEL,GetRandomColor());
   ChartRedraw(); 
   Sleep(1000);   
//--- 设置交易量的颜色和市场进入水平
   Comment("Set color of volumes and market entry levels"); 
   ChartSetInteger(0,CHART_COLOR_VOLUME,GetRandomColor());
   ChartRedraw();
  }
//+------------------------------------------------------------------+
//| 返回一个随机生成的颜色                                              |
//+------------------------------------------------------------------+
color GetRandomColor()
  {
   color clr=(color)GETRGB(XRGB(rand()%255,rand()%255,rand()%255));
   return clr;
  }

另见

事件处理函数程序运行客户端事件