cBot Robot_Forex - Problem with the label

Created at 10 Feb 2014, 00: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!
Forex19's avatar

Forex19

Joined 13.06.2013

cBot Robot_Forex - Problem with the label
10 Feb 2014, 00:24


Hi at all,
with reference to this cBot:

/algos/cbots/show/225

I tried to add a Label. So I added this code:

 private string RF_Label;

in protected override void OnStart() 

RF_Label = "RF " + Symbol.Code;

in private void SendFirstOrder(int OrderVolume)

replace

Trade.CreateBuyMarketOrder(Symbol, OrderVolume);
Trade.CreateSellMarketOrder(Symbol, OrderVolume);

with

 ExecuteMarketOrder(TradeType.Buy, Symbol, OrderVolume, RF_Label);
 ExecuteMarketOrder(TradeType.Sell, Symbol, OrderVolume, RF_Label);

The result is this:

As you can see only the FirstOrder has the label.

I also tried to modify the code in private void ControlSeries() , where are checked the positions next the first order.
But the result is this:

What is wrong? How can I fix?

 

 


@Forex19
Replies

Spotware
10 Feb 2014, 11:48

There are 4 places with Trade.Create...Order. Did you replace all? Please attach code of your cBot.


@Spotware

Forex19
10 Feb 2014, 13:47

RE:

Spotware said:

There are 4 places with Trade.Create...Order. Did you replace all? Please attach code of your cBot.

 

Here is the modified code 
The barred lines are the ones that I have changed

using System;
using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot("Robot Forex")]
    public class RobotForex : Robot
    {
        [Parameter(DefaultValue = 10000, MinValue = 10000)]
        public int FirstLot { get; set; }

        [Parameter("Take_Profit", DefaultValue = 180, MinValue = 10)]
        public int TakeProfit { get; set; }
        
        [Parameter("Tral_Start", DefaultValue = 50)]
        public int Tral_Start { get; set; }
        
        [Parameter("Tral_Stop", DefaultValue = 50)]
        public int Tral_Stop { get; set; }

        [Parameter(DefaultValue = 300)]
        public int PipStep { get; set; }

        [Parameter(DefaultValue = 5, MinValue = 2)]
        public int MaxOrders { get; set; }
        
        private Position position;
        private bool RobotStopped;
        private int LotStep=10000;
        
        private string RF_Label;

        protected override void OnStart()
        {
           RF_Label = "RF " + Symbol.Code; 
        }

        protected override void OnTick()
        {
        double Bid=Symbol.Bid;
        double Ask=Symbol.Ask;
        double Point=Symbol.PointSize;      
        
            if(Trade.IsExecuting) return;
            if(Account.Positions.Count > 0 && RobotStopped) return;
            else RobotStopped = false;

            if(Account.Positions.Count == 0) 
                SendFirstOrder(FirstLot);
            else
                ControlSeries();
                
           foreach (var position in Account.Positions)
            {
                if(position.SymbolCode == Symbol.Code)
                {
                
                    if(position.TradeType == TradeType.Buy)    
                    {
                    if (Bid-GetAveragePrice(TradeType.Buy)>=Tral_Start*Point)
                    if (Bid-Tral_Stop*Point>=position.StopLoss)
                    Trade.ModifyPosition(position, Bid-Tral_Stop*Point, position.TakeProfit);
                    }
                        
                    if(position.TradeType == TradeType.Sell)    
                    {
                    if (GetAveragePrice(TradeType.Sell)-Ask>=Tral_Start*Point)
                    if (Ask+Tral_Stop*Point<=position.StopLoss || position.StopLoss==0)
                    Trade.ModifyPosition(position, Ask+Tral_Stop*Point, position.TakeProfit);
                    }
                }
             }                
        }

        protected override void OnError(Error CodeOfError)
        {
            if(CodeOfError.Code == ErrorCode.NoMoney)
            {
                RobotStopped = true;
                Print("ERROR!!! No money for order open, robot is stopped!");
            }
            else if(CodeOfError.Code == ErrorCode.BadVolume)
            {
                RobotStopped = true;
                Print("ERROR!!! Bad volume for order open, robot is stopped!");
            }
        }
        
        private void SendFirstOrder(int OrderVolume)
        {
            int Signal = GetStdIlanSignal();
            if(!(Signal < 0))
                switch(Signal)
                {
                    case 0:
                        ExecuteMarketOrder(TradeType.Buy, Symbol, OrderVolume, RF_Label);
                        //Trade.CreateBuyMarketOrder(Symbol, OrderVolume);
                    break;
                    case 1:
                        ExecuteMarketOrder(TradeType.Sell, Symbol, OrderVolume, RF_Label);
                        //Trade.CreateSellMarketOrder(Symbol, OrderVolume);
                    break;
                }
        }
        
        protected override void OnPositionOpened(Position openedPosition)
        {
            double? StopLossPrice = null;
            double? TakeProfitPrice = null;
            
            if(Account.Positions.Count == 1)
            {
                position = openedPosition;
                if( position.TradeType == TradeType.Buy)
                    TakeProfitPrice = position.EntryPrice + TakeProfit * Symbol.PointSize;
                if( position.TradeType == TradeType.Sell)
                    TakeProfitPrice = position.EntryPrice - TakeProfit * Symbol.PointSize;
            }
            else
                switch(GetPositionsSide())
                {
                    case 0:
                        TakeProfitPrice = GetAveragePrice(TradeType.Buy) + TakeProfit * Symbol.PointSize;
                    break;
                    case 1:
                        TakeProfitPrice = GetAveragePrice(TradeType.Sell) - TakeProfit * Symbol.PointSize;
                    break;
                }

            for(int i = 0; i < Account.Positions.Count; i++)
            {
                position = Account.Positions[i];
                if(StopLossPrice != null || TakeProfitPrice != null)
                    Trade.ModifyPosition(position, position.StopLoss, TakeProfitPrice);
            }
        }

        private double GetAveragePrice(TradeType TypeOfTrade)
        {
            double Result = Symbol.Bid;
            double AveragePrice = 0;
              long Count = 0;

            for(int i = 0; i < Account.Positions.Count; i++)
            {
                position = Account.Positions[i];
                if(position.TradeType == TypeOfTrade)
                   {
                       AveragePrice += position.EntryPrice * position.Volume;
                       Count += position.Volume;
                   }
               }
              if(AveragePrice > 0 && Count > 0)
                Result = AveragePrice / Count;
            return Result;
        }

        private int GetPositionsSide()
        {
            int Result = -1;
            int i, BuySide = 0, SellSide = 0;
            
            for(i = 0; i < Account.Positions.Count; i++)
            {
                if(Account.Positions[i].TradeType == TradeType.Buy) BuySide++;
                if(Account.Positions[i].TradeType == TradeType.Sell) SellSide++;
            }
            if(BuySide == Account.Positions.Count) Result = 0;
            if(SellSide == Account.Positions.Count) Result = 1;
            return Result;
        }
        
        private void ControlSeries()
        {
            int _pipstep, NewVolume, Rem;
            int BarCount = 25;
            int Del = MaxOrders - 1;
            
            if(PipStep == 0)
                _pipstep = GetDynamicPipstep(BarCount, Del);
            else
                _pipstep = PipStep;
            
            if(Account.Positions.Count < MaxOrders)
                switch(GetPositionsSide())
                {
                      case 0:
                        if(Symbol.Ask < FindLastPrice(TradeType.Buy) - _pipstep * Symbol.PointSize)
                        {
                            NewVolume = Math.DivRem((int)(FirstLot + FirstLot*Account.Positions.Count), LotStep, out Rem) * LotStep;
                            if(!(NewVolume < LotStep))
                                ExecuteMarketOrder(TradeType.Buy, Symbol, NewVolume, RF_Label);
                                //Trade.CreateBuyMarketOrder(Symbol, NewVolume);
                        }
                       break;
                       case 1:
                        if(Symbol.Bid > FindLastPrice(TradeType.Sell) + _pipstep * Symbol.PointSize)
                        {
                            NewVolume = Math.DivRem((int)(FirstLot + FirstLot*Account.Positions.Count), LotStep, out Rem) * LotStep;
                            if(!(NewVolume < LotStep))
                                ExecuteMarketOrder(TradeType.Sell, Symbol, NewVolume, RF_Label); 
                                //Trade.CreateSellMarketOrder(Symbol, NewVolume);
                        }
                       break;
                }
        }
        
        private int GetDynamicPipstep(int CountOfBars, int Del)
        {
            int Result;
            double HighestPrice = 0, LowestPrice = 0;
            int StartBar = MarketSeries.Close.Count - 2 - CountOfBars;
            int EndBar = MarketSeries.Close.Count - 2;
            
            for(int i = StartBar; i < EndBar; i++)
            {
                if(HighestPrice == 0 && LowestPrice == 0)
                {                
                    HighestPrice = MarketSeries.High[i];
                    LowestPrice = MarketSeries.Low[i];
                    continue;
                }
                if(MarketSeries.High[i] > HighestPrice) HighestPrice = MarketSeries.High[i];
                if(MarketSeries.Low[i] < LowestPrice) LowestPrice = MarketSeries.Low[i];
            }
            Result = (int)((HighestPrice - LowestPrice) / Symbol.PointSize / Del);
            return Result;
        }
        
        private double FindLastPrice(TradeType TypeOfTrade)
        {
            double LastPrice = 0;

            for(int i = 0; i < Account.Positions.Count; i++) 
            {
                position = Account.Positions[i];
                if(TypeOfTrade == TradeType.Buy)
                    if(position.TradeType == TypeOfTrade) 
                      {
                           if(LastPrice == 0)
                           {
                               LastPrice = position.EntryPrice;
                               continue;
                           }
                        if(position.EntryPrice < LastPrice)
                              LastPrice = position.EntryPrice;
                      }
                if(TypeOfTrade == TradeType.Sell)
                    if(position.TradeType == TypeOfTrade) 
                      {
                           if(LastPrice == 0)
                           {
                               LastPrice = position.EntryPrice;
                               continue;
                           }
                        if(position.EntryPrice > LastPrice)
                              LastPrice = position.EntryPrice;
                      }
              }
              return LastPrice;
        }
        
        private int GetStdIlanSignal()
        {
            int Result = -1;
            int LastBarIndex = MarketSeries.Close.Count - 2;
            int PrevBarIndex = LastBarIndex - 1;
            
            if(MarketSeries.Close[LastBarIndex] > MarketSeries.Open[LastBarIndex])
                if(MarketSeries.Close[PrevBarIndex] > MarketSeries.Open[PrevBarIndex])
                    Result = 0;
            if(MarketSeries.Close[LastBarIndex] < MarketSeries.Open[LastBarIndex])
                if(MarketSeries.Close[PrevBarIndex] < MarketSeries.Open[PrevBarIndex])
                    Result = 1;
            return Result;
        }
    }
}

 


@Forex19

Spotware
10 Feb 2014, 15:19

The problem is that you use obsolete OnPositionOpened method with new ExecuteMarketOrder method. OnPositionOpened method works only if you create position using old trading API.

Instead of using OnPositionOpened method you need to subscribe to Positions.Opened event. But please keep in mind that it will be raised for all positions: 

/forum/whats-new/1937


@Spotware

Forex19
12 Feb 2014, 23:46

RE:

Spotware said:

The problem is that you use obsolete OnPositionOpened method with new ExecuteMarketOrder method. OnPositionOpened method works only if you create position using old trading API.

Instead of using OnPositionOpened method you need to subscribe to Positions.Opened event. But please keep in mind that it will be raised for all positions: 

/forum/whats-new/1937

 

I modified the code and used PositionOnOpened, and it seems to work properly. 

Thanks.


@Forex19

ronald1711
05 Mar 2014, 22:37

RE: RE:

Forex19 said:

Spotware said:

The problem is that you use obsolete OnPositionOpened method with new ExecuteMarketOrder method. OnPositionOpened method works only if you create position using old trading API.

Instead of using OnPositionOpened method you need to subscribe to Positions.Opened event. But please keep in mind that it will be raised for all positions: 

/forum/whats-new/1937

 

I modified the code and used PositionOnOpened, and it seems to work properly. 

Thanks.

Can you post the "new" working  code here?

I'm also using this cbot and made some changes in so you can set the lotsize and lotstep size.

Thanks

 

Ronald


@ronald1711

Forex19
11 Mar 2014, 14:47

RE: RE: RE:

ronald1711 said:

Forex19 said:

Spotware said:

The problem is that you use obsolete OnPositionOpened method with new ExecuteMarketOrder method. OnPositionOpened method works only if you create position using old trading API.

Instead of using OnPositionOpened method you need to subscribe to Positions.Opened event. But please keep in mind that it will be raised for all positions: 

/forum/whats-new/1937

 

I modified the code and used PositionOnOpened, and it seems to work properly. 

Thanks.

Can you post the "new" working  code here?

I'm also using this cbot and made some changes in so you can set the lotsize and lotstep size.

Thanks

 

Ronald

The code in post n° 3 is the my starting point.
It is some time that I'm trying various modifications (as I discussed in this topic /forum/cbot-support/2475) but but without solving the problem.


@Forex19