Replies

armstr.tradie
07 Jan 2018, 07:57

Thank you Paul, it worked perfectly. Keep up the good work. I appreciate the help you provide on this forum. 


@armstr.tradie

armstr.tradie
15 Dec 2017, 00:06

It worked!

Thank you, Panagiotis for your help. Sorry for the late reply. 

With a bit of tweaking of the code I was able to get it to work in a satisfactory manner. 

Here is my code for others to use if they are interested. Please suggest any improvements.

 

//Sets the minimum distance between a pending order and an open position.

        [Parameter("Minimum Trade Separation", DefaultValue = 3)]
        public double MinPipDistance { get; set; }
//Removes any pending order at the same level or near an already existing position of the same TradeType

        protected void PositionExists()
        {
            var longPosition = Positions.Find(Label, Symbol, TradeType.Buy);
            var shortPosition = Positions.Find(Label, Symbol, TradeType.Sell);

            if (longPosition != null)
            {
                var MinBuyDistance = longPosition.EntryPrice + MinPipDistance * Symbol.PipSize;

                foreach (var order in PendingOrders)
                {
                    if (order.TradeType == TradeType.Buy)
                    {
                        if ((order.TargetPrice >= longPosition.EntryPrice) && (order.TargetPrice < MinBuyDistance))
                        {
                            CancelPendingOrder(order);
                        }
                    }
                }
            }

            else if (shortPosition != null)
            {
                var MinSellDistance = shortPosition.EntryPrice - MinPipDistance * Symbol.PipSize;

                foreach (var order in PendingOrders)
                {
                    if (order.TradeType == TradeType.Sell)
                    {
                        if ((order.TargetPrice <= shortPosition.EntryPrice) && (order.TargetPrice > MinSellDistance))
                        {
                            CancelPendingOrder(order);
                        }
                    }
                }
            }
        }

Thank you again Panagiotis for your help. You have made using cAlgo that much easier and I very grateful for all your time and help. 


@armstr.tradie

armstr.tradie
21 Nov 2017, 20:37

Thanks again Panagiotis for the quick reply,

I've added 'not null' logic in the code, as you can see below. 

protected void PositionExistsBuy()
        {
            var longPosition = Positions.Find(Label, Symbol, TradeType.Buy);
            var MinBuyDistance = longPosition.EntryPrice + MinPipDistance * Symbol.PipSize;

            foreach (var order in PendingOrders)
            {
                if (longPosition != null)
                {
                    if (order.TradeType == TradeType.Buy)
                    {
                        //var pipDistance = Math.Abs((order.TargetPrice + longPosition.EntryPrice) / Symbol.PipValue);
                        if (order.TargetPrice < MinPipDistance)
                        {
                            CancelPendingOrder(order);
                        }
                    }
                }
            }
        }

        protected void PositionExistsSell()
        {
            var shortPosition = Positions.Find(Label, Symbol, TradeType.Sell);
            var MinSellDistance = shortPosition.EntryPrice - MinPipDistance * Symbol.PipSize;

            foreach (var order in PendingOrders)
            {
                if (shortPosition != null)
                {
                    if (order.TradeType == TradeType.Sell)
                    {
                        //var pipDistance = Math.Abs((order.TargetPrice - shortPosition.EntryPrice) / Symbol.PipValue);
                        if (order.TargetPrice > MinPipDistance)
                        {
                            CancelPendingOrder(order);
                        }
                    }
                }
            }
        }

Unfortunately,  again I get the same issue. It starts the robot, creates a series of pending orders, closes them, and then stops the bot.

Perhaps taking a different approach may be better?. My understanding of C# is not as advanced as others - so I more than welcome any other ideas that could help.

Thank you


@armstr.tradie

armstr.tradie
21 Nov 2017, 10:29

Thank you Panagiotis for your very quick reply.

Here is the code for my bot. As you can see it is based on a renko indicator. The indicator referenced in the bot is this one: /algos/indicators/show/1086 - full credit to 'tmc' who created it. The renko system helps set the price level and trend of the bot. 

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

namespace cAlgo.Robots
{
    [Robot()]
    public class RENKO : Robot
    {
        //################################################## - Bot Label - ##################################################

        [Parameter(DefaultValue = "Abot")]
        public string Label { get; set; }

        //################################################## - RENKO details - #############################################

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

        //public double RenkoPips = 50;

        public int BricksToShow = 10000;

        private Renko renko;

        //################################################## - Other parameters - #############################################

        [Parameter(DefaultValue = 1000)]
        public int Volume { get; set; }

        [Parameter("Take Profit (pips)", DefaultValue = 2000)]
        public double TakeProfit { get; set; }

        [Parameter("Stop Loss (pips)", DefaultValue = 1000)]
        public double StopLoss { get; set; }

        //################################################## - Dynamic stops - #############################################

        [Parameter("Break Even Trigger", DefaultValue = 200)]
        public double SL1Trigger { get; set; }

        [Parameter("Break Even", DefaultValue = 25)]
        public double SL1 { get; set; }

        [Parameter("Trailing Stop Trigger", DefaultValue = 1000)]
        public double SL2Trigger { get; set; }

        [Parameter("Trailing Stop Level", DefaultValue = 500)]
        public double SL2 { get; set; }

        //################################################## - Buy/Sell range - #############################################

        // Sets the level at which pending trades are separated from one another.
        [Parameter("Order Price Level", DefaultValue = 100)]
        public double TP1 { get; set; }

        // How many orders to be made when the bot runs.
        [Parameter("Number of Orders", DefaultValue = 31)]
        public int HowMuchPositions { get; set; }

        // Option to possible have multiple orders at the same level.
        [Parameter("Multiply Orders x ...", DefaultValue = 1)]
        public double MaxPendingOrders { get; set; }

        // Option designed to stop double-up of trades near a similar price level. 
        [Parameter("Minimum Trade Separation", DefaultValue = 90)]
        public double MinPipDistance { get; set; }

        //################################################## - OnStart - ##################################################

        protected override void OnStart()
        {
            renko = Indicators.GetIndicator<Renko>(RenkoPips, BricksToShow, 3, "SeaGreen", "Tomato");
        }

        //################################################## - OnTick - ##################################################

        protected override void OnTick()
        {
            BuyAndSell();
            BreakEven();
            TrailingStop();
            PositionExistsBuy();
            PositionExistsSell();
        }

        //################################################## - Buy & Sell Details - ##################################################

        protected void BuyAndSell()
        {
            double rClose = renko.Close.Last(0);
            double rOpen = renko.Open.Last(0);

            double Close = MarketSeries.Close.LastValue;
            double Open = MarketSeries.Open.LastValue;

            var MaxOrders = PendingOrders.Count < MaxPendingOrders;

            // Setup pending BUY
            if (Close > rOpen)
            {
                ClosePendingSell();
                if (MaxOrders)
                {
                    for (double i = 1; i < HowMuchPositions; i++)
                    {
                        PlaceStopOrder(TradeType.Buy, Symbol, Volume, rOpen + TP1 * i * Symbol.PipSize, Label, StopLoss, TakeProfit);

                    }
                }
            }

            // Setup pending SELL
            else if (Close < rOpen)
            {
                ClosePendingBuy();
                if (MaxOrders)
                {
                    for (double j = 1; j < HowMuchPositions; j++)
                    {
                        PlaceStopOrder(TradeType.Sell, Symbol, Volume, rOpen - TP1 * j * Symbol.PipSize, Label, StopLoss, TakeProfit);

                    }
                }
            }
        }

        //################################################## - Pending Order Close Logic - ##################################################

        protected void ClosePendingSell()
        {
            foreach (var order in PendingOrders)
            {
                if (order.TradeType == TradeType.Sell)
                    CancelPendingOrderAsync(order);
            }
        }

        protected void ClosePendingBuy()
        {
            foreach (var order in PendingOrders)
            {
                if (order.TradeType == TradeType.Buy)
                    CancelPendingOrderAsync(order);
            }
        }

        //################################################## - Removal of trade double up logic - ##################################################

        protected void PositionExistsBuy()
        {
            var longPosition = Positions.Find(Label, Symbol, TradeType.Buy);
            var MinBuyDistance = longPosition.EntryPrice + MinPipDistance * Symbol.PipSize;

            foreach (var order in PendingOrders)
            {
                if (order.TradeType == TradeType.Buy)
                {
                    //var pipDistance = Math.Abs((order.TargetPrice + longPosition.EntryPrice) / Symbol.PipValue);
                    if (order.TargetPrice < MinPipDistance)
                    {
                        CancelPendingOrder(order);
                    }
                }
            }
        }

        protected void PositionExistsSell()
        {
            var shortPosition = Positions.Find(Label, Symbol, TradeType.Sell);
            var MinSellDistance = shortPosition.EntryPrice - MinPipDistance * Symbol.PipSize;

            foreach (var order in PendingOrders)
            {
                if (order.TradeType == TradeType.Sell)
                {
                    //var pipDistance = Math.Abs((order.TargetPrice - shortPosition.EntryPrice) / Symbol.PipValue);
                    if (order.TargetPrice > MinPipDistance)
                    {
                        CancelPendingOrder(order);
                    }
                }
            }
        }

        //################################################## - Stop Loss Details - ##################################################

        protected void BreakEven()
        {
            double rClose = renko.Close.Last(0);
            double rOpen = renko.Open.Last(0);
            double Close = MarketSeries.Close.LastValue;

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

            foreach (var position in positions)
            {
                if (position.Pips >= SL1Trigger)
                {
                    if (position.TradeType == TradeType.Buy)
                    {
                        var newStopLoss = position.EntryPrice + SL1 * Symbol.PipSize;
                        if (position.StopLoss < newStopLoss)
                            ModifyPosition(position, newStopLoss, position.TakeProfit);
                    }
                    else if (position.TradeType == TradeType.Sell)
                    {
                        var newStopLoss = position.EntryPrice - SL1 * Symbol.PipSize;
                        if (position.StopLoss > newStopLoss)
                            ModifyPosition(position, newStopLoss, position.TakeProfit);
                    }
                }
            }
        }

        protected void TrailingStop()
        {
            double rClose = renko.Close.Last(0);
            double rOpen = renko.Open.Last(0);
            double Close = MarketSeries.Close.LastValue;

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

            foreach (var position in positions)
            {
                if (position.Pips >= SL2Trigger)
                {
                    if (position.TradeType == TradeType.Buy)
                    {
                        var newStopLoss2 = Symbol.Bid - SL2 * Symbol.PipSize;
                        if (position.StopLoss < newStopLoss2)
                            ModifyPosition(position, newStopLoss2, position.TakeProfit);
                    }
                    else if (position.TradeType == TradeType.Sell)
                    {
                        var newStopLoss2 = Symbol.Ask + SL2 * Symbol.PipSize;
                        if (position.StopLoss > newStopLoss2)
                            ModifyPosition(position, newStopLoss2, position.TakeProfit);

                    }
                }
            }
        }
    }
}

Thank you again for your help. 


@armstr.tradie

armstr.tradie
27 Aug 2016, 05:50

RE:

lucian said:

Try :

 

var newStopLoss =  position.EntryPrice+/-Stoplevel * Symbol.PipSize;

 

Thanks lucian.

I'l give it ago on Sunday/Monday when the markets open and see how it goes.


@armstr.tradie

armstr.tradie
19 Aug 2016, 05:56

RE:

lucian said:

First try to change:

private void MoveToBreakEven()
        {
            foreach (var position in Positions.FindAll(label, Symbol))
            {
                if (position.StopLoss != position.EntryPrice)
                {
                    if (position.Pips >= 5)
                    {
                        ModifyPosition(position, position.EntryPrice, position.TakeProfit);
                    }
                }
            }
        }

in :

private void MoveToBreakEven()
        {
            foreach (var position in Positions.FindAll(label, Symbol,TradeType.Sell))
            {
                if (position.StopLoss != position.EntryPrice)
                {
                    if (position.Pips >= 5)
                    {
                        ModifyPosition(position, position.EntryPrice-(Symbol.PipSize*1), position.TakeProfit);
                    }
                }
            }
        }

            foreach (var position in Positions.FindAll(label, Symbol,TradeType.Buy))
            {
                if (position.StopLoss != position.EntryPrice)
                {
                    if (position.Pips >= 5)
                    {
                        ModifyPosition(position, position.EntryPrice+(Symbol.PipSize*1), position.TakeProfit);
                    }
                }
            }
        }

 

 

Thanks Lucian. It appeared to work well but there seems to be an issue with the triggers I think. It worked as intended when it got to the first trigger for the break-even, but when it reached the second trigger that got the trailing stop loss going the stop loss began to jump to and from the new stop-loss price to the old one with each movement of the bid and ask price.

I wonder if a jumping stop may be the only way out of this issue. Or is there a way to have another stop-loss in same bot - kinda like the advanced take profit? I think the problem is that it has the same stop-loss following two different triggers and it doesn't know which one to follow. Could one make another stop - so you have say "SL" and "SL2"? 

Again any help on this would be greatly appreciated. Thank you. 


@armstr.tradie

armstr.tradie
30 Apr 2016, 04:52

Thank you tmc. That was of great help. 


@armstr.tradie