public Timeframe GetTimeframeByID(int TimeframeID)
        {
            Timeframe tfOut = null;

            foreach (Timeframe tf in Timeframes)
            {
                if (tf.TimeframeID == TimeframeID)
                {
                    tfOut = tf;
                    break;
                }
            }

            return(tfOut);
        }
        public Timeframe GetTimeframeByMinutes(int LifeTimeMinutes)
        {
            Timeframe tfOut = null;

            foreach (Timeframe tf in Timeframes)
            {
                if (tf.LifeTimeMinutes == LifeTimeMinutes)
                {
                    tfOut = tf;
                    break;
                }
            }

            return(tfOut);
        }
Beispiel #3
0
        public BinaryPosition SendPosition(string Symbol, string PosType, double Amount, double Price, int LifeTimeMinutes, string OriginalID = null)
        {
            if (NewPositionInProgress)
            {
                DebugLog.WriteLine("SendPosition error: Another position opening in progress");
                return(null);
            }

            NewPositionInProgress = true;

            NewPositionError            = false;
            NewPositionErrorMessage     = string.Empty;
            NewPositionOpenPrice        = 0;
            NewPositionResponseReceived = false;
            NewPositionID = 0;

            int WinPercent    = -1;
            int LossPercent   = -1;
            int ParityPercent = -1;

            Timeframe tf = null;
            Symbol    s  = null;

            try
            {
                tf = TradeSettings.GetTimeframeByMinutes(LifeTimeMinutes);
                s  = TradeSettings.GetSymbolByName(Symbol);

                WinPercent    = s.PayoutWinByTimeframeID[tf.TimeframeID];
                LossPercent   = TradeSettings.PayoutLose;
                ParityPercent = TradeSettings.PayoutParity;
            }
            catch (Exception e)
            {
                DebugLog.WriteLine("SendPosition error: position is not supported | " + e.Message);
                NewPositionInProgress = false;
                return(null);
            }

            int    ts         = DateTimeToTimestamp(DateTime.UtcNow);
            string client_lts = ts.ToString() + "000";
            string rate_lts   = ts.ToString() + "000";


            if (s.AssetID <= 0 || GetDirectionID(PosType) <= 0 || tf.TimeframeID <= 0)
            {
                DebugLog.WriteLine("SendPosition error: position is not supported");
                NewPositionInProgress = false;
                return(null);
            }



            string Request = "{\"type\":\"REQUEST\",\"action\":\"option/buy\",\"body\":{\"option_type_id\":\"1\",\"direction_id\":" + GetDirectionID(PosType) + ",\"asset_id\":\"" + s.AssetID + "\",\"amount\":" + Amount + ",\"time_frame_id\":\"" + tf.TimeframeID + "\",\"win_percent\":" + WinPercent + ",\"loss_percent\":" + LossPercent + ",\"parity_percent\":" + ParityPercent + ",\"client_lts\":" + client_lts + ",\"rate_lts\":" + rate_lts + ",\"rate_value\":" + Price + ",\"forecast1\":" + Price + "},\"rid\":" + RequestID + "}";


            bool res = SendRequest(Request);

            while (!NewPositionResponseReceived)
            {
                System.Threading.Thread.Sleep(1000);
            }


            BinaryPosition pos = new BinaryPosition();

            pos.OpenTime        = DateTime.UtcNow;
            pos.LifeTimeMinutes = LifeTimeMinutes;
            pos.OriginalID      = OriginalID;
            pos.Symbol          = Symbol;
            pos.Type            = PosType;
            pos.AlpariID        = NewPositionID;
            pos.OpenPrice       = NewPositionOpenPrice;

            pos.PayoutWin    = WinPercent;
            pos.PayoutLose   = LossPercent;
            pos.PayoutParity = ParityPercent;


            if (NewPositionError)
            {
                DebugLog.WriteLine("Error opening position with OriginalID " + OriginalID + ": " + NewPositionErrorMessage);
                pos.Status = "Error";
            }
            else
            {
                DebugLog.WriteLine("New position opened");
                pos.Status = "Open";
                OpenPositions.Add(pos);
            }

            NewPositionID        = 0;
            NewPositionOpenPrice = 0;



            NewPositionInProgress = false;
            return(pos);
        }
Beispiel #4
0
        void SetDictionary(dynamic Data)
        {
            #region json_example

            /*
             *
             * {"type":"RESPONSE","action":"dictionary","body":{"assets":[{"decimals":5,"name":"EURUSD","id":1},{"decimals":5,"name":"GBPUSD","id":2},{"decimals":5,"name":"USDCHF","id":3},{"decimals":3,"name":"USDJPY","id":4},{"decimals":5,"name":"USDCAD","id":5},{"decimals":5,"name":"AUDUSD","id":6},{"decimals":5,"name":"NZDUSD","id":7},{"decimals":3,"name":"EURJPY","id":10},{"decimals":5,"name":"EURGBP","id":11},{"decimals":3,"name":"GBPJPY","id":12},{"decimals":5,"name":"EURCAD","id":13},{"decimals":5,"name":"EURAUD","id":14},{"decimals":3,"name":"AUDJPY","id":15},{"decimals":5,"name":"GBPAUD","id":16},{"decimals":5,"name":"GBPCAD","id":17},{"decimals":5,"name":"GBPCHF","id":18},{"decimals":3,"name":"NZDJPY","id":19},{"decimals":3,"name":"XAUUSD","id":20},{"decimals":3,"name":"XAGUSD","id":21}],"currencies":[{"name":"EUR","id":978},{"name":"RUR","id":643},{"name":"USD","id":840},{"name":"GLD","id":10959}],"time_frames":[{"history_size":0,"period":60,"bar_size":1,"name":"1M","id":1},{"history_size":0,"period":300,"bar_size":5,"name":"5M","id":2},{"history_size":0,"period":900,"bar_size":15,"name":"15M","id":3},{"history_size":0,"period":1800,"bar_size":30,"name":"30M","id":4},{"history_size":0,"period":5,"bar_size":1,"name":"5S","id":100},{"history_size":0,"period":3600,"bar_size":60,"name":"1H","id":5},{"history_size":0,"period":7200,"bar_size":60,"name":"2H","id":6},{"history_size":0,"period":14400,"bar_size":300,"name":"4H","id":7},{"history_size":0,"period":86400,"bar_size":1800,"name":"1D","id":8}],"option_types":[{"id":1,"name":"call_put"},{"id":2,"name":"touch"},{"id":3,"name":"range"},{"id":4,"name":"spread"}],"operation_types":[{"name":"deposit","id":1},{"name":"withdrawal","id":2},{"name":"buy","id":3},{"name":"expiry","id":4},{"name":"compensation","id":7}],"directions":[{"name":"call","id":1},{"name":"put","id":2},{"name":"in","id":3},{"name":"out","id":4}],"option_states":[{"name":"approved","id":2},{"name":"started","id":3},{"name":"closed","id":6}],"option_expiry_reasons":[{"name":"time","id":1},{"name":"touch","id":2},{"name":"early","id":3}],"option_results":[{"name":"win","id":1},{"name":"loss","id":2},{"name":"parity","id":3}],"trade_settings":[{"name":"payout_win","id":1},{"name":"payout_lose","id":2},{"name":"payout_parity","id":3},{"name":"enable","id":6},{"name":"enable_permanent","id":7},{"name":"max_bet_643","id":13},{"name":"max_bet_840","id":14},{"name":"max_bet_978","id":15},{"name":"max_bet_10959","id":16},{"name":"min_bet_643","id":17},{"name":"min_bet_840","id":18},{"name":"min_bet_978","id":19},{"name":"min_bet_10959","id":20},{"name":"max_amount_active_options_643","id":21},{"name":"max_amount_active_options_840","id":22},{"name":"max_amount_active_options_978","id":23},{"name":"max_amount_active_options_10959","id":24},{"name":"early_win_percent","id":25},{"name":"early_loss_percent","id":26},{"name":"early_parity_percent","id":27}]},"rid":4}
             *
             *
             *
             * */
            #endregion


            DebugLog.WriteLine("Setting dictionary");

            #region settings
            try
            {
                foreach (dynamic element in Data.body.option_types)
                {
                    try
                    {
                        //DebugLog.WriteLine("Element name: " + element["name"]);

                        if (!TradeSettings.OptionTypesDictionary.ContainsKey((string)element["name"]))
                        {
                            TradeSettings.OptionTypesDictionary.Add((string)element["name"], (int)element["id"]);
                        }
                        else
                        {
                            TradeSettings.OptionTypesDictionary[(string)element["name"]] = (int)element["id"];
                        }
                    }
                    catch (Exception e) { DebugLog.WriteLine("Exception: " + e.Message); }
                }

                foreach (dynamic element in Data.body.operation_types)
                {
                    try
                    {
                        if (!TradeSettings.OperationTypesDictionary.ContainsKey((string)element["name"]))
                        {
                            TradeSettings.OperationTypesDictionary.Add((string)element["name"], (int)element["id"]);
                        }
                        else
                        {
                            TradeSettings.OperationTypesDictionary[(string)element["name"]] = (int)element["id"];
                        }
                    }
                    catch (Exception) { }
                }

                foreach (dynamic element in Data.body.directions)
                {
                    try
                    {
                        if (!TradeSettings.DirectionsDictionary.ContainsKey((string)element["name"]))
                        {
                            TradeSettings.DirectionsDictionary.Add((string)element["name"], (int)element["id"]);
                        }
                        else
                        {
                            TradeSettings.DirectionsDictionary[(string)element["name"]] = (int)element["id"];
                        }
                    }
                    catch (Exception) { }
                }

                foreach (dynamic element in Data.body.option_states)
                {
                    try
                    {
                        if (!TradeSettings.OptionStatesDictionary.ContainsKey((string)element["name"]))
                        {
                            TradeSettings.OptionStatesDictionary.Add((string)element["name"], (int)element["id"]);
                        }
                        else
                        {
                            TradeSettings.OptionStatesDictionary[(string)element["name"]] = (int)element["id"];
                        }
                    }
                    catch (Exception) { }
                }

                foreach (dynamic element in Data.body.option_expiry_reasons)
                {
                    try
                    {
                        if (!TradeSettings.OptionExpiryReasonsDictionary.ContainsKey((string)element["name"]))
                        {
                            TradeSettings.OptionExpiryReasonsDictionary.Add((string)element["name"], (int)element["id"]);
                        }
                        else
                        {
                            TradeSettings.OptionExpiryReasonsDictionary[(string)element["name"]] = (int)element["id"];
                        }
                    }
                    catch (Exception) { }
                }

                foreach (dynamic element in Data.body.option_results)
                {
                    try
                    {
                        if (!TradeSettings.OptionResultsDictionary.ContainsKey((string)element["name"]))
                        {
                            TradeSettings.OptionResultsDictionary.Add((string)element["name"], (int)element["id"]);
                        }
                        else
                        {
                            TradeSettings.OptionResultsDictionary[(string)element["name"]] = (int)element["id"];
                        }
                    }
                    catch (Exception) { }
                }

                foreach (dynamic element in Data.body.trade_settings)
                {
                    try
                    {
                        if (!TradeSettings.TradeSettingsDictionary.ContainsKey((string)element["name"]))
                        {
                            TradeSettings.TradeSettingsDictionary.Add((string)element["name"], (int)element["id"]);
                        }
                        else
                        {
                            TradeSettings.TradeSettingsDictionary[(string)element["name"]] = (int)element["id"];
                        }
                    }
                    catch (Exception) { }
                }
            }
            catch (Exception e) { DebugLog.WriteLine("SetDictiondary Error: " + e.Message); }
            #endregion


            #region symbols

            try
            {
                foreach (dynamic element in Data.body.assets)
                {
                    try
                    {
                        Symbol s = new Symbol((string)element["name"], (int)element["id"], (int)element["decimals"]);
                        TradeSettings.Symbols.Add(s);
                    }
                    catch (Exception e) { }
                }
            }
            catch (Exception e)
            {
            }


            #endregion


            #region timeframes

            try
            {
                foreach (dynamic element in Data.body.time_frames)
                {
                    try
                    {
                        int period  = (int)element["period"];
                        int minutes = (int)period / 60;
                        int id      = (int)element["id"];

                        Timeframe tf = new Timeframe(minutes, id);
                        TradeSettings.Timeframes.Add(tf);
                    }
                    catch (Exception e) { }
                }
            }
            catch (Exception e)
            {
            }

            #endregion



            return;
        }