-1

Im struggling a problem.This is for a trading bot, In an exchange, there is a rule. You can not trade after a trade, If you want to trade, you have to wait 5 seconds after first trade. After 5 seconds, trade is up to you, buy,sell or do nothing.

I have stopwatch and I call aFunction, after end of the function, timer will start and I call bFunction ,if timer value < 5 seconds, It should wait until 5 seconds pass.

I make a while(true) and I can check if(timer>5) but it will eat cpu and if its single core it will be endless loop during 5 seconds.

Is there any c# way for this ? or any idea ? Sorry for my english and any help appreciated.

UPDATE

It shouldnt wait 5 seconds, if there is no trade before(5 seconds or more). So thread sleep or Timer doesnt work.

mGungorr
  • 17
  • 4
  • https://learn.microsoft.com/en-us/dotnet/api/system.timers.timer?view=net-5.0 – Neil W Jan 30 '21 at 23:28
  • @NeilW but with timer , you have to wait always 5 second. Maybe I didnt trade before , and it should enter that function at same time without waiting. – mGungorr Jan 30 '21 at 23:31
  • 1
    Various ways to do it, but you could have boolean flag (TradeDisabled) that is changed by a 5 second timer. If you try to enter a trade while TradeDisable then it's not accepted. If you want that trade to be accepted but not executed until 5 seconds have passed then you can keep a timestamp of last trade and start a time for 5 seconds MINUS the difference between Now and the timestamp of last trade. – Neil W Jan 30 '21 at 23:35
  • @mnnavc you can change timer's wait time. – Guru Stron Jan 30 '21 at 23:41
  • Be aware of the precision of the timer. It will not fire at the precise time you want it to, regardless of which timer you're using. It's a big enough of a difference that if you're racing other automated trading systems they will get there faster. If you don't care about hitting top of book as quick as possible then a timer makes perfect sense here. – Zer0 Jan 30 '21 at 23:46

2 Answers2

0

Here's a timer based solution. The important things to note are

  • There are several timer classes in .NET. Choose what's best for you.

  • It's very important to understand on what Thread a timer fires.

  • The timer will not fire at exactly the interval you set

     public class Trade
     {
         public string Symbol { get; }
         public DateTime TradeTimeUTC { get; }
     }
    
     public class Order
     {
         public string Symbol { get; }
     }
    
     public void SendOrder(Order order)
     {
         //Find most recent trade for this order, out of scope here
         var trade = GetMostRecentTrade(order);
         var differenceInTime = DateTime.UtcNow - trade.TradeTimeUTC;
         if (differenceInTime.TotalSeconds >= 5)
         {
             ActuallySendOrder(order);
             return;
         }
         System.Timers.Timer timer = new System.Timers.Timer()
         {
             AutoReset = false,
             Interval = differenceInTime.TotalMilliseconds
         };
         //You're on a ThreadPool thread when this fires, adjust accordingly
         timer.Elapsed += (s, e) => { ActuallySendOrder(order); };
         timer.Start();
     }
    
Zer0
  • 7,191
  • 1
  • 20
  • 34
-1

Something like this ...

bool TradeEnabled = true;
DateTime LastTrade;

Timer TradeEnabler;
Timer TradeExectuer;

void RequestTrade()
{
    if (TradeEnabled)
    {
        ExecuteTrade();
    }
    else
    {    
        TimeSpan delaySpan = DateTime.Now - LastTrade;        
        int delay = (int)delaySpan.TotalMilliseconds;
        TradeExecuter = new Timer(delay);
        TradeExecuter.Elapsed += ExecuteTrade;
        TradeExecuter.Enabled = true;
    }
}

void EnableTrade(object sender, ElapsedEventArgs args) 
{
    TradeEnabled = true;
    TradeEnabler.Stop();
    TradeEnabler.Dispose();

}

void ExecuteTrade(object sender, ElapsedEventArgs args)
{
    // Execute Trade
    TradeExecuter.Stop();
    TradeExecuter.Dispose();
    LastTrade = DateTime.Now;

    TradeEnabled = false;

    TradeEnabler = new Timer(5000);
    TradeEnabler.Elapsed += EnableTrade;
    TradeEnabler.Enabled = true;
}
Neil W
  • 7,670
  • 3
  • 28
  • 41
  • Hey, Im getting an error like this. **No overload for 'ExecuteTrade' matches delegate 'ElapsedEventHandler'** I checked on google and stackoverflow but your code looks good. How can I fix it ? – mGungorr Jan 31 '21 at 23:03
  • My answer was not complete, for brevity. It was more to show the architecture of the solution. The method for handling the timer needs arguments (object source, ElapsedEventArgs e). See https://learn.microsoft.com/en-us/dotnet/api/system.timers.timer?view=net-5.0. Updating the answer. – Neil W Jan 31 '21 at 23:30
  • This has threading issues with `TradeEnabled`. – Zer0 Jan 31 '21 at 23:36
  • Yup, use ZerO's answer. – Neil W Feb 01 '21 at 09:19