Category Other  Published on 28/07/2017

Perfect for RANGE, good for TREND

Description

Hello Traders and FX aficionados

 

Based on multiple request comming over my website ( https://www.bregu.al ) , regarding the source code and how the MEDIAN_MULTI robot works (first published on https://ctdn.com/algos/cbots/show/1581 ) , I have decided to publish it with code.

You can referr to the results on https://ctdn.com/algos/cbots/show/1581 , as well as for settings to be used.

 

Just e few bulletpoints:

EURUSD, going from 10k $ to more than 300k $ 

EURGBP, going from 10 k $ to more than 50k $

GBPUSD, going from 10k $ to more than 76k $

 

DISCLAIMER: USE IT AT YOUR OWN RISK

 

Due to the fact that robot should be thoroughly tested and understood, I have put a safeguard into the robot to allow it to work only in backtest mode. (             if (!IsBacktesting) Stop(); )

If you want to use it in Demo just change the line to (         if (Account.IsLive) Stop();               )

 

If you like my work and want to support or request any changes , you can go on https://www.fiverr.com/s2/89ef42aba2   and buy any of the product(s).

 

Have Fun and enjoy your time trading

Oltion Bregu

https://www.bregu.al/


////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////                                                                                                        ////
////                        Name :  Median_Multi                                                            ////
////                        Dated :  26-May-16                                                              ////
////                        ver   :  8.1                                                                    ////
////                        Updated : 04-Mar-17                                                             ////
////                        Copyright: Oltion Bregu                                                         ////
////                                                                                                        ////
////                        If you like my work and want to support, you can go on                          ////
////                        https://www.fiverr.com/s2/8452795c43 and buy one of the product(s)              ////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
using System.Diagnostics;
using System.IO;
using System.Resources;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class Median_MULTI : Robot
    {

        #region Parametra_Publike

        [Parameter("Komenti", DefaultValue = "Dhelpra")]
        public string StrategyName { get; set; }

        //   [Parameter("Lejo Trading", DefaultValue = true)]
        //   public bool LTrading { get; set; }

        [Parameter("Volume Fix", DefaultValue = 10000, MinValue = 1)]
        public int VolFix { get; set; }

        [Parameter("Max Ord Total", DefaultValue = 20, MinValue = 0)]
        public int MaxOrdTotal { get; set; }

        [Parameter("Max Ord Buy", DefaultValue = 20, MinValue = 0)]
        public int MaxOrdBuy { get; set; }

        [Parameter("Max Ord Sell", DefaultValue = 20, MinValue = 0)]
        public int MaxOrdSell { get; set; }

        [Parameter("Def SL", DefaultValue = 200, MinValue = 0)]
        public int DefSL { get; set; }

        [Parameter("Def TP", DefaultValue = 200, MinValue = 0)]
        public int DefTP { get; set; }

        [Parameter("Target Win NET", DefaultValue = 200, MinValue = 0)]
        public int TargetWinNet { get; set; }

        [Parameter("Target Win Pct", DefaultValue = 5, MinValue = 0)]
        public double TargetWinPCt { get; set; }

        [Parameter("Max Loss Pct", DefaultValue = 5, MinValue = 0)]
        public double MaxLossPCt { get; set; }

        [Parameter("Buy/Sell Single max Loss", DefaultValue = 5, MinValue = 0)]
        public double BSMaxLoss { get; set; }

        [Parameter("Buy/Sell Single max Win", DefaultValue = 5, MinValue = 0)]
        public double BSMaxWin { get; set; }

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

        [Parameter("PIP Win", DefaultValue = 20, MinValue = 0)]
        public int PipWin { get; set; }

        [Parameter("K", DefaultValue = 1.001, MinValue = 0)]
        public double Koeficenti { get; set; }


        [Parameter("MA Period", DefaultValue = 7)]
        public int FMA_Period { get; set; }


        #endregion

        #region Parametra_Private

        private int Order_Hapur_Total;
        private int Order_Hapur_Buy;
        private int Order_Hapur_Sell;

        private double PastHourOpen;
        private double PastHourClose;
        private double PastHourHigh;
        private double PastHourLow;

        private double PastCandlePips;

        private double DifHighLow;
        private double DifOpenClose;
        private double PctBodyAll;

        private double RatioEB;
        private double TargetRatio;


        private bool OKOpenOrders;

        private MovingAverage Fast_MA;

        #endregion

        protected override void OnStart()
        {

            DateTime stopdate = new DateTime(2018, 10, 10);
            if (Server.Time.Date >= stopdate)
            {
                Stop();
            }

            if (!IsBacktesting)
                Stop();

            Fast_MA = Indicators.MovingAverage(MarketSeries.Close, FMA_Period, MovingAverageType.Simple);

            Process.Start("iexplore.exe", "http://www.bregu.al");

            Process.Start("iexplore.exe", "https://www.fiverr.com/s2/8452795c43");

        }

        protected override void OnTick()
        {


            #region Hap_Order_nqs_no_order


            if (Positions.FindAll(StrategyName, Symbol, TradeType.Sell).Length == 0)
            {
                // if (Fast_MA.Result.IsFalling())
                ExecuteMarketOrder(TradeType.Sell, Symbol, VolFix, StrategyName, DefSL, DefTP, 0, StrategyName);
            }

            if (Positions.FindAll(StrategyName, Symbol, TradeType.Buy).Length == 0)
            {
                // if (Fast_MA.Result.IsRising())
                ExecuteMarketOrder(TradeType.Buy, Symbol, VolFix, StrategyName, DefSL, DefTP, 0, StrategyName);
            }
            #endregion


            double NetWL = 0;

            double BuyNetWL = 0;
            double SellNetWL = 0;

            var POZHap = Positions.FindAll(StrategyName);

            foreach (var POZ1 in POZHap)
            {
                NetWL = NetWL + POZ1.NetProfit;

                if (POZ1.TradeType == TradeType.Buy)
                    BuyNetWL = BuyNetWL + POZ1.NetProfit;
                if (POZ1.TradeType == TradeType.Sell)
                    SellNetWL = SellNetWL + POZ1.NetProfit;
            }


            RatioEB = Account.Equity / Account.Balance;
            TargetRatio = 1 + TargetWinPCt / 100;

            double TargetWin = 0;
            double MaxLoss = 0;

            TargetWin = (TargetWinPCt * Account.Balance) / (100 * Math.Pow(Koeficenti, Order_Hapur_Total));
            MaxLoss = MaxLossPCt * Account.Balance / 100;

            double BSLoss = 0;
            BSLoss = BSMaxLoss * Account.Balance / 100;

            double BSWin = 0;
            BSWin = BSMaxWin * Account.Balance / 100;


            if (NetWL > TargetWin || NetWL < (-MaxLoss) || NetWL > TargetWinNet)
            {
                foreach (var POZ2 in POZHap)
                {
                    if (POZ2 != null)
                    {
                        ClosePosition(POZ2);
                    }
                }

            }


            if (BuyNetWL < (-BSLoss) || BuyNetWL > BSWin)
            {
                foreach (var POZ3 in POZHap)
                {
                    if (POZ3 != null && POZ3.TradeType == TradeType.Buy && POZ3.NetProfit < 0)
                    {
                        ClosePosition(POZ3);
                    }
                }

            }


            if (SellNetWL < (-BSLoss) || SellNetWL > BSWin)
            {
                foreach (var POZ4 in POZHap)
                {
                    if (POZ4 != null && POZ4.TradeType == TradeType.Sell && POZ4.NetProfit < 0)
                    {
                        ClosePosition(POZ4);
                    }
                }

            }


            #region kontrollo_order_hapur

            OKOpenOrders = false;
            Order_Hapur_Total = 0;
            Order_Hapur_Buy = 0;
            Order_Hapur_Sell = 0;

            Order_Hapur_Total = Positions.FindAll(StrategyName, Symbol).Length;
            Order_Hapur_Buy = Positions.FindAll(StrategyName, Symbol, TradeType.Buy).Length;
            Order_Hapur_Sell = Positions.FindAll(StrategyName, Symbol, TradeType.Sell).Length;

            if (Order_Hapur_Total < MaxOrdTotal)
                OKOpenOrders = true;

            double LastBuyPrice = 0;
            double LastSellPrice = 0;

            double AVGBuy = 0;
            double AVGSell = 0;



            DateTime LastBuyTime = new DateTime(2000, 10, 10);
            DateTime LastSellTime = new DateTime(2000, 10, 10);


            foreach (var PozicionHapur in Positions)
            {
                if (PozicionHapur != null && PozicionHapur.SymbolCode == Symbol.Code && PozicionHapur.Comment == StrategyName)
                {
                    if (PozicionHapur.TradeType == TradeType.Buy)
                    {
                        var temptimebuy = PozicionHapur.EntryTime;
                        if (temptimebuy > LastBuyTime)
                        {
                            LastBuyTime = temptimebuy;
                            LastBuyPrice = PozicionHapur.EntryPrice;
                        }


                        AVGBuy = PozicionHapur.EntryPrice + AVGBuy;
                    }

                    if (PozicionHapur.TradeType == TradeType.Sell)
                    {
                        var temptimesell = PozicionHapur.EntryTime;

                        if (temptimesell > LastSellTime)
                        {
                            LastSellTime = temptimesell;
                            LastSellPrice = PozicionHapur.EntryPrice;
                        }


                        AVGSell = PozicionHapur.EntryPrice + AVGSell;
                    }
                }
            }

            if (Order_Hapur_Buy > 0)
                AVGBuy = AVGBuy / Order_Hapur_Buy;

            if (Order_Hapur_Sell > 0)
                AVGSell = AVGSell / Order_Hapur_Sell;

            var SellTP = AVGSell - PipWin * Symbol.PipSize;
            //var SellTP = LastSellPrice + (Math.Abs(LastSellPrice - AVGSell) / Math.Pow(Koeficenti, Order_Hapur_Sell));

            foreach (var POZ5 in POZHap)
            {
                if (POZ5 != null && POZ5.TradeType == TradeType.Sell && AVGSell > 0 && Symbol.Bid > AVGSell && SellTP != POZ5.TakeProfit)
                {
                    ModifyPosition(POZ5, POZ5.StopLoss, SellTP);
                }
            }

            var BuyTP = AVGBuy + PipWin * Symbol.PipSize;
            //var BuyTP = LastBuyPrice - (Math.Abs(AVGBuy - LastBuyPrice) / Math.Pow(Koeficenti, Order_Hapur_Buy));

            foreach (var POZ6 in POZHap)
            {
                if (POZ6 != null && POZ6.TradeType == TradeType.Buy && AVGBuy > 0 && Symbol.Ask < AVGBuy && BuyTP != POZ6.TakeProfit)
                {
                    ModifyPosition(POZ6, POZ6.StopLoss, BuyTP);
                }
            }


            #endregion

            #region gjej_vlerat_e_nevojshme

            PastHourOpen = MarketSeries.Open.Last(1);
            PastHourClose = MarketSeries.Close.Last(1);
            PastHourHigh = MarketSeries.High.Last(1);
            PastHourLow = MarketSeries.Low.Last(1);

            DifHighLow = Math.Abs(PastHourHigh - PastHourLow);
            DifOpenClose = Math.Abs(PastHourOpen - PastHourClose);

            PctBodyAll = DifOpenClose / DifHighLow;

            PastCandlePips = DifOpenClose / Symbol.PipSize;

            #endregion




            #region Last_Buy_Sell_entry

            var OKtoBuy = false;
            var OKtoSell = false;

// || Symbol.Ask > LastBuyPrice + Step * Symbol.PipSize || 
            // && Fast_MA.Result.IsRising()
            if ((Symbol.Ask < LastBuyPrice - Step * Symbol.PipSize) && Order_Hapur_Buy < MaxOrdBuy)
            {
                OKtoBuy = true;
            }
// || Symbol.Bid < LastSellPrice - Step * Symbol.PipSize ||
            //&& Fast_MA.Result.IsFalling()
            if ((Symbol.Bid > LastSellPrice + Step * Symbol.PipSize) && Order_Hapur_Sell < MaxOrdSell)
            {
                OKtoSell = true;
            }

            #endregion

            #region vendos_order

            if (OKOpenOrders)
            {

                if (OKtoSell)
                {

                    var LotSell = Symbol.NormalizeVolume(VolFix * Math.Pow(Koeficenti, Order_Hapur_Sell), RoundingMode.ToNearest);

                    var result1 = ExecuteMarketOrder(TradeType.Sell, Symbol, LotSell, StrategyName, DefSL, DefTP, 0, StrategyName);

                    if (result1.IsSuccessful)
                    {
                        var position1 = result1.Position;
                        Print("Position Entry price is {0}", position1.EntryPrice);
                        Print("Totali i Order eshte{0}", Order_Hapur_Total);
                        Print("Totali i Order Buy eshte {0}", Order_Hapur_Buy);
                        Print("Totali i Order Sell eshte {0}", Order_Hapur_Sell);
                        Print("Account Margin eshte {0}", Account.MarginLevel);
                    }

                }

                if (OKtoBuy)
                {

                    var LotBuy = Symbol.NormalizeVolume(VolFix * Math.Pow(Koeficenti, Order_Hapur_Buy), RoundingMode.ToNearest);


                    var result2 = ExecuteMarketOrder(TradeType.Buy, Symbol, LotBuy, StrategyName, DefSL, DefTP, 0, StrategyName);

                    if (result2.IsSuccessful)
                    {
                        var position2 = result2.Position;
                        Print("Position Entry price is {0}", position2.EntryPrice);
                        Print("Totali i Order Hapur {0}", Order_Hapur_Total);
                        Print("Totali i Order Buy eshte {0}", Order_Hapur_Buy);
                        Print("Totali i Order Sell eshte {0}", Order_Hapur_Sell);
                        Print("Account Margin eshte {0}", Account.MarginLevel);
                    }

                }


            }
            #endregion


        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}


95
9544315

Joined on 29.12.2015

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: Median_MULTI.algo
  • Rating: 0
  • Installs: 2865
  • Modified: 13/10/2021 09:54
Comments
Log in to add a comment.
ZE
zedodia · 6 years ago

Hi, I think this is a great code, however I am wondering if anyone could add a script that calculates the margin used so during backtesting it won’t open a position if the margin isn’t available. Thankyou. 

95
9544315 · 7 years ago

I would not suggest anybody use a robot in LIVE account without first:

1. Understanding very throughly what the robot does.

2. What the robots risks are.

3. What part of your account can the robot lose, 0-100%.

 

If you are sure on all of the above, you can use the robot on any type of account, the code is there.

Just remove the protection

SA
Sawit_Kitti · 7 years ago

This link "https://www.fiverr.com/s2/8452795c43" cannot open.

Can I use it in live account ?

Thank you.