Opening one trade per candle or until a certain time passed

 

Hi,

I found this nice Ea, it enters a trade at the first psar signal. Then it exits at the reverse signal on a second psar. The only problem is that it enters multiple trades on a single candle if the 2 psars are to close. I tried figuring out how to make it only take one trade per candle or give it some sort of time delay before making another trade, but with no avail.

Which part of the code do i need to modify, and how do i need to modify it to limit the amount of trades per candle to one trade, or maybe give it some sort of time delay before making another trade? As you can tell, i am no coder.

Or, would someone be kind enough to make it only trade once per candle? It would really be appreciated.

I have included a screenshot to show you the problem. And i have also put the EA in question.

Thank you in advance for your reply and help, any help is greatly apriciated.


cheers,

Ramy

 

Hi Ramy,

example for timelevel D1 (1440) (code compares day, month, and year):


str date_of_prev_candle="";


start

{

string Datum = TimeDay(iTime(NULL,0,0)) + "." + TimeMonth(iTime(NULL,0,0)) + "." + TimeYear(iTime(NULL,0,0)); // get day of new tick
if (date_of_prev_candle!=Datum) //if different from previous (new candle in D1)

{

date_of_prev_candle = Datum; //take down current date to compare

// do your code

}

}


I didn't compile it, but in principle, it should work like this!


Hotte300

 
Hotte300 wrote >>

Hi Ramy,

example for timelevel D1 (1440) (code compares day, month, and year):

str date_of_prev_candle="";

start

{

string Datum = TimeDay(iTime(NULL,0,0)) + "." + TimeMonth(iTime(NULL,0,0)) + "." + TimeYear(iTime(NULL,0,0)); // get day of new tick
if (date_of_prev_candle!=Datum) //if different from previous (new candle in D1)

{

date_of_prev_candle = Datum; //take down current date to compare

// do your code

}

}

I didn't compile it, but in principle, it should work like this!

Hotte300

Wow, that was a quick relpy, thks!!!

I tried to incorparate your code, but i get a whole bunch of errors... :( Like these:

'str' - expression on global scope not allowed C:\Documents and Settings\Charles\My Documents\alpari\experts\test.mq4 (52, 1)
'date_of_prev_candle' - variable not defined C:\Documents and Settings\Charles\My Documents\alpari\experts\test.mq4 (52, 5)

And so on... I am probably not putting it in the right places, i guess...

I am a complet novice at this, and for about 4 days, on and off, have been trying to get this EA to take and close only one trade per cadle and not open another one before a couple of candles have past (user defined). Just having it not take and close more than one trade per candle would be fantastic at this point.

Your help is greatly appriciated, more help is extremly welcome.

Cheers,

Ramy

 
RAMY:

Wow, that was a quick relpy, thks!!!

I tried to incorparate your code, but i get a whole bunch of errors... :( Like these:

'str' - expression on global scope not allowed C:\Documents and Settings\Charles\My Documents\alpari\experts\test.mq4 (52, 1)
'date_of_prev_candle' - variable not defined C:\Documents and Settings\Charles\My Documents\alpari\experts\test.mq4 (52, 5)

And so on... I am probably not putting it in the right places, i guess...

I am a complet novice at this, and for about 4 days, on and off, have been trying to get this EA to take and close only one trade per cadle and not open another one before a couple of candles have past (user defined). Just having it not take and close more than one trade per candle would be fantastic at this point.

Your help is greatly appriciated, more help is extremly welcome.

Cheers,

Ramy

Files:
 

Sorry, was my mistake, "str" needs to be replaced by "string". I attached a compiled file which should be ok. The alert line is to be replaced by the code you want to do for every new (DAILY) candle (won't work on different timelevels).


I do not know whether EAs can be installed in or compiled from folders different from the "experts" one under programs/metatrader/experts.


Question to everyone:

I want to formulate the code in a more abstract manner. How to extend the principle in my piece of code on other time levels?


Hotte

 
Hotte300 wrote >>

Sorry, was my mistake, "str" needs to be replaced by "string". I attached a compiled file which should be ok. The alert line is to be replaced by the code you want to do for every new (DAILY) candle (won't work on different timelevels).

I do not know whether EAs can be installed in or compiled from folders different from the "experts" one under programs/metatrader/experts.

Question to everyone:

I want to formulate the code in a more abstract manner. How to extend the principle in my piece of code on other time levels?

Hotte

new candle execution

datetime prevtime=0;

int start()

{

if (prevtime==Time[0]) return(0);

execute yr code

prevtime=Time[0];

return(0);

}

 
ronaldosim wrote >>

new candle execution

datetime prevtime=0;

int start()

{

if (prevtime==Time[0]) return(0);

execute yr code

prevtime=Time[0];

return(0);

}

This is a great forum, people seem eager to help, wich is quite nice.

Thank you hotte300 for taking the time to try and help me out. I use the Ea on different time frames, so your option would not be valid for me. On the other hand ronaldosim seems to have givin me an alternative, (I think). But when i place the code around the start function of the EA i get no trades going. And if i spread out the code thru the Ea i get a whole bunch of errors again.

Here is where i placed the code :

// expert start function

datetime prevtime=0;
int start()
{
if (prevtime==Time[0]) return(0);
int j=0,limit=1;double BV=0,SV=0;BV=0;SV=0;
if(CntO(OP_BUY,Magic)>0)TL=1;if(CntO(OP_SELL,Magic)>0)TL=-1;for(int i=1;i<=limit;i++){
prevtime=Time[0];

return(0);

Surley there must be an easy way of getting it to only take one trade and close only one trade per candle instead of having it do multiple trades on the same candle.

I realy appreciate your help guys,

cheers,

Ramy

 
RAMY wrote >>

This is a great forum, people seem eager to help, wich is quite nice.

ok, we need details cos I didnt encounter amny errrors from the original code at H1 timeframe; what are u using for Timeframe1 and Timeframe2 and what timeframe are using on your chart - a quick question for a start.

Also tell us your settings u have made for this EA

 
ronaldosim wrote >>

ok, we need details cos I didnt encounter amny errrors from the original code at H1 timeframe; what are u using for Timeframe1 and Timeframe2 and what timeframe are using on your chart - a quick question for a start.

Also tell us your settings u have made for this EA

Hi, ronaldsim, again thank you.

I did not create the EA, i have no merite for that. I Have set the EA with thight settings so that it reproduces the problem for you. Just dowload the one attached to this reply. It does the multiple trades at any time frame so far. I am using it on 1-5-15-30 min at the moment for testing purposes, but cannot get an accurate report because as soon as it does those multiple trades, it brings the profit factor it accumulated back into the negative real quick.

Again thks, for helping me out with my coding illiteracy. :)

Cheers,

ramy

 
RAMY wrote >>

Hi, ronaldsim, again thank you.

I did not create the EA, i have no merite for that. I Have set the EA with thight settings so that it reproduces the problem for you. Just dowload the one attached to this reply. It does the multiple trades at any time frame so far. I am using it on 1-5-15-30 min at the moment for testing purposes, but cannot get an accurate report because as soon as it does those multiple trades, it brings the profit factor it accumulated back into the negative real quick.

Again thks, for helping me out with my coding illiteracy. :)

Cheers,

ramy

Set Reverse=false; and use a higher timeframe

 
ronaldosim wrote >>

Set Reverse=false; and use a higher timeframe

Funy, i think i figured that part out...

It was just to show you the problem, as per your request. Of course my settings are higher. The problem acures when there is a long candle that crosses both Psars. Thank you anyways.

If anyone one else can help i would still like to resolve this.

cheers,

Ramy

Reason: