Storing a value and changing that value?

 

I wondered if any bright spark here nos how I can store a value I get for a new bar and then for the next bar that closes, add to this stored value and then keep the new value?

What im trying to do is to find the difference in close price for bar1 and bar2, so bar1 - bar2 = difference in close price. I then want to store this value I get. Lets say its 10 pips. When a new bar closes and shifts to bar1, I would like to do the same again, find the close diffrence of the new bar1 and bar2. Lets say its 5 pips. So now I want to use the stored value before and add the new value of 5 pips to it. This should = 10 + 5 = 15 pips. Id like to carry on doing this for every new bar that closes. and plot it on extmapbuffer1 as line chart.

Any help would be grately appreciated.

 

i have made an indicator that kind of does what I want but not accuratly enough. I use this:


close1 = iClose(pair1,0, i );

Close2 = iClose(pair1,0,i+24);


closediff = close1 - close2;


The only problem with this is it will work out the difference for those 24 bars, but when the new bar comes in it doesnt add to the value I already have, so it actually different to the real cummulative close difference that Im after.


All I really need to know is how to store this value and then re-use it and keep re-using it and storing the new value I get.

 

You want to add the new value to the previous stored value in close1?

close1 = close1 + iClose(pair1,0, i );

close2 = iClose(pair1,0,i+24);

closediff = close1 - close2;



That should do the trick.

 

Cool, that looks like the kind of thing i need. Im actualy trying to add each new bars 'closediff' as coded above, rather then the close1.

I think what i need is somthing like this:


if(TimeCurrent() = Time[0])

{

close1 = iClose(pair1,0,1);

close2 = iClose(pair1,0,2);


closediff = close1 + close2;

}


Else(TimeCurrent() = Time[i])

{

close1 = iClose(pair1,0,1);

close2 = iClose(pair1,0,2);


closediff2 = closediff + (close1 - close2);

}



ExtMapBuffer1 = Closediff2



What i have tried to get is the bar1 and bar2 close difference in the if statment. Then in the else statement try to get the next bar1 and bar2 close differnece and add it to the first difference.

But i am unsure of tracking the time for each close of bar. Also I dont think this will store the value each time a bar closes and update, instead it will just add together two sets of bar1 and bar2 close difference.



Thanks a lot for your help, are u able to help me more with that? i think it is very close.

 

Also I have tried this, but this doesnt work. My problem is adding to a stored value to each time there is a new bar that closes. then storing the new value.




#property copyright "fry2010"
#property link ""

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Blue
#property indicator_color2 Red
#property indicator_color3 White


//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];

extern int barcount = 3000;
extern string pair1 = "EURUSD";
extern string pair2 = "USDCHF";

//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,ExtMapBuffer1);
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,ExtMapBuffer2);
SetIndexStyle(2,DRAW_LINE);
SetIndexBuffer(2,ExtMapBuffer3);

return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+


bool New_Bar=false; // Flag of a new bar
//--------------------------------------------------------------------
int start() // Special funct. start()
{


double close1, close_1, result1[], close2, close_2;
int counted_bars=IndicatorCounted(),
limit ;

if(counted_bars>0)
counted_bars--;


limit=Bars-counted_bars;

if(limit>barcount)
limit=barcount;

for(int i=0;i<limit;i++)
{

Fun_New_Bar(); // Function call
if (New_Bar==false) // If bar is not new..
return;

Fun_New_Bar();
if (New_Bar == true)
{
close1 = iClose(pair1,0,i+1);
close_1 = iClose(pair1,0,i+2);

result1[i] = close1 - close_1;

}
ExtMapBuffer1[i] = result1[i] + result1[i];
}


return; // Exit start()
}



//--------------------------------------------------------------------
void Fun_New_Bar() // Funct. detecting ..
{ // .. a new bar
static datetime New_Time=0; // Time of the current bar
New_Bar=false; // No new bar
if(New_Time!=Time[0]) // Compare time
{
New_Time=Time[0]; // Now time is so
New_Bar=true; // A new bar detected
}
}

//----
//+---
Reason: