Category Trend  Published on 27/06/2015

PayBack II

Description

Two orders in oposite direction with progressive close positions

Author : Abdallah HACID

Solution Visual studio

    PayBack modifie (28 juin 2014)
    version 2.2014.7.1.19h

  Utiliser : 
          Volume              =   100000
          SL                  =   57 pips
          TP                  =   150 pips
          commission          =   37.6 per Million
          Spread fixe         =   1pip
          Starting Capital    =   50000

  Results :
          sur GBPUSD en h1 entre le 1/1/2014 et 1/7/2014 a 19h30 gain de 9482 euros(+19%).
          Net profit          =   9481.93
          Ending Equity       =   10164.18 euros
          Ratio de Sharpe     =   0.24
          Ratio de Storino    =   0.55

 

gain de 9481 euros.

 


 

#region Licence
//The MIT License (MIT)
//Copyright (c) 2014 abdallah HACID, https://www.facebook.com/ab.hacid

//Permission is hereby granted, free of charge, to any person obtaining a copy of this software
//and associated documentation files (the "Software"), to deal in the Software without restriction,
//including without limitation the rights to use, copy, modify, merge, publish, distribute,
//sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
//is furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in all copies or
//substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
//BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
//DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

// Project Hosting for Open Source Software on Codeplex : https://calgobots.codeplex.com/
#endregion

#region cBot Infos
// -------------------------------------------------------------------------------
//
//		PayBack modifie (28 juin 2014)
//		version 1.2014.7.1.19h
//		Author : https://www.facebook.com/ab.hacid
//
// -------------------------------------------------------------------------------
#endregion

#region cBot Parameters Comments
// -------------------------------------------------------------------------------
//
//	Utiliser : 
//			Volume				=	100000
//          SL					=	57 pips
//          TP					=	150 pips
//			commission			=	37.6 per Million
//			Spread fixe			=	1pip
//			Starting Capital	=	50000
//
//	Results :
//          sur GBPUSD en h1 entre le 1/1/2014 et 1/7/2014 a 19h30 gain de 9482 euros(+19%).
//			Net profit			=	9481.93
//			Ending Equity		=	10164.18 euros
//			Ratio de Sharpe		=	0.24
//			Ratio de Storino	=	0.55
// -------------------------------------------------------------------------------
#endregion

#region advertisement
// -------------------------------------------------------------------------------
//			Trading using leverage carries a high degree of risk to your capital, and it is possible to lose more than
//			your initial investment. Only speculate with money you can afford to lose.
// -------------------------------------------------------------------------------
#endregion


using System;
using cAlgo.API;
using cAlgo;
using cAlgo.Lib;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class PayBackII : Robot
    {
        [Parameter("Initial Volume", DefaultValue = 100000, MinValue = 0)]
        public int InitialVolume { get; set; }
        [Parameter("Stop Loss", DefaultValue = 57)]
        public int StopLoss { get; set; }
        [Parameter("Take Profit", DefaultValue = 150)]
        public int TakeProfit { get; set; }

        const long microVolume = 1000;
        const string botLabel = "PB-II";


        protected override void OnStart()
        {
            Positions.Opened += OnPositionOpened;
            Positions.Closed += OnPositionClosed;

            relanceOrders();
        }

        private void relanceOrders()
        {
            manageOpen(TradeType.Buy, InitialVolume);
            manageOpen(TradeType.Sell, InitialVolume);
        }

        private void manageOpen(TradeType tradeType, long volume, string prefixLabel = botLabel)
        {
            int nVolumePartition = 10, part1 = 5, part2 = 3;
            long nVol = (long)Math.Floor((double)(volume / (microVolume * nVolumePartition)));
            long partialVolume = nVol * microVolume;

            var result1 = ExecuteMarketOrder(tradeType, Symbol, partialVolume * part1, prefixLabel + tradeType.ToString() + "-1");
            var result2 = ExecuteMarketOrder(tradeType, Symbol, partialVolume * part2, prefixLabel + tradeType.ToString() + "-2");
            var result3 = ExecuteMarketOrder(tradeType, Symbol, volume - (part1 + part2) * partialVolume, prefixLabel + tradeType.ToString() + "-3");
        }

        private void manageClose()
        {
            foreach (var position in Positions)
            {
                if (position.TakeProfit.HasValue)
                {
                    string labelType = position.Label.Substring(position.Label.Length - 1, 1);
                    double potentialGainPips = ((position.TradeType == TradeType.Buy) ? 1 : -1) * (position.TakeProfit.Value - position.EntryPrice) / Symbol.PipSize;
                    double potentialLosePips = ((position.TradeType == TradeType.Buy) ? 1 : -1) * (position.StopLoss.Value - position.EntryPrice) / Symbol.PipSize;
                    double percentGain = position.Pips / potentialGainPips;
                    double percentLose = -position.Pips / potentialLosePips;

                    if ((percentGain >= 0.43) && (labelType == "3"))
                        ClosePosition(position);

                    if ((percentGain >= 0.76) && (labelType == "2"))
                        ClosePosition(position);

                    if ((percentLose <= -0.33) && (labelType == "1"))
                        ClosePosition(position);

                    if ((percentLose <= -0.66) && (labelType == "2"))
                        ClosePosition(position);

                }
            }
        }


        protected void OnPositionOpened(PositionOpenedEventArgs args)
        {
            var position = args.Position;

            ModifyPosition(position, position.pipsToStopLoss(Symbol, StopLoss), position.pipsToTakeProfit(Symbol, TakeProfit));
        }

        protected void OnPositionClosed(PositionClosedEventArgs args)
        {
            Position position = args.Position;

            if (position.Pips < 0)
                manageOpen(position.inverseTradeType(), position.Volume, botLabel + "Mart-");

            if (Positions.Count == 0)
                relanceOrders();
        }


        protected override void OnTick()
        {
            manageClose();

        }
        protected override void OnError(Error error)
        {
            if (error.Code != ErrorCode.BadVolume)
            {
                Print("erreur : " + error.Code);
                Stop();
            }
        }



    }
}




AY
aysos75

Joined on 28.09.2013

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: PayBack II.algo
  • Rating: 3.33
  • Installs: 6604
  • Modified: 13/10/2021 09:54
Comments
Log in to add a comment.
AY
aysos75 · 10 years ago

Bonjour,

J'ai fait une vidéo explicative sur Youtube :

Utilisation de la librairie cAlgoBot

 

CH
chiripacha · 10 years ago

Hi,

j'ai recu cette message. Je ne sais que faire maintenant!??

Error : Project F:\dev\cAlgo\Sources\Library\cAlgoLib\cAlgo.Lib.csproj doesn't exist.

merci

 

ME
megamemo · 10 years ago

hi grat bot one cuestion can i modified this in new here 

AY
aysos75 · 10 years ago

Voici une nouvelle version plus performante
Observez qu'on n'utilise aucun signal pour prendre une position, on attends les résultats du marché (par définition efficient) pour clôturer les pertes en premier selon l'adage couper les pertes et laisser courir les gains.

ça marche assez bien!

-------------------------------------------------

Here is a new, more powerful version 
Observe that uses no signal to take a position, the results of the market (by definition efficient) we expect to close losses in the adage first cut losses and let it run gains. 

it works pretty well!

-------------------------------------------------

// -------------------------------------------------------------------------------
//
//    PayBack modifie (28 juin 2014)
//      version 1.2014.7.1.19h
//    Abdallah HACID (c) 2014
//    http://www.babooclic.com
//
//    Utiliser : 
//            Volume                =    100000
//          SL                    =    57 pips
//          TP                    =    150 pips
//            commission            =    37.6 per Million
//            Spread fixe            =    1pip
//            Starting Capital    =    50000
//
//    Results :
//          sur GBPUSD en h1 entre le 1/1/2014 et 1/7/2014 a 19h30 gain de 9482 euros(+19%).
//            Net profit            =    9481.93
//            Ending Equity        =    10164.18 euros
//            Ratio de Sharpe        =    0.24
//            Ratio de Storino    =    0.55
// -------------------------------------------------------------------------------

using System;
using cAlgo.API;
using cAlgo;

namespace cAlgo.Robots
{
    [Robot(AccessRights = AccessRights.None)]
    public class PAYBACKII : Robot
    {
        [Parameter("Initial Volume", DefaultValue = 100000, MinValue = 0)]
        public int InitialVolume { get; set; }
        [Parameter("Stop Loss", DefaultValue = 57)]
        public int StopLoss { get; set; }
        [Parameter("Take Profit", DefaultValue = 150)]
        public int TakeProfit { get; set; }

        const long microVolume = 1000;
        const string botLabel = "PB-";


        protected override void OnStart()
        {
            Positions.Opened += OnPositionOpened;
            Positions.Closed += OnPositionClosed;

            relanceOrders();
        }

        private void relanceOrders()
        {
            manageOpen(TradeType.Buy, InitialVolume);
            manageOpen(TradeType.Sell, InitialVolume);
        }

        private void manageOpen(TradeType tradeType, long volume, string prefixLabel = botLabel)
        {
            int nVolumePartition = 10, part1 = 5, part2 = 3;
            long nVol = (long)Math.Floor((double)(volume / (microVolume * nVolumePartition)));
            long partialVolume = nVol * microVolume;

            var result1 = ExecuteMarketOrder(tradeType, Symbol, partialVolume * part1, prefixLabel + tradeType.ToString() + "-1");
            var result2 = ExecuteMarketOrder(tradeType, Symbol, partialVolume * part2, prefixLabel + tradeType.ToString() + "-2");
            var result3 = ExecuteMarketOrder(tradeType, Symbol, volume - (part1 + part2) * partialVolume, prefixLabel + tradeType.ToString() + "-3");
        }

        private void manageClose()
        {
            foreach (var position in Positions)
            {
                if (position.TakeProfit.HasValue)
                {
                    string labelType = position.Label.Substring(position.Label.Length - 1, 1);
                    double potentialGainPips = ((position.TradeType == TradeType.Buy) ? 1 : -1) * (position.TakeProfit.Value - position.EntryPrice) / Symbol.PipSize;
                    double potentialLosePips = ((position.TradeType == TradeType.Buy) ? 1 : -1) * (position.StopLoss.Value - position.EntryPrice) / Symbol.PipSize;
                    double percentGain = position.Pips / potentialGainPips;
                    double percentLose = -position.Pips / potentialLosePips;

                    if ((percentGain >= 0.43) && (labelType == "3"))
                        ClosePosition(position);

                    if ((percentGain >= 0.76) && (labelType == "2"))
                        ClosePosition(position);

                    if ((percentLose <= -0.33) && (labelType == "1"))
                        ClosePosition(position);

                    if ((percentLose <= -0.66) && (labelType == "2"))
                        ClosePosition(position);

                }
            }
        }


        protected void OnPositionOpened(PositionOpenedEventArgs args)
        {
            var position = args.Position;

            //Limits limits = new Limits(position.TradeType, position.EntryPrice, Symbol.PipSize, TakeProfit, StopLoss);

            //position.LimitsExt().SL_PIPS = StopLoss;
            //position.LimitsExt().TP_PIPS = TakeProfit;

            double stopLoss = position.TradeType == TradeType.Buy ? position.EntryPrice - Symbol.PipSize * StopLoss : position.EntryPrice + Symbol.PipSize * StopLoss;
            double takeProfit = position.TradeType == TradeType.Buy ? position.EntryPrice + Symbol.PipSize * TakeProfit : position.EntryPrice - Symbol.PipSize * TakeProfit;

            ModifyPosition(position, stopLoss, takeProfit);
        }

        protected void OnPositionClosed(PositionClosedEventArgs args)
        {

            if (args.Position.Pips < 0)
                manageOpen(args.Position.TradeType==TradeType.Buy ? TradeType.Sell:TradeType.Buy, args.Position.Volume, botLabel + "Mart-");

            if (Positions.Count == 0)
                relanceOrders();
        }


        protected override void OnTick()
        {
            manageClose();

        }
        protected override void OnError(Error error)
        {
            if (error.Code != ErrorCode.BadVolume)
            {
                Print("erreur : " + error.Code);
                Stop();
            }
        }

 

    }
}