Category Trend  Published on 18/11/2022

[Back Friday] Free MA Cross 9$ Others

Description

I promote all my bots in November to 9$

You can check at

Demo Free: https://nghia312.gumroad.com/?max_price=8

BlackFriday: https://nghia312.gumroad.com/?min_price=9

 

 



using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class MACrossSample : Robot
    {
        [Parameter("Source")]
        public DataSeries SourceSeries { get; set; }
        [Parameter("Label", DefaultValue = "EMA")]
        public string label { get; set; }

        [Parameter("Slow Periods", DefaultValue = 20)]
        public int SlowPeriods { get; set; }
        [Parameter("Fast Periods", DefaultValue = 10)]
        public int FastPeriods { get; set; }

        [Parameter("By Percent", DefaultValue = false)]
        public bool IsTradeByPercent { get; set; }

        [Parameter("Quantity (Lots)", DefaultValue = 0, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

        [Parameter("Quantity (Percent)", DefaultValue = 1, MinValue = 0.01, Step = 1)]
        public double QuantityByPercent { get; set; }

        [Parameter("SL", DefaultValue = 0)]
        public double Stoploss { get; set; }

        [Parameter("TP", DefaultValue = 0)]
        public double TakeProfit { get; set; }

        [Parameter("Type Moving", DefaultValue = MovingAverageType.Exponential)]
        public MovingAverageType Type { get; set; }

        private MovingAverage slowMa;
        private MovingAverage fastMa;


        protected override void OnStart()
        {

            fastMa = Indicators.MovingAverage(SourceSeries, FastPeriods, Type);
            slowMa = Indicators.MovingAverage(SourceSeries, SlowPeriods, Type);

        }

        protected override void OnTick()
        {
            //Nếu có lợi nhuận dương mà về 0 thì tự cắt luôn cho mát

         
        }

        protected override void OnBar()
        {
            // Nếu mà giá chạy xa quá thì không vào lệnh nữa
            if (fastMa.Result.Last(2) < slowMa.Result.Last(2) && fastMa.Result.Last(1) > slowMa.Result.Last(1))
            {
                CloseAll();
                ExecuteMarketOrder(TradeType.Buy, SymbolName, VolumeInUnits, label,Stoploss,TakeProfit);
            }

            if (fastMa.Result.Last(2) > slowMa.Result.Last(2) && fastMa.Result.Last(1) < slowMa.Result.Last(1))
            {
                CloseAll();
                ExecuteMarketOrder(TradeType.Sell, SymbolName, VolumeInUnits, label, Stoploss, TakeProfit);
            }
        }
        private double VolumeInUnits
        {

            get { 
                if(IsTradeByPercent)
                {
                    return Math.Floor(Account.Balance * 10 * QuantityByPercent / 1000) * 1000;
                }

                return Symbol.QuantityToVolumeInUnits(Quantity); 
            }
        }

        private void CloseAll()
        {
            var positions = Positions.FindAll(label);

            foreach (var item in positions)
            {
                item.Close();
            }

        }
    }
}


nghiand.amz's avatar
nghiand.amz

Joined on 06.07.2020

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: MACrossBot.algo
  • Rating: 0
  • Installs: 740
Comments
Log in to add a comment.
No comments found.