What is wrong with this simple robot ?

Created at 22 Jul 2013, 15:02
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!
breakermind's avatar

breakermind

Joined 17.07.2013

What is wrong with this simple robot ?
22 Jul 2013, 15:02



Hello,

What did I do wrong with this script does not work on backtest?

Thanks and bye

 

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

namespace cAlgo.Robots
{
[Robot("Robocik")]
public class Robo : Robot
{
//================================================================================
//                                                                    Parametrs
//================================================================================

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

        [Parameter("Display Week Start", DefaultValue = true)]
        public bool DisplayWeekStart  { get; set; }
        
        [Parameter("Multiple Volume", DefaultValue = true)]
        public bool MultipleVolume { get; set; }
        
        [Parameter(DefaultValue = 10000, MinValue = 1000)]
        public int Volume { get; set; }

        [Parameter("Take Profit (pips)", DefaultValue = 50, MinValue = 1)]
        public int TakeProfit { get; set; }

        // Ma Parametrs
        [Parameter("MA Type")]
        public MovingAverageType MAType { get; set; }

        [Parameter]
        public DataSeries ASeries { get; set; }

        [Parameter("Slow Periods", DefaultValue = 10)]
        public int SlowPeriods { get; set; }

        private MovingAverage slowMa;
        
        public int lastIndexMA;
        public int prevIndexMA;
    
        public double currentSlowMa;    
        public double previousSlowMa;
    
        public double OpenPrice;
        public int minute;
        public int hour;
        public int backBars;
            
//================================================================================
//                                                                      OnStart
//================================================================================
        protected override void OnStart()
        {
        
        slowMa = Indicators.MovingAverage(ASeries, SlowPeriods, MAType);
        
        // Dayline M1      only
        minute = Server.Time.Minute;
        hour = Server.Time.Hour;
        backBars = hour*60+minute; 
        var lastindex = MarketSeries.Open.Count;
        OpenPrice = MarketSeries.Open[lastindex-backBars-1];
        
        ChartObjects.DrawHorizontalLine("Dayline", OpenPrice, Colors.GreenYellow, 1);
        ChartObjects.DrawText("DaylineText", " Dayline "+OpenPrice,StaticPosition.BottomLeft, Colors.GreenYellow);

        }//end

        
//================================================================================
//                                                                        OnBar
//================================================================================
        protected override void OnBar()
        {
         
            lastIndexMA = slowMa.Result.Count - 1;
            prevIndexMA = slowMa.Result.Count - 5;
        
            currentSlowMa = slowMa.Result[lastIndexMA];        
            previousSlowMa = slowMa.Result[prevIndexMA];
            
            ChartObjects.DrawText("DaylineText1", "CurrMA "+currentSlowMa,StaticPosition.BottomRight, Colors.GreenYellow);
            Print(Account.Positions.Count);
            if(Account.Positions.Count == 0){
            Print(Account.Positions.Count);
            if(currentSlowMa > OpenPrice && previousSlowMa < OpenPrice){Buy();}
            if(currentSlowMa < OpenPrice && previousSlowMa > OpenPrice){Sell();}
            }
        }// end
        
//================================================================================
//                                                             OnPositionOpened
//================================================================================
        protected override void OnPositionOpened(Position openedPosition)  
        {
               
            foreach (var position in Account.Positions)
            {
            
            if(position.TradeType == TradeType.Buy && currentSlowMa < OpenPrice){
            Trade.Close(position);
            }
            if(position.TradeType == TradeType.Sell && currentSlowMa > OpenPrice){
            Trade.Close(position);
            }

            }
            
        } // end


//================================================================================
//                                                                     Function
//================================================================================
    private void Buy()
    {
        Trade.CreateBuyMarketOrder(Symbol,Volume);
    }

    private void Sell()
    {
        Trade.CreateSellMarketOrder(Symbol, Volume);
    }
    
//================================================================================
//                                                                    Robot end
//================================================================================
}
}


@breakermind
Replies

cAlgo_Fanatic
22 Jul 2013, 17:43

What do you want OpenPrice to be set to? Most probably you are assigning a NaN value to it if the indexing is not right. This could result if lastIndex is smaller than backBars.

You may use Print statements throughout the code, to output the values assigned to variables that are used in the conditions that open/close trades, for instance. This way, you can identify what could be wrong.

Please be advised that the drawing of lines, has not been implemented for backtesting. 


@cAlgo_Fanatic

breakermind
22 Jul 2013, 18:31

RE:
cAlgo_Fanatic said:

What do you want OpenPrice to be set to? Most probably you are assigning a NaN value to it if the indexing is not right. This could result if lastIndex is smaller than backBars.

You may use Print statements throughout the code, to output the values assigned to variables that are used in the conditions that open/close trades, for instance. This way, you can identify what could be wrong.

Please be advised that the drawing of lines, has not been implemented for backtesting. 


I would like to OpenPrice contain the value "Open" the first bar of the day (week).
Or simply how to get the value of the first bar of the Monday (Open High Low)  when i start script on second day of week?

I use Print but sometimes shows some strange messages.

 

 


@breakermind

fzlogic
24 Jul 2013, 17:09

RE: RE:
breakermind said:
cAlgo_Fanatic said:

What do you want OpenPrice to be set to? Most probably you are assigning a NaN value to it if the indexing is not right. This could result if lastIndex is smaller than backBars.

You may use Print statements throughout the code, to output the values assigned to variables that are used in the conditions that open/close trades, for instance. This way, you can identify what could be wrong.

Please be advised that the drawing of lines, has not been implemented for backtesting. 


I would like to OpenPrice contain the value "Open" the first bar of the day (week).
Or simply how to get the value of the first bar of the Monday (Open High Low)  when i start script on second day of week?

I use Print but sometimes shows some strange messages.

 

 

        protected override void OnBar()
        {
            // Get the open price of the FirstBar of the week
            DateTime firstBar;
            
            if (!isFirstBarInitialized && MarketSeries.OpenTime.LastValue.DayOfWeek == DayOfWeek.Monday)
            {
                firstBar = MarketSeries.OpenTime.LastValue.Date; // get the date only part which will default to midnight time => first bar of monday
                if (MarketSeries.OpenTime.LastValue == firstBar)
                {
                    OpenPrice = MarketSeries.Open.LastValue;                    
                    
                    isFirstBarInitialized = true;
                }
            }
            else if (MarketSeries.OpenTime.LastValue.DayOfWeek != DayOfWeek.Monday)
                isFirstBarInitialized = false;
            
            // the rest of your code




@fzlogic

breakermind
25 Jul 2013, 15:31

RE:

Thanks so much


@breakermind