New Idea About Trading

Created at 01 Aug 2017, 09:04
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!
AL

alpha_austin

Joined 31.07.2017

New Idea About Trading
01 Aug 2017, 09:04


Dear Spotware Support Team and Coding Professionals,

I have an idea, but I don't know how to put it into cAlgo programming. Hope you guys can help.

 

The following is my thought of Logic_1:

When RSI reaches 70 or above, open a Sell Trade and 3 or more Sell Limit Orders, that is:

Open a Sell Position volume 10,000 (SL 40pips TP 10pips), then

10 pips above the opened position, Place 1st Sell Limit Order volume 10,000  (SL 30pips TP 10pips), and also

20 pips above the opened position, Place 2nd Sell Limit Order volume 20,000  (SL 20pips TP 10pips), and also

30 pips above the opened position, Place 3rd Sell Limit Order volume 40,000   (SL 10pips TP 10pips).

Once either the market order or either one limit orders TP, Close all trades including all pending orders.

Then either

- wait RSI reaches 30 or below, execute Buy types of the following; or

- open a Buy Position volume 10,000 (SL 40pips TP 10pips), then

10 pips below the opened position, Place 1st Buy Limit Order volume 10,000  (SL 30pips TP 10pips), and also

20 pips below the opened position, Place 2nd Buy Limit Order volume 20,000  (SL 20pips TP 10pips), and also

30 pips below the opened position, Place 3rd Buy Limit Order volume 40,000   (SL 10pips TP 10pips).

Once either the market order or either one limit orders TP, Close all trades including all pending orders.

----------------------------------------------------------------------------------------------------------------------------------------------------------

On the other hand, here is another thought of Logic_2:

Open a random trade type, if sell is opened, then place 3 more same type sell limit like the above data;

if buy is randomly opened, then place 3 more same type buy limit like the above data.


@alpha_austin
Replies

Spotware
01 Aug 2017, 10:18

Hi alpha_austin,

Thanks for posting in our forum. We can help you developing your cBot by answering specific questions related to cAlgo.API and C# programming, as well as by providing small code snippets demonstrating how to implement certain functions. However we cannot engage into developing cBots on demand or debugging complicated cBots.

Best Regards,
cTrader Team


@Spotware

alpha_austin
01 Aug 2017, 11:38

Can Spotware Team or someone correct my coding, many thanks.
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 SampleRSIcBot : Robot
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

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

        [Parameter("Volume", DefaultValue = 10000)]
        public double Quantity { get; set; }

        private RelativeStrengthIndex rsi;

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

        protected override void OnTick()
        {
            if (rsi.Result.LastValue > 70)
            {
                TradeOperation operation = ExecuteMarketOrderAsync(TradeType.Sell, Symbol, 10000, 40, 10);

                PlaceLimitOrderAsync(TradeType.Sell, Symbol, 10000, Symbol.Bid + 10 * Symbol.PipSize, 30, 10);

                PlaceLimitOrderAsync(TradeType.Sell, Symbol, 20000, Symbol.Bid + 20 * Symbol.PipSize, 20, 10);

                PlaceLimitOrderAsync(TradeType.Sell, Symbol, 40000, Symbol.Bid + 30 * Symbol.PipSize, 10, 10);
            }


            else if (rsi.Result.LastValue < 30)
            {
                TradeOperation operation = ExecuteMarketOrderAsync(TradeType.Buy, Symbol, 10000, 40, 10);

                PlaceLimitOrderAsync(TradeType.Buy, Symbol, 10000, Symbol.Ask + 10 * Symbol.PipSize, 30, 10);

                PlaceLimitOrderAsync(TradeType.Buy, Symbol, 20000, Symbol.Ask + 20 * Symbol.PipSize, 20, 10);

                PlaceLimitOrderAsync(TradeType.Buy, Symbol, 40000, Symbol.Ask + 30 * Symbol.PipSize, 10, 10);
            }
        }


          return 

 


@alpha_austin

Spotware
01 Aug 2017, 12:00

Hi alpha_austin,

We have modified the code so that it builds but we think that it does not do exactly what you intend it to do. Nevertheless, you can use the code below as a basis to continue your development.

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 SampleRSIcBot : Robot
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

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

        [Parameter("Volume", DefaultValue = 10000)]
        public double Quantity { get; set; }

        private RelativeStrengthIndex rsi;

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

        protected override void OnTick()
        {
            if (rsi.Result.LastValue > 70)
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, 10000, "Sell", 40, 10);

                PlaceLimitOrder(TradeType.Sell, Symbol, 10000, Symbol.Bid + 10 * Symbol.PipSize, "Sell + 10", 30, 10);

                PlaceLimitOrder(TradeType.Sell, Symbol, 20000, Symbol.Bid + 20 * Symbol.PipSize, "Sell + 20", 20, 10);

                PlaceLimitOrder(TradeType.Sell, Symbol, 40000, Symbol.Bid + 30 * Symbol.PipSize, "Sell + 30", 10, 10);
            }


            else if (rsi.Result.LastValue < 30)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, 10000, "Buy", 40, 10);

                PlaceLimitOrder(TradeType.Buy, Symbol, 10000, Symbol.Ask + 10 * Symbol.PipSize, "Buy + 10", 30, 10);

                PlaceLimitOrder(TradeType.Buy, Symbol, 20000, Symbol.Ask + 20 * Symbol.PipSize, "Buy + 20", 20, 10);

                PlaceLimitOrder(TradeType.Buy, Symbol, 40000, Symbol.Ask + 30 * Symbol.PipSize, "Buy + 30", 10, 10);
            }
        }

    }
}

Best Regards,

cTrader Team


@Spotware

alpha_austin
02 Aug 2017, 10:38

Adding more on the codes

Thanks Spotware Support, the codes show Built successful in cAlgo.

Moreover, I wanna know how to add coding if I wanna close all trades once either position hit TP?

And then return to open a new trade immediately after that?

Thanks again for the help.


@alpha_austin

Spotware
02 Aug 2017, 11:45

Hi alpha_austin,

To achieve this you need to modify your code so that it handles the Positions.Closed event. You need to do the following changes

  1. Add an event handler in OnStart() method
  2. In the event handler check if the closed position has been closed with a profit.
  3. If yes, close all the remaining open positions.
  4. Execute a new market order.

The code should be modified as follows

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SampleRSIcBot : Robot
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

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

        [Parameter("Volume", DefaultValue = 10000)]
        public double Quantity { get; set; }

        private RelativeStrengthIndex rsi;

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

        void OnPositionsClosed(PositionClosedEventArgs obj)
        {
            if (obj.Position.NetProfit > 0)
            {
                foreach (var position in Positions)
                {
                    ClosePosition(position);
                }
                ExecuteMarketOrder(obj.Position.TradeType, Symbol, 10000, "New market order", 40, 10);
            }           
        }

        protected override void OnTick()
        {
            if (rsi.Result.LastValue > 70)
            {
                ExecuteMarketOrder(TradeType.Sell, Symbol, 10000, "Sell", 40, 10);

                PlaceLimitOrder(TradeType.Sell, Symbol, 10000, Symbol.Bid + 10 * Symbol.PipSize, "Sell + 10", 30, 10);

                PlaceLimitOrder(TradeType.Sell, Symbol, 20000, Symbol.Bid + 20 * Symbol.PipSize, "Sell + 20", 20, 10);

                PlaceLimitOrder(TradeType.Sell, Symbol, 40000, Symbol.Bid + 30 * Symbol.PipSize, "Sell + 30", 10, 10);
            }


            else if (rsi.Result.LastValue < 30)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, 10000, "Buy", 40, 10);

                PlaceLimitOrder(TradeType.Buy, Symbol, 10000, Symbol.Ask + 10 * Symbol.PipSize, "Buy + 10", 30, 10);

                PlaceLimitOrder(TradeType.Buy, Symbol, 20000, Symbol.Ask + 20 * Symbol.PipSize, "Buy + 20", 20, 10);

                PlaceLimitOrder(TradeType.Buy, Symbol, 40000, Symbol.Ask + 30 * Symbol.PipSize, "Buy + 30", 10, 10);
            }
        }

    }
}

Best Regards,

cTrader Team


@Spotware

alpha_austin
02 Aug 2017, 13:53

Is that able to add random trade?

I wanna ask, if not using RSI and if I wanna open a random market order,

and at the smae time I wanna place the mentioned limit orders OF THE SAME DIRECTION OF THE MARKET ORDER,

then once either trade TP (or gross profit > 0), return to the initial position of opening a new random market order.

What coding do I need to add?

Many thanks!

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 Random_with_LimitOrders : Robot
    {
        [Parameter("Initial Volume", DefaultValue = 10000, MinValue = 0)]
        public int InitialVolume { get; set; }

        [Parameter("Stop Loss", DefaultValue = 10)]
        public int StopLoss { get; set; }

        [Parameter("Take Profit", DefaultValue = 10)]
        public int TakeProfit { get; set; }

        private Random random = new Random();

        protected override void OnStart()
        {
            Positions.Closed += OnPositionsClosed;

            ExecuteOrder(InitialVolume, GetRandomTradeType());
        }

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

            ////////////////////here, how to place limit order the same trade type of the above market order 10pips away from it?////////////////////
            ////////like this//////// PlaceLimitOrder(TradeType, Symbol, 10000, Symbol.Bid + 10 * Symbol.PipSize, "Sell + 10", 30, 10);
                                         PlaceLimitOrder(TradeType.Sell, Symbol, 20000, Symbol.Bid + 20 * Symbol.PipSize, "Sell + 20", 20, 10);
                                         PlaceLimitOrder(TradeType.Sell, Symbol, 40000, Symbol.Bid + 30 * Symbol.PipSize, "Sell + 30", 10, 10);
            }
            if (result.Error == ErrorCode.NoMoney)
                Stop();
        }

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

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



            ////////////////////////////////how to write this part////////////////////////////////
            if either position TP, close all trades including all pending orders and return to opening a new random market order
            /////////////////////////////////////////////////////////////////////////////////////////////////



        }

        private TradeType GetRandomTradeType()
        {
            return random.Next(2) == 0 ? TradeType.Buy : TradeType.Sell;
        }
    }
}

 


@alpha_austin

Spotware
02 Aug 2017, 14:33

Hi alpha_austin,

"and at the smae time I wanna place the mentioned limit orders OF THE SAME DIRECTION OF THE MARKET ORDER,"

You can use the tradeType variable when placing your limit orders so that they are on the same direction as your market order. 

then once either trade TP (or gross profit > 0), return to the initial position of opening a new random market order.

You can call your ExecuteOrder() function again within the PositionClosed function

Best Regards,

cTrader Team


@Spotware

alpha_austin
02 Aug 2017, 16:57

Can coding it out?

Dear Support Team,

In fact, I don't know how to code the mentioned concept out, can you help?

Thanks a lot ^^


@alpha_austin

Spotware
02 Aug 2017, 17:24

Hi alpha_austin,

As we mentioned above, we are happy to answer questions about cAlgo and give some guidelines but we cannot engage into developing cBots on demand. If you need professional assistance in developing your cBot you might contact a consultant or post a job in our jobs page.

Best Regards,

cTrader Team


@Spotware