Random and Limit to be the same.

Created at 26 Aug 2017, 04:28
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

Random and Limit to be the same.
26 Aug 2017, 04:28


Hi everyone, I am fresh in coding. And I would like to know to PlaceOrder the same direction of a Random opened trade. Thanks anyone who can help!

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 MTG_Original_Seed : 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());
            PlaceLimitOrder(TradeType TradeType, Symbol, 10000, null, LMTOrd, 30, 10);
           
        }

        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());
            }
        return
        }

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

 


@alpha_austin
Replies

Spotware
28 Aug 2017, 09:52

Dear alpha_austin,

In order to place an order in the same direction as a current position, you need to use the position's direction. See an example below.

            var position = Positions[0];
            ExecuteMarketOrder(position.TradeType, Symbol, 1000);

Let us know if the above answers your question.

Best Regards,

cTrader Team


@Spotware