You are on page 1of 4

//+------------------------------------------------------------------+

//| Cruzamento Medias Moveis - Vers�o Final.mq4 |


//| Ricardo Augusto de Araujo |
//| ricardoaraujo112@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Ricardo Augusto de Araujo"
#property link "ricardoaraujo112@gmail.com"
#property version "1.00"
#property strict

//+------------------------------------------------------------------+
//| INPUTS( ENTRADAS) |
//+------------------------------------------------------------------+
extern int MAGICMA = 1;
extern int periodo_menor = 8;
extern int periodo_maior = 20;
extern double fator_exp_lote = 1.4;
extern double LoteEntrada = 0.01;
extern bool OperarSELL = true;
extern bool OperarBUY = true;
extern bool PararTrade = false;
extern int TakeProfit = 1000;
extern int StopLoss = 500;

//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()

{
checkForOpen(MAGICMA);
}
//+------------------------------------------------------------------+

bool sinal_media_moveis(int BUY_SELL)


{
bool retorno = false;
double media_menor = iMA(NULL,0, periodo_menor,0,MODE_SMA,PRICE_CLOSE,1);
double media_menor_ant = iMA(NULL,0, periodo_menor,0,MODE_SMA,PRICE_CLOSE,2);
double media_maior = iMA(NULL,0, periodo_maior,0,MODE_SMA,PRICE_CLOSE,1);
double media_maior_ant = iMA(NULL,0, periodo_maior,0,MODE_SMA,PRICE_CLOSE,2);

if(BUY_SELL==OP_SELL)
{
if((media_menor_ant > media_maior_ant)&&(media_menor < media_maior))
retorno = true;
else
retorno = false;

}
else if(BUY_SELL==OP_BUY)
{
if((media_menor_ant < media_maior_ant)&&(media_menor > media_maior))
retorno = true;
else
retorno = false;
}
return(retorno);
}

double total_ordem_aberta(int magicma_n, int BUY_SELL)


{
int totalordens = OrdersTotal();
int contador = 0;

for(int i= 0; i < totalordens; i++)


{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)==false)
break;

if ((OrderMagicNumber() == magicma_n) &&


(OrderType() == BUY_SELL))

contador++;

}
return (contador);
}

void checkForOpen(int Magicma_n)


{
int res;
double total_de_ordem_venda = total_ordem_aberta(Magicma_n, OP_SELL);
double total_de_ordem_compra = total_ordem_aberta(Magicma_n, OP_BUY);
double lote_ent = 0;

if (PararTrade == true)
{
if((total_de_ordem_venda ==0) &&(total_de_ordem_compra ==0))
return;
}
if(Volume[0] > 3) return;

if (IsTradeContextBusy()==true)
return;

RefreshRates();

if( OperarSELL == true)


{

if((sinal_media_moveis(OP_SELL)==true)&&(total_de_ordem_venda==0)&&(total_de_ordem_
compra==0))
{
lote_ent = get_lotes(LoteEntrada,Magicma_n, fator_exp_lote);
res=OrderSend(Symbol(),OP_SELL,lote_ent,Bid,150,Bid + (StopLoss * Point), Bid -
(TakeProfit * Point),"", Magicma_n,0, clrRed);
}
}
if(OperarBUY == true)
{

if((sinal_media_moveis(OP_BUY)==true)&&(total_de_ordem_compra==0)&&(total_de_ordem_
venda==0))
{
lote_ent = get_lotes(LoteEntrada,Magicma_n, fator_exp_lote);
res=OrderSend(Symbol(),OP_BUY,lote_ent,Ask,150,Ask - (StopLoss * Point), Ask +
(TakeProfit * Point),"", Magicma_n,0, clrGreen);
}

}
return;
}

double get_lotes(double lote_inicial,int MAGIC, double fator_incremento = 1)


{
double tamanho_lote = 0;
int maior_ticket = 0;
double Lote = 0;
double maior_lote = 0;
double Profit = 0;

for(int i=0 ; i < OrdersHistoryTotal() ; i++)


{
if( OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)== false)
break;

if((OrderMagicNumber()==MAGIC) &&
(OrderSymbol()==Symbol() ))

{
if(OrderTicket() > maior_ticket)
{
maior_ticket = OrderTicket();
maior_lote = NormalizeDouble(OrderLots(),2);
Profit = OrderProfit();

}
}

if (OrderSelect (maior_ticket, SELECT_BY_TICKET,MODE_HISTORY)==false)


return (lote_inicial);

if (maior_lote ==0)
tamanho_lote = lote_inicial;
else
{
if (Profit > 0)
tamanho_lote = lote_inicial;
else
tamanho_lote = maior_lote * fator_incremento;

}
Lote = NormalizeDouble (tamanho_lote, 2);

return (Lote);
}

You might also like