Category Trend  Published on 03/08/2022

following bot

Description

this is just a simple bot that opens trades buy/sell and trails with sl in endles loop.


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()
        {
            // checking if any position are open
            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: 1530
  • Modified: 03/08/2022 15:51
Comments
Log in to add a comment.
HA
hashmisahab538 · 1 year ago

Only a few blogger would discuss this topic the way you do.”~;”-Bocoran RTP Slot Terpercaya

GO
godep56246 · 1 year ago

This is my first time i visit here and I found so many interesting stuff in your blog especially it's discussion, thank you.umair

SY
syedanus1100 · 1 year ago

Incredible this particular tutorial is actually fantastic it genuinely aided me plus our kids, cheers!fifa world cup 2022 venue