public IgSessionModel SwitchAccount(IgSessionModel igSession, string accountId)
        {
            string     action     = "/session";
            HttpClient httpClient = ClientFactory.Create(igSession, 1);
            string     json       = JsonConvert.SerializeObject(new { accountId = accountId });


            StringContent content = new StringContent(json, Encoding.UTF8, "application/json");

            string Url             = EndPoint() + action;
            var    responseMessage = httpClient.PutAsync(new Uri(Url), content).Result;

            if (responseMessage.StatusCode != HttpStatusCode.OK)
            {
                throw new Exception($"Unable to switch account, reason { responseMessage.ReasonPhrase } Content: {responseMessage.Content.ReadAsStringAsync().Result}.");
            }

            if (responseMessage.Headers.TryGetValues("X-SECURITY-TOKEN", out var values2))
            {
                igSession.SecurityToken = values2.First();
            }
            else
            {
                throw new Exception("Cannot find the security token in the response headers.");
            }

            return(igSession);
        }
        public IgSessionModel Session()
        {
            string action = "/session";

            HttpClient httpClient = ClientFactory.Create();

            var content = new StringContent(JsonConvert.SerializeObject(login), Encoding.UTF8, "application/json");

            string Url    = EndPoint() + action;
            string result = string.Empty;
            HttpResponseMessage responseMessage = null;

            while (string.IsNullOrWhiteSpace(result))
            {
                responseMessage = httpClient.PostAsync(new Uri(Url), content).Result;
                result          = responseMessage.Content.ReadAsStringAsync().Result;
                if (result.Contains("error.public-api.exceeded-api-key-allowance"))
                {
                    result = string.Empty;
                    Thread.Sleep(1000 * 60);
                }
            }

            IgSessionModel igSession = GetSession(responseMessage, result);

            return(igSession);
        }
        static void Main(string[] args)
        {
            IgTradingApiConfig.Environment = environment;

            ReadConfiguration();

            igTradingApiConfig = new IgTradingApiConfig(login);

            session = igTradingApiConfig.Session();
            IgAccounts igAccounts = new IgAccounts(environment, login);

            accountModels = igAccounts.Get(session);

            CommandLine.Parser.Default.ParseArguments <PositionOptions, AccountOptions, QuoteOptions, OrderOptions, UpdateOptions, BuyOptions, AlphaOptions, ExecuteOptions>(args)
            .MapResult(
                (PositionOptions opts) => PositionsList(opts),
                (AccountOptions opts) => ListAccounts(opts),
                (QuoteOptions opts) => Quote(opts),
                (OrderOptions opts) => Order(opts),
                (UpdateOptions opts) => Update(opts),
                (BuyOptions opts) => Buy(opts),
                (AlphaOptions opts) => Alpha(opts),
                (ExecuteOptions opts) => ExecuteStrategy(opts),
                errs => 1);
        }
        public IgAccountModels Get(IgSessionModel igSession)
        {
            string       action       = "/accounts";
            IgHttpClient igHttpClient = new IgHttpClient();
            string       json         = igHttpClient.Get(igSession, action, 1);

            return(JsonConvert.DeserializeObject <IgAccountModels>(json));
        }
Exemple #5
0
        public string GetConfirms(IgSessionModel igSession, string dealReference)
        {
            string action = $"/confirms/{dealReference}";

            IgHttpClient igHttpClient = new IgHttpClient();

            return(igHttpClient.Get(igSession, action, 1));;
        }
 static void SwitchAccount(string accountId)
 {
     Console.WriteLine($"Switching to account Id {accountId} from {session.CurrentAccountId} ");
     if (string.IsNullOrEmpty(accountId) == false && session.CurrentAccountId != accountId)
     {
         session = igTradingApiConfig.SwitchAccount(session, accountId);
     }
 }
        public string Post(IgSessionModel igSession, IgBuyModel buyOrder)
        {
            string action = "/positions/otc";

            StringContent content = new StringContent(JsonConvert.SerializeObject(buyOrder), Encoding.UTF8, "application/json");

            return(igHttpClient.Post(igSession, action, 2, content));;
        }
Exemple #8
0
        public IgEpicModel GetEpic(IgSessionModel igSession, string epic)
        {
            string action = $"/markets/{epic}";
            string json   = igHttpClient.Get(igSession, action, 1);

            IgEpicModel result = JsonConvert.DeserializeObject <IgEpicModel>(json);

            return(result);
        }
        public void Put(IgSessionModel igSession, PositionPosition position)
        {
            string action = "/positions/otc/";


            StringContent content = new StringContent(JsonConvert.SerializeObject(new { limitLevel = position.LimitLevel, stopLevel = position.StopLevel, trailingStop = false }), Encoding.UTF8, "application/json");

            var returnData = igHttpClient.Put(igSession, action + position.DealId, 2, content);
        }
        public string Get(IgSessionModel igSession)
        {
            string action = "/positions";

            IgHttpClient igHttpClient = new IgHttpClient();


            return(igHttpClient.Get(igSession, action, 1));
        }
        public static HttpClient Create(IgSessionModel igSession, int version)
        {
            HttpClient httpClient = new HttpClient();

            httpClient.DefaultRequestHeaders.Add("X-IG-API-KEY", ApiKey);
            httpClient.DefaultRequestHeaders.Add("Accept", "application/json; charset=UTF-8");
            httpClient.DefaultRequestHeaders.Add("Version", version.ToString());
            httpClient.DefaultRequestHeaders.Add("CST", igSession.CST);
            httpClient.DefaultRequestHeaders.Add("X-SECURITY-TOKEN", igSession.SecurityToken);

            return(httpClient);
        }
Exemple #12
0
        public string Post(IgSessionModel igSession, IgOrderModel igOrder)
        {
            string     action     = "/workingorders/otc";
            HttpClient httpClient = ClientFactory.Create(igSession, 2);

            string json = JsonConvert.SerializeObject(igOrder);

            Console.WriteLine(json.FormatJson());
            var content = new StringContent(json, Encoding.UTF8, "application/json");

            string result = igHttpClient.Post(igSession, action, 2, content);

            return(result);
        }
        public string Put(IgSessionModel igSession, string action, int apiVersion, StringContent content)
        {
            using HttpClient httpClient = ClientFactory.Create(igSession, apiVersion);

            string result = string.Empty;

            while (string.IsNullOrWhiteSpace(result))
            {
                HttpResponseMessage response = httpClient.PutAsync(GetUri(action), content).Result;
                result = response.Content.ReadAsStringAsync().Result;
                if (result.Contains("error.public-api.exceeded-api-key-allowance"))
                {
                    result = string.Empty;
                    Thread.Sleep(1000 * 60);
                }
            }
            return(result);
        }
Exemple #14
0
        public MarketSearchModel Get(IgSessionModel igSession, string term, bool getDetail)
        {
            string action = "/markets?searchTerm=" + term;


            string            json   = igHttpClient.Get(igSession, action, 1);
            MarketSearchModel result = JsonConvert.DeserializeObject <MarketSearchModel>(json);

            if (result.Markets != null && getDetail)
            {
                foreach (var market in result.Markets)
                {
                    string ticker = string.Empty;
                    if (!IgEpicMapper.TryLookupCode(market.Epic, out ticker))
                    {
                        IgEpicModel epicModel = GetEpic(igSession, market.Epic);
                        if (!string.IsNullOrWhiteSpace(epicModel.instrument.chartCode))
                        {
                            if (epicModel.instrument.country != "US")
                            {
                                if (epicModel.instrument.country == "GB")
                                {
                                    epicModel.instrument.country = "LON";
                                }

                                epicModel.instrument.chartCode += "." + epicModel.instrument.country;
                            }

                            IgEpicMapper.AddCode(market.Epic, epicModel.instrument.chartCode);
                            ticker = epicModel.instrument.chartCode;
                        }
                    }

                    market.Ticker = ticker;
                    Console.WriteLine($"{market.Ticker} {market.InstrumentName}");
                }
            }

            return(result);
        }
        private static IgSessionModel GetSession(HttpResponseMessage responseMessage, string content)
        {
            IgSessionModel igSession = JsonConvert.DeserializeObject <IgSessionModel>(content);

            if (responseMessage.Headers.TryGetValues("CST", out var values))
            {
                igSession.CST = values.First();
            }
            else
            {
                throw new Exception("Cannot find the CST token in the response headers.");
            }

            if (responseMessage.Headers.TryGetValues("X-SECURITY-TOKEN", out var values2))
            {
                igSession.SecurityToken = values2.First();
            }
            else
            {
                throw new Exception("Cannot find the security token in the response headers.");
            }

            return(igSession);
        }