168 lines
12 KiB
MQL5
168 lines
12 KiB
MQL5
//+------------------------------------------------------------------+
|
|
//| Nono.mq5 |
|
|
//| Copyright 2022, MetaQuotes Ltd. |
|
|
//| https://www.mql5.com |
|
|
//+------------------------------------------------------------------+
|
|
#property copyright "Copyright 2022, MetaQuotes Ltd."
|
|
#property link "https://www.mql5.com"
|
|
#property version "1.00"
|
|
//+------------------------------------------------------------------+
|
|
//| Expert initialization function |
|
|
//+------------------------------------------------------------------+
|
|
#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 stopLoss = 200;
|
|
input int takeProfit = 500;
|
|
|
|
int indicatorHandle = 0;
|
|
int currentSignal = 0;
|
|
bool isFirstSignal = true;
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
int OnInit() {
|
|
ResetLastError();
|
|
string path = utils.GetRelativeProgramPath();
|
|
indicatorHandle = iCustom(Symbol(), PERIOD_CURRENT, path + "\\..\\indicators\\ICrossAverage",FEMA,MEMA,SEMA);
|
|
|
|
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);
|
|
}
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Expert tick function |
|
|
//+------------------------------------------------------------------+
|
|
void OnTick() {
|
|
|
|
if(!checkIfNewBar())
|
|
return;
|
|
|
|
currentSignal=getTrendSignal();
|
|
|
|
if(isFirstSignal) {
|
|
isFirstSignal=false;
|
|
}
|
|
|
|
if(PositionSelect(Symbol())==true && checkPositionClose(currentSignal)==1)
|
|
return;
|
|
|
|
if(checkBuySignal(currentSignal)==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)==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;
|
|
}
|
|
//Print("CopyBuffer value = ",trendDirection[0]);
|
|
int direction = trendDirection[0];
|
|
return((int)trendDirection[0]);
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
int checkIfSellSignal(int currSignal) {
|
|
if(currSignal==-1) {
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
int checkBuySignal(int currSignal) {
|
|
if(currentSignal==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) {
|
|
long positionType=PositionGetInteger(POSITION_TYPE);
|
|
|
|
if(signal == 1 && positionType==(long)POSITION_TYPE_BUY) {
|
|
return 1;
|
|
}
|
|
if(signal == -1 && positionType==(long)POSITION_TYPE_SELL) {
|
|
return 1;
|
|
}
|
|
trade.PositionClose(Symbol(),10);
|
|
return 0;
|
|
}
|
|
//+------------------------------------------------------------------+
|