MACD cross over Free free

Description

This bot use MACD Cross Over

1. Signal-line crossover

2. Zero crossover

 

Warning! Executing the following cBot may result in loss of funds. Use it at your own risk.
Notification Publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section you may use the Copyright Infringement Notification form to submit a claim.
Formula / Source Code
Language: C#
Trading Platform: cAlgo
´╗┐using cAlgo.API;
using cAlgo.API.Indicators;
using System;
using System.Linq;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class NdnghiaMACD : Robot
    {
        private MacdCrossOver _MACD;

        [Parameter("Initial Volume Percent", DefaultValue = 1, MinValue = 0)]
        public double InitialVolumePercent { get; set; }

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

        [Parameter("Long Cycle", DefaultValue = 26)]
        public int LongCycle { get; set; }

        [Parameter("Short Cycle", DefaultValue = 12)]
        public int ShortCycle { get; set; }

        [Parameter("Stop Loss", DefaultValue = 100)]
        public int StopLoss { get; set; }
        [Parameter("Take Profit", DefaultValue = 100)]
        public int TakeProfit { get; set; }

        [Parameter("Signal-line crossover true:if Signal-line crossover false: Zero crossover", DefaultValue = true)]
        public bool IsSignalLineCrossover { get; set; }

        protected override void OnStart()
        {
            _MACD = Indicators.MacdCrossOver(LongCycle, ShortCycle, Period);
        }

        protected override void OnBar()
        {
            var volumne = Math.Floor(Account.Balance * 10 * InitialVolumePercent / 1000) * 1000;

            if (IsSignalLineCrossover)
            {


                if (_MACD.MACD.Last(2) < _MACD.Signal.Last(2) && _MACD.MACD.Last(1) > _MACD.Signal.Last(1))
                {
                    var position = Positions.Find("NdnghiaMACD");
                    if (position != null && position.TradeType == TradeType.Sell)
                    {
                        position.Close();
                    }
                    ExecuteMarketOrder(TradeType.Buy, Symbol.Name, volumne, "NdnghiaMACD", 0, 0);

                    // ExecuteMarketOrder(TradeType.Buy, Symbol.Name, volumne, "NdnghiaMACD", StopLoss, TakeProfit);
                }

                if (_MACD.MACD.Last(2) > _MACD.Signal.Last(2) && _MACD.MACD.Last(1) < _MACD.Signal.Last(1))
                {
                    var position = Positions.Find("NdnghiaMACD");
                    if (position != null && position.TradeType == TradeType.Buy)
                    {
                        position.Close();
                    }
                    ExecuteMarketOrder(TradeType.Sell, Symbol.Name, volumne, "NdnghiaMACD", 0, 0);
                    // ExecuteMarketOrder(TradeType.Sell, Symbol.Name, volumne, "NdnghiaMACD", StopLoss, TakeProfit);
                }
            }
            //Zero cross over
            else
            {
                if (_MACD.MACD.Last(1) > 0 && _MACD.MACD.Last(2) < 0)
                {
                    //up
                    ExecuteMarketOrder(TradeType.Buy, Symbol.Name, volumne, "NdnghiaMACD", StopLoss, TakeProfit);
                }
                else if (_MACD.MACD.Last(1) < 0 && _MACD.MACD.Last(2) > 0)
                {
                    //Down
                    ExecuteMarketOrder(TradeType.Sell, Symbol.Name, volumne, "NdnghiaMACD", StopLoss, TakeProfit);
                }
            }
        }
    }



}
Comments