Replies

8089669
27 Jul 2021, 00:32

RE:

Thank you Amusleh,

I realized that Symbol.Bid or Symbol.Ask can be used after posting the query in the forum.

But Bars.ClosePrices.LastValue is something new i learnt. Thank you.

 

amusleh said:

Hi,

You can use Symbol.Bid or Symbol.Ask if you want to get the latest Bid/Ask price of a symbol.

If you want to get the latest bar close Price you can use Bars.ClosePrices.LastValue.

For historical bars, you can use the Bars collection index and iterate over them.

 


@8089669

8089669
03 Feb 2021, 07:09

RE: Simple and robust trailing algorithm

Hi Abdul,

How can I make it work for the Pending Orders?

Thanks.

abdulhamid0yusuf said:

I came here to see how to implement a trailing stop loss, but the code provided by cTrader Team was unnecessarily complicated to me, so I would like to share my way ...

using cAlgo.API;
using System.Collections.Generic;
using System.Linq;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TestTrailingStop : Robot
    {
        // Save the maximum winning pips each position has reached
        private Dictionary<Position, double> PositionsMaxProfits;

        [Parameter("Winning Pips to protect", DefaultValue = 10)]
        public int PipsToProtect { get; set; }

        [Parameter("Trailing Stop Distance", DefaultValue = 5)]
        public int TrailingDistance { get; set; }


        protected override void OnStart()
        {
            PositionsMaxProfits = new Dictionary<Position, double>();
            // Market order for illustration purposes
            var p = ExecuteMarketOrder(TradeType.Sell, SymbolName, 1000).Position;
            // Add every order you create to the dictionary and set its initial max pips to 0
            PositionsMaxProfits.Add(p, 0);
        }

        protected override void OnTick()
        {
            // Update Stop loss on each tick once the position has yeilded winning pips >= PipsToProtect + TrailingDistance
            foreach (Position position in PositionsMaxProfits.Keys.ToList())
            {
                var positionMaxProfit = PositionsMaxProfits[position];
                // 1st condition checks if winning pips >= PipsToProtect + TrailingDistance
                // 2ed condition checks if winning pips is greater than previous winning pips
                if ((position.Pips >= PipsToProtect + TrailingDistance) && position.Pips > positionMaxProfit)
                {
                    // Assign the new higher winning pips to the position
                    PositionsMaxProfits[position] = position.Pips;
                    // Modify Stop loss to be $TrailingDistance pips away from current winnig pips
                    // ** -1 means put stop loss on oppostie direction than normal
                    position.ModifyStopLossPips(-1 * (position.Pips - TrailingDistance));
                }
            }

        }
    }
}

 

 


@8089669

8089669
24 Dec 2020, 03:35

RE:

PanagiotisCharalampous said:

Hi 8089669,

You need to explain a bit better what are you trying to do. Your description is not very clear. The OHLC information for bars is stored in the robot's Bars property.

Best Regards,

Panagiotis 

Join us on Telegram

 

Hi Panagiotis,

As of now I am using "Math.Abs(Bars.ClosePrices.Maximum(Periods) - Bars.ClosePrices.Minimum(Periods))/ Symbol.PipSize;" to get the difference between High Close price and Low Close Price for last n periods.

But I want to exclude Last(1) bar and calculate from Last(2) bar.

Just wonder if there is any way to exclude Last(1) from the above formula?

Many Thanks in advance.


@8089669