//+------------------------------------------------------------------+ //| Price.mqh | //| Andrew Young | //| http://www.expertadvisorbook.com | //+------------------------------------------------------------------+ #property copyright "Andrew Young" #property link "http://www.expertadvisorbook.com" class CBars { private: int BarCount; public: CBars(void); MqlRates bar[]; void Update(string pSymbol, ENUM_TIMEFRAMES pPeriod, int barCount); double Close(int pShift); double High(int pShift); double Low(int pShift); double Open(int pShift); datetime Time(int pShift); long TickVolume(int pShift); long Volume(int pShift); void GetCloses(double &closes[]); void GetOpens(double &opens[]); void GetHighs(double &highs[]); void GetLows(double &lows[]); void GetTimes(datetime ×[]); void GetVolumes(long &volumes[]); int GetBarCount(); int IndexByTime(datetime t); double Point(string pSymbol); double HighestHigh(string pSymbol, ENUM_TIMEFRAMES pPeriod, int pBars, int pStart = 0); int HighestHighIndex(string pSymbol, ENUM_TIMEFRAMES pPeriod, int pBars, int pStart = 0); double LowestLow(string pSymbol, ENUM_TIMEFRAMES pPeriod, int pBars, int pStart = 0); int LowestLowIndex(string pSymbol, ENUM_TIMEFRAMES pPeriod, int pBars, int pStart = 0); }; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ CBars::CBars(void) { ArraySetAsSeries(bar, true); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void CBars::Update(string pSymbol, ENUM_TIMEFRAMES pPeriod, int barCount = 1000) { CopyRates(pSymbol, pPeriod, 0, barCount, bar); BarCount = barCount; } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double CBars::Point(string pSymbol) { return(SymbolInfoDouble(pSymbol, SYMBOL_POINT)); }/**/ //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double CBars::Close(int pShift = 0) { return(bar[pShift].close); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double CBars::High(int pShift = 0) { return(bar[pShift].high); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double CBars::Low(int pShift = 0) { return(bar[pShift].low); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double CBars::Open(int pShift = 0) { return(bar[pShift].open); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ long CBars::TickVolume(int pShift = 0) { return(bar[pShift].tick_volume); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ datetime CBars::Time(int pShift = 0) { return(bar[pShift].time); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ long CBars::Volume(int pShift = 0) { return(bar[pShift].real_volume); } //+------------------------------------------------------------------+ //| Current Price Information | //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double Ask(string pSymbol = NULL) { if(pSymbol == NULL) pSymbol = _Symbol; return(SymbolInfoDouble(pSymbol, SYMBOL_ASK)); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double Bid(string pSymbol = NULL) { if(pSymbol == NULL) pSymbol = _Symbol; return(SymbolInfoDouble(pSymbol, SYMBOL_BID)); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ long Spread(string pSymbol = NULL) { if(pSymbol == NULL) pSymbol = _Symbol; return(SymbolInfoInteger(pSymbol, SYMBOL_SPREAD)); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ long StopLevel(string pSymbol = NULL) { if(pSymbol == NULL) pSymbol = _Symbol; long stopLevel = SymbolInfoInteger(pSymbol, SYMBOL_TRADE_STOPS_LEVEL); return(stopLevel); } //+------------------------------------------------------------------+ //| Highest High & Lowest Low | //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double CBars:: HighestHigh(string pSymbol = NULL, ENUM_TIMEFRAMES pPeriod = PERIOD_H1, int pBars = 10, int pStart = 0) { if(pSymbol == NULL) pSymbol = _Symbol; int maxIdx = iHighest(pSymbol, pPeriod, MODE_HIGH, pBars, pStart); double highest = High(maxIdx); return(highest); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int CBars:: HighestHighIndex(string pSymbol = NULL, ENUM_TIMEFRAMES pPeriod = PERIOD_H1, int pBars = 10, int pStart = 0) { if(pSymbol == NULL) pSymbol = _Symbol; int maxIdx = iHighest(pSymbol, pPeriod, MODE_HIGH, pBars, pStart); return(maxIdx); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double CBars:: LowestLow(string pSymbol = NULL, ENUM_TIMEFRAMES pPeriod = PERIOD_H1, int pBars = 10, int pStart = 0) { if(pSymbol == NULL) pSymbol = _Symbol; int minIdx = iHighest(pSymbol, pPeriod, MODE_HIGH, pBars, pStart); double lowest = High(minIdx); return(lowest); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int CBars:: LowestLowIndex(string pSymbol = NULL, ENUM_TIMEFRAMES pPeriod = PERIOD_H1, int pBars = 10, int pStart = 0) { if(pSymbol == NULL) pSymbol = _Symbol; int minIdx = iLowest(pSymbol, pPeriod, MODE_HIGH, pBars, pStart); return(minIdx); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void CBars::GetCloses(double &closes[]) { int size = ArraySize(bar); ArrayResize(closes, size); for(int i = 0; i < size; i++) closes[i] = bar[i].close; } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void CBars::GetOpens(double &opens[]) { int size = ArraySize(bar); ArrayResize(opens, size); for(int i = 0; i < size; i++) opens[i] = bar[i].open; } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void CBars::GetHighs(double &highs[]) { int size = ArraySize(bar); ArrayResize(highs, size); for(int i = 0; i < size; i++) highs[i] = bar[i].high; } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void CBars::GetLows(double &lows[]) { int size = ArraySize(bar); ArrayResize(lows, size); for(int i = 0; i < size; i++) lows[i] = bar[i].low; } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void CBars::GetTimes(datetime ×[]) { int size = ArraySize(bar); ArrayResize(times, size); for(int i = 0; i < size; i++) times[i] = bar[i].time; } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void CBars::GetVolumes(long &volumes[]) { int size = ArraySize(bar); ArrayResize(volumes, size); for(int i = 0; i < size; i++) volumes[i] = bar[i].tick_volume; } //+------------------------------------------------------------------+ int CBars::GetBarCount(void) { return BarCount; } //+------------------------------------------------------------------+ //| Get bar index by time | //+------------------------------------------------------------------+ int CBars::IndexByTime(datetime t) { if(t == 0 || BarCount == 0) return -1; // bars are stored as series (latest = 0) for(int i = 0; i < BarCount; i++) { if(bar[i].time == t) return i; } return -1; // not found } //+------------------------------------------------------------------+