183 lines
14 KiB
MQL5
183 lines
14 KiB
MQL5
//+------------------------------------------------------------------+
|
|
//| AntRobot.mq5 |
|
|
//| Copyright 2021, MetaQuotes Software Corp. |
|
|
//| https://www.mql5.com |
|
|
//+------------------------------------------------------------------+
|
|
#include <Trade\Trade.mqh>
|
|
#include "..\\utils\\Utils.mqh"
|
|
|
|
CTrade trade;
|
|
Utils utils;
|
|
|
|
input double Lots=0.1;
|
|
|
|
input int FEMA = 13;
|
|
input int MEMA = 26;
|
|
input int SEMA = 34;
|
|
|
|
input int FMA=12;
|
|
input int SMA=26;
|
|
input int SIGNAL=9;
|
|
input double CONFIRMATION_UMBRAL=10;
|
|
|
|
input int stopLoss=200;
|
|
input int takeProfit=500;
|
|
|
|
int indicatorHandle=0;
|
|
int indicatorTradeConfirmHandler=0;
|
|
int currentSignal=0;
|
|
int currentConfirmedSignal=0;
|
|
bool isFirstSignal=true;
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| Expert initialization function |
|
|
//+------------------------------------------------------------------+
|
|
int OnInit() {
|
|
ResetLastError();
|
|
string path = utils.GetRelativeProgramPath();
|
|
// indicatorHandle=iCustom(Symbol(),PERIOD_CURRENT,path + "\\..\\indicators\\ICrossAverage",FEMA,MEMA,SEMA);
|
|
// indicatorTradeConfirmHandler=iCustom(Symbol(),PERIOD_CURRENT,path + "\\..\\indicators\\ITrendConfirmation",FMA,SMA,SIGNAL,CONFIRMATION_UMBRAL);
|
|
// if(indicatorHandle==INVALID_HANDLE) {
|
|
// Print("CrossAverage error inicialization, Code = ",GetLastError());
|
|
// return -1;
|
|
// }
|
|
ChartSetInteger(0,CHART_COLOR_BACKGROUND,0);
|
|
ChartSetInteger(0, CHART_FOREGROUND,16777215);
|
|
ChartSetInteger(0,CHART_COLOR_CHART_UP,7451452);
|
|
ChartSetInteger(0,CHART_COLOR_CHART_DOWN,255);
|
|
ChartSetInteger(0,CHART_COLOR_CANDLE_BULL,7451452);
|
|
ChartSetInteger(0,CHART_COLOR_CANDLE_BEAR,255);
|
|
ChartSetInteger(0,CHART_COLOR_CHART_LINE,65280);
|
|
ChartSetInteger(0,CHART_COLOR_GRID,2697513);
|
|
ChartSetInteger(0,CHART_SHIFT,1);
|
|
ChartSetInteger(0,CHART_SHOW_BID_LINE,1);
|
|
ChartSetInteger(0,CHART_SHOW_LAST_LINE,1);
|
|
|
|
return(INIT_SUCCEEDED);
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Expert deinitialization function |
|
|
//+------------------------------------------------------------------+
|
|
void OnDeinit(const int reason) {
|
|
//if(indicatorHandle!=INVALID_HANDLE) {
|
|
// IndicatorRelease(indicatorHandle);
|
|
// }
|
|
// if(indicatorTradeConfirmHandler!=INVALID_HANDLE) {
|
|
// IndicatorRelease(indicatorTradeConfirmHandler);
|
|
// }
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Expert tick function |
|
|
//+------------------------------------------------------------------+
|
|
void OnTick() {
|
|
if(!checkIfNewBar())
|
|
return;
|
|
|
|
// currentSignal=getTrendSignal();
|
|
// currentConfirmedSignal=getTradeConfirmationSignal();
|
|
|
|
if(isFirstSignal) {
|
|
isFirstSignal=false;
|
|
}
|
|
|
|
// if(PositionSelect(Symbol())==true && checkPositionClose(currentSignal, currentConfirmedSignal)==1)
|
|
// return;
|
|
|
|
// if(checkBuySignal(currentSignal, currentConfirmedSignal)==1) {
|
|
double ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
|
|
double takeProfitLevel = ask + takeProfit*Point();
|
|
double stopLossLevel = ask - stopLoss*Point();
|
|
trade.Buy(Lots,_Symbol,ask,stopLossLevel,takeProfitLevel,NULL);
|
|
// }
|
|
|
|
// if(checkIfSellSignal(currentSignal,currentConfirmedSignal)==1) {
|
|
// double bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
|
|
// double takeProfitLevel = bid - takeProfit*Point();
|
|
// double stopLossLevel = bid + stopLoss*Point();
|
|
// trade.Sell(Lots,_Symbol,bid,stopLossLevel,takeProfitLevel,NULL);
|
|
// }
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
int checkIfNewBar() {
|
|
MqlRates rates[1];
|
|
ResetLastError();
|
|
|
|
if(CopyRates(Symbol(),Period(),0,1,rates)!=1) {
|
|
Print("CopyRates error de copia, Code = ",GetLastError());
|
|
return false;
|
|
}
|
|
if(rates[0].tick_volume>1) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
int getTrendSignal() {
|
|
double trendDirection[1];
|
|
ResetLastError();
|
|
if(CopyBuffer(indicatorHandle,0,0,1,trendDirection)!=1) {
|
|
Print("CopyBuffer error de copia, Code = ",GetLastError());
|
|
return 0;
|
|
}
|
|
return((int)trendDirection[0]);
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
int getTradeConfirmationSignal() {
|
|
double tradeConfirm[1];
|
|
ResetLastError();
|
|
if(CopyBuffer(indicatorTradeConfirmHandler,2,0,1,tradeConfirm)!=1) {
|
|
Print("CopyBuffer error de copia, Code = ",GetLastError());
|
|
return 0;
|
|
}
|
|
return((int)tradeConfirm[0]);
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
int checkIfSellSignal(int currSignal,int confirmedSignal) {
|
|
if(currSignal==-1 && confirmedSignal==-1 ) {
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
int checkBuySignal(int currSignal,int confirmedSignal) {
|
|
if(currentSignal==1 && confirmedSignal==1) {
|
|
return(1);
|
|
}
|
|
return(0);
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
|
|
//---------------------------------------------------------------------
|
|
// Comprueba si necesitamos cerrar la posición:
|
|
//---------------------------------------------------------------------
|
|
// retornos:
|
|
// 0 - ninguna posición abierta
|
|
// 1 - posición ya esta abierta en la dirección de la señal
|
|
//---------------------------------------------------------------------
|
|
int checkPositionClose(int signal, int confirmedSignal) {
|
|
long positionType=PositionGetInteger(POSITION_TYPE);
|
|
//if(confirmedSignal==0) {
|
|
// trade.PositionClose(Symbol(),10);
|
|
// return 0;
|
|
//}
|
|
if(signal == 1 && confirmedSignal!=-1 && positionType==(long)POSITION_TYPE_BUY) {
|
|
return 1;
|
|
}
|
|
if(signal == -1 && confirmedSignal!=1 && positionType==(long)POSITION_TYPE_SELL) {
|
|
return 1;
|
|
}
|
|
trade.PositionClose(Symbol(),10);
|
|
return 0;
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|