Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added margin order types #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion PoloniexApi.Net/General/OrderType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
public enum OrderType
{
Buy,
Sell
Sell,
MarginBuy,
MarginSell
}
}
12 changes: 12 additions & 0 deletions PoloniexApi.Net/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ internal static string ToStringNormalized(this OrderType value)

case OrderType.Sell:
return "sell";

case OrderType.MarginBuy:
return "marginBuy";

case OrderType.MarginSell:
return "marginSell";
}

throw new ArgumentOutOfRangeException("value");
Expand All @@ -89,6 +95,12 @@ internal static OrderType ToOrderType(this string value)

case "sell":
return OrderType.Sell;

case "marginbuy":
return OrderType.MarginBuy;

case "marginsell":
return OrderType.MarginSell;
}

throw new ArgumentOutOfRangeException("value");
Expand Down
9 changes: 8 additions & 1 deletion PoloniexApi.Net/TradingTools/Trading.Interface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public interface ITrading
/// <param name="type">Type of the order.</param>
/// <param name="pricePerCoin">The price to trade your coins at, compared to the base currency.</param>
/// <param name="amountQuote">The amount of quote you want to trade.</param>
Task<ulong> PostOrderAsync(CurrencyPair currencyPair, OrderType type, double pricePerCoin, double amountQuote);
Task<string> PostOrderAsync(CurrencyPair currencyPair, OrderType type, double pricePerCoin, double amountQuote);

/// <summary>
/// <para>Cancels an open order identified by the order ID.</para>
Expand All @@ -35,5 +35,12 @@ public interface ITrading
/// <param name="currencyPair">The currency pair, which consists of the currency being traded on the market, and the base's code.</param>
/// <param name="orderId">The ID of the order to cancel.</param>
Task<bool> DeleteOrderAsync(CurrencyPair currencyPair, ulong orderId);

/// <summary>Submits a new order to the market.</summary>
/// <param name="currencyPair">The currency pair, which consists of the currency being traded on the market, and the base's code.</param>
/// <param name="type">Type of the order.</param>
/// <param name="pricePerCoin">The price to trade your coins at, compared to the base currency.</param>
/// <param name="amountQuote">The amount of quote you want to trade.</param>
Task<string> PostMarginOrderAsync(CurrencyPair currencyPair, OrderType type, double pricePerCoin, double amountQuote, double lendingRate);
}
}
30 changes: 27 additions & 3 deletions PoloniexApi.Net/TradingTools/Trading.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private IList<ITrade> GetTrades(CurrencyPair currencyPair, DateTime startTime, D
return data.Any() ? data.ToList<ITrade>() : new List<ITrade>();
}

private ulong PostOrder(CurrencyPair currencyPair, OrderType type, double pricePerCoin, double amountQuote)
private string PostOrder(CurrencyPair currencyPair, OrderType type, double pricePerCoin, double amountQuote)
{
var postData = new Dictionary<string, object> {
{ "currencyPair", currencyPair },
Expand All @@ -47,7 +47,26 @@ private ulong PostOrder(CurrencyPair currencyPair, OrderType type, double priceP
};

var data = PostData<JObject>(type.ToStringNormalized(), postData);
return data.Value<ulong>("orderNumber");
if(data.Value<string>("error") != null)
return data.Value<string>("error");

return data.Value<string>("orderNumber");
}

private string PostMarginOrder(CurrencyPair currencyPair, OrderType type, double pricePerCoin, double amountQuote, double lendingRate)
{
var postData = new Dictionary<string, object> {
{ "currencyPair", currencyPair },
{ "rate", pricePerCoin.ToStringNormalized() },
{ "amount", amountQuote.ToStringNormalized() },
{ "lendingRate", lendingRate.ToStringNormalized() }
};

var data = PostData<JObject>(type.ToStringNormalized(), postData);
if (data.Value<string>("error") != null)
return data.Value<string>("error");

return data.Value<string>("orderNumber");
}

private bool DeleteOrder(CurrencyPair currencyPair, ulong orderId)
Expand Down Expand Up @@ -76,11 +95,16 @@ public Task<IList<ITrade>> GetTradesAsync(CurrencyPair currencyPair)
return Task.Factory.StartNew(() => GetTrades(currencyPair, Helper.DateTimeUnixEpochStart, DateTime.MaxValue));
}

public Task<ulong> PostOrderAsync(CurrencyPair currencyPair, OrderType type, double pricePerCoin, double amountQuote)
public Task<string> PostOrderAsync(CurrencyPair currencyPair, OrderType type, double pricePerCoin, double amountQuote)
{
return Task.Factory.StartNew(() => PostOrder(currencyPair, type, pricePerCoin, amountQuote));
}

public Task<string> PostMarginOrderAsync(CurrencyPair currencyPair, OrderType type, double pricePerCoin, double amountQuote, double lendingRate)
{
return Task.Factory.StartNew(() => PostMarginOrder(currencyPair, type, pricePerCoin, amountQuote, lendingRate));
}

public Task<bool> DeleteOrderAsync(CurrencyPair currencyPair, ulong orderId)
{
return Task.Factory.StartNew(() => DeleteOrder(currencyPair, orderId));
Expand Down