How to store historic indicator values in an EA

 

Hey guys,

Yesterday I got stuck with my EA. It uses an indicator but what happens is that it obviously changes all the time. However, when in trade i need the ea to use values that were calculated when the trade was opened in order to set exit strategy conditions.

So i created this:

double CurrentVavule=iCustom( blah blah);

double PreviousValue;

if (IsTrade==False) {

PreviousValue=CurrentValue;}

The idea is that this condition will update previousvalue according to currentvalue as long as there is no trade. When a trade is opened, it stops updating and i use previousvalue in pt/sl conditions.

The problem is that it never updates. When i put comment function in the code, i see that currentvalue is just fine but previousvalue always equals zero.

IsTrade is fine as well, since other parts of code also use it and it works there.

What am I missing? Thanks a lot.

 
rookie_forawhile:
Hey guys,

Yesterday I got stuck with my EA. It uses an indicator but what happens is that it obviously changes all the time. However, when in trade i need the ea to use values that were calculated when the trade was opened in order to set exit strategy conditions.

So i created this:

double CurrentVavule=iCustom( blah blah);

double PreviousValue;

if (IsTrade==False) {

PreviousValue=CurrentValue;}

The idea is that this condition will update previousvalue according to currentvalue as long as there is no trade. When a trade is opened, it stops updating and i use previousvalue in pt/sl conditions.

The problem is that it never updates. When i put comment function in the code, i see that currentvalue is just fine but previousvalue always equals zero.

IsTrade is fine as well, since other parts of code also use it and it works there.

What am I missing? Thanks a lot.

Try using

static double PreviousValue;

instead. That way it will keep values from previous ticks on a new tick

 

Thank you mladen for your answer but it still doesn't work. :-/ I still get only 0 values :-(

Now I have this:

double CurrentVavule=iCustom( blah blah);

static double PreviousValue;

if (IsTrade==False){PreviousValue = CurrentValue;}

My assumption is that when the program returns at the beginning the data gets lost.

Again, CurrentValue and IsTrade work just fine.

Any hints appreciated. Thank you.

 
rookie_forawhile:
Thank you mladen for your answer but it still doesn't work. :-/ I still get only 0 values :-(

Now I have this:

double CurrentVavule=iCustom( blah blah);

static double PreviousValue;

if (IsTrade==False){PreviousValue = CurrentValue;}

My assumption is that when the program returns at the beginning the data gets lost.

Again, CurrentValue and IsTrade work just fine.

Any hints appreciated. Thank you.

rookie_forawhile

If you assign a value to that variable at any moment, then it will keep that (non-zero value I assume) value. The fact that it is always 0 can mean that it was never assigned a value different than 0. Attaching a simple indicator that will show you that a value assigned to that static variable is kept between the ticks

Files:
 

Yeah, I see. That is exactly what I need except that instead of having the value 555 I need the static variable to pull updated value from another variable on each tick (or when the program returns, doesnt make much difference for my purpose)

And yes, just like you said, it seems that the value is not even assigned. But why? It should be just fine according to the code I provided and the fact that compiler finishes with no errors at all.

I mean it should be possible to replicate/copy values from one variable to another just by using assign operator = right?

Thanks.

 
rookie_forawhile:
Yeah, I see. That is exactly what I need except that instead of having the value 555 I need the static variable to pull updated value from another variable on each tick (or when the program returns, doesnt make much difference for my purpose)

And yes, just like you said, it seems that the value is not even assigned. But why? It should be just fine according to the code I provided and the fact that compiler finishes with no errors at all.

I mean it should be possible to replicate/copy values from one variable to another just by using assign operator = right?

Thanks.

If this

if (IsTrade==False){PreviousValue=CurrentValue;} is the line where you assign the value, the only cause that it is not assign can be that IsTrade is never false

 

Thank you mladen for your help. The mistake was that I put this condition into another condition where bool value was being assigned to IsTrade.

 
rookie_forawhile:
Thank you mladen for your help. The mistake was that I put this condition into another condition where bool value was being assigned to IsTrade.

Glad that you solved it

Happy coding

 

Hi Mladen,

I think I need your help one more time :-/

I have this:

CurrentValue=iCustom (blah,blah)

bool IsTrade=False;static double PreviousValue;

if(OrderType() <= OP_SELL && OrderSymbol() == Symbol()) { IsTrade = True;} if (IsTrade==False){PreviousValue = CurrentValue;}

The problem is that once I open the first trade, IsTrade changes to true, but it never goes back to false :[ and that causes the PreviousValue to remain the same saved value (after first trade). And of course since IsTrade is always true after first trade then the value of PreviousValue never changes to equal CurrentValue.

Thanks a lot.

 
rookie_forawhile:
Hi Mladen,

I think I need your help one more time :-/

I have this:

CurrentValue=iCustom (blah,blah)

bool IsTrade=False;static double PreviousValue;

if(OrderType() <= OP_SELL && OrderSymbol() == Symbol()) { IsTrade = True;} if (IsTrade==False){PreviousValue = CurrentValue;}

[/PHP]

The problem is that once I open the first trade, IsTrade changes to true, but it never goes back to false :[ and that causes the PreviousValue to remain the same saved value (after first trade). And of course since IsTrade is always true after first trade then the value of PreviousValue never changes to equal CurrentValue.

Thanks a lot.

rookie_forawhile,

Try like this :

[PHP]

CurrentValue=iCustom (blah,blah)

bool IsTrade=False;

static double PreviousValue=-1;

if(OrderType() <= OP_SELL && OrderSymbol() == Symbol())

{

IsTrade = True;

PreviousValue = CurrentValue;

}

 

Thank you for answer mladen.

Hmm, but it doesn't seem to do what I want the program to do.

Like I said last time, I need the PreviousValue to equal CurrentValue only if there are no trades placed. If there is an active trade, I want PreviousValue not to equal currentvalue but equal the last currentvalue before the trade was placed (thats why we made previousvalue static, remember?).

CurrentValue=iCustom (blah,blah)

bool IsTrade=False;

static double PreviousValue=-1;

if(OrderType() <= OP_SELL && OrderSymbol() == Symbol())

{

IsTrade = True;

PreviousValue = CurrentValue;

}

This code seems to set previousvalue equal to current value only when there is a trade active. It is an opposite of what i am trying to achieve.

Thank you.

Reason: