Daily orders with trailing stop

Created at 10 Feb 2021, 15:40
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!
PU

PUdPUd

Joined 24.01.2021

Daily orders with trailing stop
10 Feb 2021, 15:40


Hello, I would like to set multiple orders on a daily basis with a trailing stop. This code opens multiple positions on one day, and keeps trailing the pips until the trailing stop is hit. To make it place orders each day I subscribed the code to dailyBars and it creates orders each day but the trailing stop no longer works. What am I doing wrong?

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SampleTrailingbyLabel : Robot
    {

        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }

        [Parameter("Position Label", DefaultValue = "My Label")]
        public string MyLabel { get; set; }

        [Parameter("Trigger (pips)", DefaultValue = 20)]
        public int Trigger { get; set; }

        [Parameter("Trailing Stop (pips)", DefaultValue = 10)]
        public int TrailingStop { get; set; }

        private bool _isTrigerred;


        protected override void OnStart()
        {
            var dailyBars = MarketData.GetBars(TimeFrame.Daily);
            dailyBars.BarOpened += OnDailyBarsBarOpened;
        }


        void OnDailyBarsBarOpened(BarOpenedEventArgs obj)
        
            {

                Positions.Opened += PositionsOnOpened;
                ExecuteMarketOrder(TradeType.Buy, SymbolName, 10, MyLabel);
                ExecuteMarketOrder(TradeType.Buy, SymbolName, 10, MyLabel);
                PlaceStopOrder(TradeType.Buy, SymbolName, 10, Symbol.Bid + 3 * Symbol.PipSize, MyLabel);
                Positions.Closed += PositionsOnClosed;

            }
        
        private void PositionsOnOpened(PositionOpenedEventArgs args)
        {
            var position = args.Position;
            Print("Position {0} found, waiting for trigger.", position.Label);
        }

        private void PositionsOnClosed(PositionClosedEventArgs args)
        {
            _isTrigerred = false;
        }

        protected override void OnTick()
        {
            var position = Positions.Find(MyLabel);

            if (position == null)
                return;

            if (position.TradeType == TradeType.Buy)
            {
                double distance = Symbol.Bid - position.EntryPrice;

                if (distance >= Trigger * Symbol.PipSize)
                {
                    if (!_isTrigerred)
                    {
                        _isTrigerred = true;
                        Print("Trailing Stop Loss triggered...");
                    }

                    double newStopLossPrice = Math.Round(Symbol.Bid - TrailingStop * Symbol.PipSize, Symbol.Digits);

                    if (position.StopLoss == null || newStopLossPrice > position.StopLoss)
                    {
                        ModifyPosition(position, newStopLossPrice, position.TakeProfit);
                    }
                }
            }
            else
            {
                double distance = position.EntryPrice - Symbol.Ask;

                if (distance >= Trigger * Symbol.PipSize)
                {
                    if (!_isTrigerred)
                    {
                        _isTrigerred = true;
                        Print("Trailing Stop Loss triggered...");
                    }

                    double newStopLossPrice = Math.Round(Symbol.Ask + TrailingStop * Symbol.PipSize, Symbol.Digits);

                    if (position.StopLoss == null || newStopLossPrice < position.StopLoss)
                    {
                        ModifyPosition(position, newStopLossPrice, position.TakeProfit);
                    }
                }
            }
        }

    }
}


@PUdPUd
Replies

PanagiotisCharalampous
11 Feb 2021, 15:20

Hi PUdPUd,

This part of the code

 var position = Positions.Find(MyLabel);

is limiting the execution of the trailing stop loss to one position at a time. You need to execute that code for all open positions.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous