How to code? - page 311

tkuan77
89
tkuan77  

Hi Mladen, as always, u rock!

mladen:
Terrance Use Close[0] in that case. Almost every EA is using close for that purpose, so I think that it will work for you too
Mladen Rakic
186146
Mladen Rakic  

...

If your EA is executed only when the bar starts, it can be a couple of reasons :

- it is limited to work only on a new bar. This approach has some problems if trailing stops and some similar stuff are to be managed, but if it works with fixed stop loss and take profit than it is all the same

- or it is checking signals on a closed bar (which is the most common (and best) way of checking signals). As a consequence, it seems like it is doing it when a new bar is formed while, in fact, it is getting signals on a closed bar that just have been formed. Working on a closed bar signals is the best practice in EAs and if your EA is working like that, better not to change anything

Premeus:
I noticed my EA not running between price moving in M1 time frame. I know that EA will be executed every tick coming in.

But I saw it's was executed every it's start new bar.

Am I misunderstanding ?

Are there the way the force ea activate every tick coming in?

I try to control my profit and loss by use profits and loss not in pips. When spike happen EA not close order when reach to setting profits or loss until new bar occur.

Anyone help me to solve this problem.

I am new for EA coding.

Thank you in advance.
auto_free_prothemeus
137
auto_free_prothemeus  

Thank you, mladen.

I will try your suggestion.

Abdalla Doleh
1344
Abdalla Doleh  

Greetings Coding Gods,

I need assistance in finding an EA function that controls order send execution in that an order cannot be opened if an order already exists at the same price.

simple logic: if order open price and order type = an existing order open price and order type, then exit and look for next entry criteria

Also, i need a function that will prevent an order from being executed if it is x pips away from an already existing price.

simple logic: if order open price and type is < last order open price and type + x pips, then exit and look for next entry criteria

Thanks,

Pip

Mladen Rakic
186146
Mladen Rakic  

...

Try these 2 functions :
First is to find out if there is an order with the exactly same open price already

Second is checking if there is an order that is withing nnn points from the desired price

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

bool existsAtSamePrice(int magicNumber, double price)

{

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));

}

Pip:
Greetings Coding Gods,

I need assistance in finding an EA function that controls order send execution in that an order cannot be opened if an order already exists at the same price.

simple logic: if order open price and order type = an existing order open price and order type, then exit and look for next entry criteria

Also, i need a function that will prevent an order from being executed if it is x pips away from an already existing price.

simple logic: if order open price and type is < last order open price and type + x pips, then exit and look for next entry criteria

Thanks,

Pip
pearl1
18
pearl1  
mladen:
It can be a couple of reasons, but the most common are :
- stop loss and / or take profit are too close to the current price

- your broker is an ECN/STP type broker in which case you first have to first place an order with stop loss and take profit set to 0 and only when an order is opened you can modify the stop loss and / or take profit to the desired values (honoring the previous point of course)

Hi!

Thanks for help!

So, if I undrestand right, if I insert TakeProfit and StopLoss in OrderSend as Exsternal "rules", this EA work right only on Non-ECN brokers?

sorry about my english

Mladen Rakic
186146
Mladen Rakic  

...

Yes

With ECN/STP like brokers you have to open an order with stop loss and / or take profit set to 0 and when the order is already opened, only then you can modify stop loss and / or take profit

pearl1:
Hi!

Thanks for help!

So, if I undrestand right, if I insert TakeProfit and StopLoss in OrderSend as Exsternal "rules", this EA work right only on Non-ECN brokers?

sorry about my english
pearl1
18
pearl1  
mladen:
Yes With ECN/STP like brokers you have to open an order with stop loss and / or take profit set to 0 and when the order is already opened, only then you can modify stop loss and / or take profit

Thanks again, so... how can I fix this? sorry

Mladen Rakic
186146
Mladen Rakic  

...

As I said, you have to do it in 2 steps :

1. step - open an order (using OrderSend()) with stop los and take profit set to 0

2. step - modify that order (using OrderModify()) setting the stop loss and take profit to desired values

So, the code has to be changed to do the job in 2 steps, instead in one

pearl1:
Thanks again, so... how can I fix this? sorry
auto_free_aud4xtrader
24
auto_free_aud4xtrader  

New to coding

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.