초보자는 프레젠테이션 환경에서 MQL5에서 SMA를 이용한 자동 매매를 시도했다

14057 단어 FinancefintechMQL5
메타트레이더 5로 해봤어요.인터넷상에서 MQL4의 정보가 매우 많아서 MQL5의 정보를 조사하는 것은 상당히 힘들다.메타트레이더 5에서 샘플이 발견되면 그쪽을 참고하면서 작업해요.샘플에는 많은 표준적인 논리가 있는 것 같아서 이해하기 쉽고 배워야 할 예감이 든다.

참고 자료


[MQL] 골든크로스와 데스크로스로 출전한 EA[초급편]
MQL4의 보도입니다. 하지만 간단한 기술이기 때문에 참고하기 쉽습니다.

코드


사용하지 않은 속성이 있지만 월대두를 용서해 주세요.그리고 발췌문.
input int      TakeProfit=5;
input int      StopLoss=5;
input double   Lots=0.1;
input int      MagicNumber=12345;

input int FastMAPeriod = 20;  //短期移動平均の期間
input int SlowMAPeriod = 50;  //長期移動平均の期間

int FastMAHandle;
int SlowMAHandle;
CTrade ExtTrade;

string my_symbol = "USDJPY";

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   Comment("OnInit");
//--- create timer
   EventSetTimer(60);

//---
   ExtTrade.SetExpertMagicNumber(MagicNumber);
   ExtTrade.SetMarginMode();
   ExtTrade.SetTypeFillingBySymbol(Symbol());

   //SMA
   FastMAHandle = iMA(Symbol(), Period(), FastMAPeriod, 0, MODE_SMA, PRICE_CLOSE);
   if(FastMAHandle==INVALID_HANDLE)
   {
      PrintFormat("FastMAHandle: Failed to create handle of the iMA indicator for the symbol %s/%s, error code %d",
                  Symbol(),
                  EnumToString(Period()),
                  GetLastError());
      //--- the indicator is stopped early
      return(INIT_FAILED);
   }
   SlowMAHandle = iMA(Symbol(), Period(), SlowMAPeriod, 0, MODE_SMA, PRICE_CLOSE);
   if(SlowMAHandle==INVALID_HANDLE)
   {
      PrintFormat("SlowMAHandle: Failed to create handle of the iMA indicator for the symbol %s/%s, error code %d",
                  Symbol(),
                  EnumToString(Period()),
                  GetLastError());
      //--- the indicator is stopped early
      return(INIT_FAILED);
   }
   return(INIT_SUCCEEDED);
}

void OnTick()
{
//---
   Comment("OnTick");

   double FastMA[];
   double SlowMA[];
   ArraySetAsSeries(FastMA,true);
   ArraySetAsSeries(SlowMA,true);

   int start_pos=0,count=2;
   if(!CopyBuffer(FastMAHandle,0,start_pos,count,FastMA))
      return;
   if(!CopyBuffer(SlowMAHandle,0,start_pos,count,SlowMA))
      return;

   if(SelectPosition())
      CheckForClose(FastMA, SlowMA);
   else
      CheckForOpen(FastMA, SlowMA);
}

void CheckForClose(double &FastMA[], double &SlowMA[])
{
   bool signal = false;
   if(FastMA[1] >= SlowMA[1] && FastMA[0] < SlowMA[0])
      signal = true;

   if(signal && TerminalInfoInteger(TERMINAL_TRADE_ALLOWED))
      ExtTrade.PositionClose(_Symbol,3);
}

void CheckForOpen(double &FastMA[], double &SlowMA[])
{
   ENUM_ORDER_TYPE signal = WRONG_VALUE;
   if(FastMA[1] <= SlowMA[1] && FastMA[0] > SlowMA[0])
      signal=ORDER_TYPE_BUY;

   if(signal!=WRONG_VALUE && TerminalInfoInteger(TERMINAL_TRADE_ALLOWED))
   {
      bool err = ExtTrade.PositionOpen(_Symbol, signal, Lots,
                               SymbolInfoDouble(_Symbol,signal==ORDER_TYPE_SELL ? SYMBOL_BID:SYMBOL_ASK),
                               0,0);
      Print(err);
   }
}

bool SelectPosition()
{
   if(PositionSelect(_Symbol))
      return(PositionGetInteger(POSITION_MAGIC)==MagicNumber);

   return false;
}

테스트 결과



이런 결과.

설명하다


하는 일은 황금십자가와 죽음십자가를 판정하고 황금십자가에서 사서 죽음십자가에서 파는 것이다.

완주 소감.


수정할 수 있을 것 같은 데서 지금 생각나는 게...
  • 감손과 이욕에 사로잡힌 설정
  • 회 계산
  • 추가범위 시세시와 비시 판정
  • 손해와 이욕에 사로잡힌 설정에는 정지 등급의 문제가 수반되는 것 같다.잘 모릅니다 (참조: 잘못된 원인과 대책

    좋은 웹페이지 즐겨찾기