Topics
12 Mar 2017, 12:20
 3038
 4
Replies

phily1_1
16 Mar 2017, 19:25

RE:

lucian said:

You can start with this code:

 


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

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




        private DirectionalMovementSystem _dms;


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

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

        [Parameter("DIMinus", DefaultValue = 20)]
        public int DIMinus { get; set; }
        [Parameter("Volume", DefaultValue = 1000)]
        public int Volume { get; set; }
        [Parameter("Label", DefaultValue = "ADX")]
        public string Label { get; set; }

        protected override void OnStart()
        {
            _dms = Indicators.DirectionalMovementSystem(Period);

        }

        protected override void OnBar()
        {





            if (_dms.DIMinus.LastValue > DIMinus && _dms.DIPlus.LastValue < DIPlus && Positions.FindAll(Label, Symbol, TradeType.Buy).Length == 0)
            {
               
                close(TradeType.Sell);
                trade(TradeType.Buy);
            }

            if (_dms.DIMinus.LastValue < DIMinus && _dms.DIPlus.LastValue > DIPlus && Positions.FindAll(Label, Symbol, TradeType.Sell).Length == 0)
            {
                close(TradeType.Buy);
                trade(TradeType.Sell);
            }

        }
        private void close(TradeType tradeType)
        {
            foreach (var position in Positions.FindAll(Label, Symbol, tradeType))
                ClosePosition(position);
        }



        private void trade(TradeType tradetype)
        {
            ExecuteMarketOrder(tradetype, Symbol, Volume, Label);
        }





    }

}

Thank you so much ! You truly made my day. 

 


@phily1_1

phily1_1
14 Mar 2017, 20:45

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

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

        public class SampleRobot : Robot
        {

            private DirectionalMovementSystem _dms;

            private double _DIplus;
            private double _DIminus;

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

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

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


            protected override void OnStart()
            {
                _dms = Indicators.DirectionalMovementSystem(Period);

            }

            protected override void OnBar()
            {
                if (Trade.IsExecuting)
                    return;

                bool isLongPositionOpen = _position != null && _position.TradeType == TradeType.Buy;
                bool isShortPositionOpen = _position != null && _position.TradeType == TradeType.Sell;

                if (_dms.ADX.LastValue > 0.0 && _dms.DIPlus.LastValue() && !isLongPositionOpen)
                {
                    ClosePosition();
                    Buy();
                }

                if (_dms.ADX.LastValue < 0.0 && _dms.DIMinus.LastValue() && !isShortPositionOpen)
                {
                    ClosePosition();
                    Sell();
                }
            }

        private void Buy()
        {
            Trade.CreateBuyMarketOrder(Symbol, Volume);
        }

        private void Sell()
        {
            Trade.CreateSellMarketOrder(Symbol, Volume);
        }

        protected override void OnPositionOpened(Position openedPosition)
        {
            _position = openedPosition;
        }
        
      } 

    }

Like this ???


@phily1_1