TheHedgeLearner++ - page 3

 
the learner:
Oh... sorry for the confusion.. I will try my best to clear things up. I'm actually using a M5 chart for this system all along and not a H1 chart... hehe. So with the use of M5 chart, other than changing the H1 to M5 for the sample_time calculations, as far as i know there is nothing else to change in the system to make it fit into a M5 chart right?

Ok.... I think that's right, yes.

the learner:
Well, not pending trade exactly.. It is more like open a long trade only when the close of the current bar is above 25% of range + sample_price and vice versa for short trades. The rational behind this is to prevent the the Ask or Bid price to just touch the 25% of range + sample_price and reverse immediately while a long trade has been opened. Sort of like a filter to sieve out false signals. Same for the exit at close price condition.

Ah; so you basically mean to delay entry until the beginning of a bar? In thatcase, just add in a "bar opening filter" after the "Check for bar opening" comment. Something like this perhaps:

static datetime done = 0;

if ( Time[ 0 ] == done )

return( 0 );

done = Time[ 0 ];

[/PHP]

the learner:
Like this?
for ( i = OrdersHistoryTotal(); i >=0; i-- )

mmm maybe not; To be explicit, I was thinking like this for the loop:

[PHP]int count = 0;

for ( i = OrdersHistoryTotal() - 1; i >=0; i-- ) {

if ( ! OrderSelect( i, SELECT_BY_POS, MODE_HISTORY ) )

continue;

if ( magic != 0 && OrderMagicNumber() != magic )

continue;

if ( OrderSymbol() != Symbol() )

continue;

if ( OrderOpenTime() >= last_sample ) {

count += 1;

}

}

if ( count > 2 )

return( 0 );

the learner:
To clear things up on the stop and reverse condition just in case there is a misunderstanding... There is only maximum 2 trades a day. If and only if the first trade is being stopped out by the static stop loss, the system will stop and reverse the trade. If the stop and reverse gets stopped out by either the static or trailing stop, or took a profit, no more trades for the day. It is getting hot here too... (X_x)

Thanks, that's clear for me... so it's a maximum of 1 open trade at any time, and 2 closed during the day... now I'm afraid I'm a bit short of spare time for a while, and I don't think I can do anything much to it this month. You might need some code to verify that the first closed order (if count == 1) was stopped out (eg profit < 0 ), in order to avoid a follow-on trade on successful close. Apart from that, it should all work automagically withthe trades counting above....

 

Thanks Ralph .

I added in the codes and it worked! Except that some changes have to be done. I changed the count > 2 to count > 1 instead to limit it to 2 trades.

I have also tried the stop and reverse. I added in the LastTrade function at the bottom of the EA:

//Function to determine if previous trade is profit or loss.

int LastTrade()

{

/*

Returns:

0 - Take Profit

1 - Stop Loss

2 - Other

*/

int totalOrders = HistoryTotal();

if(totalOrders>=1)

{

if(OrderSelect(totalOrders-1,SELECT_BY_POS,MODE_HISTORY))

{

if(OrderClosePrice()==OrderTakeProfit())

{

return (0);

}

if(OrderClosePrice()==OrderStopLoss())

{

return (1)

}

}

}

return(2);

}[/php]

Afterwhich, I added in the following:

[php]int count = 0;

for ( i = OrdersHistoryTotal() - 1; i >=0; i-- ) {

if ( ! OrderSelect( i, SELECT_BY_POS, MODE_HISTORY ) )

continue;

if ( magic != 0 && OrderMagicNumber() != magic )

continue;

if ( OrderSymbol() != Symbol() )

continue;

if ( OrderOpenTime() >= last_sample ) {

count += 1;

}

}

if ( count > 1 )

return ( 0 );

if ( count == 1 && LastTrade() == 0)

return ( 0 );

It didn't work.....

 

What a thread! '

I do not understand one thing in it...I just wanted to post to some newcomers here a message...Forextsd has some of the best programmers here and you can witness their problem solving abilities almost in realtime...

ES

 

Hmm; as far as I can see, it should have worked, although with the caveat that it's not sure that the last closed trade is placed last in the history list. I was once told that it depends on the interactive manipulation of the Account History display, where you can choose sort column etc.

An alternative way would be to pick up the win/loss detail while counting, by using a variation like below.

int count = 0;

bool last_was_won = false;

datetime last_trade = 0;

for ( i = OrdersHistoryTotal() - 1; i >=0; i-- ) {

if ( ! OrderSelect( i, SELECT_BY_POS, MODE_HISTORY ) )

continue;

if ( magic != 0 && OrderMagicNumber() != magic )

continue;

if ( OrderSymbol() != Symbol() )

continue;

if ( OrderOpenTime() >= last_sample ) {

count += 1;

if ( OrderCloseTime() > last_trade ) {

last_trade = OrderCloseTime();

last_was_won = OrderClosePrice() == OrderTakeProfit();

}

}

}

if ( count > 1 )

return ( 0 );

if ( count == 1 && last_was_won )

return ( 0 );

 

I totally agree with you ElectricSavant. I have indeed learnt a lot from Ralph.

Ok, back to the learning process, hehe.

Below attached is the latest version of the EA for anyone that is confused with what is happening. I have tried the codes in the previous post. Somehow I got the same results.

I tested the EA on a timeframe from 5th July 2004 to 19th July 2004, with the following parameters: (as used in the EA attached below)

extern int timezone=0;

extern int magic = 96;

extern int SAMPLE_HOUR = 12;

extern int EXPIRES = 23;

extern int CLOSE_HOUR = 11;

extern int RATE = 1;

extern int METHOD = 2;

extern int Rop = 20; // Percentage of range for OP

extern int Rtp = 80; // Percentage of range for TP

extern int Rsl = 20; // Percentage of range for SL

extern double lots = 0.1;

And the results shown are as followed:

Apparently the system still does not recognise the take profit in previous trade;

Notice line #3 was closed out at line #4 at 13:57 with a profit of 21.00. And in line #5 and #6, a long trade was immediately entered and closed at 13:57 on the same day. In other words, if the system does recognise the previous order was a profit, it should not have entered anymore trades on that same day. This is also the reason why the 2nd trade was a long instead of a short; 2 trades are allowed for the day and the 2nd trade met the condition of a long trade as well as the condition to close for the long trade at the same time, therefore entered and exited immediately at 13:57.

Is it possible to compare the change in accountbalance? If it is a positive, then its a profit and if a negative, its a loss.

Files:
 

Ah; my mistake I suppose .. and maybe yours too; the order is closed by "force" and not by take-profit, so obviously the closing price differs from the order's take-profit (which is zero).

You'll need a more complex test to replace

last_was_won = OrderClosePrice() == OrderTakeProfit();[/PHP]

... something like the following:

[PHP]if ( OrderType() == OP_BUY )

last_was_won = OrderClosePrice() > OrderOpenPrice();

else

last_was_won = OrderClosePrice() < OrderOpenPrice();

 

Oh... now i understand...

After testing the codes, it does recognise the take profit and does not enter the 2nd trade when the previous trade is a profit. However, a new problem arises. If the trade is stopped out before 12 midnight and its a loss, it enters the 2nd trade. The weird thing is, it does not enter the 2nd trade when the first trade is stopped after 12 midnight with a loss. Could it have something to do with how the last_sample is used in last_was_won definition?

 

On first inspection, I would say that it's due to the time filter following the count check. According to that filter, the EA should not enter a trade "today" before the sample_time is coming up. As written, it presupposes that the "enabling period" ends before midnight.

The filter condition (the two if-statements) can be rephrased to the better one (a single if statement):

if ( TimeCurrrent() >= last_sample + EXPIRES * 3600 ) return( 0 ); // Outside the enabling period

and that will (should) allow a past-midnight, second trade if within the enabling period.

It was wrong (of me) to use sample_time as basis since that timestamp refers to "start of today's enabling period", which sometimes is after "now" and sometimes before "now". The last_sample timestamp is temporally more well-defined as "start of most recent enabling period", which always is before (or at) "now", and therefore the better thing to use for this filter.

 

Thank you so much Ralph for all the guidance you have given me! Even though this EA doesn't give a positive return in the end, I would say it has served its purpose as a learning platform for those who are interested in programming for trading. So if anyone is able to earn positive returns with variations of this EA, please give credits to Ralph Ronquist!!

Reason: