Category Range  Published on 23/01/2018

Advanced Hedging and Scalping cBot

Description

An advanced version of the Hedging and Scalping cBot. It implements an automatic position scaling out logic and has good returns on many timeframes and time periods.  See below backtesting results on minute timeframe over a two years period.


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;
        protected override void OnStart()
        {
            _equity = Account.Balance;
            ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "BUY");
            ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "SELL");
        }
        protected override void OnBar()
        {
            foreach (var position in Positions)
            {
                if (position.Pips > pips)
                {
                    ClosePosition(position);
                }
            }
            if (Positions.Count < noOfPositions)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "BUY");
                ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "SELL");
            }


            int buyCount = 0;
            int sellCount = 0;
            foreach (var position in Positions)
            {
                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, "BUY");
                ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "SELL");
            }

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

        }

    }


}


SK
skoutz.rothchild

Joined on 13.03.2017

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: Advanced Hedging and Scalping cBot.algo
  • Rating: 0
  • Installs: 6840
  • Modified: 13/10/2021 09:54
Comments
Log in to add a comment.
CI
ciaran-hughes11 · 5 months ago

when i use this bot it just opens buys and sells at the same time 

RI
rickbeeke84 · 3 years ago

hello    how can i add a stoploss in this bot.   i like the bot  but i miss a stoploss to avoid blowing my account   

TY
tyovev118 · 4 years ago

How do I stop the robot from opening more trades once the ones that are open get closed?

ricky.global9's avatar
ricky.global9 · 5 years ago

Good to Play With, not for real trade...

SE
Sergii Bokancha · 6 years ago

Greeting to you skoutz.rothchild! I appreciate the work you sharing with others.

Can you please describe the exact logic of this code and how differently it performs compared to your first H&S cBot?

Thank you

IA
IandelMar · 6 years ago

Thanks a lot, im doing backtesting and sometimes really good, but i have time which my Account gettin zero. Can you please add Stopp Loss? What Pait do you use with timeframe? Thanks Ian

thaiemoney's avatar
thaiemoney · 6 years ago

Power Full Backtest

Thank you 

NA
naviallicer@gmail.com · 6 years ago

can you write a code like this one?

 

NA
naviallicer@gmail.com · 6 years ago

can you make  just a hedging bot like ea recovery zone