//+------------------------------------------------------------------+ //| ExpertSignal.mqh | //| Copyright ?2011, MetaQuotes Software Corp. | //| http://www.metaquotes.net | //| Revision 2011.03.30 | //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| Include files. | //+------------------------------------------------------------------+ #include "ExpertBase.mqh" //+------------------------------------------------------------------+ //| Macro definitions. | //+------------------------------------------------------------------+ //--- check if a market model is used #define IS_PATTERN_USAGE(p) ((m_patterns_usage&(((int)1)<
=m_threshold_open)
{
//--- there's a signal
result=true;
//--- try to get the levels of opening
if(!OpenLongParams(price,sl,tp,expiration)) result=false;
}
//--- zeroize the base price
m_base_price=0.0;
//--- return the result
return(result);
}
//+------------------------------------------------------------------+
//| Generating a sell signal. |
//| INPUT: price - reference for placing the price of opening, |
//| sl - reference for placing the stop loss price, |
//| tp - reference for the take profit price, |
//| expiration - reference for placing the expiration time. |
//| OUTPUT: true if there is a signal, otherwise - false. |
//| REMARK: no. |
//+------------------------------------------------------------------+
bool CExpertSignal::CheckOpenShort(double& price,double& sl,double& tp,datetime& expiration)
{
bool result =false;
double direction=Direction(m_base_price);
printf("1111,%f",m_base_price);
//--- the "prohibition" signal
if(direction==EMPTY_VALUE) return(false);
//--- check of exceeding the threshold value
if(-direction>=m_threshold_open)
{
//--- there's a signal
result=true;
//--- try to get the levels of opening
if(!OpenShortParams(price,sl,tp,expiration)) result=false;
}
//--- zeroize the base price
m_base_price=0.0;
//--- return the result
return(result);
}
//+------------------------------------------------------------------+
//| Detecting the levels for buying . |
//| INPUT: price - reference for placing the price of opening, |
//| sl - reference for placing the stop loss price, |
//| tp - reference for the take profit price, |
//| expiration - reference for placing the expiration time. |
//| OUTPUT: true in case of successful execution, otherwise - false. |
//| REMARK: no. |
//+------------------------------------------------------------------+
bool CExpertSignal::OpenLongParams(double& price,double& sl,double& tp,datetime& expiration)
{
CExpertSignal *general=NULL;
if(m_general!=-1) general=m_filters.At(m_general);
//---
if(general==NULL)
{
//--- if a base price is not specified explicitly, take the current market price
if(m_base_price==0.0) m_base_price=m_symbol.Ask();
price =m_symbol.NormalizePrice(m_base_price-m_price_level*PriceLevelUnit());
sl =(m_stop_level==0.0)?0.0:m_symbol.NormalizePrice(price-m_stop_level*PriceLevelUnit());
tp =(m_take_level==0.0)?0.0:m_symbol.NormalizePrice(price+m_take_level*PriceLevelUnit());
expiration+=m_expiration*PeriodSeconds(m_period);
}
else
{
return(general.OpenLongParams(price,sl,tp,expiration));
}
//--- ok
return(true);
}
//+------------------------------------------------------------------+
//| Detecting the levels for selling. |
//| INPUT: price - reference for placing the price of opening, |
//| sl - reference for placing the stop loss price, |
//| tp - reference for the take profit price, |
//| expiration - reference for placing the expiration time. |
//| OUTPUT: true in case of successful execution, otherwise - false. |
//| REMARK: no. |
//+------------------------------------------------------------------+
bool CExpertSignal::OpenShortParams(double& price,double& sl,double& tp,datetime& expiration)
{
CExpertSignal *general=NULL;
if(m_general!=-1) general=m_filters.At(m_general);
//---
if(general==NULL)
{
//--- if a base price is not specified explicitly, take the current market price
if(m_base_price==0.0) m_base_price=m_symbol.Bid();
price =m_symbol.NormalizePrice(m_base_price+m_price_level*PriceLevelUnit());
sl =(m_stop_level==0.0)?0.0:m_symbol.NormalizePrice(price+m_stop_level*PriceLevelUnit());
tp =(m_take_level==0.0)?0.0:m_symbol.NormalizePrice(price-m_take_level*PriceLevelUnit());
expiration+=m_expiration*PeriodSeconds(m_period);
}
else
{
return(general.OpenShortParams(price,sl,tp,expiration));
}
//--- ok
return(true);
}
//+------------------------------------------------------------------+
//| Generating a signal for closing of a long position. |
//| INPUT: price - reference for placing the price of closing. |
//| OUTPUT: true if there is a signal, otherwise - false. |
//| REMARK: no. |
//+------------------------------------------------------------------+
bool CExpertSignal::CheckCloseLong(double& price)
{
bool result =false;
double direction=Direction(m_base_price);
//--- the "prohibition" signal
if(direction==EMPTY_VALUE) return(false);
//--- check of exceeding the threshold value
if(-direction>=m_threshold_close)
{
//--- there's a signal
result=true;
//--- try to get the level of closing
if(!CloseLongParams(price)) result=false;
}
//--- zeroize the base price
m_base_price=0.0;
//--- return the result
return(result);
}
//+------------------------------------------------------------------+
//| Generating a signal for closing a short position. |
//| INPUT: price - reference for placing the price of closing. |
//| OUTPUT: true if there is a signal, otherwise - false. |
//| REMARK: no. |
//+------------------------------------------------------------------+
bool CExpertSignal::CheckCloseShort(double& price)
{
bool result =false;
double direction=Direction(m_base_price);
//--- the "prohibition" signal
if(direction==EMPTY_VALUE) return(false);
//--- check of exceeding the threshold value
if(direction>=m_threshold_close)
{
//--- there's a signal
result=true;
//--- try to get the level of closing
if(!CloseShortParams(price)) result=false;
}
//--- zeroize the base price
m_base_price=0.0;
//--- return the result
return(result);
}
//+------------------------------------------------------------------+
//| Detecting the levels for closing a long position. |
//| INPUT: price - reference for placing the price of closing. |
//| OUTPUT: true in case of successful execution, otherwise - false. |
//| REMARK: no. |
//+------------------------------------------------------------------+
bool CExpertSignal::CloseLongParams(double& price)
{
CExpertSignal *general=NULL;
if(m_general!=-1) general=m_filters.At(m_general);
//---
if(general==NULL)
{
//--- if a base price is not specified explicitly, take the current market price
price=(m_base_price==0.0)?m_symbol.Bid():m_base_price;
}
else
{
return(general.CloseLongParams(price));
}
//--- ok
return(true);
}
//+------------------------------------------------------------------+
//| Detecting the levels for closing a short position. |
//| INPUT: price - reference for placing the price of closing. |
//| OUTPUT: true in case of successful execution, otherwise - false. |
//| REMARK: no. |
//+------------------------------------------------------------------+
bool CExpertSignal::CloseShortParams(double& price)
{
CExpertSignal *general=NULL;
if(m_general!=-1) general=m_filters.At(m_general);
//---
if(general==NULL)
{
//--- if a base price is not specified explicitly, take the current market price
price=(m_base_price==0.0)?m_symbol.Ask():m_base_price;
}
else
{
return(general.CloseShortParams(price));
}
//--- ok
return(true);
}
//+------------------------------------------------------------------+
//| Generating a signal for reversing a long position. |
//| INPUT: price - reference for placing the price of reverse, |
//| sl - reference for placing the stop loss price, |
//| tp - reference for the take profit price, |
//| expiration - reference for placing the expiration time. |
//| OUTPUT: true if there is a signal, otherwise - false. |
//| REMARK: no. |
//+------------------------------------------------------------------+
bool CExpertSignal::CheckReverseLong(double& price,double& sl,double& tp,datetime& expiration)
{
double c_price;
//--- check the signal of closing a long position
if(!CheckCloseLong(c_price)) return(false);
//--- check the signal of opening a short position
if(!CheckOpenShort(price,sl,tp,expiration)) return(false);
//--- difference between the close and open prices must not exceed two spreads
if(c_price!=price) return(false);
//--- there's a signal
return(true);
}
//+------------------------------------------------------------------+
//| Generating a signal for reversing a short position. |
//| INPUT: price - reference for placing the price of reverse, |
//| sl - reference for placing the stop loss price, |
//| tp - reference for the take profit price, |
//| expiration - reference for placing the time expiration. |
//| OUTPUT: true if there is a signal, otherwise - false. |
//| REMARK: no. |
//+------------------------------------------------------------------+
bool CExpertSignal::CheckReverseShort(double& price,double& sl,double& tp,datetime& expiration)
{
double c_price;
//--- check the signal of closing a short position
if(!CheckCloseShort(c_price)) return(false);
//--- check the signal of opening a long position
if(!CheckOpenLong(price,sl,tp,expiration)) return(false);
//--- difference between the close and open prices must not exceed two spreads
if(c_price!=price) return(false);
//--- there's a signal
return(true);
}
//+------------------------------------------------------------------+
//| Detecting the "weighted" direction. |
//| INPUT: no. |
//| OUTPUT: >0 - if the probability of price growth is greater, |
//| =0 - if the direction is not determined |
//| (small probability), |
//| <0 - if the probability of price falling is greater. |
//| REMARK: no. |
//+------------------------------------------------------------------+
double CExpertSignal::Direction(double& price)
{
CExpertSignal *filter;
long mask;
double direction;
double result=m_weight*(LongCondition(price)-ShortCondition(price));
int number=0; // number of "voted"
//---
int total=m_filters.Total();
//--- for debugging
//printf(__FUNCTION__+" : %s %d",EnumToString(m_period),total);
//--- loop by filters
for(int i=0;i