help

Created at 04 Dec 2020, 11:56
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!
SH

shop.n33

Joined 04.12.2020

help
04 Dec 2020, 11:56


How should the code be changed so that the bot either only sells or only buys? thanks

// -------------------------------------------------------------------------------------------------
//
//    This code is a cAlgo API sample.
//
//    This robot is intended to be used as a sample and does not guarantee any particular outcome or
//    profit of any kind. Use it at your own risk
//
//    All changes to this file will be lost on next application start.
//    If you are going to modify this file please make a copy using the "Duplicate" command.
//
//    The "Sample Martingale Robot" creates a random Sell or Buy order. If the Stop loss is hit, a new 
//    order of the same type (Buy / Sell) is created with double the Initial Volume amount. The robot will 
//    continue to double the volume amount for  all orders created until one of them hits the take Profit. 
//    After a Take Profit is hit, a new random Buy or Sell order is created with the Initial Volume amount.
//
// -------------------------------------------------------------------------------------------------

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

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

        [Parameter("Take Profit", DefaultValue = 40)]
        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 = 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(InitialVolume, TradeType.Buy());
            }
            else
            {
                ExecuteOrder((int)position.Volume * 2, position.TradeType);
            }
        }

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

 


@shop.n33
Replies

PanagiotisCharalampous
04 Dec 2020, 12:14

Hi shop.n33,

You should change the following line of code

ExecuteOrder(InitialVolume, GetRandomTradeType());

If you want only buy orders, it should be

ExecuteOrder(InitialVolume, TradeType.Buy);

 If you want only sell orders, it should be

ExecuteOrder(InitialVolume, TradeType.Sell);

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

shop.n33
04 Dec 2020, 12:34

RE: Thank you very much

PanagiotisCharalampous said:

Hi shop.n33,

You should change the following line of code

ExecuteOrder(InitialVolume, GetRandomTradeType());

If you want only buy orders, it should be

ExecuteOrder(InitialVolume, TradeType.Buy);

 If you want only sell orders, it should be

ExecuteOrder(InitialVolume, TradeType.Sell);

Best Regards,

Panagiotis 

Join us on Telegram

 


@shop.n33

shop.n33
04 Dec 2020, 15:48

RE:

 

When moving sideways, the bot opened only in a given direction. But, with an active movement, prices began to open in another. Why?


@shop.n33

PanagiotisCharalampous
07 Dec 2020, 08:38

Hi shop.n33,

Did you change the code? If yes, what is the new source code?

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

shop.n33
08 Dec 2020, 15:06

RE:

PanagiotisCharalampous said:

// -------------------------------------------------------------------------------
//
//    This is a Template used as a guideline to build your own Robot. 
//    Please use the “Feedback” tab to provide us with your suggestions about cAlgo’s API.
//
// -------------------------------------------------------------------------------

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class MartingaleRobot : Robot
    {
        [Parameter("Initial Volume", DefaultValue = 10000, MinValue = 0)]
        public int InitialVolume { get; set; }

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

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

        private Random random = new Random();

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

            ExecuteOrder(InitialVolume, TradeType.Buy);
        }

        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(InitialVolume, GetRandomTradeType());
            }
            else
            {
                ExecuteOrder((int)position.Volume * 2, position.TradeType);
            }
        }

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

 


@shop.n33

PanagiotisCharalampous
08 Dec 2020, 15:10

Hi shop.n33,

Your cBot is still executing random orders. See here

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

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

shop.n33
17 Mar 2021, 20:17

RE: thank. And what needs to be written so that the direction changes with each new order?

PanagiotisCharalampous said:

Hi shop.n33,

Your cBot is still executing random orders. See here

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

Best Regards,

Panagiotis 

Join us on Telegram

 


@shop.n33

amusleh
18 Mar 2021, 10:49

RE: RE: thank. And what needs to be written so that the direction changes with each new order?

shop.n33 said:

PanagiotisCharalampous said:

Hi shop.n33,

Your cBot is still executing random orders. See here

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

Best Regards,

Panagiotis 

Join us on Telegram

 

Hi,

Here is the code that will change the trade type to opposite trade type of previous position:

using cAlgo.API;
using System;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class MartingaleRobot : Robot
    {
        [Parameter("Initial Volume", DefaultValue = 10000, MinValue = 0)]
        public int InitialVolume { get; set; }

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

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

        private Random random = new Random();

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

            ExecuteOrder(InitialVolume, TradeType.Buy);
        }

        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;

            var nextOrderTradeType = position.TradeType == TradeType.Buy ? TradeType.Sell : TradeType.Buy;

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

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

 


@amusleh

shop.n33
18 Mar 2021, 15:18

Thank you so much!!!

amusleh said:

shop.n33 said:

PanagiotisCharalampous said:

Hi shop.n33,

Your cBot is still executing random orders. See here

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

Best Regards,

Panagiotis 

Join us on Telegram

 

Hi,

Here is the code that will change the trade type to opposite trade type of previous position:

using cAlgo.API;
using System;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class MartingaleRobot : Robot
    {
        [Parameter("Initial Volume", DefaultValue = 10000, MinValue = 0)]
        public int InitialVolume { get; set; }

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

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

        private Random random = new Random();

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

            ExecuteOrder(InitialVolume, TradeType.Buy);
        }

        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;

            var nextOrderTradeType = position.TradeType == TradeType.Buy ? TradeType.Sell : TradeType.Buy;

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

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

 

 


@shop.n33

shop.n33
18 Mar 2021, 15:19

RE: Thank you so much!!!

shop.n33 said:

amusleh said:

shop.n33 said:

PanagiotisCharalampous said:

Hi shop.n33,

Your cBot is still executing random orders. See here

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

Best Regards,

Panagiotis 

Join us on Telegram

 

Hi,

Here is the code that will change the trade type to opposite trade type of previous position:

using cAlgo.API;
using System;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class MartingaleRobot : Robot
    {
        [Parameter("Initial Volume", DefaultValue = 10000, MinValue = 0)]
        public int InitialVolume { get; set; }

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

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

        private Random random = new Random();

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

            ExecuteOrder(InitialVolume, TradeType.Buy);
        }

        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;

            var nextOrderTradeType = position.TradeType == TradeType.Buy ? TradeType.Sell : TradeType.Buy;

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

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

 

 

 


@shop.n33

shop.n33
18 Mar 2021, 15:22

RE: RE: Thank you so much!!!

shop.n33 said:

shop.n33 said:

amusleh said:

shop.n33 said:

PanagiotisCharalampous said:

Hi shop.n33,

Your cBot is still executing random orders. See here

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

Best Regards,

Panagiotis 

Join us on Telegram

 

Hi,

Here is the code that will change the trade type to opposite trade type of previous position:

using cAlgo.API;
using System;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class MartingaleRobot : Robot
    {
        [Parameter("Initial Volume", DefaultValue = 10000, MinValue = 0)]
        public int InitialVolume { get; set; }

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

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

        private Random random = new Random();

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

            ExecuteOrder(InitialVolume, TradeType.Buy);
        }

        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;

            var nextOrderTradeType = position.TradeType == TradeType.Buy ? TradeType.Sell : TradeType.Buy;

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

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

 

 

 

 


@shop.n33