초보자는 프레젠테이션 환경에서 MQL5에서 SMA를 이용한 자동 매매를 시도했다
참고 자료
[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;
}
테스트 결과
이런 결과.
설명하다
하는 일은 황금십자가와 죽음십자가를 판정하고 황금십자가에서 사서 죽음십자가에서 파는 것이다.
완주 소감.
수정할 수 있을 것 같은 데서 지금 생각나는 게...
Reference
이 문제에 관하여(초보자는 프레젠테이션 환경에서 MQL5에서 SMA를 이용한 자동 매매를 시도했다), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/emilia/items/ca5c48ae2c9490fd0b7d텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)