Running a method in separate non blocking thread

Created at 10 Feb 2025, 11:13
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
SO

sonnguyenvuson

Joined 04.07.2022

Running a method in separate non blocking thread
10 Feb 2025, 11:13


Hello,

I wrote a method (below) to open a position at a target price but only in the same bar, it stops when new bar start. At the moment, I use a while loop with Thread.Sleep() to avoid overcrowding. The problem is this method block the cbot. I want to ask how to run this method on a separate thread (async) without blocking? I tried creating async Task with await Task.Delay(5) and use Task.Run() in the main code, but it crashes the cbot on Task.Delay(5).


private Position OpenPositionLimit(DateTime barStart, TradeType tradeType, string symbolName, double volume, double targetPrice, double stopLimitRangePips, string label, double? stopLossPips, double? takeProfitPips, string comment)
       {
           Print($"Starting while loop open {tradeType} position");
           while (barStart == Bars.Last(0).OpenTime && barStart.Millisecond == Bars.Last(0).OpenTime.Millisecond)
           {
               if ((tradeType == TradeType.Buy && Ask <= targetPrice + stopLimitRangePips * Symbol.PipSize)
                   || (tradeType == TradeType.Sell && Bid >= targetPrice - stopLimitRangePips * Symbol.PipSize)
                   )
               {
                   return ExecuteMarketOrder(tradeType, symbolName, volume, label, stopLossPips, takeProfitPips, comment).Position;
               }
               Thread.Sleep(5);
           }
           return null;
       }

Thank you in advance!

best


@sonnguyenvuson
Replies

firemyst
18 Feb 2025, 05:04

What's your overall goal? What are you wanting to do?


@firemyst

sonnguyenvuson
18 Feb 2025, 08:47

RE: Running a method in separate non blocking thread

firemyst said: 

What's your overall goal? What are you wanting to do?

The idea is to run a trade operation on a separate thread, in this case modifying volume when a condition met, so that the main thread is not blocked. Because I need to check the condition using while loop, it will block my code until the condition is met.


@sonnguyenvuson

firemyst
18 Feb 2025, 09:11

RE: RE: Running a method in separate non blocking thread

sonnguyenvuson said: 

firemyst said: 

What's your overall goal? What are you wanting to do?

The idea is to run a trade operation on a separate thread, in this case modifying volume when a condition met, so that the main thread is not blocked. Because I need to check the condition using while loop, it will block my code until the condition is met.

Is there a reason you can't check the condition on every tick? A condition can't change on the current chart's symbol unless a tick comes through because the chart and data is only updated on every tick. 


@firemyst

sonnguyenvuson
18 Feb 2025, 09:18

RE: RE: RE: Running a method in separate non blocking thread

firemyst said: 

sonnguyenvuson said: 

firemyst said: 

What's your overall goal? What are you wanting to do?

The idea is to run a trade operation on a separate thread, in this case modifying volume when a condition met, so that the main thread is not blocked. Because I need to check the condition using while loop, it will block my code until the condition is met.

Is there a reason you can't check the condition on every tick? A condition can't change on the current chart's symbol unless a tick comes through because the chart and data is only updated on every tick. 

You are right, I have not thought about that. I just need to make a signal to start checking the condition in OnTick().

Thanks a lot!


@sonnguyenvuson