ExchangeSharp icon indicating copy to clipboard operation
ExchangeSharp copied to clipboard

What is the preferred way to add futures/swap support

Open szmcdull opened this issue 5 years ago • 17 comments
trafficstars

a) Add another virtual exchange that works only for futures, yet another one for swap. b) Add a marketType parameter to all methods of the existing exchange.

The first one is closer to the native APIs provided by most exchanges. The second one is more user friendly in my opinion.

szmcdull avatar Apr 16 '20 03:04 szmcdull

Are you talking about OKEx?

vslee avatar Apr 16 '20 04:04 vslee

OKEx, Huobi, Binance, Deribit, KuCoin etc.

szmcdull avatar Apr 16 '20 04:04 szmcdull

For OKEx, can you determine whether it's futures / swap from the symbol name? I haven't looked into he others.

vslee avatar Apr 16 '20 17:04 vslee

Maybe futures/swap can be determined by symbol name. But margin/spot share the same symbol names and depths.

szmcdull avatar Apr 20 '20 09:04 szmcdull

In that case, I think option (b) would be better and less confusing.

vslee avatar Apr 20 '20 16:04 vslee

Which methods would need the parameter?

jjxtra avatar Apr 20 '20 16:04 jjxtra

Actually, a better option would be to create a property in ExchangeAPI:

		protected bool isMarginTradingEnabled = false;

		public virtual bool IsMarginTradingEnabled
		{
			get { return isMarginTradingEnabled; }
			set { throw new NotImplementedException(
				"Margin trading is not implemented for this exchange. Please submit a PR if you would like to implement it."); }
		}

Any exchanges that do implement it can override the property to set the field isMarginTradingEnabled. This way, no new classes need to be created, and no method signatures need to be changed.

vslee avatar Apr 22 '20 15:04 vslee

Would returning false be better, that way people don't have unexpected exceptions? I suppose if we document the setter with a NotImplementedException that may be sufficient?

jjxtra avatar Apr 22 '20 15:04 jjxtra

isMarginTradingEnabled is by default set to false (I updated the code above to make it clearer). I think it is better to throw an execption, than to have ppl set it to true and thinking that they are using margin trading when they are not. This would be set in the initialization part of ppl's code anyways, so it's not like it would interrupt their trading in the middle of the day.

vslee avatar Apr 22 '20 16:04 vslee

Also, the exception would only be thrown if someone tries to use margin trading where not implemented. All existing user code would still work fine w/o any changes.

vslee avatar Apr 22 '20 16:04 vslee

What would a derived class look like trying to set the bool to true? Would it need it's own bool?

jjxtra avatar Apr 22 '20 17:04 jjxtra

The derived class would have this:

		public override bool IsMarginTradingEnabled
		{
			get { return isMarginTradingEnabled; }
			set { isMarginTradingEnabled = value; }
		}

vslee avatar Apr 22 '20 18:04 vslee

I would propose the following, where derived classes opt in and override IsMarginTradingPossible to true if desired.

///<summary>Indicates if margin trading is possible. Derived classes can override and return true. The default is false.</summary>
public virtual bool IsMarginTradingPossible => false;

private bool isMarginTradingEnabled = false;

///<summary>Gets whether margin trading has been turned on. If IsMarginTradingPossible is false, any attempt to set this value will throw a NotImplementedException.</summary>
public bool IsMarginTradingEnabled
{
    get { return isMarginTradingEnabled; }
    set
    {
        if (!IsMarginTradingPossible)
        {
            throw new NotImplementedException(
	    "Margin trading is not implemented for this exchange. Please submit a PR if you would like to implement it, derived classes can override IsMarginTradingPossible to return true .");
        }
        isMarginTradingEnabled = value;
    }
}

jjxtra avatar Apr 22 '20 18:04 jjxtra

Yes, I like that

vslee avatar Apr 22 '20 18:04 vslee

Awesome, sounds like a plan for whoever tackles this beast :)

jjxtra avatar Apr 22 '20 18:04 jjxtra

How is isMarginEnabled related to futures/swap?

szmcdull avatar Apr 24 '20 09:04 szmcdull

isMarginEnabled is just for whether the user wants to use margin vs spot. Since the symbol names for futures/swap are different, those can be added to the existing methods without adding any parameters.

vslee avatar Apr 24 '20 20:04 vslee