first cBot I don`t know whats wrong, please help

Created at 14 Sep 2017, 14:30
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!
W.

w.b.z

Joined 14.09.2017

first cBot I don`t know whats wrong, please help
14 Sep 2017, 14:30


Hello everyone...

when i try to build this Bot, it shows me this: Error CS1513: } expected and the cursor jumps to the same spot...after:

if (position.Pips > 4.3)
                (HERE it always sits!!

I looked at many other code`s to see if I`m missing something, but i can`t find it. can anyone please Help me out. Thanks in advanced.

what I`m trying is; Hedg&scalp bot with break even point and 9 take profit point`s

futher i want to make it capable of seeing a surtan amount of trades like; exuals 25 $ in the positive and closes the bigest trades that exuals them, automaticly.

and the same in the negative: surtan amount of trades= -25$ closes the trades of the futhest away from the is point.

and this maybe all 5 minutes(is this possible)

heres the Code:



 This code is a cAlgo API sample.
//
//    This cBot is intended to be used as a sample and does not guarantee any particular outcome or
//    profit of any kind. Use it at your own risk
//
//    The "Sample Martingale cBot" creates a random Sell or Buy order. If the Stop loss is hit, a new 
//    order of the same type (Buy / Sell) is created with double the Initial Volume amount. The cBot will 
//    continue to double the volume amount for  all orders created until one of them hits the take Profit. 
//    After a Take Profit is hit, a new random Buy or Sell order is created with the Initial Volume amount.
//
// -------------------------------------------------------------------------------------------------

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class HedgingandScalpingAlgo : Robot
    {


        [Parameter("Volume", DefaultValue = 1000, MinValue = 1000, Step = 1000)]
        public int Volume { get; set; }
        protected override void OnStart()
        {

        }
        protected override void OnBar()
        {
            ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "BUY");
            ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "SELL");
            foreach (var position in Positions)
            {

                if (position.Pips > 4.3)
                (
                    ClosePosition(position)
                   
           private const string DefaultPositionIdParameterValue = "PID";

        [Parameter("MaxOrders",DefaultValue=10)]
        public int MaxOrders { get; set; }
        
        [Parameter("Position Id", DefaultValue = DefaultPositionIdParameterValue)]
        public string PositionId { get; set; }

        [Parameter("Take Profit 1 Enabled", DefaultValue = true)]
        public bool TakeProfit1Enabled { get; set; }

        [Parameter("Take Profit 1 Pips", DefaultValue = 5.6)]
        public double TakeProfit1Pips { get; set; }

        [Parameter("Take Profit 1 Volume", DefaultValue = 467)]
        public int TakeProfit1Volume { get; set; }

        [Parameter("Take Profit 2 Enabled", DefaultValue = true)]
        public bool TakeProfit2Enabled { get; set; }

        [Parameter("Take Profit 2 Pips", DefaultValue = 9.4)]
        public double TakeProfit2Pips { get; set; }

        [Parameter("Take Profit 2 Volume", DefaultValue = 283)]
        public int TakeProfit2Volume { get; set; }

        [Parameter("Take Profit 3 Enabled", DefaultValue = true)]
        public bool TakeProfit2Enabled { get; set; }

        [Parameter("Take Profit 3 Pips", DefaultValue = 9.4)]
        public double TakeProfit2Pips { get; set; }

        [Parameter("Take Profit 4 Volume", DefaultValue = 283)]
        public int TakeProfit2Volume { get; set; }
        
        [Parameter("Take Profit 4 Enabled", DefaultValue = true)]
        public bool TakeProfit2Enabled { get; set; }

        [Parameter("Take Profit 4 Pips", DefaultValue = 9.4)]
        public double TakeProfit2Pips { get; set; }

        [Parameter("Take Profit 4 Volume", DefaultValue = 283)]
        public int TakeProfit2Volume { get; set; }
        
        [Parameter("Take Profit 5 Enabled", DefaultValue = true)]
        public bool TakeProfit2Enabled { get; set; }

        [Parameter("Take Profit 5 Pips", DefaultValue = 9.4)]
        public double TakeProfit2Pips { get; set; }

        [Parameter("Take Profit 5 Volume", DefaultValue = 283)]
        public int TakeProfit2Volume { get; set; }
        
        [Parameter("Take Profit 6 Enabled", DefaultValue = true)]
        public bool TakeProfit2Enabled { get; set; }

        [Parameter("Take Profit 6 Pips", DefaultValue = 9.4)]
        public double TakeProfit2Pips { get; set; }

        [Parameter("Take Profit 6 Volume", DefaultValue = 283)]
        public int TakeProfit2Volume { get; set; }
        
        [Parameter("Take Profit 7 Enabled", DefaultValue = true)]
        public bool TakeProfit2Enabled { get; set; }

        [Parameter("Take Profit 7 Pips", DefaultValue = 9.4)]
        public double TakeProfit2Pips { get; set; }

        [Parameter("Take Profit 7 Volume", DefaultValue = 283)]
        public int TakeProfit2Volume { get; set; }
        
        [Parameter("Take Profit 8 Enabled", DefaultValue = true)]
        public bool TakeProfit2Enabled { get; set; }

        [Parameter("Take Profit 8 Pips", DefaultValue = 9.4)]
        public double TakeProfit2Pips { get; set; }

        [Parameter("Take Profit 8 Volume", DefaultValue = 283)]
        public int TakeProfit2Volume { get; set; }
        
        [Parameter("Take Profit 9 Enabled", DefaultValue = true)]
        public bool TakeProfit2Enabled { get; set; }

        [Parameter("Take Profit 9 Pips", DefaultValue = 9.4)]
        public double TakeProfit2Pips { get; set; }

        [Parameter("Take Profit 9 Volume", DefaultValue = 283)]
        public int TakeProfit2Volume { get; set; }
        
        private TakeProfitLevel[] _levels;

        private Symbol _symbol;

        protected override void OnStart()
        {
            if (PositionId == DefaultPositionIdParameterValue)
                PrintErrorAndStop("You have to specify \"Position Id\" in cBot Parameters");

            var position = FindPositionOrStop();
            _symbol = GetSymbol(position);
            _levels = GetTakeProfitLevels();

            ValidateLevels(position);
        }

        private Symbol GetSymbol(Position position)
        {
            return MarketData.GetSymbol(position.SymbolCode);
        }

        private void ValidateLevels(Position position)
        {
            MakeSureAnyLevelEnabled();
            ValidateTotalVolume(position);
            ValidateReachedLevels(position);
            ValidateVolumes();
        }

        private void ValidateVolumes()
        {
            var enabledLevels = _levels.Where(level => level.IsEnabled);
            foreach (var level in enabledLevels)
            {
                if (level.Volume < _symbol.VolumeMin)
                    PrintErrorAndStop("Volume for " + _symbol.Code + " cannot be less than " + _symbol.VolumeMin);
                if (level.Volume > _symbol.VolumeMax)
                    PrintErrorAndStop("Volume for " + _symbol.Code + " cannot be greater than " + _symbol.VolumeMax);
                if (level.Volume % _symbol.VolumeMin != 0)
                    PrintErrorAndStop("Volume " + level.Volume + " is invalid");
            }
        }

        private void ValidateReachedLevels(Position position)
        {
            var reachedLevel = _levels.FirstOrDefault(l => l.Pips <= position.Pips);
            if (reachedLevel != null)
                PrintErrorAndStop("Level " + reachedLevel.Name + " is already reached. The amount of Pips must be more than the amount of Pips that the Position is already gaining");
        }

        private void MakeSureAnyLevelEnabled()
        {
            if (_levels.All(level => !level.IsEnabled))
                PrintErrorAndStop("You have to enable at least one \"Take Profit\" in cBot Parameters");
        }

        private void ValidateTotalVolume(Position position)
        {
            var totalVolume = _levels.Where(level => level.IsEnabled).Sum(level => level.Volume);

            if (totalVolume > position.Volume)
                PrintErrorAndStop("The sum of all Take Profit respective volumes cannot be larger than the Position's volume");
        }

        private TakeProfitLevel[] GetTakeProfitLevels()
        {
            return new[] 
            {
                new TakeProfitLevel("Take Profit 1", TakeProfit1Enabled, TakeProfit1Pips, TakeProfit1Volume),
                new TakeProfitLevel("Take Profit 2", TakeProfit2Enabled, TakeProfit2Pips, TakeProfit2Volume),
                new TakeProfitLevel("Take Profit 3", TakeProfit3Enabled, TakeProfit3Pips, TakeProfit3Volume)
            };
        }

        private Position FindPositionOrStop()
        {
            var position = Positions.FirstOrDefault(p => "PID" + p.Id == PositionId || p.Id.ToString() == PositionId);
            if (position == null)
                PrintErrorAndStop("Position with Id = " + PositionId + " doesn't exist");

            return position;
        }

        private void PrintErrorAndStop(string errorMessage)
        {
            Print(errorMessage);
            Stop();

            throw new Exception(errorMessage);
        }

        protected override void OnTick()
        {
            var position = FindPositionOrStop();
            var reachedLevels = _levels.Where(level => level.IsEnabled && !level.IsTriggered && level.Pips <= position.Pips);

            foreach (var reachedLevel in reachedLevels)
            {
                reachedLevel.MarkAsTriggered();

                Print("Level \"" + reachedLevel.Name + "\" is reached. Level.Pips: " + reachedLevel.Pips + ", Position.Pips: " + position.Pips + ", Position.Id: " + position.Id);
                var volumeToClose = Math.Min(reachedLevel.Volume, position.Volume);
                ClosePosition(position, volumeToClose);

                if (!LastResult.IsSuccessful)
                    Print("Cannot close position, Id: " + position.Id + ", Error: " + LastResult.Error);

                var remainingLevels = _levels.Where(level => level.IsEnabled && !level.IsTriggered);
                if (!remainingLevels.Any())
                {
                    Print("All levels were reached. cBot is stopping...");
                    Stop();
                    return;
                }
            }
        }
    }

    internal class TakeProfitLevel
    {
        public string Name { get; private set; }

        public bool IsEnabled { get; private set; }

        public double Pips { get; private set; }

        public int Volume { get; private set; }

        public bool IsTriggered { get; private set; }

        public TakeProfitLevel(string name, bool isEnabled, double pips, int volume)
        {
            Name = name;
            IsEnabled = isEnabled;
            Pips = pips;
            Volume = volume;
        }

        public void MarkAsTriggered()
        {
            IsTriggered = true;
        }
    }
}

 


@w.b.z
Replies

Spotware
14 Sep 2017, 14:45

Dear trader,

The code posted has several mistakes that do not allow cAlgo to build the cBot. OnBar misses several curly braces to close properly. Did you copy and paste parts of the code? Maybe you did a mistake while pastng the code. If you do not have any previous experience in c# you might need somebody to help you with the development of your cBot. You can post a job in the Jobs section or contact a professional cAlgo consultant. You can find them listed in the Consultants page.

Best Regards,

cTrader Team


@Spotware

GammaQuant
21 Sep 2017, 03:38

For a start the are two OnStart Methods. obviously it wont work with two.

this function :

 

protected override void OnBar()
        {
            ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "BUY");
            ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "SELL");
            foreach (var position in Positions)
            {
 
                if (position.Pips > 4.3)
                (
                    ClosePosition(position)

                    

should be

protected override void OnBar()
        {
            ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "BUY");
            ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "SELL");
            foreach (var position in Positions)
            {

                if (position.Pips > 4.3)
                {
                    ClosePosition(position);
                }
            }
        }

I dont understand what you are trying to achive with the rest of it....


@GammaQuant