How to add buy/sell option to this algorithm

Created at 23 Aug 2019, 17:01
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!
LU

lukaszfx

Joined 19.07.2019

How to add buy/sell option to this algorithm
23 Aug 2019, 17:01


using System;
using cAlgo.API;
using cAlgo.API.Indicators;
  
namespace cAlgo.Robots
{
    [Robot()]
    public class TreeBarsTraDerMaTriX : Robot
    {
        [Parameter("3 BARS TraDerMaTriX n°", DefaultValue = "3")]
        public string InstanceName { get; set; }
  
        [Parameter("Volume", DefaultValue = 10000)]
        public double volume { get; set; }
  
        [Parameter("SL", DefaultValue = 270)]
        public int SL { get; set; }
  
        [Parameter("TP", DefaultValue = 280)]
        public int TP { get; set; }
  
        [Parameter("Use Trail", DefaultValue = true)]
        public bool UseTrail { get; set; }
  
        [Parameter("Trigger (pips)", DefaultValue = 15)]
        public int TrailingStopTrigger { get; set; }
  
        [Parameter("Trailing (pips)", DefaultValue = 6)]
        public int TrailingStopStep { get; set; }
  
  
        protected override void OnStart()
        {
  
        }
  
        protected override void OnTick()
        {
            TRAIL();
        }
        protected override void OnBar()
        {
  
            play();
        }
  
        private void play()
        {
            int bars = MarketSeries.Close.Count - 1;
            int[] b_or_m = new int[3];
            Print("{0},{1},{2},{3},{4}", MarketSeries.High[bars - 1], MarketSeries.High[bars - 2], MarketSeries.High[bars - 3], MarketSeries.Close.Count, MarketSeries.High[1]);
            if (MarketSeries.Close[bars - 1] > MarketSeries.Open[bars - 1])
                b_or_m[0] = 1;
            else
  
                b_or_m[0] = 2;
  
            if (MarketSeries.Close[bars - 2] > MarketSeries.Open[bars - 2])
                b_or_m[1] = 1;
            else
  
                b_or_m[1] = 2;
  
            if (MarketSeries.Close[bars - 3] > MarketSeries.Open[bars - 3])
                b_or_m[2] = 1;
            else
  
                b_or_m[2] = 2;
  
            Print("{0},{1},{2}", b_or_m[0], b_or_m[1], b_or_m[2]);
  
            var cBotPositions = Positions.FindAll(InstanceName);
            if (cBotPositions.Length >= 1)
  
                return;
  
            if ((b_or_m[0] == 1) && (b_or_m[1] == 1) && (b_or_m[2] == 1))
            {
  
                ExecuteMarketOrder(TradeType.Buy, SymbolName, volume, InstanceName, SL, TP);
            }
            else if ((b_or_m[0] == 2) && (b_or_m[1] == 2) && (b_or_m[2] == 2))
            {
  
                ExecuteMarketOrder(TradeType.Sell, SymbolName, volume, InstanceName, SL, TP);
  
            }
  
        }
  
  
        protected override void OnError(Error error)
        {
            Print("Errot={0}", error.Code);
        }
        protected override void OnStop()
        {
            Print("Robot 3BARS by TraDerMaTriX  stopped by user");
  
        }
  
        private void TRAIL()
        {
            if (UseTrail)
            {
  
                var sellPositions = Positions.FindAll(InstanceName, SymbolName, TradeType.Sell);
  
                foreach (Position position in sellPositions)
                {
                    double distance = position.EntryPrice - Symbol.Ask;
  
                    if (distance < TrailingStopTrigger * Symbol.PipSize)
                        continue;
  
                    double newStopLossPrice = Symbol.Ask + TrailingStopStep * Symbol.PipSize;
  
                    if (position.StopLoss == null || newStopLossPrice < position.StopLoss)
                    {
                        ModifyPosition(position, newStopLossPrice, position.TakeProfit);
                    }
                }
  
                var buyPositions = Positions.FindAll(InstanceName, SymbolName, TradeType.Buy);
  
                foreach (Position position in buyPositions)
                {
                    double distance = Symbol.Bid - position.EntryPrice;
  
                    if (distance < TrailingStopTrigger * Symbol.PipSize)
                        continue;
  
                    double newStopLossPrice = Symbol.Bid - TrailingStopStep * Symbol.PipSize;
                    if (position.StopLoss == null || newStopLossPrice > position.StopLoss)
                    {
                        ModifyPosition(position, newStopLossPrice, position.TakeProfit);
                    }
                }
            }
  
        }
    }
}

 


@lukaszfx
Replies

srubtsov
26 Aug 2019, 10:34

I didn't understand exactly your question. But you can use `TradeType` as a parameter:

[Parameter]
public TradeType TradeType { get; set; }

 

Also, you shouldn't implement trailing stop loss, you can use it from cAlgo.API:

In a moment of creating an order with special parameter:

ExecuteMarketOrder(TradeType.Buy, SymbolName, Symbol.VolumeInUnitsMin, _robotLabel, stopLossPips, takeProfitPips, comment: null, hasTrailingStop: true);

Or modify an existing position (those methods won't work with null stop loss):

ModifyPosition(position, stopLossPips, takeProfitPips, true);
//or
position.ModifyTrailingStop(true);

 


@srubtsov