MAfast&slow crossing with Hedging (Beta)

by Patrik Enerstrand in category Trend at 16/02/2019
Description

Hello.
I have been testing out the strategy on gold and eurusd, jusing MA 9 and 20 type Simple.

And if the trend switsh it will hedge with double +0.01 quantity for change the direction. or if you turn on hedging on SL insted of close the position it open a new in oposit direction.

 

The testing are not done and I still working on the finishing and bugfix. please take a look and comment.

as example: EURUSD 4H,

from 01/01/2018 to 14th fabruary 2019. profit of 388.95. (backtesting in tickdata) max blanace drawdown 19.45% on 1000 USD account

setting:

source: close
MAtype Simple
TP 10
SL 30
Max openposition ( not implemented yet)
Slow period 20
fast period 9
use headging Yes
Close on headging breake even NO
hedge on SL YES
Quantitiy 0.01
SourceSeries Close

 

Yes there is still som work to do, some duplicates and more. Just wana se what you guys are thinking of the potential?

Please keep in mind, this is my fist ever robot, I have been in forex market since 2010 and thants to Ctraders amazing tools I have the ability to start makeing my oen algo.

 

Download
168 downloads
How to install
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
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class MAh1 : Robot
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Parameter("MAType")]
        public MovingAverageType MAType { get; set; }

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

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

        [Parameter("Max open Positions", DefaultValue = 6)]
        public int maxPos { get; set; }

        [Parameter("Slow Periods", DefaultValue = 10)]
        public int SlowPeriods { get; set; }

        [Parameter("Fast Periods", DefaultValue = 5)]
        public int FastPeriods { get; set; }

        [Parameter("Use hedging", DefaultValue = true)]
        public bool ifHedeging { get; set; }

        [Parameter("Close on hedeging breake even", DefaultValue = false)]
        public bool brakeEven { get; set; }

        [Parameter("Hedge on SL", DefaultValue = true)]
        public bool slHedeging { get; set; }

        [Parameter("Quantity (Lots)", DefaultValue = 0.01, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

        [Parameter()]
        public DataSeries SourceSeries { get; set; }

        private double headvolume;
        private double headQuantity;
        private bool inHedeging = false;
        private MovingAverage slowMa;
        private MovingAverage fastMa;
        private bool lastPositionBuy;
        private int sellPos;
        private int buyPos;
        private bool inBuy;
        private double tempSL;

        protected override void OnStart()
        {
            fastMa = Indicators.MovingAverage(SourceSeries, FastPeriods, MAType);
            slowMa = Indicators.MovingAverage(SourceSeries, SlowPeriods, MAType);
            headvolume = Quantity;
            tempSL = SL;
        }

        protected override void OnTick()
        {

            if ((Account.Equity > (Account.Balance + TP)) || (!slHedeging && (Account.Equity < (Account.Balance - SL))))
            {
                Close();
            }
            if (Account.Equity > (Account.Balance) && inHedeging && brakeEven)
            {
                Close();
            }
        }

        protected override void OnBar()
        {
            if ((Account.Balance - Account.Equity) > tempSL && slHedeging && ifHedeging)
            {
                Head();
            }

            var currentSlowMa = slowMa.Result.Last(0);
            var currentFastMa = fastMa.Result.Last(0);
            var previousSlowMa = slowMa.Result.Last(1);
            var previousFastMa = fastMa.Result.Last(1);

            if (previousSlowMa > previousFastMa && currentSlowMa <= currentFastMa)
            {
                Open(TradeType.Buy);
                inBuy = true;
                buyPos = +1;
                lastPositionBuy = true;

            }
            //SELL
            else if (previousSlowMa < previousFastMa && currentSlowMa >= currentFastMa)
            {
                Open(TradeType.Sell);
                inBuy = false;
                sellPos = +1;

            }
        }

        private void Close()
        {
            //Closeing all
            foreach (var position in Positions)
            {
                ClosePosition(position);
            }
            maxPos = 0;
            inHedeging = false;
            buyPos = 0;
            sellPos = 0;
            inBuy = false;

        }

        private void Open(TradeType tradeType)
        {
            double volumeInUnits = Symbol.QuantityToVolumeInUnits(Quantity);
            // lyckades öppna sell och open
            if (Positions.Count <= 0 && !inHedeging)
            {
                ExecuteMarketOrder(tradeType, Symbol, volumeInUnits, "RSI");
                headvolume = Quantity + 0.01;
            }
            else if (Positions.Count > 0 && Account.Equity < Account.Balance)
            {
                Head();
            }

        }
        private void Head()
        {
            headvolume = headvolume * 2;
            headQuantity = Symbol.QuantityToVolumeInUnits(headvolume);
            inHedeging = true;

            if (ifHedeging && buyPos <= sellPos && !lastPositionBuy)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, headQuantity, "Hedging buy");
                lastPositionBuy = true;
                tempSL = Account.Balance - Account.Equity + SL;
                buyPos = +1;
            }
            else if (ifHedeging && buyPos >= sellPos && lastPositionBuy)
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, headQuantity, "Hedging Sell");
                lastPositionBuy = false;
                tempSL = Account.Balance - Account.Equity + SL;
                sellPos = +1;
            }
        }
    }
}


Comments

eivaremir - March 09, 2019 @ 18:02

i'm gonna test it and Run it in the optimization process to see what happens!

0