359 lines
No EOL
35 KiB
MQL5
359 lines
No EOL
35 KiB
MQL5
//+------------------------------------------------------------------+
|
|
//| SuperCharlie_Aldridge_Dev_v0.mq5|
|
|
//| HEDGING HORN CAPITAL |
|
|
//| https://www.hhcapital.com.br |
|
|
//+------------------------------------------------------------------+
|
|
#property copyright "HEDGING HORN CAPITAL"
|
|
#property link "https://www.hhcapital.com.br"
|
|
#property version "DEV_v0"
|
|
#property description "INDICE"
|
|
//INDICE
|
|
|
|
//+===================================================================================================================================================================================+
|
|
//| 1 - SESSAO DE CABECALHO / ADMISSAO
|
|
//+===================================================================================================================================================================================+
|
|
//+------------------------------------------------------------------+
|
|
//| 1.1 - IMPORTACOES |
|
|
//+------------------------------------------------------------------+
|
|
#include "MarketBook.mqh"
|
|
#include "S1_Admissao1_v0.mqh"
|
|
#include "S5_Direcao1_v0.mqh"
|
|
|
|
//status 0 (backlog) -> status 1 (pendente)
|
|
|
|
//status 1 (pendente & sem alteração) -> status 1 (pendente & sem alteração)
|
|
//status 1 (pendente & sem alteração) -> status 2 (pendente & precisa de alteração)
|
|
//status 1 (pendente & sem alteração) -> status 3 (filled & sem alteração)
|
|
//********status 1 (pendente) -> status 5 (cancelada/reset)********
|
|
|
|
//status 2 (pendente & precisa de alteração) -> status 1 (pendente & sem alteração)
|
|
//status 2 (pendente & precisa de alteração) -> status 3 (filled & sem alteração)
|
|
|
|
//status 3 (filled) -> status 3 (atualiza valores de stop e alvo novos)
|
|
|
|
//status 4 (filled & não precisa de alteração) -> status 4 (filled & não precisa de alteração)
|
|
//status 4 (filled & não precisa de alteração) -> status 5 (filled & precisa de alteração)
|
|
//status 4 (filled & não precisa de alteração) -> status 6 (vencido)
|
|
|
|
//status 5 (filled & precisa de alteração) -> status 5 (filled & precisa de alteração)
|
|
|
|
//status 6 (vencido) -> status 0 (backlog)
|
|
|
|
|
|
//+===================================================================================================================================================================================+
|
|
//| 4 - SESSAO DE QUANTIDADES / CAMBIO
|
|
//+===================================================================================================================================================================================+
|
|
//+------------------------------------------------------------------+
|
|
//| 4.1 - FUNCAO QUE GERENCIA OS STATUS DOS SLOTS |
|
|
//+------------------------------------------------------------------+
|
|
void GerenciaSlots(int magic, int tickMin, int rajada, fluido &Fluidos, slot &SlotsC[], slot &SlotsV[], posicao &SlotPos, CSymbolInfo &simbolo, MqlTradeRequest &request, MqlTradeResult &result, MqlTradeCheckResult &check_result)
|
|
{
|
|
//Varre os slots de compra
|
|
for(int i=0; i<ArraySize(SlotsC); i++)
|
|
{
|
|
// Print("Posicoes: ", PositionsTotal());
|
|
//Para cada status do slot de compra ele faz: X
|
|
switch(SlotsC[i].status)
|
|
{
|
|
//Preencha os precos e qtd para enviar uma ordem (Backlog) - Vermelho
|
|
case 0 :
|
|
VerificaPreco(tickMin, i, Fluidos, 0, SlotsC, SlotPos, false);
|
|
VerificaQuantidade(rajada, i, SlotsC);
|
|
break;
|
|
|
|
//Slot enviado e nao precisa de alteracao (Pendente estável) - Amarelo
|
|
case 1 :
|
|
//Seleciona uma ordem
|
|
OrderSelect(SlotsC[i].idOrdem);
|
|
|
|
//Verifica se e uma ordem do robo e se esta pendente
|
|
if((OrderGetInteger(ORDER_MAGIC) == magic) && (OrderGetInteger(ORDER_STATE) == ORDER_STATE_PLACED))
|
|
{
|
|
//Armazena o estado da ordem no slot correspondente
|
|
SlotsC[i].state = OrderGetInteger(ORDER_STATE);
|
|
|
|
//Verifica se houve alteracao estatistica (media, desvio) e manda para o status 2
|
|
if((Fluidos.driftLFTTend > 200) || (Fluidos.driftLFTTend < -200) || (Fluidos.driftLFTVol > 200) || (Fluidos.driftLFTVol < -200))
|
|
{
|
|
VerificaPreco(tickMin, i, Fluidos, 0, SlotsC, SlotPos, false);
|
|
VerificaQuantidade(rajada, i, SlotsC);
|
|
SlotsC[i].status = 2;
|
|
} //senao mantem no status atual
|
|
else SlotsC[i].status = 1;
|
|
|
|
}
|
|
//Verifica se e uma ordem do robo e se esta executada
|
|
else if((HistoryOrderGetInteger(SlotsC[i].idOrdem, ORDER_MAGIC) == magic) && (HistoryOrderSelect(SlotsC[i].idOrdem) == true))
|
|
{
|
|
//Se acabou de ser executada, entao mande uma saida na ponta contraria como alvo, e manda para o status 3
|
|
if(SlotsC[i].state != ORDER_STATE_FILLED)
|
|
{
|
|
SlotsC[i].state = ORDER_STATE_FILLED;
|
|
PositionSelect(_Symbol);
|
|
SlotPos.idPosicao = PositionGetInteger(POSITION_TICKET);
|
|
SlotPos.ponta = PositionGetInteger(POSITION_TYPE);
|
|
SlotPos.precoMedio = PositionGetDouble(POSITION_PRICE_OPEN);
|
|
SlotPos.qtd = PositionGetDouble(POSITION_VOLUME);
|
|
SlotsC[i].status = 3;
|
|
// Print("Compra virou status 1->3");
|
|
}
|
|
|
|
}
|
|
break;
|
|
|
|
//Slot enviado e precisa de alteracao (Pendente alterar) - Amarelo
|
|
case 2 :
|
|
//Seleciona uma ordem
|
|
OrderSelect(SlotsC[i].idOrdem);
|
|
|
|
//Verifica se e uma ordem do robo e se esta executada
|
|
if((HistoryOrderGetInteger(SlotsC[i].idOrdem, ORDER_MAGIC) == magic) && (HistoryOrderSelect(SlotsC[i].idOrdem) == true))
|
|
{
|
|
//Se acabou de ser executada, entao mande uma saida na ponta contraria como alvo, e manda para o status 3
|
|
if(SlotsC[i].state != ORDER_STATE_FILLED)
|
|
{
|
|
SlotsC[i].state = ORDER_STATE_FILLED;
|
|
PositionSelect(_Symbol);
|
|
SlotPos.idPosicao = PositionGetInteger(POSITION_TICKET);
|
|
SlotPos.ponta = PositionGetInteger(POSITION_TYPE);
|
|
SlotPos.precoMedio = PositionGetDouble(POSITION_PRICE_OPEN);
|
|
SlotPos.qtd = PositionGetDouble(POSITION_VOLUME);
|
|
SlotsC[i].status = 3;
|
|
// Print("Compra virou status 2->3");
|
|
}
|
|
|
|
}
|
|
break;
|
|
//Slot executado e verifica o novo alvo e stop (Executado novo) - Verde
|
|
case 3 :
|
|
//Atualiza valores de alvo e stop inicial da ordem
|
|
VerificaPreco(tickMin, i, Fluidos, 0, SlotsC, SlotPos, true);
|
|
VerificaQuantidade(rajada, i, SlotsC);
|
|
for(int i=PositionsTotal()-1; i>=0; i--)
|
|
{
|
|
ulong ticket = PositionGetTicket(i);
|
|
//Verifica se e uma ordem do robo e se esta pendente
|
|
if((PositionGetInteger(POSITION_MAGIC) == magic) && (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY))
|
|
{
|
|
SlotsC[i].idOrdem = PositionGetInteger(POSITION_TICKET);
|
|
}
|
|
}
|
|
break;
|
|
//Slot executado e nao precisa de alteracao (Executado e estavel) - Verde
|
|
case 4 :
|
|
//Seleciona uma ordem
|
|
if(PositionSelect(_Symbol)==true)
|
|
{
|
|
PositionSelect(_Symbol);
|
|
if((PositionGetInteger(POSITION_MAGIC) == magic) && (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY))
|
|
{
|
|
//Verifica se houve alteracao estatistica (media, desvio) e manda para o status 2
|
|
if(true)
|
|
{
|
|
SlotsC[i].status = 5;
|
|
Print("Compra virou status 4->5");
|
|
} //senao mantem no status atual
|
|
else
|
|
{
|
|
SlotsC[i].status = 4;
|
|
// Print("Compra virou status 4->4");
|
|
}
|
|
}
|
|
|
|
if(PositionGetInteger(POSITION_TICKET) != SlotsC[i].idOrdem)
|
|
{
|
|
//ordem nao existe mais -> case 6
|
|
SlotsC[i].status = 6;
|
|
// Print("Compra virou status 4->6");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
SlotsC[i].status = 6;
|
|
// Print("Compra virou status 4->6");
|
|
}
|
|
|
|
break;
|
|
//Slot executado e precisa de alteração - Verde
|
|
case 5:
|
|
break;
|
|
//Slot vencido e precisa ser resetado - Azul
|
|
case 6 :
|
|
SlotsC[i].idOrdem = 0;
|
|
SlotsC[i].ponta = -1;
|
|
SlotsC[i].precoAlvo = -1;
|
|
SlotsC[i].precoEnvio = -1;
|
|
SlotsC[i].precoStop = -1;
|
|
SlotsC[i].qtd = 0;
|
|
SlotsC[i].status = 0;
|
|
break;
|
|
} //Termino do switch de compra
|
|
|
|
switch(SlotsV[i].status)
|
|
{
|
|
//Preencha os precos e qtd para enviar uma ordem (Backlog) - Vermelho
|
|
case 0 :
|
|
VerificaPreco(tickMin, i, Fluidos, 1, SlotsV, SlotPos, false);
|
|
VerificaQuantidade(rajada, i, SlotsV);
|
|
break;
|
|
|
|
//slot enviado e nao precisa de alteracao (Pendente estável) - Amarelo
|
|
case 1 :
|
|
//Seleciona uma ordem
|
|
OrderSelect(SlotsV[i].idOrdem);
|
|
|
|
//Verifica se e uma ordem do robo e se esta pendente
|
|
if((OrderGetInteger(ORDER_MAGIC) == magic) && (OrderGetInteger(ORDER_STATE) == ORDER_STATE_PLACED))
|
|
{
|
|
//Armazena o estado da ordem no slot correspondente
|
|
SlotsV[i].state = OrderGetInteger(ORDER_STATE);
|
|
|
|
//Verifica se houve alteracao estatistica (media, desvio) e manda para o status 2
|
|
if((Fluidos.driftLFTTend > 200) || (Fluidos.driftLFTTend < -200) || (Fluidos.driftLFTVol > 200) || (Fluidos.driftLFTVol < -200))
|
|
{
|
|
VerificaPreco(tickMin, i, Fluidos, 1, SlotsV, SlotPos, false);
|
|
VerificaQuantidade(rajada, i, SlotsV);
|
|
SlotsV[i].status = 2;
|
|
} //senao mantem no status atual
|
|
else SlotsV[i].status = 1;
|
|
|
|
} //Verifica se e uma ordem do robo e se esta executada
|
|
else if((HistoryOrderGetInteger(SlotsC[i].idOrdem, ORDER_MAGIC) == magic) && (HistoryOrderSelect(SlotsV[i].idOrdem) == true))
|
|
{
|
|
//Se acabou de ser executada, entao mande uma saida na ponta contraria como alvo, e manda para o status 3
|
|
if(SlotsV[i].state != ORDER_STATE_FILLED)
|
|
{
|
|
SlotsV[i].state = ORDER_STATE_FILLED;
|
|
PositionSelect(_Symbol);
|
|
SlotPos.idPosicao = PositionGetInteger(POSITION_TICKET);
|
|
SlotPos.ponta = PositionGetInteger(POSITION_TYPE);
|
|
SlotPos.precoMedio = PositionGetDouble(POSITION_PRICE_OPEN);
|
|
SlotPos.qtd = PositionGetDouble(POSITION_VOLUME);
|
|
SlotsV[i].status = 3;
|
|
// Print("Venda virou status 1->3");
|
|
}
|
|
|
|
}
|
|
break;
|
|
|
|
//Slot enviado e precisa de alteracao (Pendente alterar) - Amarelo
|
|
case 2 :
|
|
//Seleciona uma ordem
|
|
OrderSelect(SlotsV[i].idOrdem);
|
|
|
|
//Verifica se e uma ordem do robo e se esta executada
|
|
if((HistoryOrderGetInteger(SlotsC[i].idOrdem, ORDER_MAGIC) == magic) && (HistoryOrderSelect(SlotsV[i].idOrdem) == true))
|
|
{
|
|
//Se acabou de ser executada, entao mande uma saida na ponta contraria como alvo, e manda para o status 3
|
|
if(SlotsV[i].state != ORDER_STATE_FILLED)
|
|
{
|
|
SlotsV[i].state = ORDER_STATE_FILLED;
|
|
PositionSelect(_Symbol);
|
|
SlotPos.idPosicao = PositionGetInteger(POSITION_TICKET);
|
|
SlotPos.ponta = PositionGetInteger(POSITION_TYPE);
|
|
SlotPos.precoMedio = PositionGetDouble(POSITION_PRICE_OPEN);
|
|
SlotPos.qtd = PositionGetDouble(POSITION_VOLUME);
|
|
SlotsV[i].status = 3;
|
|
// Print("Venda virou status 2->3");
|
|
}
|
|
|
|
}
|
|
break;
|
|
//Slot executado e verifica o novo alvo e stop (Executado novo) - Verde
|
|
case 3 :
|
|
//atualiza valores de alvo e stop inicial da ordem
|
|
VerificaPreco(tickMin, i, Fluidos, 1, SlotsV, SlotPos, true);
|
|
VerificaQuantidade(rajada, i, SlotsV);
|
|
for(int i=PositionsTotal()-1; i>=0; i--)
|
|
{
|
|
ulong ticket = PositionGetTicket(i);
|
|
//Verifica se e uma ordem do robo e se esta pendente
|
|
if((PositionGetInteger(POSITION_MAGIC) == magic) && (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY))
|
|
{
|
|
SlotsV[i].idOrdem = PositionGetInteger(POSITION_TICKET);
|
|
}
|
|
}
|
|
break;
|
|
//Slot executado e nao precisa de alteracao (Executado e estavel) - Verde
|
|
case 4 :
|
|
//Seleciona uma ordem
|
|
if(PositionSelect(_Symbol)==true)
|
|
{
|
|
PositionSelect(_Symbol);
|
|
if((PositionGetInteger(POSITION_MAGIC) == magic) && (PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY))
|
|
{
|
|
//Verifica se houve alteracao estatistica (media, desvio) e manda para o status 2
|
|
if(true)
|
|
{
|
|
//regra de atualiza preco
|
|
SlotsV[i].status = 5;
|
|
// Print("Venda modificada 4->5");
|
|
} //senao mantem no status atual
|
|
else SlotsV[i].status = 4;
|
|
}
|
|
|
|
if(PositionGetInteger(POSITION_TICKET) != SlotsC[i].idOrdem)
|
|
{
|
|
//ordem nao existe mais -> case 6
|
|
SlotsV[i].status = 6;
|
|
// Print("Venda modificada 4->6");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
SlotsV[i].status = 6;
|
|
}
|
|
|
|
break;
|
|
//slot executado e precisa de alteração - Verde
|
|
case 5:
|
|
break;
|
|
//Slot vencido e precisa ser resetado - Azul
|
|
case 6 :
|
|
SlotsV[i].idOrdem = 0;
|
|
SlotsV[i].ponta = -1;
|
|
SlotsV[i].precoAlvo = -1;
|
|
SlotsV[i].precoEnvio = -1;
|
|
SlotsV[i].precoStop = -1;
|
|
SlotsV[i].qtd = 0;
|
|
SlotsV[i].status = 0;
|
|
break;
|
|
}
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| 4.2 - FUNCAO QUE CALCULA A QUANTIDADE A SER ENVIADA |
|
|
//+------------------------------------------------------------------+
|
|
int VerificaQuantidade(int rajada, int i, slot &Slots[])
|
|
{
|
|
int retorno = 0;
|
|
|
|
CalculaQuantidade(rajada, i, Slots);
|
|
|
|
return retorno;
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| 4.3 - FUNCAO QUE CALCULA A QUANTIDADE MAXIMA NAQUELE PRECO |
|
|
//+------------------------------------------------------------------+
|
|
int CalculaQuantidade(int rajada, int i, slot &Slots[])
|
|
{
|
|
int config = 1;
|
|
|
|
config = 1;
|
|
|
|
if(config == 1)
|
|
{
|
|
Slots[i].qtd = rajada;
|
|
}
|
|
|
|
return config;
|
|
}
|
|
|
|
//+===================================================================================================================================================================================+
|
|
//| FIM DO PROGRAMA
|
|
//+===================================================================================================================================================================================+ |