HasCrossedAbove/Below assistance please

Created at 17 May 2019, 05:11
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!
ZE

zedodia

Joined 30.05.2018

HasCrossedAbove/Below assistance please
17 May 2019, 05:11


Slowly im learning to code. Ive been making simple bots to test different conditions so i can see how they work individually before i stack everything together. Ive read through the forums but i cant figure this one out. The HasCrossed command.

I am using a custom vwap indicator, but i commented it out to try with 2 ema's. I still cant get it to work. What am I missing?

 

 

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        //    private VWAP _vwap;
        private ExponentialMovingAverage _ema;
        private ExponentialMovingAverage _ema2;

        protected override void OnStart()
        {
            //      _vwap = Indicators.GetIndicator<VWAP>();
            _ema = Indicators.ExponentialMovingAverage(MarketSeries.Close, 13);
            _ema2 = Indicators.ExponentialMovingAverage(MarketSeries.Close, 27);

        }

        protected override void OnTick()
        {
            var _longPos = Positions.Find(null, Symbol, TradeType.Buy);
            var _shortPos = Positions.Find(null, Symbol, TradeType.Sell);

        //    if (_longPos == null)
            {
                if (_ema.Result.HasCrossedAbove(_ema2.Result, 0))
                    ExecuteMarketOrder(TradeType.Buy, Symbol, 1000, null, 1.8, 4.8);
            }

        //    if (_shortPos == null)
            {
                if (_ema.Result.HasCrossedBelow(_ema2.Result, 0))
                    ExecuteMarketOrder(TradeType.Sell, Symbol, 1000, null, 1.8, 4.8);
            }
        }

        protected override void OnStop()
        {
        }
    }
}


@zedodia
Replies

PanagiotisCharalampous
17 May 2019, 14:43

Hi zedodia,

Can you please explain why do you think it does not work? I ran a backtesting and I get trades.

Best Regards,

Panagiotis


@PanagiotisCharalampous

ToheedAhmed
19 May 2019, 08:22

RE:
zedodia said:

Slowly im learning to code. Ive been making simple bots to test different conditions so i can see how they work individually before i stack everything together. Ive read through the forums but i cant figure this one out. The HasCrossed command.

I am using a custom vwap indicator, but i commented it out to try with 2 ema's. I still cant get it to work. What am I missing?

 

 

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        //    private VWAP _vwap;
        private ExponentialMovingAverage _ema;
        private ExponentialMovingAverage _ema2;

        protected override void OnStart()
        {
            //      _vwap = Indicators.GetIndicator<VWAP>();
            _ema = Indicators.ExponentialMovingAverage(MarketSeries.Close, 13);
            _ema2 = Indicators.ExponentialMovingAverage(MarketSeries.Close, 27);

        }

        protected override void OnTick()
        {
            var _longPos = Positions.Find(null, Symbol, TradeType.Buy);
            var _shortPos = Positions.Find(null, Symbol, TradeType.Sell);

        //    if (_longPos == null)
            {
                if (_ema.Result.HasCrossedAbove(_ema2.Result, 0))
                    ExecuteMarketOrder(TradeType.Buy, Symbol, 1000, null, 1.8, 4.8);
            }

        //    if (_shortPos == null)
            {
                if (_ema.Result.HasCrossedBelow(_ema2.Result, 0))
                    ExecuteMarketOrder(TradeType.Sell, Symbol, 1000, null, 1.8, 4.8);
            }
        }

        protected override void OnStop()
        {
        }
    }
}



@ToheedAhmed

zedodia
20 May 2019, 01:37

It wouldnt place any trades for me. however after loading another pair and downloading the tick data it did work. Not sure why it didnt work on the data i had been using.


@zedodia

zedodia
20 May 2019, 02:10

After trying again. it now does the same thing. It just jumps to the end of the backtest period and shows 0 and no trades taken. somethings not right here lol.


@zedodia

PanagiotisCharalampous
20 May 2019, 10:30

Hi zedodia,

Can you check the log in case there are any errors there?

Best Regards,

Panagiotis


@PanagiotisCharalampous