Replies

glmxvs
30 May 2022, 18:12

RE:

amusleh said:

Hi,

Try this:

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 AdvancedHedgingandScalpingcBot : Robot
    {
        [Parameter("Volume", DefaultValue = 1000, MinValue = 1, Step = 1)]
        public int Volume { get; set; }

        private double _equity;
        private int noOfPositions = 4;
        private int pips = 2;
        private string label;

        public Position[] BotPositions
        {
            get
            {
                return Positions.FindAll(label).ToArray();
            }
        }

        protected override void OnStart()
        {
            label = string.Format("AdvancedHedgingandScalpingcBot-{0}-{1}", SymbolName, TimeFrame.ToString());

            _equity = Account.Balance;
            ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label);
            ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label);
        }

        protected override void OnBar()
        {
            foreach (var position in BotPositions)
            {
                if (position.Pips > pips)
                {
                    ClosePosition(position);
                }
            }
            if (BotPositions.Length < noOfPositions)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label);
                ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label);
            }

            int buyCount = 0;
            int sellCount = 0;
            foreach (var position in BotPositions)
            {
                if (position.TradeType == TradeType.Buy)
                    buyCount++;
                if (position.TradeType == TradeType.Sell)
                    sellCount++;
            }

            if (buyCount == 0 || sellCount == 0)
            {
                Volume += 1000;
                noOfPositions += 2;
                pips++;
                ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label);
                ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label);
            }

            if (Account.Equity > _equity + Account.Equity / 100)
            {
                foreach (var position in BotPositions)
                {
                    ClosePosition(position);
                }
                _equity = Account.Equity;
                Volume = 1000;
                noOfPositions = 4;
                pips = 2;
                ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label);
                ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label);
            }
        }
    }
}

 

@amusleh thank you, it seems it works perfectly! This makes me want to learn even more! 

@m4trader4 thank you for the link as well, I can see that that link was a way better resource than the two I had. 


@glmxvs