How to stop expert advisor from trading, when profit is made

 

Hi I have written a code for auto trade in Expert Adviser. My autotrade work fine. it open trade and close trade.

My problem is, when a buy / sell position is closed. The EA automatically open another position in fewminitues, or in few hours time. That cause me to lost a lot of money, as the EA trade too frequently and too unecessary.

How to I stop the EA from executing buy & sell order, when I have made my profit for the day.

I have use the Sleep(1440000) command, but it doestn't work at all.

Example, if I have already made 50 pips profit for the day. I want to stop the EA from trading further.

As I cannot monitor the system 24 hrs a day, thus I written a automation program. I do expect some command to stop the EA from executing trade, once the profit is made for the day.

Susilo

 

The only way I can think of it to read the order history and disallow new trades if orders have been placed within X hours. I'm sure there is an easier way to do it, but I don't know of any.

 

Before placing an order, read all the completed orders for the day, adding up OrderProfit(). The profit will be in dollars. If the target has been reached then don't open another order.

 

ryanklefas,

couldn't your Swiss Army EA be used for this purpose?

(by the way, this is my first post....)

 
eastcity:
ryanklefas,

couldn't your Swiss Army EA be used for this purpose?

(by the way, this is my first post....)

Yes and no. It COULD be used in that fashion, but it doesn't currently read historic orders and also has no direct control over other EAs; I may in time be able to make it capable of telling other EAs what they can and cannot do.

 

one way is to declair a bool at the top or in init? to kill it after one trade if it won or lost but there is prolly a better way

then hitting the reset button i would think should start it over to make one trade again

int init()

bool stoptrade=false

_____________________

if (stoptrade==false)

{

if (logic)sell

if (logic)buy

stoptrade=true

}

 
lowphat:
one way is to declair a bool at the top or in init? to kill it after one trade if it won or lost but there is prolly a better way

then hitting the reset button i would think should start it over to make one trade again

int init()

bool stoptrade=false

_____________________

if (stoptrade==false)

{

if (logic)sell

if (logic)buy

stoptrade=true

}

Simple. And it could work. Then you could reset the boolean when a new day starts, at say:

Hour()==0 and Minute()==0.

 
ryanklefas:
Simple. And it could work. Then you could reset the boolean when a new day starts, at say: Hour()==0 and Minute()==0.

Ryan, ticks do not always arrive at least once every minute. When that happens at 00:00, this would skip this EA to trade that day.

Perhaps it's batter to compare the daily bar timestamp: iTime(NULL,PERIOD_D1,0)

Lowphat's code would then be something like:

int init()

bool stoptrade=false;

datetime DailyBar=0;

if (stoptrade==false && DailyBar != iTime(NULL,PERIOD_D1,0))

{

if (logic)sell

if (logic)buy

stoptrade=true

DailyBar=iTime(NULL,PERIOD_D1,0);

}

 

if init() executes only once when EA started then it is of no use if one wants an EA to resume working say in 10 minutes.

Unfortunately there is also a problem with OrderCloseTime() when a new tick happens immediately after an order was closed.

euro

 
european:
if init() executes only once when EA started then it is of no use if one wants an EA to resume working say in 10 minutes.

Unfortunately there is also a problem with OrderCloseTime() when a new tick happens immediately after an order was closed.

euro

Init() is only used to initialize the variables. This is intended to only be executed once.

To use this mechanism for shorter periods of restart time you could use the lower bartime like 15 or 30 minutes. e.g. iTime(NULL,PERIOD_M15,0)

For the OrderCloseTime() you are right; when just before the bar changes, the order is closed.

This depends however on how "next period" is defined.

If you want to restart at 24:00 just use the daily bar.

If you want to restart 24 hours after the last order was closed, then use 24 "hour-bars"

Cheers

Reason: