Advertisement
Guest User

BSMarketTrend

a guest
May 29th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.59 KB | None | 0 0
  1. //+------------------------------------------------------------------+
  2. //|                                               FractalsSignal.mq4 |
  3. //|                                                                  |
  4. //|                                                                  |
  5. //+------------------------------------------------------------------+
  6. #property copyright ""
  7. #property link      ""
  8. #property version   "1.00"
  9. #property strict
  10. #property indicator_chart_window
  11.  
  12. const extern int _BARS_FOR_TRENDCHECK = 48;
  13.  
  14. const int _MARKET_TREND_UP = 1; const int _MARKET_TREND_DOWN = 2;
  15. const int _MARKET_TREND_UNKNOWN = 0;
  16. const int _BARS_UNTIL_VERIFIED = 2;
  17.  
  18. string _sCurrentSymbol = "";
  19.  
  20. int _nM30Trend = 0;
  21. int _nH4Trend = 0;
  22.  
  23. datetime _LastBarTime[9]; //M1 to MN in increasing order
  24.  
  25. int OnInit()
  26.   {
  27.    //declaration & initialisation
  28.    int i = 0;
  29.      
  30.    //get the open time of the last bars of all periods
  31.    _LastBarTime[0] = iTime(_sCurrentSymbol, PERIOD_M1, 0);
  32.    _LastBarTime[1] = iTime(_sCurrentSymbol, PERIOD_M5, 0);
  33.    _LastBarTime[2] = iTime(_sCurrentSymbol, PERIOD_M15, 0);
  34.    _LastBarTime[3] = iTime(_sCurrentSymbol, PERIOD_M30, 0);
  35.    _LastBarTime[4] = iTime(_sCurrentSymbol, PERIOD_H1, 0);
  36.    _LastBarTime[5] = iTime(_sCurrentSymbol, PERIOD_H4, 0);
  37.    _LastBarTime[6] = iTime(_sCurrentSymbol, PERIOD_D1, 0);
  38.    _LastBarTime[7] = iTime(_sCurrentSymbol, PERIOD_W1, 0);
  39.    _LastBarTime[8] = iTime(_sCurrentSymbol, PERIOD_MN1, 0);
  40.    
  41.    _nM30Trend = GetTrend(PERIOD_M30);
  42.    _nH4Trend = GetTrend(PERIOD_H4);
  43.    
  44.    Comment(_nM30Trend + "\n" + _nH4Trend + "\n" + Symbol());
  45.    
  46.    return(INIT_SUCCEEDED);
  47.   }
  48.  
  49. void OnDeinit(const int reason)
  50.   {
  51.    Comment("");
  52.   }
  53.  
  54. int OnCalculate(const int rates_total,
  55.                 const int prev_calculated,
  56.                 const datetime &time[],
  57.                 const double &open[],
  58.                 const double &high[],
  59.                 const double &low[],
  60.                 const double &close[],
  61.                 const long &tick_volume[],
  62.                 const long &volume[],
  63.                 const int &spread[])
  64.   {
  65.    return(rates_total);
  66.   }  
  67.  
  68.  
  69. int GetTrend(int nTimeframe)
  70. {
  71.    int i = 0;
  72.    int nTrend = _MARKET_TREND_UNKNOWN;
  73.    
  74.    double dUpFractal = -1;
  75.    double dDownFractal = -1;
  76.    double dTmpFractal = -1;
  77.    
  78.    //check all bars that could have verified fractals first
  79.    for(i = _BARS_FOR_TRENDCHECK; i > _BARS_UNTIL_VERIFIED; i--)
  80.    {
  81.       // check for broken fractals
  82.       // up and down fractal broken simultaneously
  83.       if(iLow(_sCurrentSymbol, nTimeframe, i) < dDownFractal && dDownFractal > 0
  84.          && iHigh(_sCurrentSymbol, nTimeframe, i) > dUpFractal && dUpFractal > 0)
  85.       {
  86.          nTrend = _MARKET_TREND_UNKNOWN;          
  87.          dUpFractal = -1;                          //devalue fractal once broken
  88.          dDownFractal = -1;
  89.       }
  90.       else
  91.       {
  92.          if(iHigh(_sCurrentSymbol, nTimeframe, i) > dUpFractal && dUpFractal > 0)
  93.          {
  94.             nTrend = _MARKET_TREND_UP;
  95.             dUpFractal = -1;
  96.          }
  97.          if(iLow(_sCurrentSymbol, nTimeframe, i) < dDownFractal && dDownFractal > 0)
  98.          {
  99.             nTrend = _MARKET_TREND_DOWN;
  100.             dDownFractal = -1;
  101.          }
  102.       }
  103.      
  104.       //find the last active fractals (smallest up fractal that was not broken/largest down fractal that was not broken)
  105.       dTmpFractal = iFractals(_sCurrentSymbol, nTimeframe, MODE_UPPER, i);
  106.       if(dTmpFractal > 0)
  107.       {
  108.          if(dTmpFractal < dUpFractal || dUpFractal < 0)
  109.          {
  110.             dUpFractal = dTmpFractal;
  111.          }
  112.       }
  113.      
  114.       dTmpFractal = iFractals(_sCurrentSymbol, nTimeframe, MODE_LOWER, i);
  115.       if(dTmpFractal > 0)
  116.       {
  117.          if(dTmpFractal > dDownFractal || dDownFractal < 0)
  118.          {
  119.             dDownFractal = dTmpFractal;
  120.          }
  121.       }
  122.    }
  123.    //check remaining bars for broken fractals
  124.    for(i = _BARS_UNTIL_VERIFIED; i >=0; i--)
  125.    {
  126.       if(iLow(_sCurrentSymbol, nTimeframe, i) < dDownFractal && dDownFractal > 0
  127.          && iHigh(_sCurrentSymbol, nTimeframe, i) > dUpFractal && dUpFractal > 0)
  128.       {
  129.          nTrend = _MARKET_TREND_UNKNOWN;          
  130.          dUpFractal = -1;                          //devalue fractal once broken
  131.          dDownFractal = -1;
  132.       }
  133.       else
  134.       {
  135.          if(iHigh(_sCurrentSymbol, nTimeframe, i) > dUpFractal && dUpFractal > 0)
  136.          {
  137.             nTrend = _MARKET_TREND_UP;
  138.             dUpFractal = -1;
  139.          }
  140.          if(iLow(_sCurrentSymbol, nTimeframe, i) < dDownFractal && dDownFractal > 0)
  141.          {
  142.             nTrend = _MARKET_TREND_DOWN;
  143.             dDownFractal = -1;
  144.          }
  145.       }
  146.    }
  147.    return(nTrend);
  148. }
  149.  
  150. bool HasNewBar(int nPeriod)
  151. {
  152.    int i;
  153.    bool bHasNewBar = False;
  154.    
  155.    //match input periods with index of _LastBarTime  
  156.    if(nPeriod == PERIOD_M1){        i = 0;}
  157.    else if(nPeriod == PERIOD_M5){   i = 1;}
  158.    else if(nPeriod == PERIOD_M15){  i = 2;}
  159.    else if(nPeriod == PERIOD_M30){  i = 3;}
  160.    else if(nPeriod == PERIOD_H1){   i = 4;}
  161.    else if(nPeriod == PERIOD_H4){   i = 5;}
  162.    else if(nPeriod == PERIOD_D1){   i = 6;}
  163.    else if(nPeriod == PERIOD_W1){   i = 7;}
  164.    else if(nPeriod == PERIOD_MN1){  i = 8;}
  165.    
  166.    //if the open time of the last bar is greater than that in _LastBarTime, then there's a new bar
  167.    if(_LastBarTime[i] < iTime(_sCurrentSymbol, nPeriod, 0))
  168.    {
  169.       bHasNewBar = True;
  170.       //overwrite _LastBarTime to hold the newest open time
  171.       _LastBarTime[i] = iTime(_sCurrentSymbol, nPeriod, 0);
  172.    }
  173.    
  174.    return(bHasNewBar);
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement