/// <summary> /// 关闭一笔交易 /// </summary> /// <param name="sessionKey"></param> /// <param name="Tid">交易ID</param> /// <param name="CloseReason">关闭原因</param> /// <param name="ErrorMsg">错误原因</param> /// <returns></returns> public bool CloseTrade(string sessionKey, string Tid, string CloseReason, out string ErrorMsg) { ErrorMsg = null; ITopClient client = new DefaultTopClient(StaticSystemConfig.soft.ApiURL, StaticSystemConfig.soft.AppKey, StaticSystemConfig.soft.AppSecret, "json"); TradeCloseRequest req = new TradeCloseRequest(); if (string.IsNullOrEmpty(Tid)) { ErrorMsg = "订单编号不能为空!"; return(false); } else { req.Tid = 2231978090L; } req.CloseReason = CloseReason; TradeCloseResponse response = client.Execute(req, sessionKey); if (response.IsError) { ErrorMsg = response.SubErrMsg; return(false); } else { return(true); } }
static private async Task PutOnATrade() { WriteNewLine("Checking to see if EUR_USD is open for trading ..."); // first, check the market status for EUR_USD // if it is tradeable, we'll try to make some money :) if (!(await Utilities.IsMarketHalted(INSTRUMENT))) { WriteNewLine("EUR_USD is open and rockin', so let's start trading!"); long?tradeID = await PlaceMarketOrder(); if (tradeID.HasValue) { // we have an open trade. // give it some time to make money :) await Task.Delay(10000); WriteNewLine("Okay, we've waited 10 seconds. Closing trade now ..."); // now, let' close the trade and collect our profits! .. hopefully TradeCloseResponse closeResponse = null; try { closeResponse = await Rest20.CloseTradeAsync(AccountID, tradeID.Value, "ALL"); } catch { WriteNewLine("Oops. The trade can't be closed. Something went wrong. :("); } if (closeResponse != null) { WriteNewLine("Nice! The trade is closed."); var profit = closeResponse.orderFillTransaction.pl; WriteNewLine($"Our profit was USD {profit}"); if (profit > 0) { WriteNewLine($"Nice work! You are an awesome trader."); } else { WriteNewLine($"Looks like you need to learn some money-making strategies. :("); WriteNewLine($"Keep studying, learning, but most of all .. keep trading!!"); } } } else { WriteNewLine($"Looks like something went awry with the trade. you need to learn some money-making strategies. :("); } } else { WriteNewLine("Sorry, Oanda markets are closed or Euro market is not tradeable."); WriteNewLine("Try again another time."); } }
/// <summary> /// 卖家关闭一笔交易 /// </summary> /// <param name="tid"></param> /// <param name="closeReason"></param> /// <returns></returns> /// <remarks>关闭一笔订单,可以是主订单或子订单。当订单从创建到关闭时间小于10s的时候,会报“CLOSE_TRADE_TOO_FAST”错误。</remarks> public ApiResult <long> CloseTrade(long tid, string closeReason) { ITopClient client = GetTopClient(); TradeCloseRequest req = new TradeCloseRequest(); req.Tid = tid; req.CloseReason = closeReason; TradeCloseResponse rsp = client.Execute(req, AccessToken); return(rsp.AsApiResult(() => rsp.Trade.Tid)); }
/// <summary> /// 根据条件关闭订单 /// </summary> /// <param name="tid"></param> /// <param name="closeReason"></param> /// <returns></returns> public Trade CloseTradeOrders(long tid, string closeReason) { try { ITopClient client = TBManager.GetClient(); TradeCloseRequest req = new TradeCloseRequest(); req.Tid = tid; req.CloseReason = closeReason; TradeCloseResponse response = client.Execute(req, Users.SessionKey); return(response.Trade); } catch (Exception ex) { ExceptionReporter.WriteLog(ex, ExceptionPostion.TopApi); return(null); } }
/// <summary> /// 订单关闭 /// </summary> /// <param name="tid"></param> /// <param name="reason"></param> /// <returns></returns> public Trade CloseOrderByTradeID(long tid, string reason) { try { ITopClient client = TBManager.GetClient(); TradeCloseRequest req = new TradeCloseRequest(); req.Tid = tid; req.CloseReason = reason; TradeCloseResponse response = client.Execute(req, Users.SessionKey); return(response.Trade); } catch (Exception ex) { CHENGTUAN.Components.ExceptionReporter.WriteLog(ex, CHENGTUAN.Entity.ExceptionPostion.TopApi); return(null); } }
public async Task <TradeCloseResponse> CloseOrder(String accountId, long orderId) { SetCredentials(accountId); try { TradeCloseResponse tradeCloseResponse = await Rest20.CloseTradeAsync(accountId, orderId); return(tradeCloseResponse); } catch (Exception e) { if (e.Message.IndexOf("MARKET_ORDER_REJECT") > 0 && e.Message.IndexOf("TRADE_DOESNT_EXIST") > 0) { return(null); } throw e; } }
/// <summary> /// 调用Api关闭订单 /// </summary> /// <param name="tid">订单号</param> /// <param name="reason">关单理由</param> public ApiResult CloseOrderByApi(long tid, string reason, Models.UserTaoOAuth taoUserOAuth) { try { DefaultTopClient client = new DefaultTopClient(url_api, this.AppKey, this.AppSecret); client.SetDisableTrace(true); TradeCloseRequest request = new TradeCloseRequest { Tid = tid, CloseReason = reason }; TradeCloseResponse tcr = client.Execute <TradeCloseResponse>(request, taoUserOAuth.access_token); if (tcr.IsError) { return(new ApiResult(false, tcr.ErrMsg)); } return(new ApiResult(true, tcr.Trade.Status)); } catch (Exception e) { throw new Exception("TradeCloseRequest Failure.", e); } }