Category Trend  Published on 20/08/2020

Sample STOCHASTIC

Description

This Cbot use STOCHASTIC indicator to Trade.


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 DragonStochastic : Robot
    {

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

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

        [Parameter("K Slowing", DefaultValue = 3)]
        public int kSlowing { get; set; }

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

        [Parameter("MA Type", DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType maType { get; set; }


        [Parameter("UP Level", DefaultValue = 80)]
        public int Up_Level { get; set; }

        [Parameter("Down Level", DefaultValue = 20)]
        public int Down_Level { get; set; }


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

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



        private StochasticOscillator _stochastic;

        protected override void OnStart()
        {
            _stochastic = Indicators.StochasticOscillator(kPeriods, kSlowing, dPeriods, maType);
        }


        protected override void OnBar()
        {
            var volumne = Math.Floor(Account.Balance * 10 * VolumePercent / 1000) * 1000;
            if (_stochastic.PercentK.Last(2) > _stochastic.PercentD.Last(2) && _stochastic.PercentK.Last(1) < _stochastic.PercentD.Last(1))
            {
                Print("_stochastic.PercentD.Last(1) {0}", _stochastic.PercentD.Last(1));
                Print("_stochastic.PercentD.Last(2) {0}", _stochastic.PercentD.Last(2));
                if (_stochastic.PercentD.Last(2) > Up_Level && _stochastic.PercentK.Last(2) > Up_Level)
                {
                    ExecuteMarketOrder(TradeType.Sell, Symbol.Name, volumne, "DragonStochastic", StopLoss, TakeProfit);
                }

            }

            if (_stochastic.PercentK.Last(2) < _stochastic.PercentD.Last(2) && _stochastic.PercentK.Last(1) > _stochastic.PercentD.Last(1))
            {
                Print("_stochastic.PercentD.Last(1) {0}", _stochastic.PercentD.Last(1));
                Print("_stochastic.PercentD.Last(2) {0}", _stochastic.PercentD.Last(2));
                if (_stochastic.PercentD.Last(2) < Down_Level && _stochastic.PercentK.Last(2) < Down_Level)
                {
                    ExecuteMarketOrder(TradeType.Buy, Symbol.Name, volumne, "DragonStochastic", StopLoss, TakeProfit);
                }

            }
        }

        protected override void OnTick()
        {
            // Put your core logic here
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}


nghiand.amz's avatar
nghiand.amz

Joined on 06.07.2020

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: DragonStochastic.algo
  • Rating: 5
  • Installs: 2275
  • Modified: 13/10/2021 09:54
Comments
Log in to add a comment.
GOLDEN.DRAGONS's avatar
GOLDEN.DRAGONS · 1 year ago

Greetings mate, It works only on currencies like EURUSD. could you please update it to works on XAUUSD (Gold)?