สวัสดีครับเพื่อนๆ เทรดเดอร์ทุกคน! วันนี้เราจะมาพูดถึง อินดิเคเตอร์ ColorX2MA ซึ่งมีฟีเจอร์แจ้งเตือนส่งอีเมลและการแจ้งเตือนไปยังโทรศัพท์มือถือครับ
อินดิเคเตอร์นี้มีการปรับปรุงโค้ดเพื่อเพิ่มฟังก์ชันการแจ้งเตือน โดยมีการเปลี่ยนแปลงดังนี้:
- เพิ่มพารามิเตอร์ใหม่สำหรับการแจ้งเตือน
//---- ตัวแปรสำหรับการแจ้งเตือนinputuint NumberofBar=1; //จำนวนแท่งที่ใช้สำหรับสัญญาณinputbool SoundON=true; //เปิดการแจ้งเตือนเสียงinputuint NumberofAlerts=2; //จำนวนการแจ้งเตือนinputbool EMailON=false; //เปิดการส่งสัญญาณทางอีเมลinputbool PushON=false; //เปิดการส่งสัญญาณไปยังอุปกรณ์มือถือ
- เพิ่มฟังก์ชันใหม่สามฟังก์ชันที่ส่วนท้ายของโค้ดอินดิเคเตอร์: BuySignal(), SellSignal() และ GetStringTimeframe()
//+------------------------------------------------------------------+//| ฟังก์ชันสัญญาณซื้อ |//+------------------------------------------------------------------+void BuySignal(string SignalSirname,// ข้อความชื่ออินดิเคเตอร์สำหรับอีเมลและการแจ้งเตือน double &ColorArray[],// บัฟเฟอร์สี int ColorIndex,// ดัชนีสีในบัฟเฟอร์สีเพื่อสร้างสัญญาณ constint Rates_total, // จำนวนแท่งปัจจุบัน constint Prev_calculated, // จำนวนแท่งในแท่งก่อนหน้า constdouble &Close[], // ราคาปิด constint &Spread[]) // สเปรด { //--- staticuint counter=0; if(Rates_total!=Prev_calculated) counter=0; bool BuySignal=false; bool SeriesTest=ArrayGetAsSeries(ColorArray); int index,index1; if(SeriesTest) { index=int(NumberofBar); index1=index+1; } else { index=Rates_total-int(NumberofBar)-1; index1=index-1; } if(ColorArray[index1]!=ColorIndex && ColorArray[index]==ColorIndex) BuySignal=true; if(BuySignal && counter<=NumberofAlerts) { counter++; MqlDateTime tm; TimeToStruct(TimeCurrent(),tm); string text=TimeToString(TimeCurrent(),TIME_DATE)+" "+string(tm.hour)+":"+string(tm.min); SeriesTest=ArrayGetAsSeries(Close); if(SeriesTest) index=int(NumberofBar); else index=Rates_total-int(NumberofBar)-1; doubleAsk=Close[index]; doubleBid=Close[index]; SeriesTest=ArrayGetAsSeries(Spread); if(SeriesTest) index=int(NumberofBar); else index=Rates_total-int(NumberofBar)-1; Bid+=Spread[index]; string sAsk=DoubleToString(Ask,_Digits); string sBid=DoubleToString(Bid,_Digits); string sPeriod=GetStringTimeframe(ChartPeriod()); if(SoundON) Alert("BUY signal \n Ask=",Ask,"\n Bid=",Bid,"\n currtime=",text,"\n Symbol=",Symbol()," Period=",sPeriod); if(EMailON) SendMail(SignalSirname+": BUY signal alert","BUY signal at Ask="+sAsk+", Bid="+sBid+", Date="+text+" Symbol="+Symbol()+" Period="+sPeriod); if(PushON) SendNotification(SignalSirname+": BUY signal at Ask="+sAsk+", Bid="+sBid+", Date="+text+" Symbol="+Symbol()+" Period="+sPeriod); } //--- } //+------------------------------------------------------------------+//| ฟังก์ชันสัญญาณขาย |//+------------------------------------------------------------------+void SellSignal(string SignalSirname, // ข้อความชื่ออินดิเคเตอร์สำหรับอีเมลและการแจ้งเตือน double &ColorArray[], // บัฟเฟอร์สี int ColorIndex, // ดัชนีสีในบัฟเฟอร์สีเพื่อสร้างสัญญาณ constint Rates_total, // จำนวนแท่งปัจจุบัน constint Prev_calculated, // จำนวนแท่งในแท่งก่อนหน้า constdouble &Close[], // ราคาปิด constint &Spread[]) // สเปรด { //--- staticuint counter=0; if(Rates_total!=Prev_calculated) counter=0; bool SellSignal=false; bool SeriesTest=ArrayGetAsSeries(ColorArray); int index,index1; if(SeriesTest) { index=int(NumberofBar); index1=index+1; } else { index=Rates_total-int(NumberofBar)-1; index1=index-1; } if(ColorArray[index1]!=ColorIndex && ColorArray[index]==ColorIndex) SellSignal=true; if(SellSignal && counter<=NumberofAlerts) { counter++; MqlDateTime tm; TimeToStruct(TimeCurrent(),tm); string text=TimeToString(TimeCurrent(),TIME_DATE)+" "+string(tm.hour)+":"+string(tm.min); SeriesTest=ArrayGetAsSeries(Close); if(SeriesTest) index=int(NumberofBar); else index=Rates_total-int(NumberofBar)-1; doubleAsk=Close[index]; doubleBid=Close[index]; SeriesTest=ArrayGetAsSeries(Spread); if(SeriesTest) index=int(NumberofBar); else index=Rates_total-int(NumberofBar)-1; Bid+=Spread[index]; string sAsk=DoubleToString(Ask,_Digits); string sBid=DoubleToString(Bid,_Digits); string sPeriod=GetStringTimeframe(ChartPeriod()); if(SoundON) Alert("SELL signal \n Ask=",Ask,"\n Bid=",Bid,"\n currtime=",text,"\n Symbol=",Symbol()," Period=",sPeriod); if(EMailON) SendMail(SignalSirname+": SELL signal alert","SELL signal at Ask="+sAsk+", Bid="+sBid+", Date="+text+" Symbol="+Symbol()+" Period="+sPeriod); if(PushON) SendNotification(SignalSirname+": SELL signal at Ask="+sAsk+", Bid="+sBid+", Date="+text+" Symbol="+Symbol()+" Period="+sPeriod); } //--- } //+------------------------------------------------------------------+//| การดึงช่วงเวลาเป็นสตริง |//+------------------------------------------------------------------+string GetStringTimeframe(ENUM_TIMEFRAMES timeframe) { //---- return(StringSubstr(EnumToString(timeframe),7,-1)); //---- }
- เพิ่มการเรียกใช้ฟังก์ชัน BuySignal() และ SellSignal() หลังจากการคำนวณอินดิเคเตอร์ในบล็อก OnCalculate()
//--- BuySignal("X2MA_Alert",ColorX2MA,1,rates_total,prev_calculated,close,spread); SellSignal("X2MA_Alert",ColorX2MA,2,rates_total,prev_calculated,close,spread); //---
โดยที่ ColorX2MA คือชื่อของบัฟเฟอร์สีสำหรับเก็บสีเส้นในรูปแบบของดัชนี
คาดว่าการเรียกใช้ฟังก์ชัน BuySignal() และ SellSignal() จะใช้เพียงครั้งเดียวในบล็อก OnCalculate() ของโค้ดอินดิเคเตอร์
อินดิเคเตอร์นี้ใช้คลาสของ SmoothAlgorithms.mqh (ให้คัดลอกไปยัง <terminal_data_folder>\MQL5\Include) ซึ่งมีการอธิบายการใช้งานคลาสอย่างละเอียดในบทความ "การเฉลี่ยชุดราคาเพื่อการคำนวณกลางโดยไม่ใช้บัฟเฟอร์เพิ่มเติม".

Fig1. อินดิเคเตอร์ ColorX2MA_Alert บนกราฟ
Fig.2. อินดิเคเตอร์ ColorX2MA_Alert. การสร้างการแจ้งเตือน.


ความคิดเห็น 0