cBot coding help, please...

Created at 18 Jan 2015, 10:00
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!
amanev's avatar

amanev

Joined 18.01.2015

cBot coding help, please...
18 Jan 2015, 10:00


Hello to everyone.

I am new to C# and coding cBots, but have been tinkering and trying to get a good understanding of it. I started to cut and paste code from the pre-installed cBots (my first steps) and trying to merge two strategies in to 1. I have figured out some, but now I am stuck and no matter what I have typed in I am getting errors.

My objective is to merge SampleRSI bot with the Martingale bot. It opens a trade according to the RSI parameters and when it closes the current trade, if it is in negative figures the next trade opens with martingale options, in this case volume x2.

This is what I have so far, which is a copy of the SampleRSI but added couple of user inputs, the user can change the RSI trigger value (instead of the standard 30,70). I also added couple a lines to send me email notification (not sure if that is correct either, as I only have been tinkering only in cAlgo platform and backtesting).

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

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

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

        [Parameter("Trigger Low", DefaultValue = 30, MinValue = 1, MaxValue = 49)]
        public int TriggerLow { get; set; }

        [Parameter("Trigger High", DefaultValue = 70, MinValue = 51, MaxValue = 100)]
        public int TriggerHigh { get; set; }

        [Parameter("Take Profit in PIPS", DefaultValue = 25, MinValue = 0)]
        public int TakeProfit { get; set; }

        [Parameter("Stop Loss in PIPS", DefaultValue = 25, MinValue = 0)]
        public int StopLoss { get; set; }

        private RelativeStrengthIndex rsi;

        protected override void OnStart()
        {
            rsi = Indicators.RelativeStrengthIndex(Source, Periods);
        }

        protected override void OnTick()
        {
            if (rsi.Result.LastValue < TriggerLow)
            {
                Close(TradeType.Sell);
                Open(TradeType.Buy);
                Notifications.SendEmail("email@hotmail.com", "email@hotmail.com", "RSI cBot", "RSI Open BUY Trade");
            }
            else if (rsi.Result.LastValue > TriggerHigh)
            {
                Close(TradeType.Buy);
                Open(TradeType.Sell);
                Notifications.SendEmail("email@hotmail.com", "email@hotmail.com", "RSI cBot", "RSI Open SELL Trade");
            }
        }

        private void Close(TradeType tradeType)
        {
            foreach (var position in Positions.FindAll("SimpleRSI", Symbol, tradeType))
                ClosePosition(position);
        }

        private void Open(TradeType tradeType)
        {
            var position = Positions.Find("SimpleRSI", Symbol, tradeType);

            if (position == null)
                ExecuteMarketOrder(tradeType, Symbol, Volume, "SimpleRSI");
        }
    }
}

Then I coppied and pasted this part of the code from the Sample Martingale cBot before private void Close(TradeType tradeType) line

         private void ExecuteOrder(long volume, TradeType tradeType)
        {
            var result = ExecuteMarketOrder(tradeType, Symbol, volume, "Martingale", StopLoss, TakeProfit);

            if (result.Error == ErrorCode.NoMoney)
                Stop();
        }

        private void OnPositionsClosed(PositionClosedEventArgs args)
        {
            Print("Closed");
            var position = args.Position;

            if (position.Label != "Martingale" || position.SymbolCode != Symbol.Code)
                return;

            if (position.GrossProfit > 0)
            {
                ExecuteOrder(Volume, GetRandomTradeType());
            }
            else
            {
                ExecuteOrder((int)position.Volume * 2, position.TradeType);
            }
        }

Though I have to add this line to the code in order to build with no errors, (but I like omit this line, have it so it follows through with the next trade according to the RSI parameters).

         private Random random = new Random();

After executing the and testing, well first part works, when a certain RSI value is reached, it opens a trade, but it continues does not close a the takeprofit or stoploss values and the next trade it opens it does not apply the martingale options. CAn anyone please help me correcting this code, much appreciated in advance..

My second and shorter question is there any Books for Dummies on how to code cBots in cAlgo besides the API reference and Guide section of this site (though helpful a more detail explanation in to the code be nice) ??


@amanev
Replies

amanev
19 Jan 2015, 09:39

RE:

Apologies for my typo's and gramma, spell checker was not working well..... Just correcting some typo errors...

 

Though I add this line to the code in order to build with no errors, (but I like to omit the line, have it so it follows through with the next trade according to the RSI parameters).

         private Random random = new Random();

After executing and testing, the first part works well, when a certain RSI value is reached, it opens a trade, but it continues, does not close with a takeprofit or stoploss values and the next trade opens, it does not apply the martingale options. Can anyone please help me correcting this code, much appreciated in advance..

My second and shorter question is - is there any Books for Dummies on how to code cBots in cAlgo besides the API reference and Guide section of this site (though helpful a more detail explanation in to the code be nice) ??

amanev said:

Though I have to add this line to the code in order to build with no errors, (but I like omit this line, have it so it follows through with the next trade according to the RSI parameters).

         private Random random = new Random();

After executing the and testing, well first part works, when a certain RSI value is reached, it opens a trade, but it continues does not close a the takeprofit or stoploss values and the next trade it opens it does not apply the martingale options. CAn anyone please help me correcting this code, much appreciated in advance..

My second and shorter question is there any Books for Dummies on how to code cBots in cAlgo besides the API reference and Guide section of this site (though helpful a more detail explanation in to the code be nice) ??

 


@amanev

Spotware
20 Jan 2015, 11:23

Hello amanev,

 

For help with coding please contact one of our partners /consultants/  or post a job in Development Jobs section /jobs/

 

Cheers.


@Spotware

amanev
21 Jan 2015, 10:08

RE:

Spotware said:

Hello amanev,

 

For help with coding please contact one of our partners /consultants/  or post a job in Development Jobs section /jobs/

 

Cheers.

OK


@amanev

9718853
31 Jan 2015, 01:03

RE: RE:

Hi Amanev,

 

 

 

I started coding cBots without any coding knowledge and I found that printing some code onto paper and then making notes helped...

EG: notes on where brackets should be placed and why...

There's plenty of help online with C# forums or tutorials. This forum is a good resource too, even if the replies take a while to come through!

I would advise that you continue to post your code. The more you try to learn, the happier people will be to help. 

Keep at it and things will just start to click... 

 

 

amanev said:

Apologies for my typo's and gramma, spell checker was not working well..... Just correcting some typo errors...

 

Though I add this line to the code in order to build with no errors, (but I like to omit the line, have it so it follows through with the next trade according to the RSI parameters).

         private Random random = new Random();

After executing and testing, the first part works well, when a certain RSI value is reached, it opens a trade, but it continues, does not close with a takeprofit or stoploss values and the next trade opens, it does not apply the martingale options. Can anyone please help me correcting this code, much appreciated in advance..

My second and shorter question is - is there any Books for Dummies on how to code cBots in cAlgo besides the API reference and Guide section of this site (though helpful a more detail explanation in to the code be nice) ??

amanev said:

Though I have to add this line to the code in order to build with no errors, (but I like omit this line, have it so it follows through with the next trade according to the RSI parameters).

         private Random random = new Random();

After executing the and testing, well first part works, when a certain RSI value is reached, it opens a trade, but it continues does not close a the takeprofit or stoploss values and the next trade it opens it does not apply the martingale options. CAn anyone please help me correcting this code, much appreciated in advance..

My second and shorter question is there any Books for Dummies on how to code cBots in cAlgo besides the API reference and Guide section of this site (though helpful a more detail explanation in to the code be nice) ??

 

 


@9718853