Jump to content
Sign in to follow this  
wertmandu

Opening Trades, Forex uses price nowhere near what was entered.

Recommended Posts

In a java machine, I am trying to open up trades at specific positions. For the past few weeks I have been having trouble when I have positions opening pips away from the given opening price (IE: We try to call to open a trade on the EUR/USD for 1.11070 and the price it opened at was 1.11062, or 1.10162 opening at 1.10251)

We are streaming prices with a light streamer process but our listings are stored in a database filled out by the end user so as of right now there is a query to get information to plug into a trade but the data is going directly into  the process that fires the http call. I don;t know if I need to find a faster way of getting information or is this an issue going to Forex's side. A snippet for reference:

 

String response = null;
String trade_sql = "SELECT * FROM Listings WHERE "+
			"Listings.entry_point = "+offer_price+
			" AND Listings.status = 'NOT OPEN' "+
			"AND Listings.auto_flag = 'AUTO';";

resultset rs = execQuery(trade_sql);

while(rs.next())
{
	//openTrade(string username, string session, double bidPrice, double entryPoint, int quantity, string marketName, int marketID)
	response = openTrade(username_cache, session_cache, trading_account_cache bid_price, rs.getDouble("Listings.entry_point"),
                         rs.getInt("Listings.lot_size"), rs.getString("Listings.currency_pair"), rs.getInt("Listings.marketID"));
	break;
}

if (response != null)
{
	//Update Listings table and record a log.
}

// OpenTrade code
public static String openTrade(String username, String session, String tradingAccountID, double bidPrice, double entryPoint, int quantity, 
                               String marketName, int marketID) {
        String TradeBaseUrl = "https://ciapi.cityindex.com/TradingAPI/order/newtradeorder";

        CloseableHttpClient httpclient = HttpClients.createDefault();

        HttpPost postRequest = new HttpPost(TradeBaseUrl + "?UserName=" + username + "&Session=" + session);

        String jsonBody =   "{" +
                "\"MarketID\": " + marketID + ", " +
                "\"Direction\": \"buy\", " +
                "\"BidPrice\": " + bidPrice + " , " +
                "\"OfferPrice\": " + entryPoint + " , " +
                "\"Quantity\": " + quantity + " , " +
                "\"OrderID\": 0, " +
                "\"positionMethodId\": 1, " +
                "\"TradingAccountId\": " + tradingAccountID + ", " +
                "\"MarketName\": \"" + marketName + "\", " +
                "\"isTrade\": true" +
                "}";

        try {
            postRequest.setEntity(new StringEntity(jsonBody, ContentType.APPLICATION_JSON));

            CloseableHttpResponse response = httpclient.execute(postRequest);

            if (response.getStatusLine().getStatusCode() == 200) {
                String responseBody = EntityUtils.toString(response.getEntity());

                JSONObject json = new JSONObject(responseBody);
                
                return json.toString();
            } else {
                System.out.println(response.getStatusLine());
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
        } finally {
            try {
                httpclient.close();
            } catch (IOException e) {
                System.out.println("GainCapitalAPI: ERROR: " + e.getMessage());
            }
        }

I don't know if I am going too slow or what my problem is. If anyone could help me out I would be much appreciated. 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
×