Topics
08 Oct 2012, 00:36
 2831
 2
07 Oct 2012, 18:06
 4051
 5
Replies

b0risl33t
09 Oct 2012, 15:21

Ohh Yehhh :P

 

Thanks


@b0risl33t

b0risl33t
08 Oct 2012, 19:18

Done It

 

Maybe not the best but it works :d

[Parameter("Max Losses", DefaultValue = 4, MinValue = 1, MaxValue = 10)]
        public int Losses {get; set; }

private int _consecutiveLosses;

protected override void OnPositionClosed(Position closedPosition)
        {
          {
            if (closedPosition.GrossProfit <= closedPosition.Commissions)
            {
              _consecutiveLosses++;
            }
            else 
            {
               _consecutiveLosses = 0;
            }
          }
            if (closedPosition.GrossProfit > 0)
            {
                ExecuteOrder(InitialVolume, GetTradeCommand());
            }
            else if (_consecutiveLosses < Losses)
            {
                ExecuteOrder((int)_position.Volume * 2, GetTradeCommand());
            }
            else
            {
                OnStart();
            }
            
        }




@b0risl33t

b0risl33t
08 Oct 2012, 18:08

Thank you for the reply

 

I am still learning c++ :P i never even looked at it before 4 days ago. Im still a real newbie but i am willing to learn so thanks for pointing me in the right direction.

What i am trying to achive is for the robot to restart its multiplyer if it has reached say 5 losses, but i want to be able to modify how many losses required with a parameter.

Example

 

InitialVolume

  •  1 lot - Loss
  • 2 lots - Loss
  • 4 lots - Loss
  • 8 Lots - Loss

4 Lots reached now the robot should reset and restart with InitialVolume

 

Hope this makes sense.

In mean time i will try and figure it out.

 

Thank You

 


@b0risl33t

b0risl33t
08 Oct 2012, 17:09

I think this works

 

using System;
using cAlgo.API;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot]
    public class SampleMartingaleRobot : 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();
        private Position position;

        protected override void OnStart()
        {
            ExecuteOrder(InitialVolume, TradeType.Sell);
            ExecuteOrder(InitialVolume, TradeType.Buy);
        }

        private void ExecuteOrder(int volume, TradeType tradeType)
        {
            Trade.CreateMarketOrder(tradeType, Symbol, volume);
        }

        protected override void OnPositionOpened(Position openedPosition)
        {
            position = openedPosition;
            Trade.ModifyPosition(openedPosition, GetAbsoluteStopLoss(openedPosition, StopLoss), GetAbsoluteTakeProfit(openedPosition, TakeProfit));
        }

        protected override void OnPositionClosed(Position closedPosition)
        {
            if (closedPosition.GrossProfit > 0 && closedPosition.TradeType == TradeType.Sell)
            {
                ExecuteOrder(InitialVolume, TradeType.Sell);
            }
            else if (closedPosition.GrossProfit < 0 && closedPosition.TradeType == TradeType.Sell)
            {
                ExecuteOrder((int) position.Volume * 2, position.TradeType);
            }
            else if (closedPosition.GrossProfit > 0 && closedPosition.TradeType == TradeType.Buy)
            {
                ExecuteOrder(InitialVolume, TradeType.Buy);
            }
            else 
            {   
                ExecuteOrder((int) position.Volume * 2, position.TradeType);
            }
        }

        protected override void OnError(Error error)
        {
            if (error.Code == ErrorCode.BadVolume)
                Stop();
        }

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

        private double GetAbsoluteStopLoss(Position position, int stopLossInPips)
        {
            return position.TradeType == TradeType.Buy
                ? position.EntryPrice - Symbol.PipSize * stopLossInPips
                : position.EntryPrice + Symbol.PipSize * stopLossInPips;
        }

        private double GetAbsoluteTakeProfit(Position position, int takeProfitInPips)
        {
            return position.TradeType == TradeType.Buy
                ? position.EntryPrice + Symbol.PipSize * takeProfitInPips
                : position.EntryPrice - Symbol.PipSize * takeProfitInPips;
        }
    }
}




@b0risl33t

b0risl33t
08 Oct 2012, 16:39

Ok i made this but not sure what i done wrong :( i get a member modefier protected must procede member name and type error. here is code

 

using cAlgo.API;
using cAlgo.API.Indicators;

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

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

        [Parameter("Take Profit", DefaultValue = 40, MinValue = 0)]
        public int TakeProfit { get; set; }
        
        [Parameter("Max Losses", DefaultValue = 4, MinValue = 1, MaxValue = 10)]
        public int consecutiveLosses {get; set; }


        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Parameter("Period", DefaultValue = 14, MinValue = 1)]
        public int Period { get; set; }

        [Parameter("Moving Average Type", DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType MovingAverageType { get; set; }

        private Position _position;
        private MovingAverage _movingAverage;
        private _consecutiveLosses
        

        protected override void OnStart()
        {
            _movingAverage = Indicators.MovingAverage(Source, Period, MovingAverageType);            
            ExecuteOrder(InitialVolume, GetTradeCommand());
        }
        
        private void ExecuteOrder(int volume, TradeType tradeType)
        {
            Trade.CreateMarketOrder(tradeType, Symbol, volume);
        }

        protected override void OnPositionOpened(Position openedPosition)
        {
            _position = openedPosition;
            Trade.ModifyPosition(openedPosition, GetAbsoluteStopLoss(openedPosition, StopLoss), GetAbsoluteTakeProfit(openedPosition, TakeProfit));
        }

        private double GetAbsoluteStopLoss(Position pos, int stopLossInPips)
        {
            return pos.TradeType == TradeType.Buy
                ? pos.EntryPrice - Symbol.PipSize * stopLossInPips
                : pos.EntryPrice + Symbol.PipSize * stopLossInPips;
        }

        private double GetAbsoluteTakeProfit(Position pos, int takeProfitInPips)
        {
            return pos.TradeType == TradeType.Buy
                ? pos.EntryPrice + Symbol.PipSize * takeProfitInPips
                : pos.EntryPrice - Symbol.PipSize * takeProfitInPips;
        }

        protected override void OnPositionClosed(Position closedPosition)
        {
            if (closedPosition.GrossProfit < 0)
            {
                
                _consecutiveLosses++;
            }
            else if (closedPosition.GrossProfit > 0)
            {
                
               _consecutiveLosses = 0;
            }
            
            
            else if (closedPosition.GrossProfit > 0)
            {
                ExecuteOrder(InitialVolume, GetTradeCommand());
            }
            else if (_consecutiveLosses < consecutiveLosses)
            {
                ExecuteOrder((int)_position.Volume * 2, GetTradeCommand());
            }
            
            else
            {
            Stop()
            }
            
        }

        private TradeType GetTradeCommand()
        {
            var lastIndex = MarketSeries.Close.Count - 1;
            double close = MarketSeries.Close[lastIndex - 1];
            double lastClose = MarketSeries.Close[lastIndex - 2];
            if (_movingAverage.Result.IsRising() && close > lastClose)
                return TradeType.Buy;
            if (_movingAverage.Result.IsFalling() && close < lastClose) 
                return TradeType.Sell;
            return _position.TradeType;
        }

        protected override void OnError(Error error)
        {
            if (error.Code == ErrorCode.BadVolume)
                Stop();
        }
    }
}



 

Any ideas? 

Thanks


@b0risl33t

b0risl33t
08 Oct 2012, 15:40

Thanks for quick reply.

I will try and make that work, i will post my results here

 

 

thanks


@b0risl33t

b0risl33t
08 Oct 2012, 14:47

bump

any ideas yet?


@b0risl33t

b0risl33t
08 Oct 2012, 01:06

solved

solved


@b0risl33t

b0risl33t
08 Oct 2012, 01:05

solved

solved 


@b0risl33t

b0risl33t
07 Oct 2012, 23:36

Cant figure this out what am i doin wrong

Cant figure out why i get else as an error here. And is closedPosition.TradeType will tell me what was the trade type on last position?

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

 


@b0risl33t

b0risl33t
07 Oct 2012, 21:07

Little Change

I have tryed to modify the code to execute orders Depending on the Last candle close value but still cant get it to work :( please help. Im Still a real noob at this but desperate to learn all this :D

 

     [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 Position position;
        
        protected override void OnStart()
        {
            ExecuteOrder(InitialVolume, TradeCommand());
        }
        
        private void ExecuteOrder(int volume, TradeType tradeType)
        {
            Trade.CreateMarketOrder(tradeType, Symbol, volume);
        }
        
        protected override void OnPositionOpened(Position openedPosition)
        {
            position = openedPosition;
            Trade.ModifyPosition(openedPosition, GetAbsoluteStopLoss(openedPosition, StopLoss), GetAbsoluteTakeProfit(openedPosition, TakeProfit));
        }
        
        protected override void OnPositionClosed(Position closedPosition)
        {
            if (closedPosition.GrossProfit > 0)
            {
                ExecuteOrder(InitialVolume, TradeCommand());
            }
            else
            {
                ExecuteOrder((int) position.Volume * 2, position.TradeType);
            }
        }
        
        protected override void OnError(Error error)
        {
            if (error.Code == ErrorCode.BadVolume)
                Stop();
        }

        private TradeType TradeCommand()
        {
          if (Trade.IsExecuting)
                return;

            var lastIndex = MarketSeries.Close.Count - 1;
            double close = MarketSeries.Close[lastIndex - 1];
            double lastClose = MarketSeries.Close[lastIndex - 2];
            
         if (close > lastClose)
            {
                
                TradeType.Buy;
            }
            else if (close < lastClose)
            {
                
                TradeType.Sell;                
            }
        }

        private double GetAbsoluteStopLoss(Position position, int stopLossInPips)
        {
            return position.TradeType == TradeType.Buy
                ? position.EntryPrice - Symbol.PipSize * stopLossInPips
                : position.EntryPrice + Symbol.PipSize * stopLossInPips;
        }

        private double GetAbsoluteTakeProfit(Position position, int takeProfitInPips)
        {
            return position.TradeType == TradeType.Buy
                ? position.EntryPrice + Symbol.PipSize * takeProfitInPips
                : position.EntryPrice - Symbol.PipSize * takeProfitInPips;
        }
}
}




@b0risl33t