Only One Trade

 


Fellow Coders:

Any ideas on how to have my EA stop trading once it has closed out its first trade of the day?

Thanks,

pat

 

Use a global variable (defined at top of code, before init)… for example:

bool TradeOpened=false;

Then in start, put something like this

if (Hour()==0&&Minute()==1) TradeOpened=false;

// you can set Hour()== to whatever hour you want the “day” to start over

When a trade is to be opened, try something like:

if (TradeOpened==false) {

//Open the trade

TradeOpened=true;

}

This will only allow one trade until TradeOpened is reset at whatever hour and minute you choose above.

Hope this helps

 
FXpipclash:

Any ideas on how to have my EA stop trading once it has closed out its first trade of the day?

In general terms, you simply need to compare the current time with some sort of record of the last trade closure, and not place a new trade if the two timestamps fall within the same day. The practicalities, and the best way of doing this, will depend on three details: (a) whether you ever allow trades to continue overnight, (b) whether you can have multiple trades active at once, but want to stop new trades after the first of the existing trades closes, and (c) whether you're defining "day" according to local or broker time (if there's a difference).


There are broadly going to be three ways of recording the time of the last closure, to compare against the current time:

  • When a trade closes, store the current time in a variable within the EA. This record will be lost if the EA is restarted or reloaded.
  • When a trade closes, store the current time in a MetaTrader global variable. This persists across restarts of the EA and of MetaTrader.
  • Continually recalculate the last trade-close time from the MetaTrader order list when considering whether to place a new trade.

The limitation with the third option is that the time which the order list gives you is broker time, not (necessarily) local time. This may make life difficult if you want to interpret "day" based on your local time rather than broker time. The first two routes avoid this problem because you can choose whether you record local or broker time.

 

When a new day starts, set a variable, like:


TradeOpenAllowed = true;


Check for "TradeOpenAllowed" before opening an order and set "TradeOpenAllowed = false;" when you make the order.


Good Luck,

Raider

 
kctrader:

[...] Then in start, put something like this

if (Hour()==0&&Minute()==1) TradeOpened=false;

// you can set Hour()== to whatever hour you want the “day” to start over

This contains an assumption that there will always be a tick within the first minute of the day (or of a particular hour). Personally, I wouldn't regard this assumption as safe.

 
kctrader wrote >>

[...] Then in start, put something like this

if (Hour()==0&&Minute()==1) TradeOpened=false;

// you can set Hour()== to whatever hour you want the “day” to start over

This contains an assumption that there will always be a tick within the first minute of the day (or of a particular hour). Personally, I wouldn't regard this assumption as safe.

...It also assumes that a trade won't be placed in the first minute of the day. If one is placed, then the TradeOpened flag will be incorrectly cleared, because Hour()==0&&Minute()==1 is true throughout the first minute, not just at the start of it. It's generally much safer to compare times than to rely on a flag which gets periodically reset.

 
jjc wrote >>

This contains an assumption that there will always be a tick within the first minute of the day (or of a particular hour). Personally, I wouldn't regard this assumption as safe.

good point. change to:

if (Hour()>=0&&Minute()>=1) TradeOpened=false;

 
jjc wrote >>

...It also assumes that a trade won't be placed in the first minute of the day. If one is placed, then the TradeOpened flag will be incorrectly cleared, because Hour()==0&&Minute()==1 is true throughout the first minute, not just at the start of it. It's generally much safer to compare times than to rely on a flag which gets periodically reset.

also a good point. this IS based on that assumption. i would recommend choosing a "reset" hour and minute for a time that you know a trade will not be executed.

if the strategy is one that could execute a trade anytime, it might be worth coding something like what jjc suggested above.

 
kctrader:

good point. change to:

if (Hour()>=0&&Minute()>=1) TradeOpened=false;

I don't want to sound difficult, but this plays off one problem against the other. This now contains an assumption that no trades will be placed during the first hour, with the exception of the first minute. 


Let's say that a trade is placed at 00:30 and TradeOpened is set to true. Thereafter, for the rest of the first hour, the above code will incorrectly reset TradeOpened to false. That's why I'm recommending comparison of dates. Otherwise, you've got two dangerous assumptions, and reducing the risk in one increases the risk in the other.

 
jjc wrote >>

I don't want to sound difficult, but this plays off one problem against the other. This now contains an assumption that no trades will be placed during the first hour, with the exception of the first minute.

Let's say that a trade is placed at 00:30 and TradeOpened is set to true. Thereafter, for the rest of the first hour, the above code will incorrectly reset TradeOpened to false. That's why I'm recommending comparison of dates. Otherwise, you've got two dangerous assumptions, and reducing the risk in one increases the risk in the other.

Realizing this, if this were my own EA, I would def use jjc's suggestion. Hopefully this thread is helpful to the opener. It has been helpful for me.

 

Dear Friends, G'day

I don't whether this is right thread to post.. Sorry, if it is wrong thread.

Can anyone help me here? I want one simple Indicator where that indicator is 2 horizontal lines trails every minute with reference to low & high of the same day. I use this one as a reversal lines. Is there anything like?

Keep Charging

Be Awesome,
Raj,
DreamJOBZ.com

Reason: