Add Trailing Stop Loss to EMA vs Para Sar

Created at 14 Aug 2017
DF

dfwilkie83

Joined 21.01.2017 Blocked

Status

Closed


Budget

20.00 USD


Payment Method

Direct Payment

Job Description

Hi there ,

Need help adding Trailing Stop loss to these arguments, my understanding of C# has only got me this far.

The basis of this Bot is that an Order Executes when 2 EMA cross and Parabolic SAR changes and need to add Trailing stop loss to control them better

Im aiming for Buy and Sell orders to work independently with Trailing Stop loss adjusting them when the are heading in the right direction.

Need to keep same Parameters 

Most of the code is here just need help blending hopfully just a quick tidy fix 

Regards Damien

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 EMAPSARtrailing : Robot
    {
        [Parameter("Source")]
        public DataSeries SourceSeries { get; set; }


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

        [Parameter("ParaSAR min af", DefaultValue = 0.02)]
        public double minaf { get; set; }
        [Parameter("ParaSAR max af", DefaultValue = 2)]
        public double maxaf { get; set; }

        [Parameter("Quantity (Lots)", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double Quantity { 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; }

        private double _highestGain;
        private bool _isTrailing;


        private ParabolicSAR Parasar;
        private ExponentialMovingAverage slowMa;
        private ExponentialMovingAverage fastMa;

        private const string label = "EMAPSAR";

        protected override void OnStart()
        {
            Parasar = Indicators.ParabolicSAR(minaf, maxaf);
            fastMa = Indicators.ExponentialMovingAverage(SourceSeries, FastPeriods);
            slowMa = Indicators.ExponentialMovingAverage(SourceSeries, SlowPeriods);
            //Set the position's highest gain in pips
            _highestGain = Positions[0].Pips;

        }

        protected override void OnBar()
        {
            var Volume = Symbol.QuantityToVolume(Quantity);

            if (slowMa.Result.Last(0) > fastMa.Result.Last(0) && Parasar.Result.Last(0) < fastMa.Result.Last(0))
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "EMAPSAR", StopLoss, null);
            }
            else if (slowMa.Result.Last(0) < fastMa.Result.Last(0) && Parasar.Result.Last(0) > fastMa.Result.Last(0))
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "EMAPSAR", StopLoss, null);
            }

        }
        protected void Trailing()
        {
            var position = Positions.Find("EMAPSA");

            if (position == null)
            {
                Stop();
                return;
            }
            //If the trigger is reached, the robot starts trailing
            if (!_isTrailing && position.Pips >= TriggerWhenGaining)
            {
                _isTrailing = true;
            }
            //If the cBot is trailing and the profit in pips is at the highest level, we need to readjust the stop loss
            if (_isTrailing && _highestGain < position.Pips)
            {
                //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.PipValue * TrailingStopLossDistance);
                    if (newSLprice > position.StopLoss)
                    {
                        ModifyPosition(position, newSLprice, null);
                    }
                }
                else
                {
                    var newSLprice = Symbol.Bid + (Symbol.PipValue * TrailingStopLossDistance);
                    if (newSLprice < position.StopLoss)
                    {
                        ModifyPosition(position, newSLprice, null);
                    }
                }
                //We reset the highest gain
                _highestGain = position.Pips;
            }
        }

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

 

Comments
DF
dfwilkie83 · 7 years ago

Hired Satyam

HP
hpcsphere · 7 years ago

Dear Sir


Thank you posting your requirement!
I can add trailing stop-loss into your cBot, as I have programmed many such cBots.

Please check my email for details.

Thank You.