Range Bar Indicator Sync (Price) Issues

Post Reply
mperk
Posts: 2
Joined: Wed Jul 11, 2012 4:15 am

Range Bar Indicator Sync (Price) Issues

Post by mperk »

I have tried many different RB indicators over the last years. Open source and those that have been purchased. Most use far too much of the computer resources or run in a continuous loop, have scripts of which any combinations of these with 20 or more opened offline charts can cause the computer to "lock-up" or cause Empty4 to slow down tremendously.

With the current computer that I'm using, these type of RB indicators have been the only thing to "lock-up" my computer. Any software from CAD to Games, never have caused this issue.

Have recently found a new RB indicator that uses a fraction of the resources, never causes a "lock-up" but, it does have a major flaw that I have no idea how to fix.

When it runs on one currency pair its great as it has been monitored against the Market Watch Bid price. Have also applied this RB indicator to 35 different currency pairs at the same time. Still all is fine, resources, speed but, when used on 4 or 5 different offline sub-charts (individual charts) of the same currency pair, the lowest pip setting offline chart is always correct. The other 3 to 4 higher set pip offline sub-charts the price can go out of sync. Sometimes as much as 12 pips. Refreshing the chart or rebooting does not help. Only re-installation does for awhile. This much out-of-sync causes indicator misreads on the these offline sub-charts that are use as part of my trading rules.

I am in no way a programmer. Can someone help figure out what the problem is? Is a there a way to code it to make the other 3 to 4 sub-offline charts to be in sync with the currency pair lowest pip setting offline chart???


//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
#property copyright "V2.00 Copyright © 2001-2012, Shawn Harris"
#property link ""
#property show_inputs
#property indicator_chart_window


#include <WinUser32.mqh>
#include <stdlib.mqh>


#import "user32.dll"
int RegisterWindowMessageA(string a0);
#import

int MaxBars = 100000;
extern int Timeframe = 8;
extern bool ShowGaps = true;
extern int ConstantRange = 8.0;

bool _init = false;
int mt4mesg = 0;
int fileHandle = -1;


string Pair;
int windowHandle = 0;
int recordsWritten = 0;
int version = 400;
int pairDigits = 0;
int barLimit;
int m1Time;
int emptyArray[13];
double barProgress;
int currentTime;
int barTime;
double barLow;
double barHigh;
double barVolume;
double barClose;
double barOpen;
int lastFilePos;
int periodSeconds;
int limit;
int _tf;

string name = "#ADVRangeBars";
string ver = "2.00";

int __lastTime = 0;
int __divisor = -1;

int init()
{
__lastTime = 0;
__divisor = -1;

if (Timeframe == 0)
Timeframe = Period();

mt4mesg = RegisterWindowMessageA("MetaTrader4_Internal_Message");

Pair = Symbol();
}


int deinit() {
if (fileHandle >= 0) {
FileClose(fileHandle);
fileHandle = -1;
}

}


int start() {
if (!IsDllsAllowed()) {
ErrorMessage("#ADVRangeBars : you must allow DLL imports");
return (-1);
}

int c = 0;

pairDigits = _DIGITS();
while (pairDigits == 0) {
Sleep(1000);
pairDigits = _DIGITS();
if (c > 5)
break;
}

int divisor = 1;

if (IsForexPair()) {
if (pairDigits == 3 || pairDigits == 5)
divisor = 10;
else
divisor = 1;
}

double TrueRange = ConstantRange * divisor;


if (!_init || fileHandle == -1) {
s_init();
_init = true;
}

if (!RefreshRates()) {
Sleep(50);
}

if (!FileSeek(fileHandle, lastFilePos, SEEK_SET)) {
ErrorMessage("#ADVRangeBars : " + GetLastError());
return (0);
}

barLow = MathMin(barLow, _CLOSE(0));
barHigh = MathMax(barHigh, _CLOSE(0));
barVolume++;

if (NormalizeDouble((barHigh - barLow) / _POINT() / divisor, 1) <= NormalizeDouble(TrueRange / divisor, 1)) {
barClose = _CLOSE(0);
}
else {
if (CompareDoubles(barLow, _CLOSE(0))) {
if (ShowGaps)
barLow = NormalizeDouble(barHigh - TrueRange * _POINT(), _DIGITS());
else
barLow = NormalizeDouble(barLow + divisor * _POINT(), _DIGITS());

barClose = barLow;
}

if (CompareDoubles(barHigh, _CLOSE(0))) {
if (ShowGaps)
barHigh = NormalizeDouble(barLow + TrueRange * _POINT(), _DIGITS());
else
barHigh = NormalizeDouble(barHigh - divisor * _POINT(), _DIGITS());

barClose = barHigh;
}

FileWriteInteger (fileHandle, barTime, LONG_VALUE);
FileWriteDouble (fileHandle, NormalizeDouble(MathMin(MathMax(barOpen, barLow), barHigh), _DIGITS()), DOUBLE_VALUE);
FileWriteDouble (fileHandle, NormalizeDouble(MathMin(barLow, barHigh), _DIGITS()), DOUBLE_VALUE);
FileWriteDouble (fileHandle, NormalizeDouble(MathMax(barHigh, barLow), _DIGITS()), DOUBLE_VALUE);
FileWriteDouble (fileHandle, NormalizeDouble(MathMin(MathMax(barClose, barLow), barHigh), _DIGITS()), DOUBLE_VALUE);
FileWriteDouble (fileHandle, NormalizeDouble(barVolume, 0), DOUBLE_VALUE);

lastFilePos = FileTell(fileHandle);
barTime++;
barTime = MathMax(barTime, TimeCurrent());
barVolume = 1;
barOpen = _CLOSE(0);
barLow = _CLOSE(0);
barHigh = _CLOSE(0);
barClose = _CLOSE(0);
barVolume = 1.0;
}

FileWriteInteger(fileHandle, barTime, LONG_VALUE);
FileWriteDouble(fileHandle, NormalizeDouble(MathMin(MathMax(barOpen, barLow), barHigh), _DIGITS()), DOUBLE_VALUE);
FileWriteDouble(fileHandle, NormalizeDouble(MathMin(barLow, barHigh), _DIGITS()), DOUBLE_VALUE);
FileWriteDouble(fileHandle, NormalizeDouble(MathMax(barHigh, barLow), _DIGITS()), DOUBLE_VALUE);
FileWriteDouble(fileHandle, NormalizeDouble(MathMin(MathMax(barClose, barLow), barHigh), _DIGITS()), DOUBLE_VALUE);
FileWriteDouble(fileHandle, NormalizeDouble(barVolume, 0), DOUBLE_VALUE);
FileFlush(fileHandle);

_postMessage();

return (0);
}


int _TIME(int bar)
{
return (iTime(Pair, PERIOD_M1, bar));
}


double _OPEN(int bar)
{
return (iOpen(Pair, PERIOD_M1, bar));
}


double _CLOSE(int bar)
{
return (iClose(Pair, PERIOD_M1, bar));
}


double _HIGH(int bar)
{
return (iHigh(Pair, PERIOD_M1, bar));
}


double _LOW(int bar)
{
return (iLow(Pair, PERIOD_M1, bar));
}

double _VOLUME(int bar)
{
return (iVolume(Pair, PERIOD_M1, bar));
}


int _DIGITS()
{
return (MarketInfo(Pair, MODE_DIGITS));
}


double _POINT()
{
return (MarketInfo(Pair, MODE_POINT));
}


void AddErrorMesg(string mesg)
{
color c = Yellow;
SetLabelText2("advRBars-mesg", mesg, 25, 25, 0, 0, 16, "Verdana Bold", c);
}


void SetLabelText2(string _name, string _text, int _X, int _Y, int _Corner, int _nWindow, int _FontSize, string _FontName, color _FontColor) {
string n = _name;
if (ObjectFind(n) == -1) {
if (!ObjectCreate(n, OBJ_LABEL, _nWindow, 0, 0)) {
Print("error: can\'t create " + _name + "! code #", GetLastError());
return (0);
}
ObjectSet(n, OBJPROP_XDISTANCE, _X);
ObjectSet(n, OBJPROP_YDISTANCE, _Y);
ObjectSet(n, OBJPROP_CORNER, _Corner);
}

ObjectSetText(n, _text, _FontSize, _FontName, _FontColor);
}


void ErrorMessage(string m)
{
AddErrorMesg(m);
}


void s_init()
{
int c = 0;

pairDigits = _DIGITS();
while (pairDigits == 0) {
Sleep(1000);
pairDigits = _DIGITS();
if (c > 5)
break;
}

int divisor = 1;

if (IsForexPair()) {
if (pairDigits == 3 || pairDigits == 5)
divisor = 10;
else
divisor = 1;
}

double TrueRange = ConstantRange * divisor;


fileHandle = FileOpenHistory(Pair + Timeframe + ".hst", FILE_BIN|FILE_WRITE);

if (fileHandle < 0) {
ErrorMessage("#ADVRangeBars : cannot create " + Pair + Timeframe + ".hst");
return (-1);
}

string copyrightString = "(C)opyright 2003, MetaQuotes Software Corp.";

FileWriteInteger (fileHandle, version, LONG_VALUE);
FileWriteString (fileHandle, copyrightString, 64);
FileWriteString (fileHandle, Pair, 12);
FileWriteInteger (fileHandle, Timeframe, LONG_VALUE);
FileWriteInteger (fileHandle, pairDigits, LONG_VALUE);
FileWriteInteger (fileHandle, 0, LONG_VALUE);
FileWriteInteger (fileHandle, 0, LONG_VALUE);
FileWriteArray (fileHandle, emptyArray, 0, 13);
FileFlush (fileHandle);

lastFilePos = FileTell(fileHandle);
periodSeconds = 60 * Timeframe;
limit = MathMin(MaxBars, iBars(Pair, PERIOD_M1) - 1);

if (limit <= 0) {
int hbars = 0;
while (hbars == 0) {
hbars = iBars(Symbol(), PERIOD_M1);
if (hbars > 0) {
break;
}

datetime daytimes[];
ArrayCopySeries(daytimes, MODE_TIME, Symbol(), PERIOD_M1);
int err = GetLastError();

if (err != 4066) {
// ERR_HISTORY_WILL_UPDATED 4066 Requested history data in updating state.
break;
}
else {
Sleep(2000);
}
}

limit = MathMin(MaxBars, iBars(Pair, PERIOD_M1) - 1);
}

if (limit <= 0) {
ErrorMessage("#ADVRangeBars : no 1 min history available");
return (-1);
}

barTime = _TIME(limit);
barLow = _CLOSE(limit);
barHigh = barLow;
barVolume = 1.0;
barClose = barLow;
barOpen = barLow;

int barRange = 1;

for (int i = limit - 1; i >= 0; i--) {

m1Time = _TIME(i);

if (_VOLUME(i) > 0.0)
barRange = MathMax(divisor, MathCeil((_HIGH(i) - _LOW(i)) / _POINT() / _VOLUME(i)));
else
barRange = divisor;

barRange = MathRound(barRange / divisor) * divisor;

if (_OPEN(i) > _CLOSE(i)) {
barLimit = MathRound(_HIGH(i) / _POINT() / divisor) * divisor;

while (barLimit >= MathRound(_LOW(i) / _POINT() / divisor) * divisor) {
barProgress = NormalizeDouble(barLimit * _POINT(), _DIGITS());
barLow = MathMin(barLow, barProgress);
barHigh = MathMax(barHigh, barProgress);
barVolume++;

if (MathRound((barHigh - barLow) / _POINT()) >= TrueRange + divisor) {

lastFilePos = FileTell(fileHandle);

if (CompareDoubles(barLow, barProgress)) {
if (ShowGaps)
barLow = NormalizeDouble(barHigh - TrueRange * _POINT(), _DIGITS());
else
barLow = NormalizeDouble(barLow + divisor * _POINT(), _DIGITS());

barClose = barLow;
}

if (CompareDoubles(barHigh, barProgress)) {
if (ShowGaps)
barHigh = NormalizeDouble(barLow + TrueRange * _POINT(), _DIGITS());
else
barHigh = NormalizeDouble(barHigh - divisor * _POINT(), _DIGITS());

barClose = barHigh;
}


FileWriteInteger (fileHandle, barTime, LONG_VALUE);
FileWriteDouble (fileHandle, barOpen, DOUBLE_VALUE);
FileWriteDouble (fileHandle, barLow, DOUBLE_VALUE);
FileWriteDouble (fileHandle, barHigh, DOUBLE_VALUE);
FileWriteDouble (fileHandle, barClose, DOUBLE_VALUE);
FileWriteDouble (fileHandle, barVolume, DOUBLE_VALUE);

FileFlush(fileHandle);

recordsWritten++;
barTime++;

barTime = MathMax(m1Time, barTime);

barOpen = barProgress;
barLow = barProgress;
barHigh = barProgress;
barClose = barProgress;
barVolume = 1.0;

}
else {
barClose = barProgress;
}


barLimit -= barRange;
}
}
else {
barLimit = MathRound(_LOW(i) / _POINT() / divisor) * divisor;

while (barLimit <= MathRound(_HIGH(i) / _POINT() / divisor) * divisor) {

barProgress = NormalizeDouble(barLimit * _POINT(), _DIGITS());
barLow = MathMin(barLow, barProgress);
barHigh = MathMax(barHigh, barProgress);
barVolume++;

if (MathRound((barHigh - barLow) / _POINT()) >= TrueRange + divisor) {

lastFilePos = FileTell(fileHandle);

if (CompareDoubles(barLow, barProgress)) {
if (ShowGaps)
barLow = NormalizeDouble(barHigh - TrueRange * _POINT(), _DIGITS());
else
barLow = NormalizeDouble(barLow + divisor * _POINT(), _DIGITS());

barClose = barLow;
}

if (CompareDoubles(barHigh, barProgress)) {
if (ShowGaps)
barHigh = NormalizeDouble(barLow + TrueRange * _POINT(), _DIGITS());
else
barHigh = NormalizeDouble(barHigh - divisor * _POINT(), _DIGITS());

barClose = barHigh;
}

FileWriteInteger (fileHandle, barTime, LONG_VALUE);
FileWriteDouble (fileHandle, barOpen, DOUBLE_VALUE);
FileWriteDouble (fileHandle, barLow, DOUBLE_VALUE);
FileWriteDouble (fileHandle, barHigh, DOUBLE_VALUE);
FileWriteDouble (fileHandle, barClose, DOUBLE_VALUE);
FileWriteDouble (fileHandle, barVolume, DOUBLE_VALUE);
FileFlush(fileHandle);

recordsWritten++;
barTime++;
barTime = MathMax(m1Time, barTime);
barOpen = barProgress;
barLow = barProgress;
barHigh = barProgress;
barClose = barProgress;
barVolume = 1.0;
}
else {
barClose = barProgress;
}

barLimit += barRange;
}
}
}

lastFilePos = FileTell(fileHandle);
FileWriteInteger (fileHandle, barTime, LONG_VALUE);
FileWriteDouble (fileHandle, barOpen, DOUBLE_VALUE);
FileWriteDouble (fileHandle, barLow, DOUBLE_VALUE);
FileWriteDouble (fileHandle, barHigh, DOUBLE_VALUE);
FileWriteDouble (fileHandle, barClose, DOUBLE_VALUE);
FileWriteDouble (fileHandle, barVolume, DOUBLE_VALUE);
FileFlush(fileHandle);

barTime = TimeCurrent();
}


void _postMessage() {
if (Timeframe == Period())
return;

if ((TimeCurrent() - __lastTime) < 2) {
return;
}

__lastTime = TimeCurrent();

int h = WindowHandle(Pair, Timeframe);

if (h != 0) {
PostMessageA(h, WM_COMMAND, 33324, 0);
}

}


bool IsForexPair()
{
string base = StringSubstr(Symbol(), 0, 3);
string host = StringSubstr(Symbol(), 3, 6);

return (IsForex(base) && IsForex(host));
}


bool IsForex(string pre)
{
bool ret = false;

if (pre == "EUR") ret = true;
else if (pre == "USD") ret = true;
else if (pre == "GBP") ret = true;
else if (pre == "CAD") ret = true;
else if (pre == "JPY") ret = true;
else if (pre == "CHF") ret = true;
else if (pre == "AUD") ret = true;
else if (pre == "NZD") ret = true;
else if (pre == "eur") ret = true;
else if (pre == "usd") ret = true;
else if (pre == "gbp") ret = true;
else if (pre == "cad") ret = true;
else if (pre == "jpy") ret = true;
else if (pre == "chf") ret = true;
else if (pre == "aud") ret = true;
else if (pre == "nzd") ret = true;


return (ret);
}



Your help is greatly appreciated!!!
User avatar
NeoTrader
Trader
Posts: 436
Joined: Wed Apr 04, 2012 2:52 pm
Location: small Village at Lake Chiemsee, Bavaria, Germany

Re: Range Bar Indicator Sync (Price) Issues

Post by NeoTrader »

hi mperk,

if you want that anyone should help you then put either the code in <code> tags or better attach the whole file here so we can take a look in our editors.
This is not a small snippet to take a small peek at...

just my 2 pips,

happy trading,

NeoTrader
User avatar
fxozgirl
Trader
Posts: 1176
Joined: Wed Nov 16, 2011 9:16 am
Location: Melbourne, Australia

Re: Range Bar Indicator Sync (Price) Issues

Post by fxozgirl »

Also I would suggest either a mod moving this thread to the Coders Hangout or at least the OP to post there

http://www.stevehopwoodforex.com/phpBB3 ... m.php?f=15
Post Reply

Return to “Automated trading systems”