Topics
22 Apr 2013, 11:18
 2956
 2
Replies

atrader
28 Jan 2014, 18:02

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...


@atrader

atrader
28 Jan 2014, 11:18

Sorry that would be subtract because commissions are negative so:

risk += stopLoss * symbol.PipValue * position.Volume - position.Commissions*2;

 


@atrader

atrader
28 Jan 2014, 11:16

Add position.Commissions * 2 to the risk

risk += stopLoss * symbol.PipValue * position.Volume + position.Commissions*2;

 


@atrader

atrader
24 Jan 2014, 12:25

protected override void OnStart()
{
    double risk = 0;
    foreach (var position in Positions)
    {
        Symbol symbol = MarketData.GetSymbol(position.SymbolCode);
        if (position.StopLoss != null)
        {
            var stopLoss = Math.Abs(position.EntryPrice - (double)position.StopLoss) / symbol.PipSize;
            risk += stopLoss * symbol.PipValue * position.Volume;
        }

    }

    Print(risk);
    Stop();
}

I think that should work.


@atrader

atrader
21 Jan 2014, 16:43

You mean like this:

DateTime stopDate = new DateTime(2014, 01, 31);
if (Server.Time.Date == stopDate)
{
    Stop();
}

Add this in the beginning of the OnStart method so that it will prevent it from starting.


@atrader

atrader
21 Jan 2014, 11:46

RE:

http://vote.spotware.com/forums/229166-ideas-and-suggestions-for-ctrader-and-calgo/suggestions/5356525-optimization

jobenb said:

Hi Spotware

First of all - Congrats on a platform with outstanding possibilities and potential.

We really need to be able to optimise our algos. It is really annoying not to be able to optimise and to try and do it manually is a complete waste of time.

Please can you tell us when we can expect a new version of cAlgo that will have the ability to optimise?

Thank you!

 

 

 

 


@atrader

atrader
07 Jan 2014, 11:17

try:

double r = 5.0 / 100;
Print(r);

 


@atrader

atrader
19 Dec 2013, 11:17

It looks like the super profit indicator /algos/indicators/show/249


@atrader

atrader
10 Dec 2013, 15:19

Hi,

Hope this helps: /algos/robots/show/391


@atrader

atrader
10 Dec 2013, 10:24

RE:

You can use DrawText to display text on the chart. If you choose H4 or above you can see the 2011 chart.

zemotoca said:

There configuring BELKHAYATE PRC indicator to display its parameters at the time for a stipulated period (120 periods for example), but it is presented in a chart since 2011?

 


@atrader

atrader
10 Dec 2013, 10:15

RE:

It includes commissions one way.

Cerunnos said:

But I'm the opinion that position.Netprofit includes already commissions...

 


@atrader

atrader
05 Dec 2013, 14:41

Why not create those positions by another robot (used just to open the positions) assigning the respective labels to them?


@atrader

atrader
05 Dec 2013, 14:10

Why filter positions by timeframe? Or how does timeframe relate to a position?
You can filter positions by label, symbol and trade type with FindAll.

// pass the empty string as the label argument to find positions entered manually 
// and assign a label to positions opened by the robot

var positions = Positions.FindAll("", Symbol);
foreach (var position in positions)
{
   
}

@atrader

atrader
28 Nov 2013, 10:31

what about this one: /algos/indicators/show/287 ?


@atrader

atrader
21 Nov 2013, 14:38

No problem. You can upload this to the indicators so that we can easily download from there :) 


@atrader

atrader
21 Nov 2013, 09:59

RE:

gedet said:

Thanks for the response I really appreciate the help. You are right about DOM I added the code in the calculate method, however the problem is that the indicator compares the current price with the future spanA and spanB (26 periods in the future) I cannot find a way to compare the current price to the current spanA and spanB values.

Instead of cloudM1.SenkouSpanA.LastValue you would need cloudM1.SenkouSpanA[ indexM1], where indexM1 = seriesM1.Close.Count - 1 (the last index for the seriesM1 )

Also, see the examples here for translating indexes between different series: /forum/whats-new/1463#3


@atrader

atrader
20 Nov 2013, 10:44 ( Updated at: 21 Dec 2023, 09:20 )

You don't need DOM for this. Just use Symbol instead of MySymbol. Remove this:

MySymbol = MarketData.GetMarketDepth(Symbol);
MySymbol.Updated += doCalculate;

You can add the code from doCalculate in the Calculate method.

I get this result



@atrader

atrader
14 Nov 2013, 15:52

Hi this might help you get started to make your indicator: /algos/indicators/show/371


@atrader

atrader
07 Nov 2013, 17:55

Are you trying to print to the log or draw text on the chart?
Once you draw something on the chart with ChartObjects it will stay on the screen unless you call RemoveObject or RemoveAllObjects. 
If it didn't work it means the equity didn't reach the target or you set the bool targetlevel1 to true previously in the code.


@atrader

atrader
07 Nov 2013, 17:44

You can index any series so you can use a classic for statement to loop through the elements and find an index. What other methods do you need?


@atrader