2016/09/13

2016/09/13(二) EA程式長什麼樣貌 !

CopyWinner富智贏家网  (每天更新)外匯交易相關訊息http://www.a102s.com/news/211-copywinner.html  
(:本站文案除原創外,其餘訊息来自互联网收集和整理,请自行参酌。)
2016/09/13(二)

EA程式長什麼樣貌 ! (下圖為MT4平台EA嵌入圖表啟動運行畫面)



>>><<< $ ¥€ >>><<< 

EA程式長什麼樣貌呢?首先要認識EA的組成框架,標準的EA5個部分組成,分別是變數預定義EA初始化程式、EA結束程式、EA執行計算程式和自訂變數等。EA預備知識就是了解MQL4語言,在認識MQL4語言前,首先要打消自己的顧慮,不要被網上流傳的MQL4程式必須具備C語言基礎給嚇到,大多數人學不會程式設計就是自己把自己勸退的。
   
當然,學習電腦語言要求你必須有很好的邏輯思維能力。我們可以通過高階語言 (係英文語句,但非機械語言之低階語言,而是接近人類使用語言文句) 內容來理解電腦的邏輯。所有的電腦語言都包含兩個語句,一個是條件(if)語句,一個是迴圈(for)語句。 If語句顧名思義,滿足條件就執行,否則就跳過。

好了,說這麼多,有人頭有點暈覺得太囉唆,好吧,今天開門見山,簡單介紹一個EA編程內碼的樣貌。就以cci指標交易信號之市塲數據取值,創建一個簡單的EA嵌入運行於MT4交易軟件,如下圖所示↓,並將該EA源代碼發佈於後,請參酌 !

創建一個簡單的EA程式copywinner101 CCI v.10;簡易交易思路 :進倉平倉止盈止損(暫不考慮加倉)以CCI指標&市塲數據取值編程代碼如下↓

//+------------------------------------------------------------------+
//|                                 copywinner101 CCI v.10.mq4 |
//|                                   Copyright 2016, copywinner |
//|                                             https://copywinner.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, copywinner"
#property link      "https://copywinner.net"
#property version   "1.00"
#property description "CCI indicator with different colors at levels 0, 100 and -100. The cross is a signal for \"123 Exit\" as presented by "
#property description "MPlay and used in Woodies CCI system. This exit signal is effective especially in short-term breakout trades, because "
#property description "it prevents early exiting at small corrections."

#include <stdlib.mqh>
#include <stderror.mqh>

//--- indicator settings
#property indicator_separate_window
#property indicator_buffers 6

#property indicator_type1 DRAW_HISTOGRAM
#property indicator_style1 STYLE_SOLID
#property indicator_width1 2
#property indicator_color1 0x00FF00
#property indicator_label1 "Above 100"

#property indicator_type2 DRAW_HISTOGRAM
#property indicator_style2 STYLE_SOLID
#property indicator_width2 2
#property indicator_color2 0x008000
#property indicator_label2 "Above 0"

#property indicator_type3 DRAW_HISTOGRAM
#property indicator_style3 STYLE_SOLID
#property indicator_width3 2
#property indicator_color3 0x4763FF
#property indicator_label3 "Below 0"

#property indicator_type4 DRAW_HISTOGRAM
#property indicator_style4 STYLE_SOLID
#property indicator_width4 2
#property indicator_color4 0x0000FF
#property indicator_label4 "Below -100"

#property indicator_type5 DRAW_ARROW
#property indicator_width5 2
#property indicator_color5 0x00FF00
#property indicator_label5 "123 Exit"

#property indicator_type6 DRAW_ARROW
#property indicator_width6 2
#property indicator_color6 0x0000FF
#property indicator_label6 "123 Exit"

//--- indicator buffers
double Buffer1[];
double Buffer2[];
double Buffer3[];
double Buffer4[];
double Buffer5[];
double Buffer6[];

extern int Period1 = 14;
double myPoint; //initialized in OnInit

void myAlert(string type, string message)
  {
   if(type == "print")
      Print(message);
   else if(type == "error")
     {
      Print(type+" | CCI Colored Copy02 @ "+Symbol()+","+Period()+" | "+message);
     }
   else if(type == "order")
     {
     }
   else if(type == "modify")
     {
     }
  }

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {  
   IndicatorBuffers(6);
   SetIndexBuffer(0, Buffer1);
   SetIndexEmptyValue(0, 0);
   SetIndexBuffer(1, Buffer2);
   SetIndexEmptyValue(1, 0);
   SetIndexBuffer(2, Buffer3);
   SetIndexEmptyValue(2, 0);
   SetIndexBuffer(3, Buffer4);
   SetIndexEmptyValue(3, 0);
   SetIndexBuffer(4, Buffer5);
   SetIndexEmptyValue(4, 0);
   SetIndexArrow(4, 251);
   SetIndexBuffer(5, Buffer6);
   SetIndexEmptyValue(5, 0);
   SetIndexArrow(5, 251);
   //initialize myPoint
   myPoint = Point();
   if(Digits() == 5 || Digits() == 3)
     {
      myPoint *= 10;
     }
   return(INIT_SUCCEEDED);
  }

//+------------------------------------------------------------------+
//| 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[])
  {
   int limit = rates_total - prev_calculated;
   //--- counting from 0 to rates_total
   ArraySetAsSeries(Buffer1, true);
   ArraySetAsSeries(Buffer2, true);
   ArraySetAsSeries(Buffer3, true);
   ArraySetAsSeries(Buffer4, true);
   ArraySetAsSeries(Buffer5, true);
   ArraySetAsSeries(Buffer6, true);
   //--- initial zero
   if(prev_calculated < 1)
     {
      ArrayInitialize(Buffer1, 0);
      ArrayInitialize(Buffer2, 0);
      ArrayInitialize(Buffer3, 0);
      ArrayInitialize(Buffer4, 0);
      ArrayInitialize(Buffer5, 0);
      ArrayInitialize(Buffer6, 0);
     }
   else
      limit++;
  
   //--- main loop
   for(int i = limit-1; i >= 0; i--)
     {
      if (i >= MathMin(5000-1, rates_total-1-50)) continue; //omit some old rates to prevent "Array out of range" or slow calculation  
      //Indicator Buffer 1
      if(iCCI(NULL, PERIOD_CURRENT, Period1, PRICE_TYPICAL, i) > 100 //Commodity Channel Index > fixed value
      )
        {
         Buffer1[i] = iCCI(NULL, PERIOD_CURRENT, Period1, PRICE_TYPICAL, i); //Set indicator value at Commodity Channel Index
        }
      else
        {
         Buffer1[i] = 0;
        }
      //Indicator Buffer 2
      if(iCCI(NULL, PERIOD_CURRENT, Period1, PRICE_TYPICAL, i) < 100 //Commodity Channel Index < fixed value
      && iCCI(NULL, PERIOD_CURRENT, Period1, PRICE_TYPICAL, i) > 0 //Commodity Channel Index > fixed value
      )
        {
         Buffer2[i] = iCCI(NULL, PERIOD_CURRENT, Period1, PRICE_TYPICAL, i); //Set indicator value at Commodity Channel Index
        }
      else
        {
         Buffer2[i] = 0;
        }
      //Indicator Buffer 3
      if(iCCI(NULL, PERIOD_CURRENT, Period1, PRICE_TYPICAL, i) > -100 //Commodity Channel Index > fixed value
      && iCCI(NULL, PERIOD_CURRENT, Period1, PRICE_TYPICAL, i) < 0 //Commodity Channel Index < fixed value
      )
        {
         Buffer3[i] = iCCI(NULL, PERIOD_CURRENT, Period1, PRICE_TYPICAL, i); //Set indicator value at Commodity Channel Index
        }
      else
        {
         Buffer3[i] = 0;
        }
      //Indicator Buffer 4
      if(iCCI(NULL, PERIOD_CURRENT, Period1, PRICE_TYPICAL, i) < -100 //Commodity Channel Index < fixed value
      )
        {
         Buffer4[i] = iCCI(NULL, PERIOD_CURRENT, Period1, PRICE_TYPICAL, i); //Set indicator value at Commodity Channel Index
        }
      else
        {
         Buffer4[i] = 0;
        }
      //Indicator Buffer 5
      if(iCCI(NULL, PERIOD_CURRENT, Period1, PRICE_TYPICAL, i) < iCCI(NULL, PERIOD_CURRENT, Period1, PRICE_TYPICAL, 1+i) //Commodity Channel Index < Commodity Channel Index
      && iCCI(NULL, PERIOD_CURRENT, Period1, PRICE_TYPICAL, 1+i) < iCCI(NULL, PERIOD_CURRENT, Period1, PRICE_TYPICAL, 2+i) //Commodity Channel Index < Commodity Channel Index
      && Close[i] < Open[i] //Candlestick Close < Candlestick Open
      && iCCI(NULL, PERIOD_CURRENT, Period1, PRICE_TYPICAL, i) > 0 //Commodity Channel Index > fixed value
      )
        {
         Buffer5[i] = iCCI(NULL, PERIOD_CURRENT, Period1, PRICE_TYPICAL, i); //Set indicator value at Commodity Channel Index
        }
      else
        {
         Buffer5[i] = 0;
        }
      //Indicator Buffer 6
      if(iCCI(NULL, PERIOD_CURRENT, Period1, PRICE_TYPICAL, i) > iCCI(NULL, PERIOD_CURRENT, Period1, PRICE_TYPICAL, 1+i) //Commodity Channel Index > Commodity Channel Index
      && iCCI(NULL, PERIOD_CURRENT, Period1, PRICE_TYPICAL, 1+i) > iCCI(NULL, PERIOD_CURRENT, Period1, PRICE_TYPICAL, 2+i) //Commodity Channel Index > Commodity Channel Index
      && Close[i] > Open[i] //Candlestick Close > Candlestick Open
      && iCCI(NULL, PERIOD_CURRENT, Period1, PRICE_TYPICAL, i) < 0 //Commodity Channel Index < fixed value
      )
        {
         Buffer6[i] = iCCI(NULL, PERIOD_CURRENT, Period1, PRICE_TYPICAL, i); //Set indicator value at Commodity Channel Index
        }
      else
        {
         Buffer6[i] = 0;
        }
     }
   return(rates_total);
  }
//+------------------------------------------------------------------+    

>>><<< $ ¥€ >>><<<