Topics

Forum Topics not found

Replies

amusleh
30 May 2022, 10:05

Hi,

I tried but I couldn't reproduce the issue you are facing.

The code I tested:

using System.Linq;
using cAlgo.API;
using System.Threading;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class AdvancedHedgingandScalpingcBot : Robot
    {
        protected override void OnStart()
        {
            for (int i = 0; i < 10; i++)
            {
                ExecuteMarketOrder(i % 2 == 0 ? TradeType.Buy : TradeType.Sell, SymbolName, Symbol.VolumeInUnitsMin, "Test" + i);
            }

            var thread = new Thread(() =>
            {
                CLoseTrade(TradeType.Buy);
                CLoseTrade(TradeType.Sell);
            });

            thread.Start();

            // This also works fine
            //CLoseTrade(TradeType.Buy);
            //CLoseTrade(TradeType.Sell);
        }

        public void CLoseTrade(TradeType tradeType)
        {
            BeginInvokeOnMainThread(() =>
            {
                var positionsCBS = Positions.ToArray();

                foreach (var psnCBS in positionsCBS)
                {
                    if (psnCBS.TradeType != tradeType) continue;

                    ClosePosition(psnCBS);
                }
            });
        }
    }
}

You use BeginInvokeOnMainThread when you are calling or accessing an API member from another thread, on above example I call the CloseTrade method two times from another thread and it closes the positions.

Please post a full cBot example that can reproduce the issue you are facing.


@amusleh

amusleh
30 May 2022, 09:52

Hi,

Please post the cBot code, then we will be able to help you.


@amusleh

amusleh
30 May 2022, 09:52

Hi,

Try this:

            var lastTrade = History.OrderBy(trade => trade.ClosingTime).LastOrDefault();

            if (lastTrade != null)
            {
                // Do something
            }

 


@amusleh

amusleh
30 May 2022, 09:50

RE: RE:

willyvusi04 said:

firemyst said:

Not sure if it can be done, but to access a chart object in a bot that's drawn from a bot, you would do something like this example if you're looking for a horizontal line:

ChartHorizontalLine obj = (ChartHorizontalLine)Chart.FindObject("the name you gave the line when you drew it");

 

Hello firemyst,

 

Thanks for the response, So what would you suggest? Will it be possible to transfer the indicator's code inside a bot?

It's possible, all chart objects are inside Chart.Objects collection.

You can access an object by name, but you have to cast to it's type by using ObjectType property.


@amusleh

amusleh
30 May 2022, 09:44

Hi,

If you refresh your access token you will receive a new token and your previous token will be invalidated, so your session will also end and you have to send a new account auth request with your new token.

 

 


@amusleh

amusleh
30 May 2022, 09:39

Hi,

Try this:

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

namespace cAlgo
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None)]
    [Cloud("Up Kumo", "Down Kumo", Opacity = 0.2)]
    public class AwIchimoku : Indicator
    {
        [Parameter("Tenkan sen", DefaultValue = 9, MinValue = 1)]
        public int Ten_Period { get; set; }

        [Parameter("Kijun sen", DefaultValue = 26, MinValue = 1)]
        public int K_Period { get; set; }

        [Parameter("Senkou span B", DefaultValue = 52, MinValue = 1)]
        public int SB_Period { get; set; }

        [Output("Tenkan sen", LineStyle = LineStyle.Solid, LineColor = "Red")]
        public IndicatorDataSeries Ten_Result { get; set; }

        [Output("Kijun sen", LineStyle = LineStyle.Solid, LineColor = "Blue")]
        public IndicatorDataSeries K_Result { get; set; }

        [Output("Chiku span", LineStyle = LineStyle.Solid, LineColor = "Purple")]
        public IndicatorDataSeries C_Result { get; set; }

        [Output("Up Kumo", LineStyle = LineStyle.Solid, LineColor = "Green")]
        public IndicatorDataSeries Up_Result { get; set; }

        [Output("Down Kumo", LineStyle = LineStyle.Solid, LineColor = "FFFF6666")]
        public IndicatorDataSeries Down_Result { get; set; }

        [Output("Span A", LineStyle = LineStyle.Solid, LineColor = "Green")]
        public IndicatorDataSeries SA_Result { get; set; }

        [Output("Span B", LineStyle = LineStyle.Solid, LineColor = "FFFF6666")]
        public IndicatorDataSeries SB_Result { get; set; }

        private IchimokuKinkoHyo _ICHI;

        protected override void Initialize()
        {
            _ICHI = Indicators.IchimokuKinkoHyo(Ten_Period, K_Period, SB_Period);
        }

        public override void Calculate(int index)
        {
            Ten_Result[index] = _ICHI.TenkanSen[index];
            K_Result[index] = _ICHI.KijunSen[index];
            C_Result[index - 26] = _ICHI.ChikouSpan[index];
            SA_Result[index + 26] = (_ICHI.TenkanSen[index] + _ICHI.KijunSen[index]) / 2;
            SB_Result[index + 26] = _ICHI.SenkouSpanB[index];
            Up_Result[index + 26] = (_ICHI.TenkanSen[index] + _ICHI.KijunSen[index]) / 2;
            Down_Result[index + 26] = _ICHI.SenkouSpanB[index];
        }
    }
}

 


@amusleh

amusleh
30 May 2022, 09:35

Hi,

Not sure what you are referring to, can you post a screenshot?


@amusleh

amusleh
30 May 2022, 09:29

Hi,

No, there is no available data to know how a position is closed, you can check the position close reason inside position closed event but it only gives you information on what caused the position to close, not by who.

Regarding adding comment or label, yes it's possible, both comment and label are available for Positions and orders, you can set comment for a position either by using create new position window on cTrader or with automate API, labels are only available for API.


@amusleh

amusleh
30 May 2022, 09:24

Hi,

We are still investigating and trying to reproduce this issue.


@amusleh

amusleh
30 May 2022, 09:22

Hi,

Try this:

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

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class InteractivePriceLine : Indicator
    {
        [Parameter("Show Interactive Price Line?", Group = "Chart", DefaultValue = false)]
        public bool _show_interactive_price_line { get; set; }

        private ChartStaticText _price_info;
        private ExponentialMovingAverage _fast;
        private ChartHorizontalLine _price_line;
        private bool _price_line_exists;

        protected override void Initialize()
        {
            _fast = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 12);
            _price_info = Chart.DrawStaticText("_price_info", string.Empty, VerticalAlignment.Top, HorizontalAlignment.Center, Color.Chocolate);
            _price_line_exists = false;

            if (!_show_interactive_price_line)
            {
                Chart.RemoveObject("_price_line");
            }
        }

        public override void Calculate(int index)
        {
            if (!IsLastBar) return;

            if (_show_interactive_price_line)
            {
                double _current_pips_from_line;
                if (!_price_line_exists)
                {
                    _price_line = Chart.DrawHorizontalLine("_price_line", Bars.ClosePrices.LastValue, Color.White, 2, LineStyle.LinesDots);
                    _price_line_exists = true;
                }

                _price_line.IsInteractive = true;
                _price_info.Color = Color.White;

                if (_price_line.Y > Bars.ClosePrices.LastValue)
                {
                    _price_line.Color = Color.Blue;
                    _current_pips_from_line = Math.Round((_price_line.Y - Bars.ClosePrices.LastValue) / Symbol.PipSize, 1);
                    _price_info.Text = string.Format("Price Lower Than Line By {0} Pips. | Price: {1}", _current_pips_from_line, Bars.ClosePrices.LastValue);
                }

                if (_price_line.Y < Bars.ClosePrices.LastValue)
                {
                    _price_line.Color = Color.Yellow;
                    _current_pips_from_line = Math.Round((Bars.ClosePrices.LastValue - _price_line.Y) / Symbol.PipSize, 1);
                    _price_info.Text = string.Format("Price Higher Than Line By {0} Pips. | Price: {1}", _current_pips_from_line, Bars.ClosePrices.LastValue);
                }
            }

            if (!_show_interactive_price_line)
            {
                if (_price_line != null)
                {
                    Chart.RemoveObject("_price_line");
                    _price_line_exists = false;
                }
            }
        }
    }
}

 


@amusleh

amusleh
30 May 2022, 09:14

Hi,

When you start optimization the remaining time is unknown, once an optimization pass if finished optimizer knows how long it will take for a single optimization pass to finish.

After that it multiplies the value to the number of passes and shows the remaining time, it's an estimated time based on the time a single pass took.


@amusleh

amusleh
30 May 2022, 09:12

Hi,

Not sure what you are looking for, If the issue was mismatch between indicator and cBot values then it's resolved on the code I posted.


@amusleh

amusleh
30 May 2022, 09:09

Hi,

Did you checked the position commission, spread, and swap?


@amusleh

amusleh
30 May 2022, 09:05

Hi,

Try this:

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 AdvancedHedgingandScalpingcBot : Robot
    {
        [Parameter("Volume", DefaultValue = 1000, MinValue = 1, Step = 1)]
        public int Volume { get; set; }

        private double _equity;
        private int noOfPositions = 4;
        private int pips = 2;
        private string label;

        public Position[] BotPositions
        {
            get
            {
                return Positions.FindAll(label).ToArray();
            }
        }

        protected override void OnStart()
        {
            label = string.Format("AdvancedHedgingandScalpingcBot-{0}-{1}", SymbolName, TimeFrame.ToString());

            _equity = Account.Balance;
            ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label);
            ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label);
        }

        protected override void OnBar()
        {
            foreach (var position in BotPositions)
            {
                if (position.Pips > pips)
                {
                    ClosePosition(position);
                }
            }
            if (BotPositions.Length < noOfPositions)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label);
                ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label);
            }

            int buyCount = 0;
            int sellCount = 0;
            foreach (var position in BotPositions)
            {
                if (position.TradeType == TradeType.Buy)
                    buyCount++;
                if (position.TradeType == TradeType.Sell)
                    sellCount++;
            }

            if (buyCount == 0 || sellCount == 0)
            {
                Volume += 1000;
                noOfPositions += 2;
                pips++;
                ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label);
                ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label);
            }

            if (Account.Equity > _equity + Account.Equity / 100)
            {
                foreach (var position in BotPositions)
                {
                    ClosePosition(position);
                }
                _equity = Account.Equity;
                Volume = 1000;
                noOfPositions = 4;
                pips = 2;
                ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label);
                ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label);
            }
        }
    }
}

 


@amusleh

amusleh
27 May 2022, 13:15 ( Updated at: 21 Dec 2023, 09:22 )

RE: RE: RE: RE:

abondi said:

70 ticks

 

Hi,

It works fine on my IC cTrader:

It's 70 tick custom chart with an EMA and RSI attached to custom close series.


@amusleh

amusleh
27 May 2022, 13:09

Hi,

We are working on a new cTrader.com site which support partially markdown.


@amusleh

amusleh
27 May 2022, 12:37 ( Updated at: 21 Dec 2023, 09:22 )

RE: RE:

abondi said:

I tried the latest version of the overlay indicator (V1.2.0.0) and still gives me the same faulty result, see image below.

I have tried restarting the application but no luck.

I use IC markets  Raw Trading version 4.1 (latest)

 

 

 

What's the tick size on indicator?


@amusleh

amusleh
27 May 2022, 10:49

RE:

Jiri said:

On my end, it fixed implicit usings but not comments in the .csproj. Please, try the project below.


<Project Sdk="Microsoft.NET.Sdk">

	<PropertyGroup>
		<TargetFramework>net6.0</TargetFramework>
		<ImplicitUsings>enable</ImplicitUsings>
		<!--<LangVersion>latest</LangVersion>-->
	</PropertyGroup>

	<ItemGroup>
		<PackageReference Include="cTrader.Automate" Version="1.*" />
	</ItemGroup>

</Project>

using cAlgo.API;

namespace cAlgo
{
    [Indicator(AccessRights = AccessRights.None)]
    public class ErrorCT0001 : Indicator
    {
        protected override void Initialize()
        {
            Print($"The PI value is '{Math.PI}'.");
        }

        public override void Calculate(int index) { }
    }
}

 

Hi,

I was able to reproduce the issue, it happens only if you put the comment tag inside another child tag like PropertyGroup or ItemGroup, but if you put it outside a child tag it works fine.

We will investigate this issue, thanks for reporting.


@amusleh

amusleh
27 May 2022, 10:34

Hi,

To build an indicator with source code by using dotnet CLI you can use: 

dotnet build -p:IncludeSource=True

It's not documented anywhere yet, we will add it on Automate API documentation.


@amusleh

amusleh
27 May 2022, 10:20

Hi,

I tested implicit usings and XML comments, they work if you select the SDK compiler,

So if you change the compiler from embedded to SDK both of those issues will be resolved, and regarding building with source code, you can build with source code with SDK compiler from cTrader itself if you change the compiler to SDK.

And I'm not aware of any parameter that allows building with source code via dotnet CLI.

In my previous message I guided you on how to change the compiler because by changing the compiler both of your issues will be resolved.


@amusleh