Trend robot 2 (and Simple Trend robot)

Created at 07 Jul 2013, 14:23
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
TR

TraderM

Joined 30.12.2012

Trend robot 2 (and Simple Trend robot)
07 Jul 2013, 14:23


Hi,

can anyone help me understand why these robots seem to miss the crossing of the EMAs:

Also,

when calculating the EMAs, how many decimal places are calculated. Are the slow and fast EMAs ever likely to be the same?

Thanks!

TraderM

 

 


@TraderM
Replies

TraderM
14 Jul 2013, 20:48 ( Updated at: 21 Dec 2023, 09:20 )

This seems to be working OK now:

I have no explanation for this, wierd.

TraderM


@TraderM

lec0456
15 Jul 2013, 19:28

Just off the top of my head,  If you are comparing the current period verses the previous period, and if your comparison for one of the points is zero(or extremely close enough), meaning the ema's cross exactly on the period, that could cause code to miss this cross.  I use a 3 period comparison when I want to trigger crossings.  So, I use the previous period and the period before that.  When compared with the current period if the sign of the differneces are not the same, then you have a cross.  Don't know if that helps any...good luck 


@lec0456

gorin
16 Jul 2013, 14:34

Is this backtesting or live? If it's live it could be a technical error. Did you check the log? You can try printing something in the event there is a technical error.


@gorin

TraderM
20 Jul 2013, 16:27

Hi,

I think I have found the problem. Each time the comparison is made OnBar() it uses a different data set. This means that the current EMA at time X is not the previous EMA value at time Y (one bar later). (You can verify this with print statements.) It is therefore possible that the EMA cross happens but it is not recognised by:

if (previousSlowMa > previousFastMa && currentSlowMa <= currentFastMa && !isLongPositionOpen)

as you are not comparing the same things.

To fix this I have set all these variables to be global when the robot is created. And on each bar:

1. Overwrote the previous values:

            previousSlowMa = currentSlowMa;
            previousFastMa = currentFastMa;

2. Then calculated the new values:

            currentSlowMa = slowMa.Result[lastIndex];
            currentFastMa = fastMa.Result[lastIndex];

3. Then made the comparison:

if (previousSlowMa >= previousFastMa && currentSlowMa < currentFastMa && !isLongPositionOpen) 
//and so on

This seems to work.

But I think the sample
(Trend robot 2, using OnBar() for the comparison, with the Stop Loss and Take Profit added) may be flawed and should be looked at again.

Thanks for you comments above!


TraderM

@TraderM