fxmaster 发表于 2016-11-7 15:44:11

点击创建的按钮获取ask值,经过规定时间t重新获取ask值。...

本帖最后由 fxmaster 于 2016-11-7 16:04 编辑

点击创建的按钮获取ask值m,经过规定时间t重新获取ask值n。要求:点击一次按钮,t只执行一次。如何通过源码实现,谢谢?

tempasdf 发表于 2016-11-7 17:14:02

ontimer 里完成呀

fxmaster 发表于 2016-11-7 17:58:25

本帖最后由 fxmaster 于 2016-11-7 18:03 编辑

tempasdf 发表于 2016-11-7 17:14
ontimer 里完成呀
可以在Start里面执行吗,Ontime好像是单独的。
我查过ontimereventsettimer,但是这个好像是循环时间执行,我要实现的是点击一次执行一次。

但其实最关键原因是因为源码不会写,连测试都测不了......

@tempasdf   论坛铁杆,看你每天都会回复新手问题,给你点赞。我看好多老的帖子,经常看到你

void OnInit()
{
   bool result = EventSetTimer(1);
   Print("OnInit EventSetTimer result:", result);
}


void OnTimer()
{
   Print("OnTimer hit");
}

这个找的一个比较看的懂的源码,但是不会改造成我想要的。。。。



tempasdf 发表于 2016-11-7 18:28:17

这没有关系定个变量

点按钮 取ask值
       变量tt=1
ontimer按1秒更新
   
ontimer里 如果tt=1变量ss 就加1 因为是1秒级别 每秒累加1加到ss=10 取ask值tt=2




tempasdf 发表于 2016-11-7 19:05:15

有空有闲 就凑了个代码 测试还可以 函数 扒的帮助

//+------------------------------------------------------------------+
//|                                                            S.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window

//--- Input parameters
int TT=3;
int SS=0;

extern int 时间间隔=5;
//---=======================================================================
string ButUpName;

string         InpName="Button";            // Button name
ENUM_BASE_CORNER InpCorner=CORNER_LEFT_UPPER; // Chart corner for anchoring
string         InpFont="Arial";             // Font
int            InpFontSize=14;            // Font size
color            InpColor=clrBlack;         // Text color
color            InpBackColor=C'236,233,216'; // Background color
color            InpBorderColor=clrNONE;      // Border color
bool             InpState=false;            // Pressed/Released
bool             InpBack=false;               // Background object
bool             InpSelection=false;          // Highlight to move
bool             InpHidden=true;            // Hidden in the object list
long             InpZOrder=0;               // Priority for mouse click
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   EventSetTimer(1);
//---=======================================================================
   ButUpName="butUp_";
   ButtonDelete(0,ButUpName);
   ButtonCreate(0,ButUpName,0,120,125,125,40,InpCorner,"取值",InpFont,InpFontSize, InpColor,InpBackColor,InpBorderColor,InpState,InpBack,InpSelection,InpHidden,InpZOrder);


//---
   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//--- indicator buffers mapping

   ButtonDelete(0,ButUpName);
   ObjectDelete("ask_val1");
   ObjectDelete("ask_val2");
//---

}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{

   


//--- last counted bar will be recounted

//--- return value of prev_calculated for next call
   return(rates_total);
}
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
{
//---
   if(TT==1)
   {
      SS=SS+1;
   }
   if(SS==时间间隔)
   {
   TT=0;
   SS=0;
   DrawLabel("ask_val2", 0, 0, 10, 65, "秒数取值:"+DoubleToString(MarketInfo(NULL,MODE_ASK),Digits()), White, 15, "宋体", true);      
   }
   
}
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
{
//---
   static int hwnd = 0;
   hwnd = WindowHandle(Symbol(), Period());
      
   if(sparam==ButUpName) // Close button has been pressed
   {
       ObjectDelete("ask_val1");
       ObjectDelete("ask_val2");
       ObjectSetInteger(0,ButUpName,OBJPROP_STATE,FALSE);         
       WindowRedraw();         
       DrawLabel("ask_val1", 0, 0, 10, 35, "点击取值:"+DoubleToString(MarketInfo(NULL,MODE_ASK),Digits()), White, 15, "宋体", true);      
       TT=1;
   }
}
//+------------------------------------------------------------------+



   
void DrawLabel(string 标签名称, int 窗口序号, int 角落位置, int 横坐标, int 竖坐标, string 文字内容, color 文字颜色, int 文字大小,string 字体名称, bool 显示于背景) {
   ObjectDelete(标签名称);
   ObjectCreate(标签名称, OBJ_LABEL, 窗口序号, 0, 0);
   ObjectSetText(标签名称, 文字内容, 文字大小, 字体名称, 文字颜色);
   ObjectSet(标签名称, OBJPROP_CORNER, 角落位置);
   ObjectSet(标签名称, OBJPROP_XDISTANCE, 横坐标);
   ObjectSet(标签名称, OBJPROP_YDISTANCE, 竖坐标);
   ObjectSet(标签名称, OBJPROP_BACK, 显示于背景);
}
         

//+------------------------------------------------------------------+
//| Create the button                                                |
//+------------------------------------------------------------------+
bool ButtonCreate(const long            chart_ID=0,               // chart's ID
                  const string            name="Button",            // button name
                  const int               sub_window=0,             // subwindow index
                  const int               x=0,                      // X coordinate
                  const int               y=0,                      // Y coordinate
                  const int               width=50,               // button width
                  const int               height=18,                // button height
                  const ENUM_BASE_CORNERcorner=CORNER_LEFT_UPPER, // chart corner for anchoring
                  const string            text="Button",            // text
                  const string            font="Arial",             // font
                  const int               font_size=10,             // font size
                  const color             clr=clrBlack,             // text color
                  const color             back_clr=C'236,233,216',// background color
                  const color             border_clr=clrNONE,       // border color
                  const bool            state=false,            // pressed/released
                  const bool            back=false,               // in the background
                  const bool            selection=false,          // highlight to move
                  const bool            hidden=true,            // hidden in the object list
                  const long            z_order=0)                // priority for mouse click
{
//--- reset the error value
   ResetLastError();
//--- create the button
   if(!ObjectCreate(chart_ID,name,OBJ_BUTTON,sub_window,0,0))
   {
      Print(__FUNCTION__,
            ": failed to create the button! Error code = ",GetLastError());
      return(false);
   }
//--- set button coordinates
   ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x);
   ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y);
//--- set button size
   ObjectSetInteger(chart_ID,name,OBJPROP_XSIZE,width);
   ObjectSetInteger(chart_ID,name,OBJPROP_YSIZE,height);
//--- set the chart's corner, relative to which point coordinates are defined
   ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);
//--- set the text
   ObjectSetString(chart_ID,name,OBJPROP_TEXT,text);
//--- set text font
   ObjectSetString(chart_ID,name,OBJPROP_FONT,font);
//--- set font size
   ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);
//--- set text color
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
//--- set background color
   ObjectSetInteger(chart_ID,name,OBJPROP_BGCOLOR,back_clr);
//--- set border color
   ObjectSetInteger(chart_ID,name,OBJPROP_BORDER_COLOR,border_clr);
//--- display in the foreground (false) or background (true)
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
//--- set button state
   ObjectSetInteger(chart_ID,name,OBJPROP_STATE,state);
//--- enable (true) or disable (false) the mode of moving the button by mouse
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
//--- hide (true) or display (false) graphical object name in the object list
   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
//--- set the priority for receiving the event of a mouse click in the chart
   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
//--- successful execution
   return(true);
}
//+------------------------------------------------------------------+
//| Move the button                                                |
//+------------------------------------------------------------------+
bool ButtonMove(const long   chart_ID=0,    // chart's ID
                const string name="Button", // button name
                const int    x=0,         // X coordinate
                const int    y=0)         // Y coordinate
{
//--- reset the error value
   ResetLastError();
//--- move the button
   if(!ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,x))
   {
      Print(__FUNCTION__,
            ": failed to move X coordinate of the button! Error code = ",GetLastError());
      return(false);
   }
   if(!ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,y))
   {
      Print(__FUNCTION__,
            ": failed to move Y coordinate of the button! Error code = ",GetLastError());
      return(false);
   }
//--- successful execution
   return(true);
}
//+------------------------------------------------------------------+
//| Change button size                                             |
//+------------------------------------------------------------------+
bool ButtonChangeSize(const long   chart_ID=0,    // chart's ID
                      const string name="Button", // button name
                      const int    width=50,      // button width
                      const int    height=18)   // button height
{
//--- reset the error value
   ResetLastError();
//--- change the button size
   if(!ObjectSetInteger(chart_ID,name,OBJPROP_XSIZE,width))
   {
      Print(__FUNCTION__,
            ": failed to change the button width! Error code = ",GetLastError());
      return(false);
   }
   if(!ObjectSetInteger(chart_ID,name,OBJPROP_YSIZE,height))
   {
      Print(__FUNCTION__,
            ": failed to change the button height! Error code = ",GetLastError());
      return(false);
   }
//--- successful execution
   return(true);
}
//+------------------------------------------------------------------+
//| Change corner of the chart for binding the button                |
//+------------------------------------------------------------------+
bool ButtonChangeCorner(const long             chart_ID=0,               // chart's ID
                        const string         name="Button",            // button name
                        const ENUM_BASE_CORNER corner=CORNER_LEFT_UPPER) // chart corner for anchoring
{
//--- reset the error value
   ResetLastError();
//--- change anchor corner
   if(!ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner))
   {
      Print(__FUNCTION__,
            ": failed to change the anchor corner! Error code = ",GetLastError());
      return(false);
   }
//--- successful execution
   return(true);
}
//+------------------------------------------------------------------+
//| Change button text                                             |
//+------------------------------------------------------------------+
bool ButtonTextChange(const long   chart_ID=0,    // chart's ID
                      const string name="Button", // button name
                      const string text="Text")   // text
{
//--- reset the error value
   ResetLastError();
//--- change object text
   if(!ObjectSetString(chart_ID,name,OBJPROP_TEXT,text))
   {
      Print(__FUNCTION__,
            ": failed to change the text! Error code = ",GetLastError());
      return(false);
   }
//--- successful execution
   return(true);
}
//+------------------------------------------------------------------+
//| Delete the button                                                |
//+------------------------------------------------------------------+
bool ButtonDelete(const long   chart_ID=0,    // chart's ID
                  const string name="Button") // button name
{
//--- reset the error value
   ResetLastError();
//--- delete the button
   if(!ObjectDelete(chart_ID,name))
   {
      Print(__FUNCTION__,
            ": failed to delete the button! Error code = ",GetLastError());
      return(false);
   }
//--- successful execution
   return(true);
}

fxmaster 发表于 2016-11-7 23:33:35

本帖最后由 fxmaster 于 2016-11-7 23:38 编辑

tempasdf 发表于 2016-11-7 19:05
有空有闲 就凑了个代码 测试还可以 函数 扒的帮助

//+--------------------------- ...
//|                                                 test5_script.mq4 |
//|                     Copyright ?2011, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright ?2011, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
int m,n;

int start()
{
   Print("Thank you");
}
//+------------------------------------------------------------------+



太强了。太佩服了。印象在论坛发帖你一直在帮我,真的非常感谢。

代码测试可行,大部分可以读懂,一些细节还要学习。



zhou138 发表于 2016-11-12 13:02:59

学习了~~~
页: [1]
查看完整版本: 【已解决】点击创建的按钮获取ask值,经过规定时间t重新获取ask值。...