Replies

3082492
08 Oct 2016, 15:33

This idea was raised more than two years ago! What's up with Spotware? They keep updating cTrader fairly regularly; however, I have not seen meaningful enhancements to cAlgo API in a while. Frankly, I am wondering whether I have chosen the right platform to work with.


@3082492

3082492
26 May 2016, 18:24

RE:

Hi, 

I seem to have found a solution that, while it might not be the perfect solution, works for me. I am sharing it so that others who face the same challenge can save some time. Please forgive me in case I do not use the correct technical words; multi-threading and concurrent processes really is way beyond my understanding. 

The problem: The following piece of code DOES NOT prevent parallel instances of the same cBot, e.g. one running on EURUSD and another one on GBPUSD, from opening more than one position. I believe this is because concurrent processes (the parallel cBot instances) execute this piece of code at the same time so that both get zero as a return value from Positions.Count().

protected override void OnTick()
{
    if(Positions.Count() == 0) // Prevent concurrent processes (same cBot running in parallel multiple times). DOES NOT WORK.
    {
       // Open new position.
    }
}

 

While all my attempts to use a lock or mutex to solve the problem failed, the following piece of code did the trick for me: 

using System.Threading;

...

private static int tradeOperationIsExecuting = 0; // Indicates whether a tradeOperation is in progress.

...

protected override void OnTick()
{
    if(Interlocked.Exchange(ref tradeOperationIsExecuting, 1) == 0) // Lock the following section for concurrent processes.
    {
        if (Positions.Count(p => p.Comment.Contains(Name)) == 0) // Allow a single position at a time.
        {
           // Business logic to open a new position.
        }
        Interlocked.Exchange(ref tradeOperationIsExecuting, 0); // Release lock.
    }
}

 

If you know a way how to improve this or do it in a more correct way, please let me know.

 

Regards,

John

 


@3082492

3082492
25 May 2016, 23:30

Hi Solark, 

I am facing another threading-related challenge. Maybe you are willing help if you read this:

I am running multiple instances of the same cBot in parallel, e.g. one on EURUSD and a second one on USDJPY. I want to limit the number of open positions to exactly one across all instances at any point in time. The following piece of code does not seem to do the trick. I also tried to wrap mutex around the if-statement to make both instances wait for the other to finish creating a position; however, two parallel cBot instances each opened a position. Could you provide some advise?

protected override void OnTick()
{
   if (Positions.Count(p => p.Comment.Contains(Name)) == 0) // Allow a single position at a time.
   {
      // do stuff
   }
}

Best regards,

John


@3082492

3082492
14 May 2016, 20:04

RE:

Hi Solark, 

Thanks a ton for your reply and code example. Multi-threading is beyond my coding skills, so your example really is a great help!

Best regards,

John


@3082492

3082492
28 Jul 2015, 11:02

Use this indicator to display the current spread of the symbol on the chart:

/algos/indicators/show/331


@3082492

3082492
28 Jul 2015, 10:59

I would also like to have this feature for all objects rendered on a chart by means of the ChartObjects class.


@3082492

3082492
08 Feb 2015, 19:16

RE:

3082492 said:

I found the problem myself. Here is the solution: 

This line in the indicator leads to wrong results even though they show up correctly on the charts:

int pivotIndex = this.pivotSeries.OpenTime.GetIndexByTime(this.MarketSeries.OpenTime[index]) - 1;

Instead, use this to get the previous period (in this case, one day) within the indicator timeframe.

int pivotIndex = this.pivotSeries.OpenTime.GetIndexByTime(this.MarketSeries.OpenTime[index].AddDays(-1));

 

Best regards, 

Henry


@3082492