How to use MACD Crossover to calgo

RE:

lucian said:

using System;
using System.Linq;
using cAlgo.API;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FileSystem)]
    public class MACD : Robot
    {



        [Parameter("Volume", DefaultValue = 1000)]
        public int volume { get; set; }

        [Parameter(DefaultValue = 50, MinValue = 1)]
        public int StopLoss { get; set; }

        [Parameter(DefaultValue = 50, MinValue = 1)]
        public int TakeProfit { get; set; }


        [Parameter("MACD LongCycle", DefaultValue = 26, MinValue = 1)]
        public int LongCycle { get; set; }

        [Parameter("MACD ShortCycle", DefaultValue = 12, MinValue = 1)]
        public int ShortCycle { get; set; }

        [Parameter("MACD Period", DefaultValue = 9, MinValue = 1)]
        public int MACDPeriod { get; set; }

        private MacdCrossOver _MACD;

        protected override void OnStart()
        {

            _MACD = Indicators.MacdCrossOver(LongCycle, ShortCycle, MACDPeriod);
        }

        protected override void OnBar()
        {

            if (_MACD.MACD.Last(1) < _MACD.Signal.Last(1) && _MACD.MACD.Last(0) > _MACD.Signal.Last(0) && _MACD.Signal.Last(0) < 0)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, volume, "korakodmy", StopLoss, TakeProfit, 5);
            }

            if (_MACD.MACD.Last(1) > _MACD.Signal.Last(1) && _MACD.MACD.Last(0) < _MACD.Signal.Last(0) && _MACD.Signal.Last(0) > 0)
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, volume, "korakodmy", StopLoss, TakeProfit, 5);

            }


        }

    }

}

Thanks very much , I will try it 

RE:

lucian said:

using System;
using System.Linq;
using cAlgo.API;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FileSystem)]
    public class MACD : Robot
    {



        [Parameter("Volume", DefaultValue = 1000)]
        public int volume { get; set; }

        [Parameter(DefaultValue = 50, MinValue = 1)]
        public int StopLoss { get; set; }

        [Parameter(DefaultValue = 50, MinValue = 1)]
        public int TakeProfit { get; set; }


        [Parameter("MACD LongCycle", DefaultValue = 26, MinValue = 1)]
        public int LongCycle { get; set; }

        [Parameter("MACD ShortCycle", DefaultValue = 12, MinValue = 1)]
        public int ShortCycle { get; set; }

        [Parameter("MACD Period", DefaultValue = 9, MinValue = 1)]
        public int MACDPeriod { get; set; }

        private MacdCrossOver _MACD;

        protected override void OnStart()
        {

            _MACD = Indicators.MacdCrossOver(LongCycle, ShortCycle, MACDPeriod);

            var anotherSeries = MarketData.GetSeries(AnotherTimeFrame);

            Print("My TimeFrame: {0}, Last high: {1}, Last Low: {2}", TimeFrame, MarketSeries.High.LastValue, MarketSeries.Low.LastValue);
            Print("Another TimeFrame: {0}, Last high: {1}, Last Low: {2}", AnotherTimeFrame, anotherSeries.High.LastValue, anotherSeries.Low.LastValue);
        }

        protected override void OnBar()
        {

            if (_MACD.MACD.Last(1) < _MACD.Signal.Last(1) && _MACD.MACD.Last(0) > _MACD.Signal.Last(0) && _MACD.Signal.Last(0) < 0)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, volume, "korakodmy", StopLoss, TakeProfit, 5);
            }

            if (_MACD.MACD.Last(1) > _MACD.Signal.Last(1) && _MACD.MACD.Last(0) < _MACD.Signal.Last(0) && _MACD.Signal.Last(0) > 0)
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, volume, "korakodmy", StopLoss, TakeProfit, 5);

            }

        }

    }

}

I have attepmted to add a higher time frame to korakodmy's MACD crossover algo, for confirmation before a trade is initiated, however the robot is still opening the trade based on the current chart time frame. Can anyone provide some pointers as to how this would be possible?  Many thanks.

Hi cfuorvito@gmail.com,

It is because anotherSeries and AnotherTimeFrame are "dummy" variable names. Since you posted them in your code, I was with the impression that you will replace them yourself with whatever suits you. See below an example that builds using a daily timeframe and the Close values

using System.Linq;
using cAlgo.API;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FileSystem)]
    public class MACD : Robot
    {

        [Parameter("Volume", DefaultValue = 1000)]
        public int volume { get; set; }

        [Parameter(DefaultValue = 50, MinValue = 1)]
        public int StopLoss { get; set; }

        [Parameter(DefaultValue = 50, MinValue = 1)]
        public int TakeProfit { get; set; }


        [Parameter("MACD LongCycle", DefaultValue = 26, MinValue = 1)]
        public int LongCycle { get; set; }

        [Parameter("MACD ShortCycle", DefaultValue = 12, MinValue = 1)]
        public int ShortCycle { get; set; }

        [Parameter("MACD Period", DefaultValue = 9, MinValue = 1)]
        public int MACDPeriod { get; set; }

        private MacdCrossOver _MACD;

        protected override void OnBar()
        {
            var dailySeries = MarketData.GetSeries(TimeFrame.Daily);
            _MACD = Indicators.MacdCrossOver(anotherSeries.Close, LongCycle, ShortCycle, MACDPeriod);
            if (_MACD.MACD.Last(1) < _MACD.Signal.Last(1) && _MACD.MACD.Last(0) > _MACD.Signal.Last(0) && _MACD.Signal.Last(0) < 0)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, volume, "korakodmy", StopLoss, TakeProfit, 5);
            }

            if (_MACD.MACD.Last(1) > _MACD.Signal.Last(1) && _MACD.MACD.Last(0) < _MACD.Signal.Last(0) && _MACD.Signal.Last(0) > 0)
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, volume, "korakodmy", StopLoss, TakeProfit, 5);
            }
        }

    }

}

Best Regards,

Panagiotis