当我只需要收盘时的指标时,如何停止指标计算每个报价单?
How to stop Indicator from calculating on every tick, when I only need the indicator at bar close?
这是我从网上下载的指标,我做了一些修改。我注意到该指标计算线性回归线以及每个价格变动的上限和下限。
我发现这很浪费资源,因为我只需要在每个柱的收盘时计算线;即,当柱 0 结束并形成新的柱 0 时。
当柱 0 还未完成时,它不应该计算任何东西。
我如何进行必要的更改?
谢谢!!
//+------------------------------------------------------------------+
//| Linear Regression Line.mq4 |
//| MQL Service |
//| scripts@mqlservice.com |
//+------------------------------------------------------------------+
#property copyright "MQL Service"
#property link "www.mqlservice.com"
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 White
#property indicator_width1 2
#property indicator_color2 Orange
#property indicator_width2 2
#property indicator_color3 Orange
#property indicator_width3 2
//---- input parameters
extern int LRLPeriod = 20;
extern int Number_SD = 2;
//---- buffers
double LRLBuffer[], LRLBuffer_Upper[], LRLBuffer_Lower[];
//int shift = 0;
int n = 0;
double sumx = 0;
double sumy = 0;
double sumxy = 0;
double sumx2 = 0;
double sumy2 = 0;
double yint = 0;
double r = 0;
double m = 0;
//+------------------------------------------------------------------+
//| INITIALIZATION FUNCTION |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0, DRAW_LINE);
SetIndexBuffer(0, LRLBuffer);
SetIndexStyle(1, DRAW_LINE);
SetIndexBuffer(1, LRLBuffer_Upper);
SetIndexStyle(2, DRAW_LINE);
SetIndexBuffer(2, LRLBuffer_Lower);
IndicatorDigits(Digits);
if (LRLPeriod < 2)
LRLPeriod = 2;
IndicatorShortName("Linear Regression Line ("+LRLPeriod+")");
SetIndexDrawBegin(0, LRLPeriod+2);
IndicatorDigits(MarketInfo(Symbol(), MODE_DIGITS)+4);
return(0);
}
//+------------------------------------------------------------------+
//| DEINITIALIZATION FUNCTION |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| ITERATION FUNCTION |
//+------------------------------------------------------------------+
int start()
{
int limit, j, Counted_bars;
int counted_bars = IndicatorCounted();
if (counted_bars < 0)
counted_bars = 0;
if (counted_bars > 0)
counted_bars--;
limit = Bars - counted_bars;
for (int shift=limit-1; shift >= 0; shift--)
{
sumx = 0;
sumy = 0;
sumxy = 0;
sumx2 = 0;
sumy2 = 0;
for (n = 0; n <= LRLPeriod-1; n++)
{
sumx = sumx + n;
sumy = sumy + Close[shift + n];
sumxy = sumxy + n * Close[shift + n];
sumx2 = sumx2 + n * n;
sumy2 = sumy2 + Close[shift + n] * Close[shift + n];
}
double temp = LRLPeriod * sumx2 - sumx * sumx;
if (temp == 0)
temp = .0000001;
// m = (LRLPeriod * sumxy - sumx * sumy) / (LRLPeriod * sumx2 - sumx * sumx);
m = (LRLPeriod * sumxy - sumx * sumy) / temp;
temp = LRLPeriod;
if (temp == 0)
temp = .0000001;
yint = (sumy + m * sumx) / temp; // was LRLPeriod (obviously)
temp = MathSqrt((LRLPeriod * sumx2 - sumx * sumx) * (LRLPeriod * sumy2 - sumy * sumy));
if (temp == 0)
temp = .0000001;
r = (LRLPeriod * sumxy - sumx * sumy) / temp;
LRLBuffer[shift] = yint - m * LRLPeriod;
//Print (" "+shift+" "+LRLBuffer[shift]);
}
//----------Added Upper and Lower Bands--------------//
int nBARs = 0;
double LRLBuffer_CPY[];
ArraySetAsSeries(LRLBuffer_CPY,True);
j = Bars - Counted_bars - 1;
while( j > 0 )
{
ArrayCopy( LRLBuffer_CPY, LRLBuffer, 0, j, WHOLE_ARRAY );
double StDev = iStdDevOnArray( LRLBuffer_CPY, nBARs, LRLPeriod, 0, MODE_SMA, 0 );
LRLBuffer_Upper[j] = LRLBuffer[j] + (Number_SD * StDev);
LRLBuffer_Lower[j] = LRLBuffer[j] - (Number_SD * StDev);
j--;
}
return(0);
}
//+------------------------------------------------------------------+
最简单的方法是使用标准 OnCalculate(***)
函数和 运行 主循环 if(rates_total>prev_calculated)
。
另外,在主循环中尝试for(int shift=limit-1;shift>0;shift--){
(注意),顺便问一下,你确定你需要shift=limit-1
而不只是shift=limit
吗?
新的-MQL4
语法OnCalculate()
不会帮助实现目标,
而是使用这个:
OnCalculate()
语法可用,但代码块实际上不受您的控制,它何时启动以及允许处理多少渐进步骤。
解决方案是使用 "old"-语法并添加 "soft"-锁定。
使用static
变量有助于在调用和刚刚热端重新计算之间存储临时值,因此不需要从整个深度重新迭代,如果移动寄存器用于仅从刚刚冻结的 [1] 栏更新。 Bar [0] 持续时间的其余部分它确实如此。NOP/JIT/RET 并且您的代码不会破坏单线程性能的脆弱性,由 ALL[= 共享(是!共享!!) 27=] 自定义指标(最近 New-MQL4 更新中真正邪恶的反模式...):
int start(){
static aCurrentTIME = EMPTY;
if ( aCurrentTIME == Time[0] ) return 0; // .NOP/JIT/RET --^
aCurrentTIME = Time[0]; // .MOV/LOCK
// -------------------------------------------- // .CALC:
int limit = Bars - counted_bars;
...
/* update just the "HOT"-end has just got into OHLCVT[1]-cells */
// -------------------------------------------- // .FIN
}
视情况而定,您还可以:
datetime calculatedBarTime;
void OnCalculate()
{
if ( calculatedBarTime != Time[0] )
{
onBar();
}
}
void onBar()
{
calculatedBarTime = Time[0];
// on bar logic....
}
这是我从网上下载的指标,我做了一些修改。我注意到该指标计算线性回归线以及每个价格变动的上限和下限。
我发现这很浪费资源,因为我只需要在每个柱的收盘时计算线;即,当柱 0 结束并形成新的柱 0 时。
当柱 0 还未完成时,它不应该计算任何东西。
我如何进行必要的更改?
谢谢!!
//+------------------------------------------------------------------+
//| Linear Regression Line.mq4 |
//| MQL Service |
//| scripts@mqlservice.com |
//+------------------------------------------------------------------+
#property copyright "MQL Service"
#property link "www.mqlservice.com"
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 White
#property indicator_width1 2
#property indicator_color2 Orange
#property indicator_width2 2
#property indicator_color3 Orange
#property indicator_width3 2
//---- input parameters
extern int LRLPeriod = 20;
extern int Number_SD = 2;
//---- buffers
double LRLBuffer[], LRLBuffer_Upper[], LRLBuffer_Lower[];
//int shift = 0;
int n = 0;
double sumx = 0;
double sumy = 0;
double sumxy = 0;
double sumx2 = 0;
double sumy2 = 0;
double yint = 0;
double r = 0;
double m = 0;
//+------------------------------------------------------------------+
//| INITIALIZATION FUNCTION |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0, DRAW_LINE);
SetIndexBuffer(0, LRLBuffer);
SetIndexStyle(1, DRAW_LINE);
SetIndexBuffer(1, LRLBuffer_Upper);
SetIndexStyle(2, DRAW_LINE);
SetIndexBuffer(2, LRLBuffer_Lower);
IndicatorDigits(Digits);
if (LRLPeriod < 2)
LRLPeriod = 2;
IndicatorShortName("Linear Regression Line ("+LRLPeriod+")");
SetIndexDrawBegin(0, LRLPeriod+2);
IndicatorDigits(MarketInfo(Symbol(), MODE_DIGITS)+4);
return(0);
}
//+------------------------------------------------------------------+
//| DEINITIALIZATION FUNCTION |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| ITERATION FUNCTION |
//+------------------------------------------------------------------+
int start()
{
int limit, j, Counted_bars;
int counted_bars = IndicatorCounted();
if (counted_bars < 0)
counted_bars = 0;
if (counted_bars > 0)
counted_bars--;
limit = Bars - counted_bars;
for (int shift=limit-1; shift >= 0; shift--)
{
sumx = 0;
sumy = 0;
sumxy = 0;
sumx2 = 0;
sumy2 = 0;
for (n = 0; n <= LRLPeriod-1; n++)
{
sumx = sumx + n;
sumy = sumy + Close[shift + n];
sumxy = sumxy + n * Close[shift + n];
sumx2 = sumx2 + n * n;
sumy2 = sumy2 + Close[shift + n] * Close[shift + n];
}
double temp = LRLPeriod * sumx2 - sumx * sumx;
if (temp == 0)
temp = .0000001;
// m = (LRLPeriod * sumxy - sumx * sumy) / (LRLPeriod * sumx2 - sumx * sumx);
m = (LRLPeriod * sumxy - sumx * sumy) / temp;
temp = LRLPeriod;
if (temp == 0)
temp = .0000001;
yint = (sumy + m * sumx) / temp; // was LRLPeriod (obviously)
temp = MathSqrt((LRLPeriod * sumx2 - sumx * sumx) * (LRLPeriod * sumy2 - sumy * sumy));
if (temp == 0)
temp = .0000001;
r = (LRLPeriod * sumxy - sumx * sumy) / temp;
LRLBuffer[shift] = yint - m * LRLPeriod;
//Print (" "+shift+" "+LRLBuffer[shift]);
}
//----------Added Upper and Lower Bands--------------//
int nBARs = 0;
double LRLBuffer_CPY[];
ArraySetAsSeries(LRLBuffer_CPY,True);
j = Bars - Counted_bars - 1;
while( j > 0 )
{
ArrayCopy( LRLBuffer_CPY, LRLBuffer, 0, j, WHOLE_ARRAY );
double StDev = iStdDevOnArray( LRLBuffer_CPY, nBARs, LRLPeriod, 0, MODE_SMA, 0 );
LRLBuffer_Upper[j] = LRLBuffer[j] + (Number_SD * StDev);
LRLBuffer_Lower[j] = LRLBuffer[j] - (Number_SD * StDev);
j--;
}
return(0);
}
//+------------------------------------------------------------------+
最简单的方法是使用标准 OnCalculate(***)
函数和 运行 主循环 if(rates_total>prev_calculated)
。
另外,在主循环中尝试for(int shift=limit-1;shift>0;shift--){
(注意),顺便问一下,你确定你需要shift=limit-1
而不只是shift=limit
吗?
新的-MQL4
语法OnCalculate()
不会帮助实现目标,
而是使用这个:
OnCalculate()
语法可用,但代码块实际上不受您的控制,它何时启动以及允许处理多少渐进步骤。
解决方案是使用 "old"-语法并添加 "soft"-锁定。
使用static
变量有助于在调用和刚刚热端重新计算之间存储临时值,因此不需要从整个深度重新迭代,如果移动寄存器用于仅从刚刚冻结的 [1] 栏更新。 Bar [0] 持续时间的其余部分它确实如此。NOP/JIT/RET 并且您的代码不会破坏单线程性能的脆弱性,由 ALL[= 共享(是!共享!!) 27=] 自定义指标(最近 New-MQL4 更新中真正邪恶的反模式...):
int start(){
static aCurrentTIME = EMPTY;
if ( aCurrentTIME == Time[0] ) return 0; // .NOP/JIT/RET --^
aCurrentTIME = Time[0]; // .MOV/LOCK
// -------------------------------------------- // .CALC:
int limit = Bars - counted_bars;
...
/* update just the "HOT"-end has just got into OHLCVT[1]-cells */
// -------------------------------------------- // .FIN
}
视情况而定,您还可以:
datetime calculatedBarTime;
void OnCalculate()
{
if ( calculatedBarTime != Time[0] )
{
onBar();
}
}
void onBar()
{
calculatedBarTime = Time[0];
// on bar logic....
}