Dont start breakeven and trailingstop please please please help me

Created at 24 Apr 2016, 12:09
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
ST

stahur

Joined 24.04.2016

Dont start breakeven and trailingstop please please please help me
24 Apr 2016, 12:09


 

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 Smoczus : Robot
    {
        [Parameter(DefaultValue = 10000, Step = 1000, MinValue = 1000)]
        public int Volume { get; set; }
        [Parameter("Stop Loss", DefaultValue = 6)]
        public int StopLoss { get; set; }
        [Parameter(DefaultValue = true)]
        public bool EnableStopLoss { get; set; }
        [Parameter(DefaultValue = true)]
        public bool EnableTakeProfit { get; set; }
        [Parameter(DefaultValue = 50, MinValue = 1, Step = 1)]
        public double TrailingStop { get; set; }
        [Parameter(DefaultValue = 4, MinValue = 0, Step = 1)]
        public double BreakEvenPips { get; set; }
        [Parameter(DefaultValue = true)]
        public bool EnableTrailingStop { get; set; }
        [Parameter(DefaultValue = true)]
        public bool EnableBreakEven { get; set; }
        [Parameter(DefaultValue = 9, MinValue = 0, Step = 1)]
        public double BreakEvenGain { get; set; }
        [Parameter(DefaultValue = 9, MinValue = 1, Step = 1)]
        public double TrailingStart { get; set; }
        [Parameter("Take Profit", DefaultValue = 9)]
        public int TakeProfit { get; set; }
        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Parameter("Periods", DefaultValue = 10)]
        public int Periods { get; set; }
        [Parameter(DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType MaType { get; set; }

        [Parameter("K Periods", DefaultValue = 5)]
        public int KPeriods { get; set; }

        [Parameter("D Periods", DefaultValue = 3)]
        public int DPeriods { get; set; }

        [Parameter("K Slowing", DefaultValue = 3)]
        public int K_Slowing { get; set; }

        [Parameter("Quantity (Lots)", DefaultValue = 0.1, MinValue = 0.1, Step = 0.1)]
        public double Quantity { get; set; }
        private string label;
        private int index;

        private RelativeStrengthIndex rsi;
        private StochasticOscillator _SOC;

        protected override void OnStart()
        {
            _SOC = Indicators.StochasticOscillator(KPeriods, K_Slowing, DPeriods, MaType);
            rsi = Indicators.RelativeStrengthIndex(Source, Periods);
            index = MarketSeries.Close.Count - 1;
        }

        protected override void OnTick()
        {

            if (_SOC.PercentD.LastValue < 20)
            {
                if (_SOC.PercentK.LastValue < 20)
                {
                    if (_SOC.PercentK.LastValue > _SOC.PercentD.LastValue)
                    {
                        if (rsi.Result.LastValue < 17)
                        {
                            Open(TradeType.Buy);


                        }
                    }
                }
            }
            if (rsi.Result.LastValue < 25)
            {
                Close(TradeType.Sell);
            }
            {

                if (_SOC.PercentD.LastValue > 80)
                {
                    if (_SOC.PercentK.LastValue > 80)
                    {
                        if (_SOC.PercentK.LastValue < _SOC.PercentD.LastValue)
                        {
                            if (rsi.Result.LastValue > 83)
                            {
                                Open(TradeType.Sell);

                            }
                        }
                    }
                    if (rsi.Result.LastValue > 75)
                    {
                        Close(TradeType.Buy);
                    }
                    UpdateTrailingStops();
                    MoveToBreakEven();
                    index = MarketSeries.Close.Count - 1;
                }

            }
        }
        protected void UpdateTrailingStops()
        {

            if (!EnableTrailingStop)
                return;

            var positions = Positions.FindAll(label);
            if (positions == null)
                return;

            foreach (var position in positions)
            {
                if (position.Pips >= TrailingStart)
                {
                    if (position.TradeType == TradeType.Buy)
                    {
                        var newStopLoss = Symbol.Bid - TrailingStop * Symbol.PipSize;
                        if (position.StopLoss < newStopLoss)
                            ModifyPosition(position, newStopLoss, null);
                    }
                    else if (position.TradeType == TradeType.Sell)
                    {
                        var newStopLoss = Symbol.Ask + TrailingStop * Symbol.PipSize;
                        if (position.StopLoss > newStopLoss)
                            ModifyPosition(position, newStopLoss, null);
                    }
                }
            }
        }

        protected void MoveToBreakEven()
        {

            if (!EnableBreakEven)
                return;

            var positions = Positions.FindAll(label);
            if (positions == null)
                return;

            foreach (var position in positions)
            {
                if (position.Pips >= BreakEvenPips)
                {
                    if (position.TradeType == TradeType.Buy)
                    {
                        var newStopLoss = Symbol.Bid - BreakEvenGain * Symbol.PipSize;
                        if (position.StopLoss < newStopLoss)
                            ModifyPosition(position, newStopLoss, null);
                    }
                    else if (position.TradeType == TradeType.Sell)
                    {
                        var newStopLoss = Symbol.Ask + BreakEvenGain * Symbol.PipSize;
                        if (position.StopLoss > newStopLoss)
                            ModifyPosition(position, newStopLoss, null);
                    }
                }
            }
        }
        protected TradeResult EnterInPosition(TradeType direction)
        {

            if (!EnableStopLoss && EnableTakeProfit)
                return ExecuteMarketOrder(direction, Symbol, Volume, label, null, TakeProfit);

            if (!EnableStopLoss && !EnableTakeProfit)
                return ExecuteMarketOrder(direction, Symbol, Volume, label, null, null);

            if (EnableStopLoss && !EnableTakeProfit)
                return ExecuteMarketOrder(direction, Symbol, Volume, label, StopLoss, null);

            return ExecuteMarketOrder(direction, Symbol, Volume, label, StopLoss, TakeProfit);
        }
        private void Close(TradeType tradeType)
        {
            foreach (var position in Positions.FindAll("SampleRSI", Symbol, tradeType))
                ClosePosition(position);
        }

        private void Open(TradeType tradeType)
        {
            var position = Positions.Find("SampleRSI", Symbol, tradeType);
            var volumeInUnits = Symbol.QuantityToVolume(Quantity);

            if (position == null)
                ExecuteMarketOrder(tradeType, Symbol, volumeInUnits, "SampleRSI", StopLoss, TakeProfit, TrailingStart);
        }
    }
}

 


@stahur
Replies

... Deleted by UFO ...

Spotware
25 Apr 2016, 17:50

Dear Trader,

We would like to inform you that we do not provide coding assistance services. We more than happy to assist you with specific questions about cAlgo.API. You can contact one of our Partners or post a job in Development Jobs section for further coding assistance.


@Spotware