How to stop triggering on a re-print?

 

I'm currently setting up a buy order when a certain condition is true.  The problem I'm running into is the stoploss is triggering and then trying to re-purchase the buy order right away.

So, the end result is multiple trades per day that is just eroding the capital.

How can I put something in my code that basically says...  "When s/l triggered, don't buy anything else today"?

 
jthornton: I'm currently setting up a buy order when a certain condition is true.
Don't do that. Open the order when you get a change in condition.
static bool isBuyCondition=false;
bool wasBuyCondition = isBuyCondition;
isBuyCondition = ...;
if(!wasBuyCondition && isBuyCondition) // Now open.
 
whroeder1:
Don't do that. Open the order when you get a change in condition.

So let's go with a more specific situation:

if close[0] is > than MA(5) --> BUY

if close[0] is <= SL --> SELL

The problem is arising that on the same day the price will drop below the SL and then pop back up and re-purchase.  I know one solution is to not set the SL so close, but I also want to add in "Don't BUY if it was closed today".

Can you show me a more specific example using these parameters?

 
jthornton:

I'm currently setting up a buy order when a certain condition is true.  The problem I'm running into is the stoploss is triggering and then trying to re-purchase the buy order right away.

So, the end result is multiple trades per day that is just eroding the capital.

How can I put something in my code that basically says...  "When s/l triggered, don't buy anything else today"?

When you open a trade for the day, set a variable containing the dateTime value for the daily candle.

https://docs.mql4.com/series/itime

iTime() will tell you information you need.

Then just check that before you open a new order.


iTime - Timeseries and Indicators Access - MQL4 Reference
iTime - Timeseries and Indicators Access - MQL4 Reference
  • docs.mql4.com
iTime - Timeseries and Indicators Access - MQL4 Reference
 
Jack Thomas:

When you open a trade for the day, set a variable containing the dateTime value for the daily candle.

https://docs.mql4.com/series/itime

iTime() will tell you information you need.

Then just check that before you open a new order.


The variable carries the value from bar to bar?
 
jthornton: "Don't BUY if it was closed today".
If you want that, code that specifically.
  1. Go through history and find the latest order open time.
  2. Compare it to today's date. See Draw rectangle around range of bars by hours - MQL4 and MetaTrader 4 - MQL4 programming forum
Reason: