anyone can help me with the logic?

 

i'm trying to build an EA, but i still can't get the logic how to implement it..

the condition is like this.

if MA line 1 and MA line 2 cross, it would open a position -> this one i guess i can.

when it reach the specific price i've decided, it will close all opened position -> this one i guess also i can

when the last opened position closed, it will wait until the MA line 1 and MA line 2 cross again, then it will opened another position -> this one i guess also can

but if the last opened position closed at candle 0 AND THEN MA line 1 and MA line 2 cross again at the same candle (candle 0), it will wait to the next candle to check the condition again -> this one is the problem

is there any code that can get the last closed position detail?

 
Have you tried looking through the Mql4-Book and the Code-Base?
 
just only look a glance...but haven found any..just found the code to take the last opened position...
 
Well, you'll need to do more than just a glance. You'll need to become a full fledge mql coder in order to create what you're looking for.
 
lucif:
just only look a glance...but haven found any..just found the code to take the last opened position...

Have you also look thru the sample EA that comes together? Moving Average.mq4?

You can try and build yours from there...

Just a Suggestion

 

i would implement an automate, more or less like this (i don't have the exact synthax in mind) :

integer status;

if openposition is not null then status= 1; // there is open position and you are waiting for the target price to be reached or a buying signal to add to your position

if openposition is null then status= 2; // there is no open position and you are waiting for a buy signal

if status = 2 then and BuySignal then BuyOrder;

Else if status = 1 then

If TargetPrice=CurrentPrice then sellOrder;

Else if Buy signal then buyOrder;

End if

It should work, in case you have opened positions, if the targetprice is reached, you sell and you exit the EA till the next execution.

If the target price is not reached you still check for Buying condition.

Let me know if it works.

 
ebal:

i would implement an automate, more or less like this (i don't have the exact synthax in mind) :

integer status;

if openposition is not null then status= 1; // there is open position and you are waiting for the target price to be reached or a buying signal to add to your position

if openposition is null then status= 2; // there is no open position and you are waiting for a buy signal

if status = 2 then and BuySignal then BuyOrder;

Else if status = 1 then

If TargetPrice=CurrentPrice then sellOrder;

Else if Buy signal then buyOrder;

End if

It should work, in case you have opened positions, if the targetprice is reached, you sell and you exit the EA till the next execution.

If the target price is not reached you still check for Buying condition.

Let me know if it works.

Those are Visual Basic syntax. MQL is built on C/C++
 
ebal:

i would implement an automate, more or less like this (i don't have the exact synthax in mind) :

integer status;

if openposition is not null then status= 1; // there is open position and you are waiting for the target price to be reached or a buying signal to add to your position

if openposition is null then status= 2; // there is no open position and you are waiting for a buy signal

if status = 2 then and BuySignal then BuyOrder;

Else if status = 1 then

If TargetPrice=CurrentPrice then sellOrder;

Else if Buy signal then buyOrder;

End if

It should work, in case you have opened positions, if the targetprice is reached, you sell and you exit the EA till the next execution.

If the target price is not reached you still check for Buying condition.

Let me know if it works.

hi ebal, thanks a lot for your time...

seems like i got your logic

If TargetPrice=CurrentPrice then sellOrder; -> do you mean when this condition fulfilled, it will close the opened order and open another sellorder?

if yes, i guess unfortunately it won't work..because when the status 1, and it reach my TargetPrice, it will close my order..and open sell order (which i don't want)..

the logic i'm trying to figure out is, like when the status 1, and it reach my TargetPrice, it will wait until the next candle open, then check for the BuySignal again (or Sell Signal perhaps)

any idea?

anyway, really appreciate your advice

 

hmmm...is there any code to permanently store a value to a single variable?

for example like when the EA is imported, and price tick, it will run all code stored in int start() and will result a = 123

and when the price tick again, the EA will run again with a value already 123..

i'm sorry if the logic quite confusing, since i'm find it hard to write it down

i've tried to declare the "a" value in int init() which run at the first time EA attached..but seems like when i just put the value "a" in int start(), it will still return me "a - variable not defined"

 

Hi lucif,

I know this is not C++ synthax. What i intented to show you is the implementation of a state automat, which is a powerfull way of coding system when they can be determined by state variables. It seems to be the case with your problem.


like when the status 1, and it reach my TargetPrice, it will wait until the next candle open, then check for the BuySignal again (or Sell Signal perhaps)

=> so may be you can compare target price with the previous candle to determine whether or not it has been reached. But the algo remain more or less the same. If conditions are not fullfilled, you just exit the EA.

May be i've put some SellOrder instead of CloseOrder in the algo. But trust me the underlying logic is very powerfull since it used in realtime system, aircraft for example.


For permanent variables there is two way normally :

- read and write it from a file on drive => you could create getValue and SetValue functions dedicated to it with : variable, filename and path as arguments. It is part of a tool box.

- Define a global variable => will disapear when you exit metatrader


by the way, the totally correct coding would required a switch - case structure, but your case is simple enough to use a if.

 
ebal:

Hi lucif,

I know this is not C++ synthax. What i intented to show you is the implementation of a state automat, which is a powerfull way of coding system when they can be determined by state variables. It seems to be the case with your problem.


like when the status 1, and it reach my TargetPrice, it will wait until the next candle open, then check for the BuySignal again (or Sell Signal perhaps)

=> so may be you can compare target price with the previous candle to determine whether or not it has been reached. But the algo remain more or less the same. If conditions are not fullfilled, you just exit the EA.

May be i've put some SellOrder instead of CloseOrder in the algo. But trust me the underlying logic is very powerfull since it used in realtime system, aircraft for example.


For permanent variables there is two way normally :

- read and write it from a file on drive => you could create getValue and SetValue functions dedicated to it with : variable, filename and path as arguments. It is part of a tool box.

- Define a global variable => will disapear when you exit metatrader


by the way, the totally correct coding would required a switch - case structure, but your case is simple enough to use a if.

Hi ebal,

the one i'm still have been figuring out is about the global variable...but i still don't know how to declare global variable in meta editor..this is quite an obstacle..

anyway, your advice about read and write it from a file on drive, that's quite opening my eyes...thanks a lot..trying to figure out the code first....

Reason: