Category Trend  Published on 03/08/2022

following bot

Description
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 TrailingStopLossSample : Robot
    {
        [Parameter("VolumeInLots", DefaultValue = 1)]
        public double VolumeInLots { get; set; }
        [Parameter("Buy")]
        public bool Buy { get; set; }
        [Parameter("Stop Loss", DefaultValue = 5)]
        public double StopLoss { get; set; }
        [Parameter("Trigger When Gaining", DefaultValue = 1)]
        public double TriggerWhenGaining { get; set; }
        [Parameter("Trailing Stop Loss Distance", DefaultValue = 1)]
        public double TrailingStopLossDistance { get; set; }
        [Parameter("Step (pips)", DefaultValue = 5)]
        public double Step { get; set; }

        private double _highestGain;
        private bool _isTrailing;



        protected override void OnStart()
        {
        }



        protected override void OnTick()
        {
            // Schaut nach ob Positionen offen sind =)
            if (Positions.Count == 0)
            {

                //Execute a market order based on the direction parameter
                ExecuteMarketOrder(Buy ? TradeType.Buy : TradeType.Sell, Symbol, VolumeInLots * 100000, "SampleTrailing", StopLoss, null);
                //Set the position's highest gain in pips
                _highestGain = Positions[0].Pips;

            }

            {
                var position = Positions.Find("SampleTrailing");

                if (position == null)
                {
                    Stop();
                    return;
                }
                //If the trigger is reached, the robot starts trailing
                if (position.Pips >= TriggerWhenGaining)
                {
                    //Based on the position's direction, we calculate the new stop loss price and we modify the position
                    if (position.TradeType == TradeType.Buy)
                    {
                        var newSLprice = Symbol.Ask - (Symbol.PipSize * TrailingStopLossDistance);
                        if (newSLprice > position.StopLoss)
                        {
                            ModifyPosition(position, newSLprice, null);
                        }
                    }
                    else
                    {
                        var newSLprice = Symbol.Bid + (Symbol.PipSize * TrailingStopLossDistance);
                        if (newSLprice < position.StopLoss)
                        {
                            ModifyPosition(position, newSLprice, null);
                        }
                    }
                    TriggerWhenGaining += Step;
                }
            }
        }

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

 


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 TrailingStopLossSample : Robot
    {
        [Parameter("VolumeInLots", DefaultValue = 1)]
        public double VolumeInLots { get; set; }
        [Parameter("Buy")]
        public bool Buy { get; set; }
        [Parameter("Stop Loss", DefaultValue = 5)]
        public double StopLoss { get; set; }
        [Parameter("Trigger When Gaining", DefaultValue = 1)]
        public double TriggerWhenGaining { get; set; }
        [Parameter("Trailing Stop Loss Distance", DefaultValue = 1)]
        public double TrailingStopLossDistance { get; set; }
        [Parameter("Step (pips)", DefaultValue = 5)]
        public double Step { get; set; }

        private double _highestGain;
        private bool _isTrailing;



        protected override void OnStart()
        {
        }



        protected override void OnTick()
        {
            // Schaut nach ob Positionen offen sind =)
            if (Positions.Count == 0)
            {

                //Execute a market order based on the direction parameter
                ExecuteMarketOrder(Buy ? TradeType.Buy : TradeType.Sell, Symbol, VolumeInLots * 100000, "SampleTrailing", StopLoss, null);
                //Set the position's highest gain in pips
                _highestGain = Positions[0].Pips;

            }

            {
                var position = Positions.Find("SampleTrailing");

                if (position == null)
                {
                    Stop();
                    return;
                }
                //If the trigger is reached, the robot starts trailing
                if (position.Pips >= TriggerWhenGaining)
                {
                    //Based on the position's direction, we calculate the new stop loss price and we modify the position
                    if (position.TradeType == TradeType.Buy)
                    {
                        var newSLprice = Symbol.Ask - (Symbol.PipSize * TrailingStopLossDistance);
                        if (newSLprice > position.StopLoss)
                        {
                            ModifyPosition(position, newSLprice, null);
                        }
                    }
                    else
                    {
                        var newSLprice = Symbol.Bid + (Symbol.PipSize * TrailingStopLossDistance);
                        if (newSLprice < position.StopLoss)
                        {
                            ModifyPosition(position, newSLprice, null);
                        }
                    }
                    TriggerWhenGaining += Step;
                }
            }
        }

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


AL
al-n

Joined on 03.01.2021

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: following.algo
  • Rating: 0
  • Installs: 1058
  • Modified: 03/08/2022 15:48
Comments
Log in to add a comment.
WI
williamjameschar · 2 years ago

Web development, also known as website development, refers to the tasks involved in the creation, construction, and maintenance of websites and web applications that are accessible via a browser. I'm familiar with development because I work at a logoistic agency that also creates websites.

LA
lanchris004 · 2 years ago

I do have the same problem and same question. looking for the solution and order panel should be more user friendly and option to synchronize the Order Panel. 

 

tg macro not working

OS
osamashoukat48 · 2 years ago

Stainless steel spring manufacturers and suppliers necessitate a unique design based on mechanical and strength calculations.