Forex
Google

Go Back   Forex Trading > Programming > Metatrader Programming
Forex Forum Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


Register in Forex TSD!
Trading Systems Leaders in this forum (automated trading systems) are winning more than 3000 pips in a month (30000$ investing one lot every time).
Click here to register and get more information

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-23-2008, 10:51 PM
fxbs fxbs is offline
Senior Member
 
Join Date: Jan 2007
Location: not so remote
Posts: 1,574
fxbs is on a distinguished road
Best way to add MaxBarsToCount (History) to the limit?

Dear Admin, would be great if we didn't merge this thread and leave it separate -
question (problem) very common and specific thread much easier for Search
(plus this question allready in general thread, but no answer...)
--------------------------------------------------------------------


When we limit MaxBarsToCount (History) sometimes it require to add Correction, etc

is the best (safest, easiest, universal) way exist? (rule of tumb? dirty triks?)
----------------------

like here we have light fisher 4 stoch smothing:
----------

int start()
{
int counted_bars=IndicatorCounted();
//---- check for possible errors
if(counted_bars<0) return(-1);
int limit=Bars-counted_bars;
if(limit>maxbars)limit=maxbars;
if (limit>Bars-lenth-1)limit=Bars-lenth-1;
//----
for (int shift = limit; shift>=0;shift--)
{
AuxBuffer[shift]=(iStochastic(NULL,0,lenth,2,1,MODE_SMA,0,MODE_MAI N,shift)/100-0.5)
+0.5*AuxBuffer[shift+1];

FishBuffer[shift]= 0.25* MathLog((1+AuxBuffer[shift])/(1-AuxBuffer[shift]))+
0.5*FishBuffer[shift+1];
SignalBuffer[shift]=FishBuffer[shift+1];

}

//----
return(0);
}
------------------------

for fisher limit f-la:

int limit;
int counted_bars=IndicatorCounted();

if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;

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

for Stoch:


int start()
{
int i,k;
int counted_bars=IndicatorCounted();
double price;
//----
if(Bars<=draw_begin2) return(0);
//---- initial zero
if(counted_bars<1)
{
for(i=1;i<=draw_begin1;i++) MainBuffer[Bars-i]=0;
for(i=1;i<=draw_begin2;i++) SignalBuffer[Bars-i]=0;
}
//---- minimums counting
i=Bars-KPeriod;
if(counted_bars>KPeriod) i=Bars-counted_bars-1;
while(i>=0)
{
double min=1000000;
k=i+KPeriod-1;
while(k>=i)
{
price=Low[k];
if(min>price) min=price;
k--;
}
LowesBuffer[i]=min;
i--;
}
....


p.s. in attached indicator, based on clean fisher transform and Stoch; MaxBars (tail) needs to be straighten-up a bit... (when MaxBars out - no problem)

pps. same thing with many others; try to add maxBars to regular (stoc) macd and look on the tail...
Attached Files
File Type: mq4 test_Trans_Stoch_smz_limitMaxBars_test.mq4 (2.8 KB, 11 views)

Last edited by fxbs : 01-23-2008 at 10:58 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 02:59 AM
fxbs fxbs is offline
Senior Member
 
Join Date: Jan 2007
Location: not so remote
Posts: 1,574
fxbs is on a distinguished road
thank you everybody who helped!
here it is:

when we have no arrays - works basic formula:

limit = MathMin(limit, MaxBarsToCount);

but when we have 4ex. MAonArray - we need more bars (MA period) to count first MaonArray bar:
MaxBarsToCount + MA period

macd limit test3.gif

so, two ways possible- count more or draw less:

- draw MaxBarsToCount but count more
limit = MathMin(limit, MaxBarsToCount)+ MA period

- or count the same but draw less
SetIndexDrawBegin(i,Bars-MaxBarsToCount+MA period);

_____


in all cases SetIndexDrawBegin in "int start()" not at "init()" section

for (i=0;i<indicator_buffers;i++) SetIndexDrawBegin(i,Bars-MaxBarsToCount + MA period);
return(0);
}
Attached Files
File Type: mq4 MACD_limit_test1.mq4 (3.0 KB, 2 views)
File Type: mq4 MACD_limit_test2a.mq4 (2.6 KB, 2 views)
File Type: mq4 MACD_limit_test2b.mq4 (2.6 KB, 2 views)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 03:00 AM
fxbs fxbs is offline
Senior Member
 
Join Date: Jan 2007
Location: not so remote
Posts: 1,574
fxbs is on a distinguished road
________

whole thing 4 MAGD:
PHP Code:

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- drawing settings
   
SetIndexStyle(0,DRAW_HISTOGRAM);
   
SetIndexStyle(1,DRAW_LINE);
   
SetIndexDrawBegin(1,SignalSMA);
   
IndicatorDigits(Digits+1);
//---- indicator buffers mapping
   
SetIndexBuffer(0,MacdBuffer);
   
SetIndexBuffer(1,SignalBuffer);
//---- name for DataWindow and indicator subwindow label
   
IndicatorShortName("MACD("+FastEMA+","+SlowEMA+","+SignalSMA+")");
   
SetIndexLabel(0,"MACD");
   
SetIndexLabel(1,"Signal");
   
   
//
   //
   //    two aproaches
   //     make an BarsToDraw that is going to be < MaxBarsToCount
   //
   //    or when setting limit add max (calculated here) to MaxBarsToCount
   //    calculate MaxBarsToCount+max and than 
   //   draw just MaxBarsToCount on chart
   //
   //
   
   
int max MathMax(FastEMA,SlowEMA);
       
max MathMax(SignalSMA,max);
       
BarsToDraw MaxBarsToCount-max;
//---- initialization done
   
return(0);
  }
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence                           |
//+------------------------------------------------------------------+
int start()
  {
   
int limit;
   
int counted_bars=IndicatorCounted();
//---- last counted bar will be recounted
   
if(counted_bars>0counted_bars--;
   
limit=Bars-counted_bars;
   
limit MathMin(limitMaxBarsToCount);

//---- macd counted in the 1-st buffer
   
for(int i=0i<limiti++)
      
MacdBuffer[i]=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-
                   
iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
//---- signal line counted in the 2-nd buffer
   
for(i=0i<limiti++)
      
SignalBuffer[i]=iMAOnArray(MacdBuffer,Bars,SignalSMA,0,MODE_SMA,i);

   
//
   //
   //    don't draw everything
   //
   //

   
for (i=0;i<indicator_buffers;i++) SetIndexDrawBegin(i,Bars-BarsToDraw);   
   return(
0);
  }
//+------------------------------------------ 

Last edited by fxbs : 03-01-2008 at 05:22 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-29-2008, 09:41 PM
fxbs fxbs is offline
Senior Member
 
Join Date: Jan 2007
Location: not so remote
Posts: 1,574
fxbs is on a distinguished road
another way - but that's - dirty trick:
simple macd;

only "-SignalSMA" added - that's it

//---- signal line counted in the 2-nd buffer
for(i=0; i<limit-SignalSMA; i++)

before:
PHP Code:
int start()
  {
   
int limit;
   
int counted_bars=IndicatorCounted();
//---- last counted bar will be recounted
   
if(counted_bars>0counted_bars--;
   
limit=Bars-counted_bars;
   
limit MathMin(limitMaxBarsToCount);

//---- macd counted in the 1-st buffer
   
for(int i=0i<limiti++)
      
MacdBuffer[i]=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-
iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
//---- signal line counted in the 2-nd buffer
   
for(i=0i<limiti++)
      
SignalBuffer[i]=iMAOnArray(MacdBuffer,0,SignalSMA,0,MODE_SMA,i);
//---- done
   
return(0);
  }
//+------------------------------------------------------------------+ 
after

PHP Code:
//+------------------------------------------------------------------+
int start()
  {
   
int limit;
   
int counted_bars=IndicatorCounted();
//---- last counted bar will be recounted
   
if(counted_bars>0counted_bars--;
   
limit=Bars-counted_bars;
   
limit MathMin(limitMaxBarsToCount);

//---- macd counted in the 1-st buffer
   
for(int i=0i<limiti++)
      
MacdBuffer[i]=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-
iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
//---- signal line counted in the 2-nd buffer
   
for(i=0i<limit-SignalSMAi++)
      
SignalBuffer[i]=iMAOnArray(MacdBuffer,0,SignalSMA,0,MODE_SMA,i);
//---- done
   
return(0);
  }
//+------------------------------------------------------------------+ 
macd limit test3 3lim.gif

not cleanest solution, but ....
(as been mentioned- dirty trick; and TRO can use it for his book...) : - )))))))))
____________

P.S.

whoops - people say this slaking approach no good: limit-SignalSMA can be negative - not refreshing, etc, etc...

whoopsy daisy ... what to do?
____________

gets trickier and trickier...

MaxBarsToCount instead of limit - should take care of it?

for(i=0; i<MaxBarsToCount -SignalSMA; i++)
instead of
for(i=0; i<limit-SignalSMA; i++)

-sure, but we sacrificing our beloved limit....

well, gets even more trickier:

PHP Code:
   limit=Bars-counted_bars;
   
limit MathMin(limitMaxBarsToCount);
//---- macd counted in the 1-st buffer
   
for(int i=0i<limit+SignalSMAi++)
      
MacdBuffer[i]=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-
iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
//---- signal line counted in the 2-nd buffer
//   for(i=0; i<limit-SignalSMA; i++)
     
for(i=0i<limiti++)
      
SignalBuffer[i]=iMAOnArray(MacdBuffer,Bars,SignalSMA,0,MODE_SMA,i);
//---- done 

so: for(int i=0; i<limit+SignalSMA; i++)
they wouldn't call it "dirty trick" for nothing...
to do it right - everybody can; - try to do it ... different.... : ))))

new update: now - even more less dirtier (setting limit4 each buffer):
PHP Code:
   limit=Bars-counted_bars;
   
limit MathMin(limitMaxBarsToCount);

//---- macd counted in the 1-st buffer
   
for(int i=0i<limiti++)
      
MacdBuffer[i]=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-
        
iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
//---- signal line counted in the 2-nd buffer
   
limit MathMin(limitMaxBarsToCount-SignalSMA); 
   for(
i=0i<limiti++)
  
      
SignalBuffer[i]=iMAOnArray(MacdBuffer,Bars,SignalSMA,0,MODE_SMA,i);
//---- done
   
return(0); 
1. limit = MathMin(limit, MaxBarsToCount);
...
2. limit = MathMin(limit, MaxBarsToCount-SignalSMA);

!nobody messes with our Limit!! : ((((

Last edited by fxbs : 03-01-2008 at 08:14 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-29-2008, 11:49 PM
Michel Michel is offline
Senior Member
 
Join Date: Feb 2006
Posts: 502
Michel is on a distinguished road
Quote:
Originally Posted by fxbs View Post
another way - but that's - dirty trick:
...
There is no ONE best solution.
It depends of what you want to do: for example, some indics must run at every tick of bar 0, but some other needs to run only once at the open of the bar, it's stupid to let them run at each tick; some cannot run the last "x" bars of the history because they need the datas, some run from the past to the present and some others no, and so on.
The best solution is the one YOU understand !
I must admit that sometime it's very tricky...
I such cases, in order to test how it works, I suggest you to add a command inside the "for" loop to print or comment the bar number, for example :

for(int i = limitblablabla...)
{
Print(i + " " + Bars + " " + IndicatorCounted());

In a generic case, if you only want to add a MaxBars count at a working indic , the easiest way is to add at the first line of the "for" loop this line :

for(int i = limitblablabla...) //original command , no need to understand it
{
if(i > MaxBars) continue;

....
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 03-01-2008, 12:05 AM
fxbs fxbs is offline
Senior Member
 
Join Date: Jan 2007
Location: not so remote
Posts: 1,574
fxbs is on a distinguished road
perky's trick - eft sto smz (post1) tale cut-off
Attached Images
File Type: gif sto eft smz maxbars.gif (15.0 KB, 102 views)
Attached Files
File Type: mq4 test_Trans_Stoch_smz_limitMaxBars_test2.mq4 (2.9 KB, 6 views)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 03-01-2008, 05:39 AM
fxbs fxbs is offline
Senior Member
 
Join Date: Jan 2007
Location: not so remote
Posts: 1,574
fxbs is on a distinguished road
Quote:
Originally Posted by Michel View Post
There is no ONE best solution.
It depends of what you want to do: for example, some indics must run at every tick of bar 0, but some other needs to run only once at the open of the bar, it's stupid to let them run at each tick; some cannot run the last "x" bars of the history because they need the datas, some run from the past to the present and some others no, and so on.
The best solution is the one YOU understand !
I must admit that sometime it's very tricky...
I such cases, in order to test how it works, I suggest you to add a command inside the "for" loop to print or comment the bar number, for example :

for(int i = limitblablabla...)
{
Print(i + " " + Bars + " " + IndicatorCounted());

In a generic case, if you only want to add a MaxBars count at a working indic , the easiest way is to add at the first line of the "for" loop this line :

for(int i = limitblablabla...) //original command , no need to understand it
{
if(i > MaxBars) continue;

....
Thanks a lot, Michel!
- that's how we learn, day by day
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 03-01-2008, 06:53 AM
fxbs fxbs is offline
Senior Member
 
Join Date: Jan 2007
Location: not so remote
Posts: 1,574
fxbs is on a distinguished road
Hey, Michel!
how to use this line:
if(i > MaxBars) continue;
i tryed - no go...

but if put like this:
if(i < MaxBarsToCount)
- works (MACD):

PHP Code:
int start()
  {
   
int limit;
   
int counted_bars=IndicatorCounted();
//---- last counted bar will be recounted
   
if(counted_bars>0counted_bars--;
   
limit=Bars-counted_bars;

//---- macd counted in the 1-st buffer
   
for(int i=0i<limiti++)
   if(
MaxBarsToCount)
   
      
MacdBuffer[i]=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-
iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
//---- signal line counted in the 2-nd buffer
   
for(i=0i<limiti++)
      if(
MaxBarsToCount-SignalSMA

      
SignalBuffer[i]=iMAOnArray(MacdBuffer,Bars,SignalSMA,0,MODE_SMA,i);
//---- done
   
return(0); 
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 03-01-2008, 07:38 AM
Michel Michel is offline
Senior Member
 
Join Date: Feb 2006
Posts: 502
Michel is on a distinguished road
Quote:
Originally Posted by fxbs View Post
Hey, Michel!
how to use this line:
if(i > MaxBars) continue;
i tryed - no go...

but if put like this:
if(i < MaxBarsToCount)
- works (MACD):

PHP Code:
int start()
  {
   
int limit;
   
int counted_bars=IndicatorCounted();
//---- last counted bar will be recounted
   
if(counted_bars>0counted_bars--;
   
limit=Bars-counted_bars;

//---- macd counted in the 1-st buffer
   
for(int i=0i<limiti++)
   if(
MaxBarsToCount)
   
      
MacdBuffer[i]=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-
iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
//---- signal line counted in the 2-nd buffer
   
for(i=0i<limiti++)
      if(
MaxBarsToCount-SignalSMA

      
SignalBuffer[i]=iMAOnArray(MacdBuffer,Bars,SignalSMA,0,MODE_SMA,i);
//---- done
   
return(0); 
both should do the same, but the problem here is that you don't use the brackets for the for loop because there is only one command.
"if(i < MaxBarsToCount) MacdBuffer[i]=iMA(NULL,0,FastEMA... ;" is only one command ( there is only one ";" ) so the for loop doesn't need any brackets.
Now, the use "if(i >MaxBarsToCount) continue;" means one more command in the for loop, so you have to use brackets to group them together.
so :
for(int i=0; i<limit; i++)
if(i > MaxBarsToCount) continue;
does'nt work because there is only one command, the if test line, which does nothing.
but
for(int i=0; i<limit; i++)
{
if(i > MaxBarsToCount) continue;
MacdBuffer[i]=iMA(NULL,0,FastEMA,0,MODE blabla ;
blahblahblah...;
}
works.

I a general case, the for loop use brackets, thus "if(i > MaxBarsToCount) continue;" is ready to use (you do not need to add other brakets or modify the code)

Last edited by Michel : 03-01-2008 at 07:42 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 03-01-2008, 09:35 AM
fxbs fxbs is offline
Senior Member
 
Join Date: Jan 2007
Location: not so remote
Posts: 1,574
fxbs is on a distinguished road
Thank you, Michel!
well, i did as you sayed -
PHP Code:
//---- signal line counted in the 2-nd buffer
   
for(i=0i<limiti++)
   {
    if(
MaxBarsToCount) continue; 
SignalBuffer[i]=iMAOnArray(NULL,0,FastEMA,0,MODE blabla ;
blahblahblah...;
     }
//---- done 

works good but when onArray - old story - steping on tail
(maybe should have use more braskets: )

macd maxbars limit.gif

well, 4 onArray buffer :
if(i > MaxBarsToCount - SignalSMA) continue;
and that's it! : ))))))))
PHP Code:
int start()
  {
   
int limit;
   
int counted_bars=IndicatorCounted();
//---- last counted bar will be recounted
   
if(counted_bars>0counted_bars--;
   
limit=Bars-counted_bars;
//  limit = MathMin(limit, MaxBarsToCount);
//---- macd counted in the 1-st buffer
   
for(int i=0i<limiti++)
  {
if(
MaxBarsToCount) continue;   
      
MacdBuffer[i]=iMA(NULL,0,FastEMA,0