Hello every1,
I have some code that is connected to a websocket. And it can execute less than .0005 ms so it can be called basically instantly multiple times. I can only let this code execute once at a time. After the code finishes executing it can then execute again. I have tried using a lock statement, using a Counter, and switching many different parts of code around to make this happen properly. I got the code functioning how I want it to, or at least I think I do.. its working for now, I created a TradeDelay method but I know it this code is not setup properly and I would to learn proper implementation. I believe I should be able to completely remove this TradeDelay() and my ‘Counter’ once I have everything setup correctly.
- public partial class MainWindow : Window
- {
- static readonly object lockIt = new object();
- private void TurnOnSocket(){
- var incoming = SocketClient.SubscribeToTradeUpdatesAsync("BTCUSD",(data) =>
- {
- if ((tExecute == null) || !tExecute.Status.Equals(TaskStatus.Running))
- {
- ExecuteTradeDelay();
-
-
-
-
-
-
-
-
-
- _ = AddNewIncomingTradeAsync(data);
-
-
-
-
- }
- }
- }
- private async Task ExecuteTradeDelay()
- {
- await Task.Delay(1500);
- lock (lockIt)
- {
- if(counterExecute == 0)
- {
- counterExecute++;
- _ = ExecuteTradeAsync();
- }
- }
- await Task.Delay(2500);
- counterExecute = 0;
- }
- private async Task ExecuteTradeAsync()
- {
- cancelExecute = new CancellationTokenSource();
-
-
- if (running)
- {
- tExecute = Task.Factory.StartNew(async () =>
- {
- try
- {
- if (!did1B)
- {
- if (ActiveTrades.ContainsKey("Trade1B"))
- {
- if(UnsureWhyPriceValid(Convert.ToDecimal(CurrentPrice.Price), ActiveTrades["Trade1B"].StopPrice))
- {
- StopOrder stopOrder = new StopOrder(ActiveTrades["Trade1B"].TradeSymbol, ActiveTrades["Trade1B"].StopPrice, ActiveTrades["Trade1B"].LimitPrice, ActiveTrades["Trade1B"].CryptoAmount);
- var placedOrder = await tradingClient.StopOrderBuyAsyncV2(stopOrder).ConfigureAwait(false);
- AddTradeStatusList(placedOrder, "Trade1B");
- HarkinsLogger.Log("Trade 1 - Buy Executed & added to 'TradeStatusList'");
- trade1B = false;
- ActiveTrades.Remove("Trade1B");
- _ = PrintActiveTradesAsync();
- return;
- }
- }
- }
- }
- }