Keep Stoploss in previous bar

Created at 06 Jan 2021, 18:16
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!
NG

nguyendan81985

Joined 24.10.2018

Keep Stoploss in previous bar
06 Jan 2021, 18:16


Hello,

I'm new C# language coding all together, and im stuck with problem as follow

pls refer to picture, when i will Open Sell position 1 as current as Candle 3 with Stoploss above Candle 1. If th

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 test : Robot
    {
        [Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 0.1, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }


        protected override void OnStart()
        {

        }

        protected override void OnBar()
        {


            var SellOrder = Positions.Find("Sell", SymbolName, TradeType.Sell);
            var SellOrder2 = Positions.Find("Sell2", SymbolName, TradeType.Sell);

            var Open1 = MarketSeries.Open.Last(1);
            var Close1 = MarketSeries.Close.Last(1);
            var High1 = MarketSeries.High.Last(1);
            var Low1 = MarketSeries.Low.Last(1);

            var Open2 = MarketSeries.Open.Last(2);
            var Close2 = MarketSeries.Close.Last(2);
            var High2 = MarketSeries.High.Last(2);
            var Low2 = MarketSeries.Low.Last(2);



            var SL = (High1 - Close1) * 10000;



            if (SellOrder == null)
            {
                if (Close1 < Close2)
                {

                    ExecuteMarketOrder(TradeType.Buy, SymbolName, VolumeInUnits, "Sell", SL, 20);

                }
            }

            if (SellOrder != null)
            {
                if (SellOrder.TradeType == TradeType.Sell)
                {
                    if (SellOrder.Pips <= -20 && SellOrder2 == null)
                    {
                        ExecuteMarketOrder(TradeType.Sell, SymbolName, VolumeInUnits, "Sell2", SL, 20);
                    }

                }
            }

        }
        private long VolumeInUnits
        {
            get { return Symbol.QuantityToVolume(Quantity); }
        }

    }
}



e price go up 20pip, i will open Sell Position 2, and i want to put Stoploss with same position 1 (candle 3). The candle 4 and 5 is appeared, then the open sell position 2, but the Stoploss get the value of candle 4 not candle 1 (same with position 1). it is wrong. because it get the previous bar. and wrong. Could you pls help me to solve this error?

 

 


@nguyendan81985
Replies

PanagiotisCharalampous
07 Jan 2021, 09:31

Hi nguyendan81985,

If you want to use the previous position's SL, try the following

            if (SellOrder != null)
            {
                if (SellOrder.TradeType == TradeType.Sell)
                {
                    if (SellOrder.Pips <= -20 && SellOrder2 == null)
                    {
                        var position = ExecuteMarketOrder(TradeType.Sell, SymbolName, VolumeInUnits, "Sell2", SL, 20).Position;
                        position.ModifyStopLossPrice(SellOrder.StopLoss);
                    }

                }
            }

Best Regards,

Panagiotis 

Join us on Telegram 

 


@PanagiotisCharalampous

nguyendan81985
08 Jan 2021, 03:09

RE:

PanagiotisCharalampous said:

Hi nguyendan81985,

If you want to use the previous position's SL, try the following

            if (SellOrder != null)
            {
                if (SellOrder.TradeType == TradeType.Sell)
                {
                    if (SellOrder.Pips <= -20 && SellOrder2 == null)
                    {
                        var position = ExecuteMarketOrder(TradeType.Sell, SymbolName, VolumeInUnits, "Sell2", SL, 20).Position;
                        position.ModifyStopLossPrice(SellOrder.StopLoss);
                    }

                }
            }

Best Regards,

Panagiotis 

Join us on Telegram 

 

Dear Mr.Panagiotis 

thanks for your feedback. 

now i have problem with below coding. when selloder.pips <-20pip, the sell position 2 will be opened, but it only open when the bar is closed. How can it open position 2 when selloder.pips <-20, no need to waiting bar closed?

            if (SellOrder != null)
            {
                if (SellOrder.TradeType == TradeType.Sell)
                {
                    if (SellOrder.Pips <= -20 && SellOrder2 == null)
                    {
                        var position = ExecuteMarketOrder(TradeType.Sell, SymbolName, VolumeInUnits, "Sell2", SL, 20).Position;
                        position.ModifyStopLossPrice(SellOrder.StopLoss);
                    }

                }
            }

 


@nguyendan81985

PanagiotisCharalampous
08 Jan 2021, 08:30

Hi nguyendan81985,

You should put your code in OnTick() method rather than in OnBar().

Best Regards,

Panagiotis 

Join us on Telegram 


@PanagiotisCharalampous

nguyendan81985
08 Jan 2021, 11:01

RE:

PanagiotisCharalampous said:

Hi nguyendan81985,

You should put your code in OnTick() method rather than in OnBar().

Best Regards,

Panagiotis 

Join us on Telegram 

Hi Panagiotis,

i put code in Ontick, but there are many position 2 which is opened when sellorder.pip <=20. i need only ONE position 2 is opened.

Best Regards,

Panagiotis 

 


@nguyendan81985

... Deleted by UFO ...

... Deleted by UFO ...

... Deleted by UFO ...

PanagiotisCharalampous
08 Jan 2021, 11:28

Hi nguyendan81985,

Then you have done something wrong but I do not know what it is without the source code.

Best Regards,

Panagiotis 

Join us on Telegram 


@PanagiotisCharalampous

nguyendan81985
09 Jan 2021, 02:51

RE:

PanagiotisCharalampous said:

Hi nguyendan81985,

Then you have done something wrong but I do not know what it is without the source code.

Best Regards,

Panagiotis 

Join us on Telegram 

Dear Mr. Panagiotis,

thanks for you advise, my code is working for open position 2 and position 3 also. however, i want to close all position if the position 3.pips is > 30 pip. my source code as below and there is some wrong: only position 3 is closed if position.pip>30pip. could u pls advise me?

 

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 test : Robot
    {
        [Parameter("Quantity (Lots)", Group = "Volume", DefaultValue = 0.1, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }
        [Parameter("Slow Periods", DefaultValue = 21)]
        public int SlowPeriods { get; set; }

        [Parameter("Source")]
        public DataSeries SourceSeries { get; set; }

        private ExponentialMovingAverage slowMa;
        protected override void OnStart()
        {
            slowMa = Indicators.ExponentialMovingAverage(SourceSeries, 21);

        }


        protected override void OnTick()
        {
            var SellOrder = Positions.Find("Sell", SymbolName, TradeType.Sell);
            var SellOrder2 = Positions.Find("Sell2", SymbolName, TradeType.Sell);
            var SellOrder3 = Positions.Find("Sell3", SymbolName, TradeType.Sell);


            if (SellOrder != null)
            {
                if (SellOrder.TradeType == TradeType.Sell)
                {
                    if (SellOrder.Pips <= -20 && SellOrder2 == null)
                    {
                        ExecuteMarketOrder(TradeType.Sell, SymbolName, VolumeInUnits, "Sell2", null, null);
                    }

                    if (SellOrder.Pips <= -40 && SellOrder3 == null)
                    {
                        ExecuteMarketOrder(TradeType.Sell, SymbolName, VolumeInUnits, "Sell3", null, null);
                    }
                }
            }



            if (SellOrder != null && SellOrder2 != null && SellOrder3 == null)
            {

                foreach (var position in Positions)
                {
                    if (position.Pips > 20)
                    {
                        ClosePosition(position);
                    }
                }
            }

            else if (SellOrder3 != null)
            {

                foreach (var position in Positions)
                {
                    if (position.Pips > 30)
                    {
                        ClosePosition(position);
                    }
                }
            }

        }


        protected override void OnBar()
        {


            var SellOrder = Positions.Find("Sell", SymbolName, TradeType.Sell);
            var SellOrder2 = Positions.Find("Sell2", SymbolName, TradeType.Sell);

            var SellOrder3 = Positions.Find("Sell3", SymbolName, TradeType.Sell);

            var Open1 = MarketSeries.Open.Last(1);
            var Close1 = MarketSeries.Close.Last(1);
            var High1 = MarketSeries.High.Last(1);
            var Low1 = MarketSeries.Low.Last(1);

            var Open2 = MarketSeries.Open.Last(2);
            var Close2 = MarketSeries.Close.Last(2);
            var High2 = MarketSeries.High.Last(2);
            var Low2 = MarketSeries.Low.Last(2);

            var Close3 = Bars.ClosePrices.Last(3);
            var Open3 = Bars.OpenPrices.Last(3);
            var High3 = Bars.HighPrices.Last(3);
            var Low3 = MarketSeries.Low.Last(3);

            double sma21_1 = slowMa.Result.LastValue;
            double sma21_2 = slowMa.Result.Last(2);
            double sma21_3 = slowMa.Result.Last(3);

            if (SellOrder == null)
            {
                if (Close3 > sma21_3 && Close2 > sma21_2 && Close1 < sma21_1)
                {
                    ExecuteMarketOrder(TradeType.Sell, SymbolName, VolumeInUnits, "Sell", null, null);
                }
            }


        }
        private long VolumeInUnits
        {
            get { return Symbol.QuantityToVolume(Quantity); }
        }

    }
}

 


@nguyendan81985

PanagiotisCharalampous
11 Jan 2021, 08:57

Hi ,

Try changing

            else if (SellOrder3 != null)
            {

                foreach (var position in Positions)
                {
                    if (position.Pips > 30)
                    {
                        ClosePosition(position);
                    }
                }
            }

to 

            if (SellOrder3 != null && SellOrder3.Pips > 30)
            {

                foreach (var position in Positions)
                {
                     ClosePosition(position);                    
                }
            }

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

nguyendan81985
11 Jan 2021, 15:55

RE:

Dear Mr. Panagiotis,

Thanks for your help. it is worked for me.

 


@nguyendan81985