Forex | Trade and Stop-Limit Orders

The REST API exposes five order-management endpoints. Each endpoint is provided in three overload forms so that the caller can choose the level of control required.

Endpoints

Method Description
NewTradeOrder Places a market / at-best trade order on a CFD market.
UpdateTradeOrder Attaches or changes attached stop / limit protection on an existing open trade.
NewStopLimitOrder Places a pending stop or limit order (not yet in the market).
UpdateStopLimitOrder Amends an existing pending stop or limit order.
CancelOrder Cancels a pending stop or limit order.

Overload Tiers

Every order method is available in three overloads:

  1. Raw JSON overload — takes a single aJson: string. The caller builds the exact JSON payload described in the Forex.com API reference. This is the escape hatch when a brand-new field is required before the typed helpers catch up.
  2. Scalar-parameter overload — takes the most-used fields (MarketId, TradingAccountId, Direction, Quantity, BidPrice / OfferPrice, TriggerPrice, Applicability, ExpiryDateTimeUTC). The helper builds the JSON internally. Useful for plain, protection-less orders.
  3. Typed order-object overload — takes a TsgcHTTPForexTradeOrder, TsgcHTTPForexStopLimitOrder or TsgcHTTPForexCancelOrder. These objects expose every documented field including IfDone (attached stop / limit brackets), Close arrays (close-by-order-id semantics), PositionMethodId, Reference and partial-fill flags. This is the convention shared with other venues such as Kucoin.

TsgcHTTPForexTradeOrder Fields

Sample

Buy 1000 EUR/USD at market with an attached take-profit and stop-loss


oForex := TsgcWSAPI_Forex.Create(nil);
oForex.Credentials.UserName := '<username>';
oForex.Credentials.Password := '<password>';
oForex.Credentials.AppKey  := '<appkey>';
oForex.Connect;

oOrder := TsgcHTTPForexTradeOrder.Create;
try
  oOrder.MarketId        := 401484830;
  oOrder.TradingAccountId:= oForex.TradingAccountId;
  oOrder.Direction       := fdBuy;
  oOrder.Quantity        := 1000;
  oOrder.BidPrice        := 1.09120;
  oOrder.OfferPrice      := 1.09130;

  // attached take-profit at +50 pips (limit)
  oIfDoneTP := TsgcHTTPForexIfDoneOrder.Create;
  oIfDoneTP.Direction := fdSell;
  oIfDoneTP.Quantity  := 1000;
  oIfDoneTP.TriggerPrice := 1.09630;
  oOrder.IfDone.AddLimit(oIfDoneTP);

  // attached stop-loss at -30 pips (stop)
  oIfDoneSL := TsgcHTTPForexIfDoneOrder.Create;
  oIfDoneSL.Direction := fdSell;
  oIfDoneSL.Quantity  := 1000;
  oIfDoneSL.TriggerPrice := 1.08830;
  oOrder.IfDone.AddStop(oIfDoneSL);

  ShowMessage(oForex.REST_API.NewTradeOrder(oOrder));
finally
  oOrder.Free;
end;