Crashautomatico
Crashautomatico
//| MM_CROS_IFR.mq5 |
//| Copyright 2018. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "rafaelfvcs. Copyright 2018."
#property link "https://www.mql5.com"
#property version "1.00"
#include <Trade\Trade.mqh>
CTrade trade;
//---
enum STRATEGY_IN
{
ONLY_RSI, // Only RSI
ONLY_MA, // Only moving averages
MA_AND_RSI // moving averages plus RSI
};
//---
// Variables Input
sinput string s0; //-----------Strategy-------------
input STRATEGY_IN strategy = ONLY_MA; // Trader Entry
Strategy
int Opeb=0;
int Opes=0;
int OpesRSI1=0; // contador operaciones abiertas RSI 90
int OpesRSI2=0; // contador operaciones abiertas RSI más alto del día
int totalT=0;
int a=0;
int b=0;
int c=0;
int cruceA=0;
int tiempo=0;
int gananciaB =5; //ganancia buy diferentes de 0.5)
int gananciaS =5; //ganancia sell
int ganancia1 =1; //ganancia buy 0.5
double stop=0;
//+------------------------------------------------------------------+
//| Variables for indicators |
//+------------------------------------------------------------------+
//--- Moving Averages
// FASTER - shorter period
int ma_faster_Handle; // Handle Fast Moving Average
double ma_faster_Buffer[]; // Buffer Fast Moving Average
//--- RSI
int rsi_Handle; // Handle for RSI
double rsi_Buffer[]; // Buffer for RSI
//+------------------------------------------------------------------+
//| Variables for functions |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
ma_faster_Handle =
iMA(_Symbol,ma_time_graphic,ma_faster_period,0,ma_method,ma_price);
ma_fast_Handle =
iMA(_Symbol,ma_time_graphic,ma_fast_period,0,ma_method,ma_price);
ma_slow_Handle =
iMA(_Symbol,ma_time_graphic,ma_slow_period,0,ma_method,ma_price);
rsi_Handle = iRSI(_Symbol,rsi_time_graphic,rsi_period,rsi_price);
CopyRates(_Symbol,_Period,0,4,candle);
ArraySetAsSeries(candle,true);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
IndicatorRelease(ma_faster_Handle);
IndicatorRelease(ma_fast_Handle);
IndicatorRelease(ma_slow_Handle);
IndicatorRelease(rsi_Handle);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
if(Opeb==0){
b=0;
gananciaB=3;
}
if(Opes==0){
c=0;
gananciaS=3;
OpesRSI1=0;
OpesRSI2=0;
}
totalT = PositionsTotal();
for(int i=totalT-1 ; i>=0 ; i--){
ulong ticket = PositionGetTicket(i);
if(ticket<=0){Print("Failed to open posistion ticket"); }
if(!PositionSelectByTicket(ticket)){Print("Failed to select
position");}
long type;
double lotaje;
double profit;
if(!PositionGetInteger(POSITION_TYPE,type)){Print("Failed to
get position types");}
if(!PositionGetDouble(POSITION_PROFIT,profit)){Print("Failed
to get position profit");}
if(!PositionGetDouble(POSITION_VOLUME,lotaje)){Print("Failed
to get position profit");}
// modificacion sl buy
if(type==0 && profit>gananciaB && lotaje!=0.5){
SL2=((gananciaB*2000)-(SL1*b));
double sl = NormalizeDouble(tick.bid - SL2*_Point,_Digits);
trade.PositionModify(ticket,sl,0);
b++;
gananciaB=gananciaB+3;
}
// modificacion sl sell
if(type==1 && profit>gananciaS){
SL3=((gananciaS*2000)-(SL1*c));
double sl = NormalizeDouble(tick.bid + SL3*_Point,_Digits);
trade.PositionModify(ticket,sl,0);
c++;
gananciaS=gananciaS+3;
//---
// Copy a three-dimensional data vector to Buffer
CopyBuffer(ma_faster_Handle,0,0,4,ma_faster_Buffer);
CopyBuffer(ma_fast_Handle,0,0,4,ma_fast_Buffer);
CopyBuffer(ma_slow_Handle,0,0,4,ma_slow_Buffer);
CopyBuffer(rsi_Handle,0,0,4,rsi_Buffer);
Comment("\nb ", b,
"\nc ", c,
"\nopeb ", Opeb,
"\nGanancia ", gananciaB,
"\nGanancia1 ", ganancia1 );
if(strategy == ONLY_MA)
{
Buy = buy_ma_cros;
Sell = sell_ma_cros;
}
else if(strategy == ONLY_RSI)
{
Buy = buy_rsi;
Sell = sell_rsi;
}
else
{
Buy = buy_ma_cros && buy_rsi;
Sell = sell_ma_cros && sell_rsi;
}
// Sell Condition:
if(rsi_Buffer[0]>RSI1 && OpesRSI1<1 )
{
drawVerticalLine("Sell",candle[1].time,clrRed);
SellAtMarket();
OpesRSI1++;
}
if(rsi_Buffer[0]>RSI2 && OpesRSI2<1 )
{
drawVerticalLine("Sell",candle[1].time,clrRed);
SellAtMarket();
OpesRSI2++;
}
tiempo++;
}
//+------------------------------------------------------------------+
//| FUNCTIONS TO ASSIST IN THE VISUALIZATION OF THE STRATEGY |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| FUNCTIONS FOR SENDING ORDERS |
//+------------------------------------------------------------------+
// BUY TO MARKET
void BuyAtMarket()
{
MqlTradeRequest request; // request
MqlTradeResult response; // response
ZeroMemory(request);
ZeroMemory(response);
//---
OrderSend(request,response);
//---
if(response.retcode == 10008 || response.retcode == 10009)
{
Print("Order Buy executed successfully!!");
}
else
{
Print("Error sending Order to Buy. Error = ", GetLastError());
ResetLastError();
}
// SELL TO MARKET
void SellAtMarket()
{
MqlTradeRequest request; // request
MqlTradeResult response; // response
ZeroMemory(request);
ZeroMemory(response);
ZeroMemory(request);
ZeroMemory(response);
//---
OrderSend(request,response);
//---
if(response.retcode == 10008 || response.retcode == 10009)
{
Print("Order to Sell executed successfully!");
}
else
{
Print("Error sending Order to Sell. Error =", GetLastError());
ResetLastError();
}
}
void CloseSell()
{
MqlTradeRequest request; // request
MqlTradeResult response; // response
ZeroMemory(request);
ZeroMemory(response);
//---
OrderSend(request,response);
//---
if(response.retcode == 10008 || response.retcode == 10009)
{
Print("Order Buy executed successfully!!");
}
else
{
Print("Error sending Order to Buy. Error = ", GetLastError());
ResetLastError();
}
}*/
//+------------------------------------------------------------------+
//| USEFUL FUNCTIONS |
//+------------------------------------------------------------------+
//--- for bar change
bool isNewBar()
{
//--- memorize the time of opening of the last bar in the static variable
static datetime last_time=0;
//--- current time
datetime lastbar_time= (datetime)
SeriesInfoInteger(Symbol(),Period(),SERIES_LASTBAR_DATE);
void OnTrade()
{//inicio
Opeb=0;
Opes=0;
totalT = PositionsTotal();
for(int i=totalT-1 ; i>=0 ; i--){
ulong ticket = PositionGetTicket(i);
if(ticket<=0){Print("Failed to open posistion ticket"); }
if(!PositionSelectByTicket(ticket)){Print("Failed to select
position");}
long type;
double lotaje;
double profit;
if(!PositionGetInteger(POSITION_TYPE,type)){Print("Failed to
get position types");}
if(!PositionGetDouble(POSITION_PROFIT,profit)){Print("Failed
to get position profit");}
if(!PositionGetDouble(POSITION_VOLUME,lotaje)){Print("Failed
to get position profit");}
if(type==0){
Opeb++;
}
if(type==1){
Opes++;
}
}//fin
// nomalize price