Category Range  Published on 29/09/2020

Zone Hedging v1

Description

This bot places orders between the zone. Every position compensates for losing trade with a higher volume. 

Here is a sample hedging. Avoid to trade while ranging market. 

 


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

        [Parameter(DefaultValue = 1000, MinValue = 1, Step = 1000, MaxValue = 100000)]
        public int Volume { get; set; }

        [Parameter(DefaultValue = 0.5, MinValue = 0.1, Step = 0.1, MaxValue = 100)]
        public double Range { get; set; }

        [Parameter(DefaultValue = 10, MinValue = 1, Step = 1, MaxValue = 500)]
        public int ProfitX { get; set; }

        [Parameter(DefaultValue = 1.5, MinValue = 1, Step = 0.1, MaxValue = 5)]
        public double Factor { get; set; }

        [Parameter(DefaultValue = 12, MinValue = 0, Step = 1, MaxValue = 23)]
        public int Time { get; set; }




        protected override void OnStart()
        {
            Positions.Opened += PositionsOnOpened;
            Positions.Closed += PositionsOnClosed;

        }

        protected override void OnTick()
        {

            var price = Math.Round((Symbol.Bid + Symbol.Ask) / 2, 4);



            if (PendingOrders.Count == 0 && Positions.Count == 0)
            {

                if (Server.Time.Hour >= Time && Server.Time.Hour < Time + 2)
                {
                    PlaceStopOrderAsync(TradeType.Buy, Symbol, Volume, price + Range * Symbol.PipSize, "BuyyStop1", null, ProfitX, null, null, false);
                    PlaceStopOrderAsync(TradeType.Sell, Symbol, Volume, price - Range * Symbol.PipSize, "SellStop1", null, ProfitX, null, null, false);
                }
            }
        }


        private void PositionsOnOpened(PositionOpenedEventArgs args)
        {

            var vol = Volume * Math.Round(Math.Pow(Factor, Positions.Count), 0);
            var pos = args.Position;
            var labCount = Positions.Count - 1;
            var labelx = int.Parse(pos.Label.Substring(8, pos.Label.Length - 8));
            var lablab = pos.Label.Substring(0, 8);
            string positionCount = Positions.Count.ToString();

            Print("Position opened at {0}, {1}", pos.EntryPrice, pos.Label, pos.VolumeInUnits);


            if (Positions.Count == 1)
            {
                foreach (var order in PendingOrders)
                {
                    if (order.Label == "SellStop1" && pos.Label == "BuyyStop1")
                    {
                        ModifyPendingOrderAsync(order, order.TargetPrice, null, ProfitX, null, vol);
                    }
                    if (order.Label == "BuyyStop1" && pos.Label == "SellStop1")
                    {
                        ModifyPendingOrderAsync(order, order.TargetPrice, null, ProfitX, null, vol);
                    }
                }
            }
            else if (Positions.Count >= 2)
            {
                if (lablab == "BuyyStop" && labCount == labelx)
                {
                    PlaceStopOrderAsync(TradeType.Sell, Symbol, vol, (pos.EntryPrice - Range * 2 * Symbol.PipSize), ("SellStop" + positionCount), null, ProfitX, null, null, false);
                }
                if (lablab == "SellStop" && labCount == labelx)
                {
                    PlaceStopOrderAsync(TradeType.Buy, Symbol, vol, (pos.EntryPrice + Range * 2 * Symbol.PipSize), ("BuyyStop" + positionCount), null, ProfitX, null, null, false);
                }
            }

        }

        private void PositionsOnClosed(PositionClosedEventArgs args)
        {

            foreach (var position in Positions)
            {
                ClosePositionAsync(position);
            }

            foreach (var order in PendingOrders)
            {
                CancelPendingOrderAsync(order);
            }


        }
    }
}


CT
cTraderX

Joined on 03.06.2020

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: ZoneHedging.algo
  • Rating: 0
  • Installs: 2078
  • Modified: 13/10/2021 09:54
Comments
Log in to add a comment.
EM
emaile2008 · 3 months ago

Thank you very much this is very nice cbot only problem is that when i back tested it will always fail or has huge drawdown when there is news!

 

i think this will work great if there is (I wish someone could help us on this)

1-  it stops when there is news coming maybe same day or 2 hours prior and 2 hours after news.

2- ability to use lot size as risk percentage of the account 1,2,3,4,5 % lot size so account can grow bit faster

3- ability to place orders again and again at the same day since this only allow one time order every day

Thanks again

DE
demiszx@gmail.com · 4 years ago

in real time it has too many bugs