What's wrong in this code?

Created at 02 Dec 2018, 12:50
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!
RO

rovailore

Joined 02.12.2018

What's wrong in this code?
02 Dec 2018, 12:50


Hi, i've programmed this robot within an custom indicator.

if i build, everything is good, during the backtest it execute the orders but not the trailing stop.

I've copied the trailing stop from a guy on youtube.

the orders doesn't have take profit and stop loss, and i put 100% of balance per trade( i know what i do).

ps: i wrote in a comment a conditions that doesn't works,why?

 

thanks a lot!

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 SbancaScalper : Robot
    {

        [Parameter("Name ", DefaultValue = "SbancaScalper")]
        public string Name { get; set; }

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

        [Parameter("Trailing", DefaultValue = 3)]
        public int Trailing { get; set; }

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

        [Parameter(DefaultValue = 28)]
        public int Period { get; set; }

        [Parameter("MA Type", DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType MAType { get; set; }

        private Keltner KeltnerCH;

        protected override void OnStart()
        {

            KeltnerCH = Indicators.GetIndicator<Keltner>(Source, MAType, Period);

        }

        protected override void OnBar()
        {


            Check_Entry();

            Check_TrailingStop();

        }

        private void Check_Entry()
        {
            var Upper = KeltnerCH.UpperBand.Last(0);

            var Basis = KeltnerCH.Middle.Last(0);

            var Lower = KeltnerCH.LowerBand.Last(0);

            var Balance = Account.Balance;



            if (Positions.Count == 0)
            {

                if (Functions.HasCrossedAbove(MarketSeries.Close, Lower, 0))                /*&& MarketSeries.Close < Basis*/
                {
                    Print("long");

                    ExecuteMarketOrder(TradeType.Buy, Symbol, Balance);
                }

                if (Functions.HasCrossedBelow(MarketSeries.Close, Upper, 0))                /*&& MarketSeries.Close < Basis*/
                {
                    Print("short");

                    ExecuteMarketOrder(TradeType.Sell, Symbol, Balance);
                }

            }
        }

        private void Check_TrailingStop()
        {
            var sellPositions = Positions.FindAll(Name, Symbol, TradeType.Sell);

            foreach (Position position in sellPositions)
            {
                double distance = position.EntryPrice - Symbol.Ask;

                Print("stocazzo");

                if (distance < Trigger * Symbol.PipSize)
                    continue;

                double newStopLossPrice = Symbol.Ask + Trailing * Symbol.PipSize;

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

                }
            }

            var buyPositions = Positions.FindAll(Name, Symbol, TradeType.Buy);

            foreach (Position position in buyPositions)
            {
                double distance = Symbol.Bid - position.EntryPrice;

                if (distance < Trigger * Symbol.PipSize)
                    continue;

                double newStopLossPrice = Symbol.Bid - Trailing * Symbol.PipSize;

                if (position.StopLoss == null || newStopLossPrice > position.StopLoss)
                {
                    ModifyPosition(position, newStopLossPrice, position.TakeProfit);
                    Print("stocazzo");
                }
            }

        }

    }
}

 


@rovailore
Replies

PanagiotisCharalampous
03 Dec 2018, 11:10

Hi rovailore,

Can you please share with us the indicator code as well?

Best Regards,

Panagiotis


@PanagiotisCharalampous

rovailore
03 Dec 2018, 15:38

RE:

Panagiotis Charalampous said:

Hi rovailore,

Can you please share with us the indicator code as well?

Best Regards,

Panagiotis

hi, the indicator is a normal keltner channel with some adjustements

i've found the error in the code for trailing stop but i have another problem...

i don't know how to put 100% of balance per trade

 


@rovailore

PanagiotisCharalampous
03 Dec 2018, 16:48

Hi rovailore,

Can you explain what do you mean when you say "100% of balance per trade"? Do you mean to adjuct the SL so that you risk x% of your balance each time?

Best Regards,

Panagiotis 


@PanagiotisCharalampous

rovailore
03 Dec 2018, 18:38

RE:

Panagiotis Charalampous said:

Hi rovailore,

Can you explain what do you mean when you say "100% of balance per trade"? Do you mean to adjuct the SL so that you risk x% of your balance each time?

Best Regards,

 

I mean 100% of my balance each time 


@rovailore

rovailore
03 Dec 2018, 18:41

Example:

 

if i have a initial capital of 10000 EUR, i want to put 10000EUR in a trade. if trade close in profit of 1%, i will put 10000+1% in the next trade


@rovailore

PanagiotisCharalampous
04 Dec 2018, 09:57

Hi rovailore,

If your balance is in EUR and you are trading EUR then you can use the code below to calculate the volume.

var volume = Symbol.NormalizeVolumeInUnits(Account.Balance, RoundingMode.ToNearest);

If you are not trading EUR but you want to calculate the volume equivalent to your account's balance then this will become more complicated as you will need to make the necessary conversion from EUR to the traded asset.

Best Regards,

Panagiotis


@PanagiotisCharalampous