BREAK-EVEN

Created at 10 Aug 2013, 13:24
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!
TR

tradermatrix

Joined 24.07.2012

BREAK-EVEN
10 Aug 2013, 13:24


hello,
can you help me to modify my robot?
I would replace Trailling stop by brekeven.

[Parameter (DefaultValue = 40)]
public double Trigger {get; set;}

[Parameter (DefaultValue = 20)]
public double breakeven {get; set;}

cordially

////////////////////////////////////////////////////////////////////////////////////////////////////////

//#reference: ..\Indicators\ZigZag.algo
using System;
using cAlgo.API;
using cAlgo.Indicators;
using cAlgo.API.Requests;
namespace cAlgo.Robots

        {
        
        [Robot]
        public class ZiGZaG : Robot
        
        {
       
        [Parameter(DefaultValue = 12)]
        public int ZzDepth { get; set;}
 
        [Parameter(DefaultValue = 5)]
        public int ZzDeviation { get; set;}
 
        [Parameter(DefaultValue = 3)]
        public int ZzBackStep { get; set;}
 
        [Parameter(DefaultValue = 100000)]
        public int Volume { get; set;}
 
        [Parameter(DefaultValue = 40)]
        public int StopLoss { get; set;}
        
        [Parameter(DefaultValue = 0)]   
        public int TakeProfit { get; set;} 

        [Parameter(DefaultValue = 0)]
        public double Trigger { get; set;}
 
        [Parameter(DefaultValue = 0)]
        public double TrailingStop { get; set;}
 
//////////////////////////////////////////////////////////////        
 
        private Position _position;
        private ZigZag _zigZag;
        private double prevValue;
        private MovingAverageType _matype;
         
/////////////////////////////////////////////////////////////

        protected bool UseTrailingStop
        
        {
        
        get { return Trigger > 0.0 && TrailingStop > 0.0; }
        
        }
        
        protected override void OnTick()
        
        {
             
        if (Trade.IsExecuting || _position == null) return;
 
        if (UseTrailingStop)
               Trail();
            
        }
        
        protected override void OnStart() 
        
        {
        
        _matype = MovingAverageType.Simple;
        _zigZag = Indicators.GetIndicator<ZigZag>(ZzDepth, ZzDeviation, ZzBackStep);
        
        }
        
        protected override void OnBar()
        
        {
        
        if (Trade.IsExecuting)
                return;
 
        bool isLongPositionOpen = _position != null && _position.TradeType == TradeType.Buy;
        bool isShortPositionOpen = _position != null && _position.TradeType == TradeType.Sell;
 
                         double lastValue = _zigZag.Result.LastValue;
 
        if (!double.IsNaN(lastValue))
        
        {
 
        if (lastValue < prevValue && !isLongPositionOpen)
        
        {
                         ClosePosition();
                              Buy();
        }
               
        else if (lastValue > prevValue && prevValue > 0.0 && !isShortPositionOpen)
        
        {
                          ClosePosition();
                              Sell();
        }
        
        prevValue = lastValue; 
        
        }         
        }
  
        private void ClosePosition()
        
        {
        
        if (_position == null) return;
 
        Trade.Close(_position);
        _position = null;
        
        }
 
        private void Buy()
        
        {
        
        Request request = new MarketOrderRequest(TradeType.Buy, Volume)
        
        {
                                 Label = "ZIGZAG",
                                      
        StopLossPips = StopLoss > 0 ? (int?)StopLoss : null,
        TakeProfitPips = TakeProfit > 0 ? (int?)TakeProfit : null
        
        };
        
                                Trade.Send(request);
                                
        }
 
        private void Sell()
        
        {
        
        Request request = new MarketOrderRequest(TradeType.Sell, Volume)
        
        {
                                Label = "ZIGZAG",
               
        StopLossPips = StopLoss > 0 ? (int?)StopLoss : null,
        TakeProfitPips = TakeProfit > 0 ? (int?)TakeProfit : null
        
        };
        
                              Trade.Send(request);
        }
 
        protected override void OnPositionOpened(Position openedPosition)
        
        {
        
                           _position = openedPosition;
            
        }
 
        private void Trail()
        
        {

        foreach (var position in Account.Positions)

        {

        if (position.TradeType == TradeType.Sell)

        {

        double distance = position.EntryPrice - Symbol.Ask;

        if (distance >= Trigger * Symbol.PipSize)

        {

        double newStopLossPrice = Symbol.Ask + TrailingStop * Symbol.PipSize;

        if (position.StopLoss == null || newStopLossPrice < position.StopLoss)

        {

        Trade.ModifyPosition(position, newStopLossPrice, position.TakeProfit);

        }
        }
        }

        else

        {

        double distance = Symbol.Bid - position.EntryPrice;

        if (distance >= Trigger * Symbol.PipSize)

        {

        double newStopLossPrice = Symbol.Bid - TrailingStop * Symbol.PipSize;

        if (position.StopLoss == null || newStopLossPrice > position.StopLoss)

        {

        Trade.ModifyPosition(position, newStopLossPrice, position.TakeProfit);

        }
          }
            }
              }
            }
          }
        }


@tradermatrix
Replies

tradermatrix
13 Aug 2013, 14:58

hello
I have changed my code. (trail)
it works well.
except for breakeven Buy.
it always changes its position to "0". (entry price)
for example:
stoploss: 40
breakeven 30:
trigger: 40

when the currency reaches trigger: 40
the stop loss moves to the price of entry ...?
it does not position 30 above the entry price ...

But it works with "shell".

( you can see the error more easily by removing "ClosePosition ();
of "protected override void OnBar ()" )

thank you for your help

 

//#reference: ..\Indicators\ZigZag.algo
using System;
using cAlgo.API;
using cAlgo.Indicators;
using cAlgo.API.Requests;
namespace cAlgo.Robots

        {
        
        [Robot]
        public class ZiGZaG : Robot
        
        {
       
        [Parameter(DefaultValue = 12)]
        public int ZzDepth { get; set;}
 
        [Parameter(DefaultValue = 5)]
        public int ZzDeviation { get; set;}
 
        [Parameter(DefaultValue = 3)]
        public int ZzBackStep { get; set;}
 
        [Parameter(DefaultValue = 100000)]
        public int Volume { get; set;}
 
        [Parameter(DefaultValue = 40)]
        public int StopLoss { get; set;}
        
        [Parameter(DefaultValue = 80)]   
        public int TakeProfit { get; set;} 

        [Parameter(DefaultValue = 40)]
        public double Trigger { get; set;}
 
        [Parameter(DefaultValue = 0)]
        public double TrailingStop { get; set;}
        
        [Parameter("Breakeven", DefaultValue = 30)]
        public double Breakeven { get; set;}
       
           
           
       
 
//////////////////////////////////////////////////////////////        
 
        private Position _position;
        private ZigZag _zigZag;
        private double prevValue;
        private MovingAverageType _matype;
         
/////////////////////////////////////////////////////////////

       
        protected override void OnTick()
        
        {
             
        Trail();
        
        }
               
        protected override void OnStart() 
        
        {
        
        _matype = MovingAverageType.Simple;
        _zigZag = Indicators.GetIndicator<ZigZag>(ZzDepth, ZzDeviation, ZzBackStep);
        
        }
        
        protected override void OnBar()
        
        {
        
        if (Trade.IsExecuting)
                return;
 
        bool isLongPositionOpen = _position != null && _position.TradeType == TradeType.Buy;
        bool isShortPositionOpen = _position != null && _position.TradeType == TradeType.Sell;
 
                         double lastValue = _zigZag.Result.LastValue;
 
        if (!double.IsNaN(lastValue))
        
        {
 
        if (lastValue < prevValue && !isLongPositionOpen)
        
        {
                         ClosePosition();

                              Buy();
                             
        }
               
        else if (lastValue > prevValue && prevValue > 0.0 && !isShortPositionOpen)
        
        {
                         ClosePosition();

                              Sell();
        }
        
        prevValue = lastValue; 
        
        }         
        }
  
        private void ClosePosition()
        
        {
        
        if (_position == null) return;
        
        Trade.Close(_position);
      
        _position = null;
        
        }
 
        private void Buy()
        
        {
        
        Request request = new MarketOrderRequest(TradeType.Buy, Volume)
        
        { 
                                 Label = "ZIGZAG",
                                 
        StopLossPips = StopLoss > 0 ? (int?)StopLoss : null,
        TakeProfitPips = TakeProfit > 0 ? (int?)TakeProfit : null
        
        };
        
                                Trade.Send(request);
                                
        }
 
        private void Sell()
        
        {
        
        Request request = new MarketOrderRequest(TradeType.Sell, Volume)
        
        {
                                 Label = "ZIGZAG",
                                
        StopLossPips = StopLoss > 0 ? (int?)StopLoss : null,
        TakeProfitPips = TakeProfit > 0 ? (int?)TakeProfit : null
       
        };
        
                               Trade.Send(request);
        }
 
       
 
        private void Trail()
        
        {
          
        foreach (Position _position in base.Account.Positions)
        
        {
        {
        
        if (_position.TradeType == TradeType.Sell)
        
        {
        
        if (_position.Pips >= (double)this.Trigger && this.Breakeven > 0)
        
        {
        
        double N2 = _position.EntryPrice - (double)this.Breakeven * base.Symbol.PipSize;
        double N3 = N2;
        double? stopLoss = _position.StopLoss;
        
        if (N3 < stopLoss.GetValueOrDefault() && stopLoss.HasValue)
        
        {
        
        base.Trade.ModifyPosition(_position, new double?(N3), _position.TakeProfit);
        
        }
        }
        
        if (_position.Pips >= (double)this.Trigger && this.Breakeven < 1)
        
        {
        
        double N2 = base.Symbol.Ask + (double)this.TrailingStop * base.Symbol.PipSize;
        double N3 = N2;
        double? stopLoss = _position.StopLoss;
        
        if (N3 < stopLoss.GetValueOrDefault() && stopLoss.HasValue)
        
        {
        
        base.Trade.ModifyPosition(_position, new double?(N3),_position.TakeProfit);
        
        }
           }
        }
        
        else
        
        {
        
        if (_position.Pips >= (double)this.Trigger && this.Breakeven > 0)
        
        {
        
        double entryPrice = _position.EntryPrice;
        double? stopLoss = _position.StopLoss;
        double N2 = entryPrice;
        
        if (stopLoss.GetValueOrDefault() < N2 && stopLoss.HasValue)
        
        {
        
        base.Trade.ModifyPosition(_position, new double?(entryPrice), _position.TakeProfit);
        
        }
        }
        
        if (_position.Pips >= (double)this.Trigger && this.Breakeven < 1)
        
                        {
        double N2 = base.Symbol.Bid - (double)this.TrailingStop * base.Symbol.PipSize;
        double? stopLoss = _position.StopLoss;
        double N3 = N2;
        
        if (stopLoss.GetValueOrDefault() < N3 && stopLoss.HasValue)
        
        {
        
        base.Trade.ModifyPosition(_position, new double?(N2), _position.TakeProfit);
        
        }
          }
            }
              }
                }
                  }
                  
        protected override void OnPositionOpened(Position openedPosition)
        
        {
        
        _position = openedPosition;
           
        }
          }
        }


@tradermatrix

cAlgo_Fanatic
13 Aug 2013, 17:31

Can you please clarify what the intended logic is:

when the currency reaches trigger: 40
the stop loss moves to the price of entry ...?
it does not position 30 above the entry price ...

You are setting a new stop loss if the trade type is sell:

Trade.ModifyPosition(position, N3, position.TakeProfit);

But if the type is Buy you are setting stop loss to entry price:

 Trade.ModifyPosition(position, entryPrice, position.TakeProfit);


 


@cAlgo_Fanatic

tradermatrix
13 Aug 2013, 17:46

EUREKA.....!!!

j ai trouvé la solution.

 


@tradermatrix