Can't Seem to Find Whats Wrong With Code?

Created at 24 Apr 2013, 20:45
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!
HI

Hisam

Joined 24.04.2013

Can't Seem to Find Whats Wrong With Code?
24 Apr 2013, 20:45


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

namespace cAlgo.Robots
{
    [Robot]
    public class Bollinger : Robot
    {
    	[Parameter("Source")]
    	public DataSeries Source { get; set; }
    	
    	[Parameter(DefaultValue = 56)]
    	public int period { get; set; }
    	
    	[Parameter("Stop Loss", DefaultValue = 10)]
    	public int StopLossInPips { get; set; }
    	
    	[Parameter("MA Type", DefaultValue = MovingAverageType.Simple)]
    	public MovingAverageType MAType { get; set; }
    	
    	[Parameter(DefaultValue = 2)]
    	public double std { get; set; }
    	
    	[Parameter("Take Profit", DefualtValue = 10)]
    	public int TakeProfitInPips { get; set;}
    	
    	[Parameter("Volume", DefaultValue = 10000, MinValue = 1000)]
    	public int Volume { get; set; }
    	
    	private BollingerBands bb;
    	
        protected override void OnStart()
        {
            bb = Indicators.BollingerBands(Source, period, std, MAType);
        }

        protected override void OnTick()
        {
        	if (Trade.IsExecuting) return;
            
            if (bb.Top.LastValue < Symbol.Bid)
            {
            	OpenPosition(TradeType.Sell);
            }
            else if (bb.Bottom.LastValue > Symbol.Ask)
            {
            	OpenPosition(TradeType.Buy);
            }
        }
        
        protected override void OnPositionOpened(Position openedPosition)
        {
        	var StopLoss = GetAbsoluteStopLoss(openedPosition, StopLossInPips);
        	var takeProfit = GetAbsoluteTakeProfit(openedPosition, TakeProfitInPips);
        	Trade.ModifyPosition(openedPosition, StopLoss, takeProfit);
        }
        
        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;
        }
        		
		}
	}

 

Whenever I try and build I get multiple errors, maybe someone sees something I don't?


@Hisam
Replies

lec0456
25 Apr 2013, 03:53

Looks like you are calling the OpenPosition procedure and I don''t see one in your code...  What are the exact errors?


@lec0456

Hisam
29 Apr 2013, 10:49

RE:
lec0456 said:

Looks like you are calling the OpenPosition procedure and I don''t see one in your code...  What are the exact errors?

"Type or namespace definition, or end-of-file expected" 

 

This is the first thing ive put together so not sure what I'm missing? could you eye something out?


@Hisam

tradermatrix
30 Apr 2013, 01:54

hello
can be like this:

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

namespace cAlgo.Robots

        {
        
        [Robot]
        public class Bollinger : Robot
        
        {
        
        [Parameter("Source")]
        public DataSeries Source { get; set; }
        
        [Parameter("MA Type", DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType MAType { get; set; }
        
        [Parameter(DefaultValue = 56)]
        public int period { get; set; }
        
        [Parameter(DefaultValue = 2)]
        public double std { get; set; }
        
        [Parameter("Volume", DefaultValue = 100000, MinValue = 10000)]
        public int Volume { get; set; }
        
        [Parameter("Stop Loss", DefaultValue = 40)]
        public int StopLossInPips { get; set; }
        
        [Parameter("Take Profit", DefaultValue = 40)]
        public int TakeProfitInPips { get; set;}
        
        private BollingerBands bb;
        
        protected override void OnStart()
        {
            bb = Indicators.BollingerBands(Source, period, std, MAType);
        }

        protected override void OnTick()
        
        {
        
        if (Trade.IsExecuting) return;
            
        if (bb.Top.LastValue < Symbol.Bid)
            
        {
            
        OpenPosition(TradeType.Sell);
        
        }
            
        else if (bb.Bottom.LastValue > Symbol.Ask)
            
        {
            
        OpenPosition(TradeType.Buy);
        
        }
        }
        
        private void OpenPosition(TradeType tradeType)
        
        {
        
        foreach (var position in Account.Positions)
        
        {
        
        if (position.Label == "Bollinger" && position.TradeType != tradeType)
        
        {
        
        Trade.Close(position);
        
        }
        }
        
        bool hasNeededPosition = Account.Positions.Any(position => position.Label == "Bollinger" && position.TradeType == tradeType);
        
        if (!hasNeededPosition)
        
        {
        
        var request = new MarketOrderRequest(tradeType, Volume)
        
        {
                         Label = "Bollinger",
                    StopLossPips = StopLossInPips,
                   TakeProfitPips = TakeProfitInPips
                   
         };

                           Trade.Send(request);
         }
            }
               }
                  }

 


@tradermatrix

Hisam
30 Apr 2013, 22:14

Thank You!


@Hisam

cAlgo_Fanatic
07 May 2013, 12:39 ( Updated at: 21 Dec 2023, 09:20 )

Below are the coding errors:

        // DefualtValue should be DefaultValue (misspelled)
        // Incorrect:
        //[Parameter("Take Profit", DefualtValue = 10)]
        // Correct:
        [Parameter("Take Profit", DefaultValue = 10)]
        public int TakeProfitInPips { get; set;}

        //...

        if (bb.Top.LastValue < Symbol.Bid)
        {
            // *** Method OpenPosition needs to be defined
            OpenPosition(TradeType.Sell);
        }
        else if (bb.Bottom.LastValue > Symbol.Ask)
        {
            // *** Method OpenPosition needs to be defined
            OpenPosition(TradeType.Buy);
        }

        //...

        // Define OpenPosition
        private void OpenPosition(TradeType tradeType)
        {
            // code for OpenPosition
        }

        //...

        private double GetAbsoluteStopLoss(Position position, int stopLossInPips)
        {
        	return position.TradeType == TradeType.Buy
        		? position.EntryPrice - Symbol.PipSize * stopLossInPips
            
                // *** Mistake *** 
                // *** Position and position are different. Position is the type and position is the variable.
                
                // Incorrect:
                // Position.EntryPrice + Symbol.PipSize * stopLossInPips;
                // Correct:
        		: position.EntryPrice + Symbol.PipSize * stopLossInPips;
        }
        
        private double GetAbsoluteTakeProfit(Position position, int takeProfitInPips)
        {
        	return position.TradeType == TradeType.Buy
        		? position.EntryPrice + Symbol.PipSize * takeProfitInPips
                
                        // *** Mistake: Same as above plus Pipsize should be PipSize
                        // Incorrect:
                        // Position.EntryPrice - Symbol.Pipsize * takeProfitInPips;
        		// Correct:
                       : position.EntryPrice - Symbol.PipSize * takeProfitInPips;
        }

C# is case sensitive: abc is different than Abc.

To avoid typos use the intellisense. By typing enter when the correct variable/type etc is located (highlighed) from the intellisense dropdown menu the selected keyword is inserted:

To get started with C# please visit csharp-station.com

For cAlgo help and examples see the cAlgo API Reference, the samples indicators and robots that are included with cAlgo as well as the reference-samples page of the forum.


@cAlgo_Fanatic