第十二節 子函數及下單面板開發
下面我們來試一下做一個簡單的面板EA,這個面板應該具有可手動調節的開倉、平倉功能,有掛單的功能以及刪除掛單的功能。由於創建面板需要反复創建按鈕、文本框、這一類的對象,我們就想是不是能把創建這某類對象的功能集成到一個模塊裡面,我們只要輸入參數就可以完成創建呢?那麼為了實現模塊化的功能,下面我們來介紹一下編寫模塊用的子函數。
子函數
什麼是子函數呢?我們知道我們可以調用函數來幫我們做一些事,比如說創建對象的函數可以完成創建對象的工作,那麼子函數其實也可以這麼定義,就是能夠通過調用幫我們完成特定目的的一類函數。子函數不會自己運行,必須調用才能運行。任何函數都可以調用子函數,子函數之間也可以相互調用。
那麼子函數怎麼用代碼來表達呢?下面用腳本做一個簡單的示例:
void OnStart() { int a=1; int b=1; int c=add(a,b); Print("C="+(string)c); } int add(int a,int b) { int c=a+b; return(c); }
我們想用子函數來計算a+b的結果並打印它,於是我們寫了一個名字叫add的子函數,add()函數里面的參數有兩個,分別是整數類型的a以及整數類型的b ,注意這裡的參數和主函數里面的參數是不一樣的,在函數里面的聲明的變量為主函數里面的局部變量,只在該函數範圍內有效,比如說主函數里面的a和b就只能在主函數里面發揮效果,而子函數里面的a和b也只能在子函數里面發揮效果,所以子函數里面的變量是和主函數沒有關係的。需要注意的是在主函數外部聲明過的全局變量名稱不可作為函數的局部變量名稱。
當子函數被調用時,我們就把主函數里面的a的值賦給了子函數里面的a,然後把主函數里面的b的值賦給了子函數里面的b,在子函數內聲明了局部變量c,把a+b的值賦給了c,然後返回了c的值。注意我們的子函數的名稱前有一個int,這個意思是我們的子函數有一個整數型的返回值,所謂返回值,就是子函數的計算結果,有一些子函數不需要返回值,那麼它們的前面就是void,如果返回的是小數,那就可以用double,如果是字符串就可以用string。
有了子函數,我們就可以輕易地將新建按鈕和新建文本框的代碼模塊化了。我們把上節課按鈕和文本框的代碼拿過來,塞到子函數里面,就形成了我們的按鈕創建模塊,以及文本框創建模塊,代碼如下:
void OnStart() { button("button",100,50,clrBlue,clrWhite,clrBlue,15,CORNER_RIGHT_UPPER,110,10, "Buy"); edittext("TEXT",100,50,clrGray,clrWhite,clrGray,15,CORNER_RIGHT_UPPER,210,10, ALIGN_CENTER,false,"0.01"); } void button(string name,int width,int height,int clr_border,int clr_text,int clr_bg, int fontsize,int corner,int xd,int yd,string text) { ObjectCreate(0,name,OBJ_BUTTON,0,0,0); ObjectSetInteger(0,name,OBJPROP_BORDER_COLOR, clr_border); ObjectSetInteger(0,name,OBJPROP_XSIZE, width); ObjectSetInteger(0,name,OBJPROP_YSIZE, height); ObjectSetInteger(0,name,OBJPROP_COLOR, clr_text); ObjectSetInteger(0,name,OBJPROP_BGCOLOR, clr_bg); ObjectSetInteger(0,name,OBJPROP_FONTSIZE, fontsize); ObjectSetInteger(0,name,OBJPROP_CORNER, corner); ObjectSetInteger(0,name,OBJPROP_XDISTANCE, xd); ObjectSetInteger(0,name,OBJPROP_YDISTANCE, yd); ObjectSetString(0,name,OBJPROP_TEXT, text); } void edittext(string name, int width, int height, int clr_border, int clr_text, int clr_bg, int fontsize, int corner, int xd, int yd, int align, int readonly, string text) { ObjectCreate(0, name,OBJ_EDIT,0,0,0); ObjectSetInteger(0, name,OBJPROP_XSIZE,width); ObjectSetInteger(0,name,OBJPROP_YSIZE,height); ObjectSetInteger(0,name,OBJPROP_BORDER_COLOR, clr_border); ObjectSetInteger(0,name,OBJPROP_COLOR, clr_text); ObjectSetInteger(0,name,OBJPROP_BGCOLOR, clr_bg); ObjectSetInteger(0,name,OBJPROP_FONTSIZE, fontsize); ObjectSetInteger(0,name,OBJPROP_CORNER, corner); ObjectSetInteger(0,name,OBJPROP_XDISTANCE, xd); ObjectSetInteger(0,name,OBJPROP_YDISTANCE, yd); ObjectSetInteger(0,name,OBJPROP_ALIGN, align); ObjectSetInteger(0,name,OBJPROP_READONLY, readonly); ObjectSetString(0,name,OBJPROP_TEXT, text); }
這樣的話我們就可以不用每一個按鈕和文本框都去設置一遍參數了,直接調用button和edittext子函數,把參數輸進去就可以很方便地創建對象,而且減少了重複的代碼,當建立多個對象時整個程序的可讀性就好多了。
下單面板創建
有了子函數我們就可以嘗試著去做一個下單的面板了,這對手動交易會比較方便。一般來說我們的面板會做成指標的形式,因為一個圖表內EA只能加載一個,而指標可以加載多個。在此由於還沒講指標,所以我們暫時用EA來做一個演示,等到日後我們接觸到了指標就可以把麵板做成指標的形式了。
我們把按鈕和文本框的模塊拿過去,然後再做一個創建矩形面闆對象的模塊,方便我們畫矩形面板,這個子函數的代碼如下:
void lable(string name, int width, int height, int clr_border, int clr_bg, int corner, int xd, int yd, bool back) { ObjectCreate(0,name,OBJ_RECTANGLE_LABEL,0,0,0); ObjectSetInteger(0,name,OBJPROP_XSIZE,width); ObjectSetInteger(0,name,OBJPROP_YSIZE,height); ObjectSetInteger(0,name,OBJPROP_BORDER_COLOR,clr_border); ObjectSetInteger(0,name,OBJPROP_BGCOLOR,clr_bg); ObjectSetInteger(0,name,OBJPROP_CORNER,corner); ObjectSetInteger(0,name,OBJPROP_XDISTANCE,xd); ObjectSetInteger(0,name,OBJPROP_YDISTANCE,yd); ObjectSetInteger(0,name,OBJPROP_BACK,back); }
有了創建以上三種對象的子函數,我們就可以輕易地創建對象了。首先我們創建一個面板:
int OnInit() { ObjectDelete(0, "lable"); lable("lable",180,280,clrDarkViolet,clrLightSlateGray,CORNER_RIGHT_UPPER, 190,10,false); return(INIT_SUCCEEDED); } …………
以上程序中省略的是三個子函數,之所以要在創建之前先用ObjectDelete()函數把對象刪除一遍是因為怕對象的名稱衝突造成不可預知的問題。我們把lable()子函數的參數一個一個輸進去就可以把麵板給創建出來了,運行結果如下圖:
可以看到我們創建出來了一個銀灰色的面板,它離圖表的頂部邊界距離為10,離右邊界的距離也為10,寬度180,高度280。
按鈕和文本的創建也十分簡單了,參照面板的創建過程即可,大家可以自己練習一下,在此不多講,完整的代碼如下:
int OnInit() { ObjectDelete(0,"lable"); ObjectDelete(0,"button"); ObjectDelete(0,"TEXT"); ObjectDelete(0,"button1"); ObjectDelete(0,"TEXT1"); ObjectDelete(0,"button2"); ObjectDelete(0,"TEXT2"); ObjectDelete(0,"button3"); ObjectDelete(0,"TEXT3"); ObjectDelete(0,"button4"); ObjectDelete(0,"button5"); ObjectDelete(0,"button6"); ObjectDelete(0,"TEXT4"); ObjectDelete(0,"button7"); ObjectDelete(0,"button8"); ObjectDelete(0,"button9"); ObjectDelete(0,"button10"); ObjectDelete(0,"button11"); ObjectDelete(0,"button12"); lable("lable",180,280,clrDarkViolet,clrLightSlateGray,CORNER_RIGHT_UPPER,190, 10,false); button("button1",50,25,clrNONE,clrBlack,clrDeepPink,10,CORNER_RIGHT_UPPER,175,20,"Lot",true); edittext("TEXT1",100,25,clrBlack,clrBlack,clrWhite,10,CORNER_RIGHT_UPPER,125,20,ALIGN_CENTER,false,"0.01"); button("button2",50,25,clrNONE,clrBlack,clrDeepPink,10,CORNER_RIGHT_UPPER,175, 50,"SL",true); edittext("TEXT2",100,25,clrBlack,clrBlack,clrWhite,10,CORNER_RIGHT_UPPER,125, 50,ALIGN_CENTER,false,"100"); button("button3",50,25,clrNONE,clrBlack,clrDeepPink,10,CORNER_RIGHT_UPPER,175, 80,"TP",true); edittext("TEXT3",100,25,clrBlack,clrBlack,clrWhite,10,CORNER_RIGHT_UPPER,125,80,ALIGN_CENTER,false,"100"); button("button4",70,30,clrNONE,clrWhite,clrRed,10,CORNER_RIGHT_UPPER,175, 110,"BUY", false); button("button5",70,30,clrNONE,clrWhite,clrGreen,10,CORNER_RIGHT_UPPER,95, 110,"SELL", false); button("button6",50,25,clrNONE,clrBlack,clrDeepPink,10,CORNER_RIGHT_UPPER,175, 145,"Price",true); edittext("TEXT4",100,25,clrBlack,clrBlack,clrWhite,10,CORNER_RIGHT_UPPER,125, 145,ALIGN_CENTER,false,"0"); button("button7",70,30,clrNONE,clrWhite,clrRed,10,CORNER_RIGHT_UPPER,175, 175,"BUYLIMIT", false); button("button8",70,30,clrNONE,clrWhite,clrGreen,10,CORNER_RIGHT_UPPER,95, 175,"SELLLIMIT", false); button("button9",70,30,clrNONE,clrWhite,clrRed,10,CORNER_RIGHT_UPPER,175, 210,"BUYSTOP", false); button("button10",70,30,clrNONE,clrWhite,clrGreen,10,CORNER_RIGHT_UPPER,95, 210,"SELLSTOP", false); button("button11",70,30,clrNONE,clrWhite,clrRed,10,CORNER_RIGHT_UPPER,175, 245," Close", false); button("button12",70,30,clrNONE,clrWhite,clrGreen,10,CORNER_RIGHT_UPPER,95, 245," Delete", false); return(INIT_SUCCEEDED); } void OnDeinit(const int reason) { } void button(string name,int width,int height,int clr_border,int clr_text,int clr_bg, int fontsize,int corner,int xd,int yd,string text) { ObjectCreate(0,name,OBJ_BUTTON,0,0,0); ObjectSetInteger(0,name,OBJPROP_BORDER_COLOR, clr_border); ObjectSetInteger(0,name,OBJPROP_XSIZE, width); ObjectSetInteger(0,name,OBJPROP_YSIZE, height); ObjectSetInteger(0,name,OBJPROP_COLOR, clr_text); ObjectSetInteger(0,name,OBJPROP_BGCOLOR, clr_bg); ObjectSetInteger(0,name,OBJPROP_FONTSIZE, fontsize); ObjectSetInteger(0,name,OBJPROP_CORNER, corner); ObjectSetInteger(0,name,OBJPROP_XDISTANCE, xd); ObjectSetInteger(0,name,OBJPROP_YDISTANCE, yd); ObjectSetString(0,name,OBJPROP_TEXT, text); } void edittext(string name, int width, int height, int clr_border, int clr_text, int clr_bg, int fontsize, int corner, int xd, int yd, int align, int readonly, string text) { ObjectCreate(0, name,OBJ_EDIT,0,0,0); ObjectSetInteger(0, name,OBJPROP_XSIZE,width); ObjectSetInteger(0,name,OBJPROP_YSIZE,height); ObjectSetInteger(0,name,OBJPROP_BORDER_COLOR, clr_border); ObjectSetInteger(0,name,OBJPROP_COLOR, clr_text); ObjectSetInteger(0,name,OBJPROP_BGCOLOR, clr_bg); ObjectSetInteger(0,name,OBJPROP_FONTSIZE, fontsize); ObjectSetInteger(0,name,OBJPROP_CORNER, corner); ObjectSetInteger(0,name,OBJPROP_XDISTANCE, xd); ObjectSetInteger(0,name,OBJPROP_YDISTANCE, yd); ObjectSetInteger(0,name,OBJPROP_ALIGN, align); ObjectSetInteger(0,name,OBJPROP_READONLY, readonly); ObjectSetString(0,name,OBJPROP_TEXT, text); } void lable(string name, int width, int height, int clr_border, int clr_bg, int corner, int xd, int yd, bool back) { ObjectCreate(0,name,OBJ_RECTANGLE_LABEL,0,0,0); ObjectSetInteger(0,name,OBJPROP_XSIZE,width); ObjectSetInteger(0,name,OBJPROP_YSIZE,height); ObjectSetInteger(0,name,OBJPROP_BORDER_COLOR,clr_border); ObjectSetInteger(0,name,OBJPROP_BGCOLOR,clr_bg); ObjectSetInteger(0,name,OBJPROP_CORNER,corner); ObjectSetInteger(0,name,OBJPROP_XDISTANCE,xd); ObjectSetInteger(0,name,OBJPROP_YDISTANCE,yd); ObjectSetInteger(0,name,OBJPROP_BACK,back); }
運行程序後我們的EA創建出來的面板如圖所示:
按鈕功能的實現
我們已經把麵板的對像都建立好了,那麼接下來我們要通過OnChartEvent()來實現按鈕的功能,就如上節課那樣。
我們先把買單的功能實現,首先我們要識別在BUY這個按鈕上的點擊事件,然後我們需要識別按鈕的狀態,如果按鈕狀態時按下的狀態,那麼我們就做一個買單,原理非常簡單,代碼如下:
void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam) { double lot=(double)ObjectGetString(0,"TEXT1",OBJPROP_TEXT,0); double price=(double)ObjectGetString(0,"TEXT4",OBJPROP_TEXT,0); double sl; double tp; int check; int mag=12345; int huadian=10; int i; if(id==CHARTEVENT_OBJECT_CLICK && sparam=="button4") { if(ObjectGetInteger(0,"button4",OBJPROP_STATE,0)) { sl=MarketInfo(Symbol(),MODE_BID)-(int)ObjectGetString(0,"TEXT2", OBJPROP_TEXT,0)*MarketInfo(Symbol(),MODE_POINT)*10; tp=MarketInfo(Symbol(),MODE_BID)+(int)ObjectGetString(0,"TEXT3", OBJPROP_TEXT,0)*MarketInfo(Symbol(),MODE_POINT)*10; check=OrderSend(Symbol(),OP_BUY,lot,MarketInfo(Symbol(),MODE_ASK), huadian,sl,tp,"BUY",mag,0,clrBlue); } ObjectSetInteger(0,"button4",OBJPROP_STATE,false); } }
以上代碼中,我們用ObjectGetString ()函數得到了文本框內的交易手數、止損點數、止盈點數以及掛單的價格,然後我們判斷了是否存在Buy按鈕的鼠標點擊事件,接下來用ObjectGetInteger()函數判斷了BUY按鈕的按壓狀態,如果這個按鈕時壓下去的,那麼我們就根據面板中的止損止盈點數來計算止損止盈的價格,最後開單。
我們試一下現在按鈕是不是可以用了,按下之後如圖所示:
可以看到我們在當前價位開出了一個訂單,止損、止盈、手數都對得上,所以程序運行正常。
那麼其他的按鈕也是照葫蘆畫瓢,基本類似,只不過掛單需要把文本框裡的價格用上,詳細的過程就不講了,完整的代碼如下:
int OnInit() { ObjectDelete(0,"lable"); ObjectDelete(0,"button"); ObjectDelete(0,"TEXT"); ObjectDelete(0,"button1"); ObjectDelete(0,"TEXT1"); ObjectDelete(0,"button2"); ObjectDelete(0,"TEXT2"); ObjectDelete(0,"button3"); ObjectDelete(0,"TEXT3"); ObjectDelete(0,"button4"); ObjectDelete(0,"button5"); ObjectDelete(0,"button6"); ObjectDelete(0,"TEXT4"); ObjectDelete(0,"button7"); ObjectDelete(0,"button8"); ObjectDelete(0,"button9"); ObjectDelete(0,"button10"); ObjectDelete(0,"button11"); ObjectDelete(0,"button12"); lable("lable",180,280,clrDarkViolet,clrLightSlateGray,CORNER_RIGHT_UPPER,190, 10,false); button("button1",50,25,clrNONE,clrBlack,clrDeepPink,10,CORNER_RIGHT_UPPER,175,20,"Lot",true); edittext("TEXT1",100,25,clrBlack,clrBlack,clrWhite,10,CORNER_RIGHT_UPPER,125,20,ALIGN_CENTER,false,"0.01"); button("button2",50,25,clrNONE,clrBlack,clrDeepPink,10,CORNER_RIGHT_UPPER,175, 50,"SL",true); edittext("TEXT2",100,25,clrBlack,clrBlack,clrWhite,10,CORNER_RIGHT_UPPER,125, 50,ALIGN_CENTER,false,"100"); button("button3",50,25,clrNONE,clrBlack,clrDeepPink,10,CORNER_RIGHT_UPPER,175, 80,"TP",true); edittext("TEXT3",100,25,clrBlack,clrBlack,clrWhite,10,CORNER_RIGHT_UPPER,125,80,ALIGN_CENTER,false,"100"); button("button4",70,30,clrNONE,clrWhite,clrRed,10,CORNER_RIGHT_UPPER,175, 110,"BUY", false); button("button5",70,30,clrNONE,clrWhite,clrGreen,10,CORNER_RIGHT_UPPER,95, 110,"SELL", false); button("button6",50,25,clrNONE,clrBlack,clrDeepPink,10,CORNER_RIGHT_UPPER,175, 145,"Price",true); edittext("TEXT4",100,25,clrBlack,clrBlack,clrWhite,10,CORNER_RIGHT_UPPER,125, 145,ALIGN_CENTER,false,"0"); button("button7",70,30,clrNONE,clrWhite,clrRed,10,CORNER_RIGHT_UPPER,175, 175,"BUYLIMIT", false); button("button8",70,30,clrNONE,clrWhite,clrGreen,10,CORNER_RIGHT_UPPER,95, 175,"SELLLIMIT", false); button("button9",70,30,clrNONE,clrWhite,clrRed,10,CORNER_RIGHT_UPPER,175, 210,"BUYSTOP", false); button("button10",70,30,clrNONE,clrWhite,clrGreen,10,CORNER_RIGHT_UPPER,95, 210,"SELLSTOP", false); button("button11",70,30,clrNONE,clrWhite,clrRed,10,CORNER_RIGHT_UPPER,175, 245," Close", false); button("button12",70,30,clrNONE,clrWhite,clrGreen,10,CORNER_RIGHT_UPPER,95, 245," Delete", false); return(INIT_SUCCEEDED); } void OnDeinit(const int reason) { ObjectDelete(0,"lable"); ObjectDelete(0,"button"); ObjectDelete(0,"TEXT"); ObjectDelete(0,"button1"); ObjectDelete(0,"TEXT1"); ObjectDelete(0,"button2"); ObjectDelete(0,"TEXT2"); ObjectDelete(0,"button3"); ObjectDelete(0,"TEXT3"); ObjectDelete(0,"button4"); ObjectDelete(0,"button5"); ObjectDelete(0,"button6"); ObjectDelete(0,"TEXT4"); ObjectDelete(0,"button7"); ObjectDelete(0,"button8"); ObjectDelete(0,"button9"); ObjectDelete(0,"button10"); ObjectDelete(0,"button11"); ObjectDelete(0,"button12"); } void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam) { double lot=(double)ObjectGetString(0,"TEXT1",OBJPROP_TEXT,0); double price=(double)ObjectGetString(0,"TEXT4",OBJPROP_TEXT,0); double sl; double tp; int check; int mag=12345; int huadian=10; string sym= Symbol(); int i; if(id==CHARTEVENT_OBJECT_CLICK && sparam=="button4") { //买单按钮设置if(ObjectGetInteger(0,"button4",OBJPROP_STATE,0)) { sl=MarketInfo(Symbol(),MODE_BID)-(int)ObjectGetString(0,"TEXT2", OBJPROP_TEXT,0)*MarketInfo(Symbol(),MODE_POINT)*10; tp=MarketInfo(Symbol(),MODE_BID)+(int)ObjectGetString(0,"TEXT3", OBJPROP_TEXT,0)*MarketInfo(Symbol(),MODE_POINT)*10; check=OrderSend(Symbol(),OP_BUY,lot,MarketInfo(Symbol(),MODE_ASK), huadian,sl,tp,"BUY",mag,0,clrBlue); } ObjectSetInteger(0,"button4",OBJPROP_STATE,false); } //卖单按钮设置if(id==CHARTEVENT_OBJECT_CLICK && sparam=="button5") { if(ObjectGetInteger(0,"button5",OBJPROP_STATE,0)) { sl=MarketInfo(Symbol(),MODE_ASK)+(int)ObjectGetString(0,"TEXT2", OBJPROP_TEXT,0)*MarketInfo(Symbol(),MODE_POINT)*10; tp=MarketInfo(Symbol(),MODE_ASK)-(int)ObjectGetString(0,"TEXT3", OBJPROP_TEXT,0)*MarketInfo(Symbol(),MODE_POINT)*10; check=OrderSend(sym,OP_SELL,lot,MarketInfo(sym,MODE_BID), huadian,sl,tp,"SELL",mag,0,clrRed); } ObjectSetInteger(0,"button5",OBJPROP_STATE,false); } //买单限价挂单if(id==CHARTEVENT_OBJECT_CLICK && sparam=="button7") { if(ObjectGetInteger(0,"button7",OBJPROP_STATE,0)==true && price<MarketInfo(Symbol(),MODE_ASK)) { sl=price-(int)ObjectGetString(0,"TEXT2",OBJPROP_TEXT,0)* MarketInfo(Symbol(),MODE_POINT)*10; tp=price+(int)ObjectGetString(0,"TEXT3",OBJPROP_TEXT,0)* MarketInfo(Symbol(),MODE_POINT)*10; check=OrderSend(sym,OP_BUYLIMIT,lot,price,huadian,sl,tp,"BUY",mag,0,clrBlue); } ObjectSetInteger(0,"button7",OBJPROP_STATE,false); } //卖单限价挂单if(id==CHARTEVENT_OBJECT_CLICK && sparam=="button8") { if(ObjectGetInteger(0,"button8",OBJPROP_STATE,0)==true && price>MarketInfo(Symbol(),MODE_BID)) { sl=price+(int)ObjectGetString(0,"TEXT2",OBJPROP_TEXT,0)* MarketInfo(Symbol(),MODE_POINT)*10; tp=price-(int)ObjectGetString(0,"TEXT3",OBJPROP_TEXT,0)* MarketInfo(Symbol(),MODE_POINT)*10; check=OrderSend(sym,OP_SELLLIMIT,lot,price,huadian,sl,tp,"SELL",mag,0,clrRed); } ObjectSetInteger(0,"button8",OBJPROP_STATE,false); } //买单止损挂单if(id==CHARTEVENT_OBJECT_CLICK && sparam=="button9") { if(ObjectGetInteger(0,"button9",OBJPROP_STATE,0)==true && price>MarketInfo(Symbol(),MODE_ASK)) { sl=price-(int)ObjectGetString(0,"TEXT2",OBJPROP_TEXT,0)* MarketInfo(Symbol(),MODE_POINT)*10; tp=price+(int)ObjectGetString(0,"TEXT3",OBJPROP_TEXT,0)* MarketInfo(Symbol(),MODE_POINT)*10; check=OrderSend(sym,OP_BUYSTOP,lot,price,huadian,sl,tp,"BUY",mag,0,clrBlue); } ObjectSetInteger(0,"button9",OBJPROP_STATE,false); } //卖单止损挂单if(id==CHARTEVENT_OBJECT_CLICK && sparam=="button10") { if(ObjectGetInteger(0,"button10",OBJPROP_STATE,0)==true && price<MarketInfo(Symbol(),MODE_BID)) { sl=price+(int)ObjectGetString(0,"TEXT2",OBJPROP_TEXT,0)* MarketInfo(Symbol(),MODE_POINT)*10; tp=price-(int)ObjectGetString(0,"TEXT3",OBJPROP_TEXT,0)* MarketInfo(Symbol(),MODE_POINT)*10; check=OrderSend(sym,OP_SELLSTOP,lot,price,huadian,sl,tp,"SELL",mag,0,clrRed); } ObjectSetInteger(0,"button10",OBJPROP_STATE,false); } //平仓if(id==CHARTEVENT_OBJECT_CLICK && sparam=="button11") { if(ObjectGetInteger(0,"button11",OBJPROP_STATE,0)==true) { for(i=OrdersTotal()-1;i>=0;i--) { if(OrderSelect(i,SELECT_BY_POS)) { if(OrderSymbol()==sym && OrderMagicNumber()==mag) { if(OrderType()==OP_SELL) { check=OrderClose(OrderTicket(),OrderLots(), MarketInfo(sym,MODE_ASK),huadian); } else if(OrderType()==OP_BUY) { check=OrderClose(OrderTicket(),OrderLots(), MarketInfo(sym,MODE_BID),huadian); } } } } } ObjectSetInteger(0,"button11",OBJPROP_STATE,false); } //删除挂单if(id==CHARTEVENT_OBJECT_CLICK && sparam=="button12") { if(ObjectGetInteger(0,"button12",OBJPROP_STATE,0)==true) { for(i=OrdersTotal()-1;i>=0;i--) { if(OrderSelect(i,SELECT_BY_POS)) { if(OrderSymbol()==sym && OrderMagicNumber()==mag) { if(OrderType()==OP_SELLLIMIT || OrderType()==OP_BUYLIMIT || OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP) { check=OrderDelete(OrderTicket(),clrRed); } } } } } ObjectSetInteger(0,"button12",OBJPROP_STATE,false); } } void button(string name,int width,int height,int clr_border,int clr_text,int clr_bg, int fontsize,int corner,int xd,int yd,string text, bool select) { ObjectCreate(0,name,OBJ_BUTTON,0,0,0); ObjectSetInteger(0,name,OBJPROP_BORDER_COLOR, clr_border); ObjectSetInteger(0,name,OBJPROP_XSIZE, width); ObjectSetInteger(0,name,OBJPROP_YSIZE, height); ObjectSetInteger(0,name,OBJPROP_COLOR, clr_text); ObjectSetInteger(0,name,OBJPROP_BGCOLOR, clr_bg); ObjectSetInteger(0,name,OBJPROP_FONTSIZE, fontsize); ObjectSetInteger(0,name,OBJPROP_CORNER, corner); ObjectSetInteger(0,name,OBJPROP_XDISTANCE, xd); ObjectSetInteger(0,name,OBJPROP_YDISTANCE, yd); ObjectSetString(0,name,OBJPROP_TEXT, text); ObjectSetInteger(0,name, OBJPROP_SELECTABLE, select); } void edittext(string name, int width, int height, int clr_border, int clr_text, int clr_bg, int fontsize, int corner, int xd, int yd, int align, int readonly, string text) { ObjectCreate(0, name,OBJ_EDIT,0,0,0); ObjectSetInteger(0, name,OBJPROP_XSIZE,width); ObjectSetInteger(0,name,OBJPROP_YSIZE,height); ObjectSetInteger(0,name,OBJPROP_BORDER_COLOR, clr_border); ObjectSetInteger(0,name,OBJPROP_COLOR, clr_text); ObjectSetInteger(0,name,OBJPROP_BGCOLOR, clr_bg); ObjectSetInteger(0,name,OBJPROP_FONTSIZE, fontsize); ObjectSetInteger(0,name,OBJPROP_CORNER, corner); ObjectSetInteger(0,name,OBJPROP_XDISTANCE, xd); ObjectSetInteger(0,name,OBJPROP_YDISTANCE, yd); ObjectSetInteger(0,name,OBJPROP_ALIGN, align); ObjectSetInteger(0,name,OBJPROP_READONLY, readonly); ObjectSetString(0,name,OBJPROP_TEXT, text); } void lable(string name, int width, int height, int clr_border, int clr_bg, int corner, int xd, int yd, bool back) { ObjectCreate(0,name,OBJ_RECTANGLE_LABEL,0,0,0); ObjectSetInteger(0,name,OBJPROP_XSIZE,width); ObjectSetInteger(0,name,OBJPROP_YSIZE,height); ObjectSetInteger(0,name,OBJPROP_BORDER_COLOR,clr_border); ObjectSetInteger(0,name,OBJPROP_BGCOLOR,clr_bg); ObjectSetInteger(0,name,OBJPROP_CORNER,corner); ObjectSetInteger(0,name,OBJPROP_XDISTANCE,xd); ObjectSetInteger(0,name,OBJPROP_YDISTANCE,yd); ObjectSetInteger(0,name,OBJPROP_BACK,back); }
之前也說了,其實面板的編寫應該放到指標裡,但是之前我們沒有學指標,所以我們用EA來做一下,原理是一樣的。那麼後面的課程,我們將學習一下指標的編寫,至於本節課寫的面板,在學完指標後大家可以自己試著把內容寫到指標裡面去。那麼下節課見。