
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
...
How is your LastProfitHigh declared?
If it is declared within the body of the function (start() for example) it will always change value if you do not declare it as static. So, if it is declared as local (within the function) try declaring it at the global scope or as static variable and that way it will "inherit" values between two ticks.
Hi all,
I am new to coding and have been slowly learning the language and syntax but have come up with a question about how MT4 updates the variables.
I have tried to code and simple EA that displays the highest profit a currently open trade has had and should only update if a new high is reached but what is happening is the amount is moving down and up as the profit does, I can't seem to see my error in the code logic.
I check to make sure to selected trade is still open
t_CloseTime=OrderCloseTime(); //returns 0 if order is not closed
if(t_CloseTime==0) //Order is closed if not zero.
{
if(OrderProfit() > LastProfitHigh) LastProfitHigh=OrderProfit();
if(LastProfitHigh >= MinProfit && MinProfitReached==false) MinProfitReached=true;
}//endif
So if the logic is correct why does this variable "LastProfitHigh" go up and down....
Thanks for your help.New to coding
How is your LastProfitHigh declared? If it is declared within the body of the function (start() for example) it will always change value if you do not declare it as static. So, if it is declared as local (within the function) try declaring it at the global scope or as static variable and that way it will "inherit" values between two ticks.
Hi mladen,
I have now moved the variable outside the start() func and declared it "static double", thanks for the answer and fingers crossed it will display correctly.
The logic of the code is stated correctly??
aud4xtrader
The logic is OK
If you move the variable outside the start then you don't need to declare it static (it is automatically a global variable when outside of any function body - and it is "visible" through the whole code) If you want the variable to keep local to some function and still keep previous state of it (the state on a previous tick, for example) then you declare it as static
Here are two examples that will end in the same result
int start()
{
...
if (OrderProfit()>previiousProfit) previousProfit = OrderProfit();
}
[/PHP]
or the same thing but with static variable
[PHP]
int start()
{
static double previousProfit=0;
...
if (OrderProfit()>previiousProfit) previousProfit = OrderProfit();
}
Hi mladen,
I have now moved the variable outside the start() func and declared it "static double", thanks for the answer and fingers crossed it will display correctly.
The logic of the code is stated correctly??How to code?
It works great, thanks again for the excellent help.
aud4xtrader
The logic is OK
If you move the variable outside the start then you don't need to declare it static (it is automatically a global variable when outside of any function body - and it is "visible" through the whole code) If you want the variable to keep local to some function and still keep previous state of it (the state on a previous tick, for example) then you declare it as static
Here are two examples that will end in the same result
int start()
{
...
if (OrderProfit()>previiousProfit) previousProfit = OrderProfit();
}
[/PHP]
or the same thing but with static variable
[PHP]
int start()
{
static double previousProfit=0;
...
if (OrderProfit()>previiousProfit) previousProfit = OrderProfit();
}
Perfect! Thanks.
Try these 2 functions :
Both return true if the price is equal or if the distance from the price of some currently opened orders is withing the distance specified
{
for (int i=OrdersTotal()-1; i>=0 ; i--)
{
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
if (OrderSymbol()!=Symbol()) continue;
if (OrderMagicNumber()!=magicNumber) continue;
if (CompareDouble(OrderOpenPrice(),price)) return(true);
}
return(false);
}
//
//
//
//
//
bool existsAtApproximatePrice(int magicNumber, double price, double distance)
{
for (int i=OrdersTotal()-1; i>=0 ; i--)
{
if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
if (OrderSymbol()!=Symbol()) continue;
if (OrderMagicNumber()!=magicNumber) continue;
if (MathAbs(OrderOpenPrice()-price)< distance) return(true);
}
return(false);
}
//
//
//
//
//
bool CompareDouble(double val1, double val2)
{
return(NormalizeDouble(val1,Digits)==NormalizeDouble(val2,Digits));
}
How to code?
Hello Pip,
Thanks for the code snippets, it is great the way people can share ideas and coding styles, as my knowledge grows I can draw lessons from people like you and mladen.
Perfect! Thanks.
Hi,
i have a request.
If i want to delete all object that contain in the name the word "example" with ObjectDelete command how i must write the code?
For Example
Object name
"Example13241"
"Example453623"
i want to delete all object that containt the word "Example"
Thank you very much
...
Use something like this (this will delete all objects that have names that begin with "Example") :
int lookForLength = StringLen(lookFor);
for (int i=ObjectsTotal()-1; i>=0; i--)
{
string objectName = ObjectName(i);
if (StringSubstr(objectName,0,lookForLength) == lookFor) ObjectDelete(objectName);
}
Hi,
i have a request.
If i want to delete all object that contain in the name the word "example" with ObjectDelete command how i must write the code?
For Example
Object name
"Example13241"
"Example453623"
i want to delete all object that containt the word "Example"
Thank you very muchUse something like this (this will delete all objects that have names that begin with "Example") :
int lookForLength = StringLen(lookFor);
for (int i=ObjectsTotal()-1; i>=0; i--)
{
string objectName = ObjectName(i);
if (StringSubstr(objectName,0,lookForLength) == lookFor) ObjectDelete(objectName);
}
Thank you it's perfect.
I have another question ^^.
I create a segment of trend line in the chart with declared lenght. I can set the color and it's ok. I would know if it is possible to set more then 1 color in the same trend line.
For example for the first 1/3 part one color, for the 2/3 part another color and for 3/3 part another one.
It is possible?
Thank you^^
..
In short, if you are referring to objects, the answer is no. You would have to create 3 objects for that
If you are using buffers for that, then you would need 5 drawing buffers for a 3 color non-repainting line, and since there are only 8 drawing buffers, it limits you significantly
Thank you it's perfect.
I have another question ^^.
I create a segment of trend line in the chart with declared lenght. I can set the color and it's ok. I would know if it is possible to set more then 1 color in the same trend line.
For example for the first 1/3 part one color, for the 2/3 part another color and for 3/3 part another one.
It is possible?
Thank you^^