Topics

Forum Topics not found

Replies

cvphuoc
30 Jan 2014, 04:55

Hi virtuesoft & chiller,

Thank all of you for this valuble topic. Badly i'm a newbie with Ctrader and don't know how to assemble these into full indicators/cbots. Could you pls kindly give full code for volume calculation? Sorry for my disturbance.

Regards.

CVPhuoc


@cvphuoc

cvphuoc
29 Jan 2014, 04:53

RE:

atrader said:

using System;
using cAlgo.API;
using cAlgo.API.Internals;

namespace cAlgo.Robots
{
    [Robot]
    public class OnePercentRiskBot : Robot
    {
        private Rates _rate = Rates.Direct;

        [Parameter(DefaultValue = "Volume on Risk")]
        public string MyLabel { get; set; }

        [Parameter("0:Buy 1:Sell", DefaultValue = 0)]
        public int TType { get; set; }

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

        [Parameter("Take Profit", DefaultValue = 10, MinValue = 0, MaxValue = 500)]
        public int TakeProfit { get; set; }

        // Modify DefaultValue, MinValue, MaxValue
        [Parameter("Risk Percentage", DefaultValue = 1, MinValue = 0.01, MaxValue = 5)]
        public double RiskPercent { get; set; }


        protected TradeType Trade_Type
        {
            get
            {
                return
                    TType == 0 ? TradeType.Buy : TradeType.Sell;
            }
        }

        protected override void OnStart()
        {
            Positions.Opened += PositionsOnOpened;
            // Initialize _rate variable
            SetRate();
            int volume = GetVolume();
            Print("Volume = {0}", volume);
            ExecuteMarketOrder(Trade_Type, Symbol, volume, MyLabel);
        }

        private void PositionsOnOpened(PositionOpenedEventArgs args)
        {
            double risk = 0;
            foreach (Position position in Positions)
            {
                Symbol symbol = MarketData.GetSymbol(position.SymbolCode);
                if (position.StopLoss != null)
                {
                    double stopLoss = Math.Abs(position.EntryPrice - (double) position.StopLoss)/symbol.PipSize;
                    risk += stopLoss*symbol.PipValue*position.Volume - position.Commissions*2;
                }
            }
            Print(risk);
        }

        private int GetVolume()
        {
            double risk = RiskPercent/100.0;
            double volume;

            switch (_rate)
            {
                case Rates.Direct:
                    volume = Account.Equity*risk/(StopLoss*Symbol.PipValue);
                    break;
                case Rates.Indirect:
                    double stopLossPrice = Trade_Type == TradeType.Buy
                                               ? Symbol.Ask + StopLoss*Symbol.PipSize
                                               : Symbol.Bid - StopLoss*Symbol.PipSize;
                    volume = Account.Equity*risk*stopLossPrice/(StopLoss*Symbol.PipValue);
                    break;
                default: // pending
                    volume = 0;
                    break;
            }

            return (int) Symbol.NormalizeVolume(volume);
        }

        private void SetRate()
        {
            switch (Symbol.Code)
            {
                case "EURUSD":
                case "GBPUSD":
                case "AUDUSD":
                case "NZDUSD":
                    _rate = Rates.Direct;
                    break;
                case "USDJPY":
                case "USDCHF":
                case "USDCAD":
                    _rate = Rates.Indirect;
                    break;
                default:
                    _rate = Rates.Cross;
                    break;
            }
        }

        #region Nested type: Rates

        private enum Rates
        {
            Direct,
            Indirect,
            Cross
        };

        #endregion
    }
}

Maybe this is a start...

This would be nice if Gold (XAUUSD) is added up as well.


@cvphuoc

cvphuoc
29 Jan 2014, 04:48

RE:

Hi virtuesoft,

Could you please also modify this for USD-based account.

Many thanks in advance.

Cvphuoc


@cvphuoc

cvphuoc
21 Jan 2014, 02:59 ( Updated at: 21 Dec 2023, 09:20 )

RE:

Spotware said:

You can double click on the position on the chart and it will pop up the window where you can modify the stop loss and take profit. 

 

 

Thanks for your soon feedback, but i already found out that this will be available when i select "Cursor" mode instead of "Crosshair" mode. That's so simple. Thanks again.


@cvphuoc

cvphuoc
20 Jan 2014, 03:47

RE: RE:

 

cAlgo_Fanatic said:

themchose said:

Hi, I have selected 'position' and 'order' in the view option icon, but when I move the cursor to the position line, it can't highlight the line and allow me to modify the stop and take profit level, any other setting? Thank you

 You can modify the SL and TP by right click on the position/order row in positions/orders and select modify position/order.

I got the same problem too. So this very handy function doesn't exist in Ctrader any longer?


@cvphuoc

cvphuoc
17 Jan 2014, 14:21

RE:

Spotware said:

Yes, a cBot may be coded to create trades based on input such as risk and existing positions stop loss and then stop. 

Yes, i definitely need this for my trading on Ctrader. Position Sizing is my most important thing and i miss this function when switching from MT4 to Ctrader. Hope anybody kindly help us in this regard.

 


@cvphuoc