TheHedgeLearner++

 

Although I'm gradually moving to NinjaTrader as autotrading platform, I found reason to polish up the TheHedge EA, and add a couple of alternative history review methods to it. It now has three methods, where the two new are due to Learner; the first one as he wanted it, and the second one as how I first misunderstood it.

By my cursory backtesting, the original method looked better than yours, Learner, but then I'm sure I didn't tune the level percentages well enough to really say anything. My "mistaken The Learner" method is not bad either

Please look at the source comments about the parameters.

Files:
 

Thanks a million! I will look at it right away!

 

Thank you once again for the codes. Yes, apparently thehedge is still the best.

I was looking through the codes and I came across this portion

int i;

double price;

bool some_open = false;

int h = TimeHour( TimeCurrent() ) - timezone;

if ( h < 0 ) h += 24;

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

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

continue;

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

continue;

if ( OrderSymbol() != Symbol() )

continue;

if ( OrderType() == OP_BUY ) {

if ( h != CLOSE_HOUR ) {

if ( Bid < OrderOpenPrice() + TP * Point &&

Bid > OrderOpenPrice() - SL * Point ) {

some_open = true;

continue; // Avoids closing this trade

}

}

price = Bid;

} else if ( OrderType() == OP_SELL ) {

if ( h != CLOSE_HOUR ) {

if ( Ask > OrderOpenPrice() - TP * Point &&

Ask < OrderOpenPrice() + SL * Point ) {

some_open = true;

continue; // Avoids closing this trade

}

}

price = Ask;

}

if ( ! OrderClose( OrderTicket(), OrderLots(), price, Yellow ) )

some_open = true;

}

// Avoid opening new trade if there is one open

if ( some_open )

return( 0 );

According to the comments, these are the closing conditions. However, I'm puzzled as to why the closing condition for OP_BUY (for e.g.) is bid price lesser than takeprofit or higher than stoploss instead of bid price higher than takeprofit or lower than stoploss. I'm guessing that these codes are conditions for the trade to remain open while the price is still within the takeprofit and stoploss level. But I'm unable to find any commands for closing the trades that has hit the takeprofit and stoploss. How did you do it? If I would like to add in another condition for closing the trade (for e.g. Close all open trades when the day of week is Friday and time = 7pm.), how should I go about doing it?

I hope I'm not giving you too much trouble with my questions..

 

Yes, the expressed conditions are for not closing, and closing happens by virtue of the conditions being false (which leads the execution past the then-clauses...)

To add your closing condition, you could e.g. add the following just after

"if ( h < 0 ) h += 24;"

int d = TimeDayOfWeek( TimeCurrent() - timezone * 86400 );

bool close = h == CLOSE_HOUR || ( d == 5 && h == 19 );[/PHP]

and then replace both the conditions "if ( h != CLOSE_HOUR )..." to instead be

[PHP]if ( ! close ) ...

Obviously the 5 and 19 could also be made parameters.

 

Thank you for your guidance Ralph. I'm gonna add in some extra conditions and if there are any progression, will let you know.

 

Hi Ralph,

I'm stuck again. I'm trying to insert a trailing stop condition where it follows the highest high value since 7am GMT. Let me elaborate further; The trailing stop takes on the value of ( highest high value since 7am GMT - 25% of sampleRange() ). In other words, only when there is a new high since 7am GMT, the trailing stop moves. Otherwise it is stationary.

I have thought of using the code below to obtain the highest high value, but I'm not sure how to tweak it to the start calculating at 7am and ends at 6am the next day.

double HHV = High[/php]

After obtaining the Highest High Value, I'm thinking of substituting this

if ( Bid OrderOpenPrice() - SL * Point )

with this

[php]if ( Bid < OrderOpenPrice() + TP * Point &&

Bid > HHV - SL * Point )

Am I on the right track?

 

doing well: the HHV computation looks a little fishy, but the application of it seems fine. I assume you'll want something similar for the short side later.

For HHV you would need the last_sample value, which I had put fairly late in the sequence (since it wasn't used for closing trades). You will need to move it earlier, as well as the sample_time calculation that it depends on, and the first_tick fiddling; basically the whole code section starting with "// Determine the date stamp" and including "last_sample -= 86400".

Move that up to be before "// Review open trades....". (It'll result in a very minor behaviour change regarding the first tick, but I think you'll agree that it's an ignorable difference)

Then, you'll want the following code to compute HHV:

int SB = iBarShift( Symbol(), Period(), last_sample, false );

double HHV = High[ iHighest( Symbol(), Period(), MODE_HIGH, SB, 0 ) ];

I.e., find the highest point between now and the bar of the last sample time (inclusive).

cheers

 

Yeah.. I wasn't feeling good about my own HHV calculations as well. Thanks you Im will be trying out the short side, and as well as a "trailing take profit" using similar calculations for LLV. Hope all goes well.

 

Yeah... I wasn't feeling comfortable with my HHV calculations as well.. Thank you . I will be trying out the short side as well as making a "trailing take profit" based on similar calculations using LLV instead.

Hope all goes well.

 

Sorry for the double posting. Thought the previous post wasn't published.

I have added in the "trailing take profit" for both the long and short side. Now I'm trying to add in another trailing stop for the system.

The new trailing stop goes like this:

Assuming my TP for the day is 100 pips. When my profit earned so far hits 60% of the TP, which is 60 pips, the trailing stop kicks in. The level of the stop loss is set at 80% of the profit earned so far, which would be 0.8*60 pips = 48 pips.

This is how I do it:

double MaxLongProfitSoFar = HHV - sample_price;

double MaxShortProfitSoFar = sample_price - LLV;

double MaxProfitTgt = TP;

if (MaxLongProfitSoFar >= 0.6*MaxProfitTgt)

{

double LongBufferStop = 0.8*MaxLongProfitSoFar;

}

if (MaxShortProfitSoFar >= 0.6*MaxProfitTgt)

{

double ShortBufferStop = 0.8*MaxShortProfitSoFar;

}

[/php]

As for the condition to remain open, I added in " && Bid > LongBufferStop + sample_price" and " && Ask < sample_price - ShortBufferStop" to the codes:

if ( OrderType() == OP_BUY ) {

if ( !close ) {

if ( Bid < LLV + TP * Point &&

Bid > HHV - SL * Point && Bid > LongBufferStop + sample_price) {

some_open = true;

continue; // Avoids closing this trade

}

}

price = Bid;

} else if ( OrderType() == OP_SELL ) {

if ( !close ) {

if ( Ask > HHV - TP * Point &&

Ask < LLV + SL * Point && Ask < sample_price - ShortBufferStop) {

some_open = true;

continue; // Avoids closing this trade

}

}

price = Ask;

}

[/php]

I have also shifted

[php]

// Limit the enabling period to be EXPIRES hours after sample SAMPLE_HOUR each day.

if ( TimeCurrent() < sample_time ) return( 0 ); // Too early

if ( TimeCurrent() >= sample_time + EXPIRES * 3600 ) return( 0 ); // Too late

double sample_price =

iOpen( Symbol(), PERIOD_H1, iBarShift( Symbol(), PERIOD_H1, sample_time, true ) );

to the space below this:

[php]

// Determine the date stamp for the sample

// gmt_day is the current date at GMT time

// sample_time is the time stamp the broker has for the selected GMT sample hour

string gmt_day = TimeToStr( TimeCurrent() - timezone * 3600, TIME_DATE );

datetime sample_time = StrToTime( gmt_day ) + ( SAMPLE_HOUR + timezone ) * 3600 ;

// Avoid trading at today's sample time if it is before the very first EA tick time

static datetime first_tick = 0;

if ( first_tick == 0 ) first_tick = TimeCurrent();

if ( sample_time < first_tick ) return( 0 );

// last_sample is the datetime for the most recent sample time

datetime last_sample = sample_time;

if ( last_sample > TimeCurrent() )

last_sample -= 86400;

so as to utilise the sample_price in the calculations for maximum profit earned so far.

I hope these codes would work... Please correct my mistakes if there is any! Thanks!

 

Looks good. Except that the two lines:

if ( TimeCurrent() < sample_time ) return( 0 ); // Too early

if ( TimeCurrent() >= sample_time + EXPIRES * 3600 ) return( 0 ); // Too late

should go back to where they were, as otherwise the EA won't consider closing trades outside of the trade entry (time) window.

The rest should be fine. The QA department sent me a memo about lack of comments, which is of no importance for the running of the code.