Topics

Forum Topics not found

Replies

amusleh
27 Sep 2021, 10:36

Hi,

Try this:

using cAlgo.API;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class AdvancedHedgingBot : Robot
    {
        [Parameter("Volume", DefaultValue = 1000, MinValue = 1, Step = 1)]
        public int Volume { get; set; }

        private int noOfPositions = 4;
        private int pips = 2;

        protected override void OnStart()
        {
            ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "BUY");
            ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "SELL");
        }

        protected override void OnTick()
        {
            foreach (var position in Positions)
            {
                if (position.Pips > pips)
                {
                    ClosePosition(position);
                }
            }
            if (Positions.Count < noOfPositions)
            {
                ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "BUY");
                ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "SELL");
            }

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

            // This will keep opening buy and sell positions unless your margin reach 10%, then it stops
            // It opens at most two buy and two sell positions (total 4)
            while (Account.Margin / Account.Equity * 100 < 10 && buyCount + sellCount < 4)
            {
                if (buyCount < 2)
                {
                    buyCount++;
                    ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "BUY");
                }

                if (sellCount < 2)
                {
                    sellCount++;
                    ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "SELL");
                }
            }
        }
    }
}

Modify it based on your needs, it uses a while loop to keep executing new positions until you reach 10% of account margin, then it stops.


@amusleh

amusleh
25 Sep 2021, 08:40

Hi,

You can follow this repository on Github: spotware/openapi-proto-messages: Open API Proto Messages (github.com)

Whenever we released a new version you can download that version proto message files from repository releases.


@amusleh

amusleh
24 Sep 2021, 16:00

Hi,

I still can't compile your source code as it has lots of errors.

You can use the below sample cBot as a guide on how to use SMA with close prices:

using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Robots
{
    // This sample cBot shows how to use the Simple Moving Average indicator
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SimpleMovingAverageSample : Robot
    {
        private double _volumeInUnits;
        private SimpleMovingAverage _sma;

        [Parameter("Source", Group = "MA")]
        public DataSeries MaSource { get; set; }

        [Parameter("Period", DefaultValue = 9, Group = "MA")]
        public int MaPeriod { get; set; }

        [Parameter("Volume (Lots)", DefaultValue = 0.01, Group = "Trade")]
        public double VolumeInLots { get; set; }

        [Parameter("Stop Loss (Pips)", DefaultValue = 10, Group = "Trade")]
        public double StopLossInPips { get; set; }

        [Parameter("Take Profit (Pips)", DefaultValue = 10, Group = "Trade")]
        public double TakeProfitInPips { get; set; }

        [Parameter("Label", DefaultValue = "Sample", Group = "Trade")]
        public string Label { get; set; }

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

        protected override void OnStart()
        {
            _volumeInUnits = Symbol.QuantityToVolumeInUnits(VolumeInLots);
            _sma = Indicators.SimpleMovingAverage(MaSource, MaPeriod);
        }

        protected override void OnBar()
        {
            if (_sma.Result.HasCrossedAbove(Bars.ClosePrices, 0))
            {
                ClosePositions(TradeType.Sell);
                ExecuteMarketOrder(TradeType.Buy, SymbolName, _volumeInUnits, Label, StopLossInPips, TakeProfitInPips);
            }
            else if (_sma.Result.HasCrossedBelow(Bars.ClosePrices, 0))
            {
                ClosePositions(TradeType.Buy);
                ExecuteMarketOrder(TradeType.Sell, SymbolName, _volumeInUnits, Label, StopLossInPips, TakeProfitInPips);
            }
        }

        private void ClosePositions(TradeType tradeType)
        {
            foreach (var position in BotPositions)
            {
                if (position.TradeType != tradeType) continue;
                ClosePosition(position);
            }
        }
    }
}

 


@amusleh

amusleh
23 Sep 2021, 12:43

Hi,

You can use automate API to add more hotkeys, as an example check our hotkeys tool cBot: Hotkeys Tool Robot | Algorithmic Forex Trading | cTrader Community

It allows you to show/hide deal map with an hotkey, you can modify it based on your needs or change it to an indicator so it will not lock your chart time frame / symbol while running.


@amusleh

amusleh
23 Sep 2021, 12:30

RE: RE:

ryanjadidi said:

amusleh said:

Hi,

For orders you have the "trailingStopLoss" field, but for positions right now there is no such field to get this data.

Thank you for your message Amusleh,

Is there a way we could request for this feature in positions? As it is quite important to distinguish between stop loss and trailing stop loss.

Ryan.

Hi,

The trailing stop field for positions will be added on next version of API.


@amusleh

amusleh
23 Sep 2021, 09:07

RE: RE: RE: RE:

m4trader4 said:

 

When your order #2 is cancelled then how it gets filled again? I didn't got it at all.

You can use order label/comment or a boolean flag to avoid executing your code on PendingOrders_Cancelled event handler when you cancel your other orders.

For Order#2 which is not filled, PendingOrders_Cancelled is called and in that ExecuteMarketRangeOrder is executed using the parameters passed by PendingOrders.Cancelled event. 

Orders "label/Comment" will not be unique, or to have a unique identifier.  Let me know your suggestion of unique identifier.

There should be a intermittent order state :  Executed - Cancelled, Not Executed - Cancelled

 

 

 

Hi,

I think there is a misunderstanding, I still don't know what's the issue you have.

Regarding unique identifier you can use Order/position IDs.


@amusleh

amusleh
23 Sep 2021, 09:05

RE:

travkinsm1 said:

At the same time, there can be no confusion when calling several indicators of the following type.

ind0.Calculate(Bars.Count - 1);
ind1.Calculate(Bars.Count - 1);
ind2.Calculate(Bars.Count - 1);
ind3.Calculate(Bars.Count - 1);
ind4.Calculate(Bars.Count - 1);
           

Because on backtesting, the variable I call behaves strangely. Very strangely.

Hi,

I recommend you to use an indicator data series output to get data back from indicator, and if possible you can move your indicators logic to your cBot instead of referencing them.

If your indicators aren't using indicator data series output then move the logic to your cBot, use indicator reference only if your indicator has an output data series.


@amusleh

amusleh
22 Sep 2021, 13:53 ( Updated at: 22 Sep 2021, 13:54 )

Hi,

Your code syntax is invalid, please post full code of your cBot then we will be able to help you.

Please check the simple moving average API reference examples: cAlgo API Reference - SimpleMovingAverage Interface (ctrader.com)


@amusleh

amusleh
21 Sep 2021, 10:46

RE: RE: RE:

olegchubukov said:

amusleh said:

Hi,

You shouldn't use ProtoOAReconcileReq to keep your app up to date with account orders, use it only once and then use execution event to remove/modify/add new account orders.

Regarding Heatbeats, its a response to sending heartbeat, if you don't send any heartbeat you will not receive back an heartbeat message.

You should use a queue to send messages on a specific time interval so you will not reach maximum allowed messages number per second, otherwise you will receive an error message.

Please check our WPF sample code, there we have all of what I just said.

Well, I need to start with saying that I was accidently sending ProtoOAReconcileReq instead of ProtoHeartbeatEvent. Copy/paste... My bad. Is it that's why you commented about ProtoOAReconcileReq?

I have now changed my scheduler to send ProtoHeartbeatEvent and I see ProtoHeartbeatEvent comes every 30 secs again.

But you said that "if you don't send any heartbeat you will not receive back an heartbeat message." This is not true on my side. Even if I dont send a hearbeat, I still receive ProtoHeartbeatEvent every 30 seconds for some period. Sometimes it's 5 minutes, sometimes a couple of hours. But my key point is that ProtoHeartbeatEvent comes without me sending ProtoHeartbeatEvent.

Regarding exceeding the limits, I dont send that many messages to be able to exceed the limits. My application only listens for events.

What is WPF? Is it this source https://github.com/spotware/ctrader-open-api-v2-java-example? If so, that was exactly the code I used.

Here is our WPF sample: OpenAPI.Net/src/WPF.Sample at master · spotware/OpenAPI.Net (github.com)


@amusleh

amusleh
21 Sep 2021, 08:33

RE:

olegchubukov said:

Hi!

I see that the heartbet that usually comes every 30 secconds stopped coming after I started sending ProtoOAReconcileReq every 10 seconds to the API. Is it the right behaviour?

Thanks in advance!

Hi,

You shouldn't use ProtoOAReconcileReq to keep your app up to date with account orders, use it only once and then use execution event to remove/modify/add new account orders.

Regarding Heatbeats, its a response to sending heartbeat, if you don't send any heartbeat you will not receive back an heartbeat message.

You should use a queue to send messages on a specific time interval so you will not reach maximum allowed messages number per second, otherwise you will receive an error message.

Please check our WPF sample code, there we have all of what I just said.


@amusleh

amusleh
21 Sep 2021, 08:29

RE: RE:

m4trader4 said:

amusleh said:

Hi,

The PendingOrders.Cancelled event will be called whenever a pending order is canceled, and that's how it should work.

I didn't understand your point on its not correct to call the Cancelled event when trader cancels the order, why?

If trader manually or a cBot via automate API cancels a pending order the Cancelled event will be triggered and the reason would be PendingOrderCancellationReason.Cancelled.

If the order had an expiry time and it got expired the Cancelled event will be triggered and the reason would be PendingOrderCancellationReason.Expired.

If the order got rejected before getting filled then the Cancelled event will be triggered and the reason would be PendingOrderCancellationReason.Rejected.

You can use the reason property to filter the event and only execute your code is the reasons is expired or rejected.

Hi

Case Scenario: Current Market price = 9.0

Cbot or Manually 5 pending orders are placed

Order#1 10.00 with StopLimitPriceRange  of  00.00 - 00.40 

Order#2 15.00 with StopLimitPriceRange  of  00.00 - 00.40

Order#3 20.00 with StopLimitPriceRange  of  00.00 - 00.40

Order#4 25.00 with StopLimitPriceRange  of  00.00 - 00.40

Order#1 is filled @ 10.10 

Order#2 is not filled @ 15.00 So the PendingOrders_Cancelled called, then the ExecuteMarketRangeOrder is executed and Order#2 is filled.

After Order#2, Cbot or Manually cancel other orders #3, #4, #5 PendingOrders_Cancelled is called and ExecuteMarketRangeOrder is executed possibly may get filled. which should not happen. And Cbot cannot be stopped as there are other function are being executed.

So Need details of the executed pending orders details, with these captured details ExecuteMarketRangeOrder orders are placed and executed or any other logic.

 

 

When your order #2 is cancelled then how it gets filled again? I didn't got it at all.

You can use order label/comment or a boolean flag to avoid executing your code on PendingOrders_Cancelled event handler when you cancel your other orders.


@amusleh

amusleh
20 Sep 2021, 09:40 ( Updated at: 20 Sep 2021, 09:52 )

RE: RE: RE: RE: RE: RE: Exactly same issue

i.maghsoud said:

amusleh said:

mohanrajdeenadayalan said:


pepperstone

iphone 11 Pro 256 GB

iphone software 14.6

c trader version 4.1.54294

 

amusleh said:

witmq said:

myrtlegonzalez73 said:

Hello,

I've been troubleshooting this same problem as well, where it wont connect to ctrader server on Telstra 4G mobile network, seems to
be after a recent update, where its been rock solid for last 6 months.

I also tried the legacy app with same symtoms.

I have also updated to IOS 14.4.2 on both iphone x and ipad and doesn't work on 4G network and works always when on wifi network

I couldn't see how the ISP blocking as the same device same IOS same app that wont connect on 4G connects immediately on telstra wifi

I've also deleted the app and its data and re signed in again and get exact same issue

Lastly I just tried connecting from ipad wifi only device using my ios iPhone as hotspot and works yet wont work directly from iphone unless it is connection to wifi.

 

Hi there,

I’ve been on undergoing with exactly the same issue and advised from Telstra support to contact CTrader support as per case ref.# 16500936. At the moment I’m looking forward to what solution of it. 
 

Hi,

Can you give me these details please:

  • Your broker name
  • cTrader app version
  • Your phone model
  • Your phone OS version

 

- Pepperstone

- 4.2.54296

-Iphone 6s

- IOS 14.4

- Telstra 4G (Boost mobile service provider)

 

 

Hi,

Can you please send me your Telstra phone number, account number and service number linked to the Wifi via Telegram?


@amusleh

amusleh
20 Sep 2021, 08:53

RE:

vs3238194 said:

3.7 Multi Symbol optimization Risk Management enabling. cTrader Automate ... What about optimizing ctrader forex hand trading strategies?

Hi,

There are several manual strategy testers developed by community, search on the site and you will find them on cBots section.

Here is one you can use: Manual Strategy Tester - AlgoDeveloper


@amusleh

amusleh
20 Sep 2021, 08:51

Hi,

For orders you have the "trailingStopLoss" field, but for positions right now there is no such field to get this data.


@amusleh

amusleh
20 Sep 2021, 08:48

Hi,

You can find the API limitations here: https://spotware.github.io/open-api-docs/protocol-buffers/#endpoints


@amusleh

amusleh
20 Sep 2021, 08:46 ( Updated at: 04 Oct 2021, 10:22 )

Hi,

The PendingOrders.Cancelled event will be called whenever a pending order is canceled, and that's how it should work.

I didn't understand your point on its not correct to call the Cancelled event when trader cancels the order, why?

If trader manually or a cBot via automate API cancels a pending order the Cancelled event will be triggered and the reason would be PendingOrderCancellationReason.Cancelled.

If the order had an expiry time and it got expired the Cancelled event will be triggered and the reason would be PendingOrderCancellationReason.Expired.

If the order got rejected before getting filled then the Cancelled event will be triggered and the reason would be PendingOrderCancellationReason.Rejected.

You can use the reason property to filter the event and only execute your code if the reasons is expired or rejected.


@amusleh

amusleh
20 Sep 2021, 08:34

RE: Resend indicator

javad.919 said:

Hello to every one .can some one resend indicator again .the link for downloding indicator dont work .thank,s

Hi,

You can download the indicator compiled algo file from here: Release 1.0.0 · afhacker/Bar-Color-Change-Alert (github.com)


@amusleh

amusleh
20 Sep 2021, 08:26 ( Updated at: 20 Sep 2021, 08:28 )

Hi,

We recommend not using the obsolete API functions, and if you did on past you can easily replace them with new API functions.

Most of the time there is an equivalent for obsolete API functions with just a different name.

Regarding how long we will support the obsolete API functions, there is no fixed time, we will stop supporting them whenever it was not feasible to keep supporting them, so please update your code.


@amusleh

amusleh
20 Sep 2021, 08:24

RE: RE: I have the same problem the AverageTrueRange can be negative

acrigney said:

Looks like this has not been fixed?

Spotware said:

Dear Trader,

Could you please provide us with more information regarding your issue and a code example that causes it?

You can post the code example here or send an email at troubleshooting@spotware.com.

 

Hi,

Please post a code sample so we will be able to replicate the issue you are facing.


@amusleh

amusleh
20 Sep 2021, 08:23

Hi,

I recommend you to read automate API references and code examples: cTDN | cAlgo.API (ctrader.com)

You can search and find similar indicators, then you can modify them based on your needs.


@amusleh