Trailing stop cod

Created at 13 Jul 2020, 20:22
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!
RI

ricckytrading

Joined 13.07.2020

Trailing stop cod
13 Jul 2020, 20:22


Is someone have a sample of code for trailing stop. Thanks


@ricckytrading
Replies

PanagiotisCharalampous
14 Jul 2020, 08:27

Hi ricckytrading,

Check here.

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous

ricckytrading
14 Jul 2020, 18:26

RE:

PanagiotisCharalampous said:

Hi ricckytrading,

Check here.

Best Regards,

Panagiotis 

Join us on Telegram

 

Dear,

Thank you for your response, I'm going to do it but can you tell me why this code don't work?

Especially in this part:

▲Print("Position Pips", position.Pips); // print show only one value (value at entry position) or we need value for each bar or each tick if in case OnTick()

We need to integrate trailing stop code and our programm don't work on this command

 

 

 


            }
            // PuPt your core logic here
        }

Thanks a lot


@ricckytrading

PanagiotisCharalampous
15 Jul 2020, 09:55

Hi ricckytrading,

You need to provide more information regarding your problem. You need to provide at least the following

1) The complete cBot code so that we can use it and get the same results as you.

2) A clear description of what does the code do and what should it do instead.

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

ricckytrading
24 Jul 2020, 11:45

RE:

PanagiotisCharalampous said:

Hi ricckytrading,

You need to provide more information regarding your problem. You need to provide at least the following

1) The complete cBot code so that we can use it and get the same results as you.

2) A clear description of what does the code do and what should it do instead.

Best Regards,

Panagiotis 

Join us on Telegram

Hi,

Here is the complete code, the problem is that on backtest the traling stop is not working well (sometimes it works, sometimes no).

The results are also different in real life demo and backtesting depending on the broker. 
I'm only talking about the trailing stop part.
more explanation: normally when the trigger is reached the SL should move a fixed distance from the bid or ask depending on whether it is a buy or a sell. All of this works randomly with the code. Sometimes it works, sometimes it doesn't. and we tested in demo simultaneously on 2 brokers and the results are different


        #region ENTRER en POSITION
        public void Take_position_Buy()
        {
            if ((ichimoku.TenkanSen.LastValue >= Bars.OpenPrices.Last(1)) && (ichimoku.TenkanSen.LastValue <= Bars.OpenPrices.Last(0)))
            {
                Print("Cassure Tenkan VERTE");
                compte_bougie = 1;
                confirmation_condition_buy = true;
                Print("Tenkan lastvalue: ", ichimoku.TenkanSen.LastValue);
            }
            compte_bougie += 1;
            //Print(compte_bougie);
            if (compte_bougie > 3)
            {
                compte_bougie = 0;
                confirmation_condition_buy = false;
            }
            if (compte_bougie != 0 && confirmation_condition_buy == true)
            {
                if ((ichimoku.KijunSen.LastValue >= Bars.OpenPrices.Last(1)) && (ichimoku.KijunSen.LastValue <= Bars.OpenPrices.Last(0)))
                {
                    Print("Cassure Kijun VERTE");
                    if ((Bars.ClosePrices.Last(1) > ichimoku.SenkouSpanA.Last(27)) && (Bars.ClosePrices.Last(1) > ichimoku.SenkouSpanB.Last(27)))
                    {
                        switch (et)
                        {
                            case true:
                                if ((high_tf_series.ClosePrices.Last(1) > ichimoku_htf.SenkouSpanA.Last(27)) && (high_tf_series.ClosePrices.Last(1) > ichimoku_htf.SenkouSpanB.Last(27)))
                                {
                                    if ((very_high_tf_series.ClosePrices.Last(1) > ichimoku_very_htf.SenkouSpanA.Last(27)) && (very_high_tf_series.ClosePrices.Last(1) > ichimoku_very_htf.SenkouSpanB.Last(27)))
                                    {
                                        double taille_bougie = Math.Round(((Bars.ClosePrices.Last(1) - Bars.OpenPrices.Last(1)) / Symbol.PipSize), 2);
                                        Print("Taille Bougie: ", taille_bougie);


                                        if (taille_bougie > stoploss)
                                        {
                                            _stoploss = taille_bougie + 1;
                                            Print("New inital Stop Loss", _stoploss);
                                            Print("Take position BUY");
                                            Open(TradeType.Buy);
                                            compte_bougie = 0;
                                            confirmation_condition_buy = false;

                                        }
                                        else
                                        {
                                            _stoploss = stoploss;
                                            Print("Default Stoploss maintained", _stoploss);
                                            Print("Take position BUY");
                                            Open(TradeType.Buy);
                                            compte_bougie = 0;
                                            confirmation_condition_buy = false;
                                        }

                                    }
                                }
                                break;
                            case false:
                                if ((high_tf_series.ClosePrices.Last(1) > ichimoku_htf.SenkouSpanA.Last(27)) && (high_tf_series.ClosePrices.Last(1) > ichimoku_htf.SenkouSpanB.Last(27)) || ((very_high_tf_series.ClosePrices.Last(1) > ichimoku_very_htf.SenkouSpanA.Last(27)) && (very_high_tf_series.ClosePrices.Last(1) > ichimoku_very_htf.SenkouSpanB.Last(27))))
                                {
                                    double taille_bougie = Math.Round(((Bars.ClosePrices.Last(1) - Bars.OpenPrices.Last(1)) / Symbol.PipSize), 2);
                                    Print("Taille Bougie: ", taille_bougie);


                                    //modification du stoploss initial selon la taille de la bougie
                                    if (taille_bougie > stoploss)
                                    {
                                        _stoploss = taille_bougie + 1;
                                        Print("New initial Stop Loss", _stoploss);
                                        Print("Take position BUY");
                                        Open(TradeType.Buy);
                                        compte_bougie = 0;
                                        confirmation_condition_buy = false;
                                    }
                                    else
                                    {
                                        _stoploss = stoploss;
                                        Print("Take position BUY");
                                        Open(TradeType.Buy);
                                        compte_bougie = 0;
                                        confirmation_condition_buy = false;
                                    }


                                }
                                break;
                        }
                    }
                }
            }
        }

        public void Take_position_Sell()
        {
            if ((ichimoku.TenkanSen.LastValue <= Bars.OpenPrices.Last(1)) && (ichimoku.TenkanSen.LastValue >= Bars.OpenPrices.Last(0)))
            {
                compte_bougie_sell = 1;
                confirmation_condition_sell = true;
                //Print(compte_bougie_sell);
                Print("Cassure Tenkan Bougie Rouge");
            }
            compte_bougie_sell += 1;
            //Print(compte_bougie);
            if (compte_bougie_sell > 3)
            {
                compte_bougie_sell = 0;
                confirmation_condition_sell = false;
            }
            if (compte_bougie_sell != 0 && confirmation_condition_sell == true)
            {
                if ((ichimoku.KijunSen.LastValue <= Bars.OpenPrices.Last(1)) && (ichimoku.KijunSen.LastValue >= Bars.OpenPrices.Last(0)))
                {
                    Print("Cassure Kijun Bougie Rouge");
                    if ((Bars.ClosePrices.Last(1) < ichimoku.SenkouSpanA.Last(27)) && (Bars.ClosePrices.Last(1) < ichimoku.SenkouSpanB.Last(27)))
                    {
                        switch (et)
                        {
                            case true:
                                //Convergence des UT
                                if ((high_tf_series.ClosePrices.Last(1) < ichimoku_htf.SenkouSpanA.Last(27)) && (high_tf_series.ClosePrices.Last(1) < ichimoku_htf.SenkouSpanB.Last(27)))
                                {
                                    if ((very_high_tf_series.ClosePrices.Last(1) < ichimoku_very_htf.SenkouSpanA.Last(27)) && (very_high_tf_series.ClosePrices.Last(1) < ichimoku_very_htf.SenkouSpanB.Last(27)))
                                    {
                                        double taille_bougie = Math.Round(((Bars.OpenPrices.Last(1) - Bars.ClosePrices.Last(1)) / Symbol.PipSize), 2);
                                        Print("Taille Bougie: ", taille_bougie);

                                        //modification du stoploss initial selon la taille de la bougie d'entrer
                                        if (taille_bougie > stoploss)
                                        {
                                            _stoploss = taille_bougie + 1;
                                            Print("New initial Stop Loss", _stoploss);
                                            Print("Take position SELL");
                                            Open(TradeType.Sell);
                                            compte_bougie_sell = 0;
                                            confirmation_condition_sell = false;
                                        }
                                        else
                                        {
                                            _stoploss = stoploss;
                                            Print("Take position SELL");
                                            Open(TradeType.Sell);
                                            compte_bougie_sell = 0;
                                            confirmation_condition_sell = false;
                                        }

                                    }
                                }
                                break;
                            case false:
                                if (((high_tf_series.ClosePrices.Last(1) < ichimoku_htf.SenkouSpanA.Last(27)) && (high_tf_series.ClosePrices.Last(1) < ichimoku_htf.SenkouSpanB.Last(27)) || (very_high_tf_series.ClosePrices.Last(1) < ichimoku_very_htf.SenkouSpanA.Last(27))) && (very_high_tf_series.ClosePrices.Last(1) < ichimoku_very_htf.SenkouSpanB.Last(27)))
                                {
                                    double taille_bougie = Math.Round(((Bars.OpenPrices.Last(1) - Bars.ClosePrices.Last(1)) / Symbol.PipSize), 2);
                                    Print("Taille Bougie: ", taille_bougie);

                                    if (taille_bougie > stoploss)
                                    {
                                        _stoploss = taille_bougie + 1;
                                        Print("New initial Stop Loss", _stoploss);
                                        Print("Take position SELL");
                                        Open(TradeType.Sell);
                                        compte_bougie_sell = 0;
                                        confirmation_condition_sell = false;
                                    }
                                    else
                                    {
                                        _stoploss = stoploss;
                                        Print("Take position SELL");
                                        Open(TradeType.Sell);
                                        compte_bougie_sell = 0;
                                        confirmation_condition_sell = false;
                                    }

                                }
                                break;
                        }
                    }
                }
            }
        }
        #endregion
        //******************************************************************************////////

        //*****************************************************************************///////////

        public void dynamique_stoploss()
        {
            foreach (var position in Positions)
            {

                //Quand la position atteind un niveau de gain (pips)
                if (position.Pips >= trigger)
                {
                    Print("Gain");
                    if (position.TradeType == TradeType.Buy)
                    {
                        // BUY    
                        var newSLprice = Symbol.Ask - (Symbol.PipSize * distance_trailing);
                        Print("new SL: ", newSLprice);
                        if (newSLprice > position.StopLoss)
                        {
                            ModifyPosition(position, newSLprice, position.TakeProfit);
                        }
                    }
                    else
                    {
                        // SELL    
                        var newSLprice = Symbol.Bid + (Symbol.PipSize * distance_trailing);
                        Print("new SL: ", newSLprice);
                        if (newSLprice < position.StopLoss)
                        {
                            ModifyPosition(position, newSLprice, position.TakeProfit);
                        }
                    }

                }
            }
        }


        ////**********************************************************************************/////
        double Calcul_lot(double capital)
        {
            double nbr_lot = (((capital * (risque / 100)) / stoploss) / 10);
            return Math.Round(nbr_lot, 2);
        }
        //***********************************************************************************///// 
        public bool verifie_chikou_sup_nuage()
        {
            int latest_bar = Bars.Count - 1;
            double valeur_chikou = ichimoku.ChikouSpan[latest_bar - (kijunPeriod + 1)];
            if (valeur_chikou > ichimoku.SenkouSpanA.Last(53) && valeur_chikou > ichimoku.SenkouSpanB.Last(53))
            {
                Print("Chikou valeur au moment du signal", valeur_chikou);
                return true;
            }
            Print("Chikou valeur au moment du signal", valeur_chikou);
            return false;
        }
        //**********************************************************************************/////


        public bool Verifie_chikou_libre()
        {
            int latest_bar = Bars.Count - 1;
            double valeur_chikou = ichimoku.ChikouSpan[latest_bar - (kijunPeriod + 1)];

            //Print("Chikoou value", ChikouSpanLatest);
            //Test si chikou 

            //Test s'il n'y a pas de bougie devant
            int indice_bougie;
            for (indice_bougie = 26; indice_bougie > 0; indice_bougie--)
            {
                if ((!(valeur_chikou >= Bars.OpenPrices.Last(indice_bougie) && valeur_chikou >= Bars.ClosePrices.Last(indice_bougie)) && (!(valeur_chikou <= Bars.OpenPrices.Last(indice_bougie) && valeur_chikou <= Bars.ClosePrices.Last(indice_bougie)))))
                {
                  
            return true;

        }
        //************************************************************************************///        
    }
}

 

 


@ricckytrading

ricckytrading
24 Jul 2020, 17:02

RE:

PanagiotisCharalampous said:

Hi ricckytrading,

You need to provide more information regarding your problem. You need to provide at least the following

1) The complete cBot code so that we can use it and get the same results as you.

2) A clear description of what does the code do and what should it do instead.

Best Regards,

Panagiotis 

Join us on Telegram

our trade entry strategy is based on a breakout of kijunSen after / or with a breakout of tenkansen.
The breakout of tenkanen can occur four candles before the breakout of the kijunSen at most or on the same breakout of the kijunSen.
 We notice that in live demo the code does not work.
It only retains the only breaks on the same candle of kijunSen and tenkanSen.
On the other hand in backtesting the code manages to find the other entry points.


WHY ??

Thank you .


@ricckytrading