Zone Hedging v1 free

by s.selim.c in category Range at 29/09/2020
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. 

 

Warning! Executing the following cBot may result in loss of funds. Use it at your own risk.
Notification Publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section you may use the Copyright Infringement Notification form to submit a claim.
Formula / Source Code
Language: C#
Trading Platform: cAlgo
´╗┐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);
            }


        }
    }
}
Comments