QuickStart - Break Even

15 Jan 2014, 16:21QuickStart - Break Even#1
Spotwareposts: 2938since: 23 Sep 2013
using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot]
    public class SampleBreakEven:Robot
    {
        private TradeType _tradeType;

        [Parameter("Trigger (pips)", DefaultValue = 20)]
        public int Trigger { get; set; }

        [Parameter("Buy", DefaultValue = true)]
        public bool Buy { get; set; }

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

        [Parameter(DefaultValue = 10)]
        public double TakeProfitPips { get; set; }

        [Parameter(DefaultValue = 10)]
        public double StopLossPips { get; set; }

        [Parameter(DefaultValue = "Break Even cBot")]
        public string MyLabel { get; set; }

        protected override void OnStart()
        {            
            _tradeType = Buy ? TradeType.Buy : TradeType.Sell;
            var result = ExecuteMarketOrder(_tradeType, Symbol, Volume,
                MyLabel, StopLossPips, TakeProfitPips);
            if(!result.IsSuccessful)
            {
                Print("Stopping cBot due to failure executing market order");
                Stop();
            }
        }

        protected override void OnTick()
        {
            var position = Positions.Find(MyLabel, Symbol, _tradeType);
            if(position == null)
            {
                Print("Position not found. Stopping cBot");
                Stop();
                return;
            }
            
            var entryPrice = position.EntryPrice;
            var distance = _tradeType == TradeType.Buy
                               ? Symbol.Bid - entryPrice
                               : entryPrice - Symbol.Ask;

            if (distance >= Trigger*Symbol.PipSize)
            {
                ModifyPosition(position, entryPrice, position.TakeProfit);
                Print("Stop Loss to Break Even set for position {0}", position.Id);
                Stop();
            }
        }

    }
}

 


TRADERS FIRST™ Vote for your favorite features: http://vote.spotware.com/
17 Jan 2014, 08:42#2
hgorskiposts: 4since: 15 Jan 2014

Thanks,

 

And if I wanted to close, let's say 50k of 100k position in the same time to moving stops to BE? How should I modify this example code?

 

09 Jul 2015, 00:55RE:#3
aimerdouxposts: 43since: 07 Jun 2015

hgorski said:

Thanks,

 

And if I wanted to close, let's say 50k of 100k position in the same time to moving stops to BE? How should I modify this example code?

 

you have to modify the method Close to receive the amount of volume you want to close when the breakeven is hitted then use a bool to notify the method that the breakeven is activated 

 

private void Close1(TradeType tradeType, long volume)
        {
            foreach (var position in Positions.FindAll("ace1", Symbol, tradeType))
                ClosePosition(position, long Volume);
        }

22 May 2017, 21:51#4
krickaposts: 407since: 13 Mar 2013

Maybe the free download and use of Market Order 2.0 or 3.0 will give a hint what can be accomplished.

Totally free to download and use at RMMRobot.com


http://rmmrobot.com