EMA is both Rising and Falling on the same OnBar, why?

Created at 01 Oct 2015, 18:22
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

EMA is both Rising and Falling on the same OnBar, why?
01 Oct 2015, 18:22


Hi,

I am using the code below to buy/sell depending on whether an EMA is rising or falling. Note that this is not an if...else, just two if statements, one after the other.

The code is called OnBar; in this case the Hourly bar.

Strangely, just once so far, two positions were opened, one Buy, one Sell on the same hourly OnBar:

My expectation is that on any one bar the EMA cannot be both rising and falling, so only one position should have been opened.

I will change the code to exclude one of the possibilities (with an if else, or a variable), but I would like to understand why this occured, in case it has any other consequences for me.

Any ideas?

Thanks,

TraderM

-------------Code below------------

if (MaXX.Result.IsRising())
                {
                    Print("EMA rising, should be buying");

                    var result = PlaceLimitOrder(TradeType.Buy, Symbol, Volume, Symbol.Bid - BetterEntry, null, SLinPips, TPinPips, orderExpiration);
                    var order = result.PendingOrder;

                    Print("The pending order's ID: {0}", order.Id);
                    Print("Symbol.Bid: {0}", Symbol.Bid);
                    Print("Symbol.Ask: {0}", Symbol.Ask);
                }


                if (MaXX.Result.IsFalling())
                {
                    Print("EMA falling, should be selling");

                    var result = PlaceLimitOrder(TradeType.Sell, Symbol, Volume, Symbol.Bid + BetterEntry, null, SLinPips, TPinPips, orderExpiration);
                    var order = result.PendingOrder;

                    Print("The pending order's ID: {0}", order.Id);
                    Print("Symbol.Bid: {0}", Symbol.Bid);
                    Print("Symbol.Ask: {0}", Symbol.Ask);
                }


@TraderM
Replies

moneybiz
02 Oct 2015, 14:32

IsRising: Checks if the last value in a dataseries is greater than the previous.

IsFalling: Checks if the last value in a dataseries is less than the previous

If the data series is constructed on Close price then the last value will always fluctuate since the last price (close price) is changing. The prices before that last one will be settled.

In your situation when you're checking for IsRising the tick data (current close price) is rising but just before the second condition is tested for IsFalling a new tick data is received asynchronously and the condition is tested with that fresh data which is lower than the close price of the closed bar (previous bar).

That's why you're receiving two different results inside OnBar event. This should be a very rare case, since it takes few milliseconds or less to calculate the conditions.

You can avoid this by adding ELSE statement in your code to guarantee single execution.

if (MaXX.Result.IsRising())
{
	// Open BUY.
}
else if (MaXX.Result.IsFalling())
{
	// Open SELL.
}

 


@moneybiz

TraderM
05 Oct 2015, 10:17

Hi moneybiz,

I had assumed that all calculations were complete. I also did not consider that the two calls (IsRising and IsFalling) could be asynchronous.

Thanks for sharing this!

TraderM


@TraderM