How to code for Price below or above a EMA?
How to code for Price below or above a EMA?
31 Oct 2021, 19:40
I am new to cBots and trying to code my first cBot.
I have managed to have my cBot enter buy and sell trades based on the MACD crossover signals. I now like to add another step by filtering it with the 200 EMA.
I want the bot to check that if the price is below the 200 ema, it takes only sell entries and when the price is above the 200 EMA, it takes only buy entries each time the MACD crosses over in the direction of the trend.
As I already have the MACD part done, I appreciate help on the 200ema code. A sample code is much appreciated.
Thank you in advance!
Replies
Zorroman
01 Nov 2021, 17:37
RE:
amusleh said:
Hi,
Try this:
using cAlgo.API; using cAlgo.API.Indicators; namespace cAlgo.Robots { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class NewcBot : Robot { private ExponentialMovingAverage _ema; protected override void OnStart() { _ema = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 200); } protected override void OnBar() { if (Bars.ClosePrices.Last(1) > _ema.Result.Last(1)) { // Buy } else if (Bars.ClosePrices.Last(1) < _ema.Result.Last(1)) { // Sell } } } }
Thank you! Much appreciated.
@Zorroman
amusleh
01 Nov 2021, 08:19
Hi,
Try this:
@amusleh