不懂就问。编程0基础,想学MQL5语言,有没有必要先学C++?谢谢各位大佬! - 页 2

 
Junjie Shao:

如题。如果有必要先学一遍C++的话,请问需要学到什么程度够用?比如,读透并且掌握 《C++ Primer》是不是就够了呢?谢谢!

还是说,直接学MQL5即可?谢谢!

差点以为是我提问的…我在别的地方问过同样问题,经过总结,潘神最合适吧,我是这么觉得的。可以配合GPT,会省很多事
 
Biao Chen Wang #:
差点以为是我提问的…我在别的地方问过同样问题,经过总结,潘神最合适吧,我是这么觉得的。可以配合GPT,会省很多事

请问潘神是什么意思啊?

 


RSI+MA  EA,MQL5,Compilation error free, but unable to place an order. Can any friend improve the code?

#include <Trade\Trade.mqh>

//+------------------------------------------------------------------+
//| inputs                                                           |
//+------------------------------------------------------------------+
static input long    InpMagicnumber = 546812;   // magic number
static input double  InpLotSize = 0.01;         // lot size
input int            InpRSIPeriod = 21;         // rsi period
input int            InpRSILevel = 70;          // rsi level (upper)
input int            InpStopLoss = 200;         // stop loss in points (0-off)
input int            InpTakeProfit = 100;       // take profit in points (0=off)
input bool           InpCloseSignal = false;    // close trades by opposite signal

//+------------------------------------------------------------------+
//| globalvariable                                                   |
//+------------------------------------------------------------------+

int handle;
double buffer[];
MqlTick currentTick;
CTrade trade;
datetime openTimeBuy = 0;
datetime openTimeSell = 0;

///----------------

int OnInit()
{
   // check user inputs
   if (InpMagicnumber<=0) {
      Alert("Magicnumber <= 0");
      return INIT_PARAMETERS_INCORRECT;
   }
   if (InpLotSize<=0 || InpLotSize>10) {
      Alert("Lot size <= 0 or > 10"); 
      return INIT_PARAMETERS_INCORRECT;
   }
   if (InpRSIPeriod<=1) {
      Alert("RSI period <= 1");
      return INIT_PARAMETERS_INCORRECT;
   }
   if (InpRSILevel>=100 || InpRSILevel<=50) {
      Alert ("RSI level >= 100 or <= 50");
      return INIT_PARAMETERS_INCORRECT;
   }
   if (InpStopLoss<0) {
      Alert ("Stop loss < 0");
      return INIT_PARAMETERS_INCORRECT;
   }
   
   if (InpTakeProfit<0) {
      Alert ("Take profit < 0");
      return INIT_PARAMETERS_INCORRECT;
    }
    
   // set magic number to trade object
   trade.SetExpertMagicNumber (InpMagicnumber);
   
   // create rsi handle
   handle = iRSI (_Symbol, PERIOD_CURRENT, InpRSIPeriod, PRICE_CLOSE);
   if (handle == INVALID_HANDLE) {
      Alert("Failed to create indicator handle"); 
      return INIT_FAILED;
   }
   
   // set buffer as series
   ArraySetAsSeries (buffer, true);

   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
   // release indicator handle
   if (handle!= INVALID_HANDLE) {IndicatorRelease (handle); }
   
}
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
   // get current tick
   if (!SymbolInfoTick (_Symbol, currentTick)) {Print ("Failed to get current tick"); return; }
   
   // get rsi values
   int values =CopyBuffer (handle,0,0,2,buffer);
   if (values!=2) {
      Print ("Failed to get indicator values");
      return;
   }
   Comment("buffer[0]:",buffer[0],"\nbuffer[1]:",buffer[1]);
   
   ///--------------------------------
   // count open positions
   int cntBuy, cntsell;
   if (!CountOpenPositions (cntBuy, cntsell)) {return;}
   
   // check for buy position
   if (cntBuy==0 && buffer[1]>= (100-InpRSILevel) && buffer[0]<(100-InpRSILevel) && openTimeBuy!=iTime (_Symbol, PERIOD_CURRENT, 0)) {
      openTimeBuy=iTime (_Symbol, PERIOD_CURRENT, 0);
      if (InpCloseSignal) {if (!ClosePositions (2)) {return;}}
      double sl = InpStopLoss==0 ? 0: currentTick.bid -InpStopLoss * _Point; 
      double tp = InpTakeProfit==0 ? 0: currentTick.bid + InpTakeProfit* _Point;
      if (!NormalizePrice(sl)) {return;}
      if (!NormalizePrice(tp)) {return;}
      
      trade.PositionOpen (_Symbol, ORDER_TYPE_BUY, InpLotSize, currentTick.ask,sl,tp, "RSI EA"); 
   }
   
   // check for sell position
   if (cntsell == 0 && buffer[1] <= InpRSILevel && buffer[0]>InpRSILevel && openTimeSell!=iTime (_Symbol, PERIOD_CURRENT, 0)){
      openTimeSell=iTime (_Symbol, PERIOD_CURRENT, 0);
      if (InpCloseSignal) {if (!ClosePositions (1)) {return;}}
      double sl= InpStopLoss ==0 ? 0: currentTick.ask + InpStopLoss *_Point;
      double tp = InpTakeProfit==0 ? 0: currentTick.ask -InpTakeProfit * _Point;
      
      if (!NormalizePrice (sl)) {return;}
      if (!NormalizePrice (tp)) {return;}
      trade.PositionOpen (_Symbol, ORDER_TYPE_SELL, InpLotSize, currentTick.bid, sl, tp, "RSI EA");
   }
 }
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| custom function                                             |
//+------------------------------------------------------------------+

// count open positions
bool CountOpenPositions (int &cntBuy, int &cntSell) {
   cntBuy = 0;
   cntSell = 0;
   int total = PositionsTotal();
   for (int i=total-1; 1>=0; i--) {
      ulong ticket = PositionGetTicket (i);
      if (ticket<=0) {Print ("Failed to get position ticket"); return false;}
      if (!PositionSelectByTicket (ticket)) {Print ("Failed to select position"); return false;}
      long magic;
      if (!PositionGetInteger (POSITION_MAGIC, magic)) {Print ("Failed to get position magicnumber"); return false;}
      if (magic==InpMagicnumber) {
         long type;
         if (!PositionGetInteger (POSITION_TYPE, type)) {Print ("Failed to get position type"); return false;}
         if (type==POSITION_TYPE_BUY) {cntBuy++;}
         if (type==POSITION_TYPE_SELL) {cntSell++; }

      }
   }

   return true;

}

// normalize price
bool NormalizePrice (double &price) {

   double ticksize=0;
   if (!SymbolInfoDouble (_Symbol,SYMBOL_TRADE_TICK_SIZE, ticksize)) { 
      Print ("Failed to get tick size");
      return false;
   }
   price = NormalizeDouble (MathRound (price/ticksize) *ticksize, _Digits);
   return true;
}



// close positions
bool ClosePositions (int all_buy_sell) {

   int total = PositionsTotal();
   for(int i=total-1; i>=0; i--) {
      ulong ticket = PositionGetTicket (i);
      if (ticket<=0) {Print ("Failed to get position ticket"); return false;}
      if (! PositionSelectByTicket (ticket)) {Print ("Failed to select position"); return false;}
      long magic;
      if (!PositionGetInteger (POSITION_MAGIC, magic)) {Print ("Failed to get position magicnumber"); return false;}
      if (magic==InpMagicnumber) {
         long type;
         if (!PositionGetInteger (POSITION_TYPE, type)) {Print ("Failed to get position type"); return false;} 
         if (all_buy_sell==1 && type==POSITION_TYPE_SELL) { continue; }
         if (all_buy_sell==2 && type==POSITION_TYPE_BUY) {continue; }
         trade. PositionClose (ticket); 
         if (trade.ResultRetcode () !=TRADE_RETCODE_DONE) {
             Print ("Failed to close position. ticket: ",
                    (string) ticket," result:", (string) trade. ResultRetcode (), ":", trade. CheckResultRetcodeDescription());
         }

      }
 
   }
   return true;

}

 
De Zhou Liu #:


RSI+MA  EA,MQL5,Compilation error free, but unable to place an order. Can any friend improve the code?



您好,例行提醒一下。


中文论坛,请使用中文提问和交流。

另外,请不要在没有关联的帖子提问。谢谢。


【如何防止机器人误判】讨论代码时请使用代码表述功能

https://www.mql5.com/zh/forum/448896

【如何防止机器人误判】讨论代码时请使用代码表述功能
【如何防止机器人误判】讨论代码时请使用代码表述功能
  • 2023.06.13
  • www.mql5.com
大家好,我是官网版主。 官网内部有机器人辅助管理,目的是自动下架一些有误导性的内容。 内容过长,或同一个IP多次注册,容易导致机器人误判,而被无辜删帖。 如果您被无故删帖,我们对这种体验感到万分抱歉。 为了防止机器人误判,请在讨论代码的时候使用代码表述功能。(如图) 感谢您的配合...
 

MQL已经可以说和C++差别不大,MQL有的C++也有。MQL可以说是C++的移植削减版本。MQL没有的,作为写EA你也没必要去学.如果你对编程有兴趣,先把MQL学了,如果有必要学C++(开发其它平台应用、深入操作系统方面的编程)再啃C++也不迟。

总之可以按MQL官方手册去学就行,它是面向小白编写的。如果MQL都啃不透,你也没有能力去啃C++

 
Junjie Shao:

如题。如果有必要先学一遍C++的话,请问需要学到什么程度够用?比如,读透并且掌握 《C++ Primer》是不是就够了呢?谢谢!

还是说,直接学MQL5即可?谢谢!

你的问题如同学汉语要不要先学广东话。
 
kalaaikala #:

请问潘神是什么意思啊?

Python
 

學MQL4/MQL5的語法就可以了。

要了解的東西不用很多,了解幾個關鍵詞就好,因為你日後會很常用到。

1.if and else

2.for and continue

3.break

4.return

5.Functions and parameter

6.variable

然後程式碼要寫的清楚,不是越短越好也不是越長越好,而是要讓自己好閱讀,讓你下次打開文本時立刻看得懂,這很重要。

多善用分治法,將條件儲存在變數中,別每次條件選擇時塞一堆函數在框框裡面,很難閱讀。

別急著寫程式,先想清楚後再寫出來,這很重要,因為寫不好,彈性差的程式碼,日後必定會一直修改,這就浪費自己許多的寶貴時間。

然後你一定會遇到許多不懂的內建函數,寫的時候邊查閱文檔就好,不用急著全部背下來。

當寫出來的功能不如預期時,請多善加利用Print()函數,將函數或變數給打印出來,了解函數返回的值或變數儲存的值。

如果你能做到以上這些事情,那你就是很厲害的開發者了,這些都很簡單但要有紀律有原則的去實施。