public void SendOrderToMarketMANUALCLOSE(Trade trade)
        {
            double price = trade.BuyorSell == Trade.BuySell.Buy ? HiSat.LivePrice.Offer : HiSat.LivePrice.Bid;

            ExcelLink.xlTradeOrder o = new xlTradeOrder()
            {
                BS = trade.BuyorSell,
                Price = price,
                Volume = trade.TradeVolume,
                Contract = WebSettings.General.OTS_INST,

            };

            try
            {
                e.Connect();
                e.WriteOrder(o);
                e.Disconnect();
                OrderSendEvent oe = new OrderSendEvent();
                oe.Success = true;
                oe.Trade = trade;
                onOrderSend(this, oe);
                e.StartCheckWhenOrderCompletedTimer(10000);
            }
            catch (Exception ex)
            {
                OrderSendEvent oe = new OrderSendEvent();
                oe.Success = false;
                oe.Trade = trade;
                onOrderSend(this, oe);
            }
        }
        public void SendOrderToMarketGenetic(Trade trade)
        {
            if (trade.BuyorSell != Trade.BuySell.None)
            {
                double price = WebSettings.TradeApproach.AdjustPriceToStrategy(trade, HiSat.LivePrice.Bid, HiSat.LivePrice.Offer);
                trade.TradedPrice =  price;
                ExcelLink.xlTradeOrder o = new xlTradeOrder()
                {
                    BS = trade.BuyorSell,
                    Price = price,
                    Volume = trade.TradeVolume,
                    Contract = trade.InstrumentName,

                };

               
                e.Connect();
                e.WriteOrder(o);
                e.Disconnect();
                OrderSendEvent oe = new OrderSendEvent();
                oe.Success = true;
                oe.Trade = trade;
                onOrderSend(this, oe);
                e.StartCheckWhenOrderCompletedTimer(10000);
            }
        }
        public Trade GetCloseTrade()
        {

            Trade nT = new Trade();
            nT = LastTrade;
            nT.TimeStamp = DateTime.UtcNow.AddHours(2);
            nT.IndicatorNotes = "MANUAL CLOSE";
            nT.ForeColor = Color.Orange;
            nT.BackColor = Color.Black;
            nT.TradeVolume = WebSettings.General.VOL;


            if (LastTrade.Reason == Trade.Trigger.OpenLong)
            {
                nT.Reason = Trade.Trigger.CloseLong;
                nT.BuyorSell = Trade.BuySell.Sell;
            }
            if (LastTrade.Reason == Trade.Trigger.OpenShort)
            {
                nT.Reason = Trade.Trigger.CloseShort;
                nT.BuyorSell = Trade.BuySell.Buy;
            }

            return nT;

        }
        static public void InsertTradeLog(Trade TradeObject)
        {
            try
            {
                AlsiDBDataContext dc = new AlsiDBDataContext();
                dc.Connection.ConnectionString = AlsiUtils.Data_Objects.GlobalObjects.CustomConnectionString;

                TradeLog l = new TradeLog
                {

                    Time = TradeObject.TimeStamp,
                    BuySell = TradeObject.BuyorSell.ToString(),
                    Reason = TradeObject.Reason.ToString(),
                    Notes = TradeObject.IndicatorNotes.ToString(),
                    Price = (int)TradeObject.TradedPrice,
                    Volume = TradeObject.TradeVolume,
                    ForeColor = TradeObject.ForeColor.ToKnownColor().ToString(),
                    BackColor = TradeObject.BackColor.ToKnownColor().ToString()

                };
                dc.TradeLogs.InsertOnSubmit(l);
                dc.SubmitChanges();
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Cannot write Log");
                Debug.WriteLine(ex.Message);
            }





        }
        public static void LoadPrice()
        {

            var path = @"D:\ohlcPL.csv";
            int tradecount = 0;
            using (StreamReader sr = new StreamReader(path))
            {
                while (sr.Peek() >= 0)
                {
                    var s = sr.ReadLine().Split(',');
                 
                    var d = DateTime.Parse(s[0]);
                    var t = DateTime.Parse(s[1]);
                    var stamp = new DateTime(d.Year, d.Month, d.Day, t.Hour, t.Minute, 00);
                    var trade = new Trade()
                    {
                        TimeStamp = stamp,
                        OHLC = new Price(stamp, double.Parse(s[3]), double.Parse(s[4]), double.Parse(s[5]), double.Parse(s[6]), "Test"),
                        TradeVolume = int.Parse(s[7]),
                        TradeTrigger = GetTrigger(s[2]),
                    };

                    if (trade.TradeTrigger == Trade.Trigger.CloseLong || trade.TradeTrigger == Trade.Trigger.CloseShort)
                    {
                        trade.Position = false;
                        _position = false;
                        trade.Notes = tradecount.ToString();
                    }
                    if (trade.TradeTrigger == Trade.Trigger.OpenLong || trade.TradeTrigger == Trade.Trigger.OpenShort) 
                    {
                        trade.Position = true;
                        _position = true;
                        tradecount++;
                        trade.Notes = tradecount.ToString();
                    }
                    if (trade.TradeTrigger == Trade.Trigger.None)
                    {
                        trade.Position = _position;
                        trade.Notes = tradecount.ToString();
                    }


                    Trades.Add(trade);
                }
            }
            var T = Trades.Last();
            Console.WriteLine(Trades.Count + " prices imported");
            Console.WriteLine("Last trade : " + T.TimeStamp + "\nOpen - " + T.OHLC.Open + "\nHigh - " + T.OHLC.High + "\nLow - " + T.OHLC.Low + "\nClose - " + T.OHLC.Close + "\nVol - " + T.TradeVolume + "\nTrigger - " + T.TradeTrigger);
            Console.WriteLine("\n");
        }
Beispiel #6
0
        private AlsiUtils.Trade BuildTrade(RuleModelCalc.CalcDataObject.FinalTrigger t)
        {
            ////Test
            //t.Trade.TradeTrigger_Open = objTrade.TriggerOpen.ReverseLong;
            //t.Trade.TradeTrigger_Close = objTrade.TriggerClose.CloseShort;

            var vol = 0;
            var T   = new AlsiUtils.Trade();

            T.BackColor      = Color.Black;
            T.InstrumentName = WebSettings.General.OTS_INST;
            T.BuyorSell      = GetBS(t.Trade, out vol);
            T.TradeVolume    = vol;
            return(T);
        }
       //public static bool PositionManuallyClosed;                           
        
        public static bool CanCloseTrade(Trade NewTrade)
        {
            
            if (!WebSettings.General.MANUAL_CLOSE_TRIGGER) return true;
            else
            {
                if (NewTrade.Reason == Trade.Trigger.CloseLong || NewTrade.Reason == Trade.Trigger.CloseShort)
                    return false;

                if (NewTrade.Reason == Trade.Trigger.OpenLong || NewTrade.Reason == Trade.Trigger.OpenShort)
                {
                    WebSettings.General.MANUAL_CLOSE_TRIGGER = false;
                    return true;
                }
            }
            return false;
        }
        public void SendOrderToMarket(Trade trade)
        {
            if (trade.BuyorSell != Trade.BuySell.None)
            {
                double price = WebSettings.TradeApproach.AdjustPriceToStrategy(trade, HiSat.LivePrice.Bid, HiSat.LivePrice.Offer);

                ExcelLink.xlTradeOrder o = new xlTradeOrder()
                {
                    BS = trade.BuyorSell,
                    Price = price,
                    Volume = trade.TradeVolume,
                    Contract = trade.InstrumentName,

                };

                try
                {

                    if (ManualTrade.CanCloseTrade(trade))
                    {
                        WebUpdate.SetManualTradeTrigger(false);
                        e.Connect();
                        e.WriteOrder(o);
                        e.Disconnect();
                        OrderSendEvent oe = new OrderSendEvent();
                        oe.Success = true;
                        oe.Trade = trade;
                        onOrderSend(this, oe);
                        e.StartCheckWhenOrderCompletedTimer(10000);
                    }

                }
                catch (Exception ex)
                {
                    OrderSendEvent oe = new OrderSendEvent();
                    oe.Success = false;
                    oe.Trade = trade;
                    onOrderSend(this, oe);
                }
            }
        }
Beispiel #9
0
        private void SendTEST()
        {
            bool Matched = true;
            var  t       = new AlsiUtils.Trade()
            {
                TimeStamp      = DateTime.Now,
                BuyorSell      = Trade.BuySell.Buy,
                InstrumentName = "Test",
                TradedPrice    = 34465,
                TradeVolume    = 1,
            };


            var o = new AlsiWebService.xlTradeOrder();

            if (t.BuyorSell == Trade.BuySell.Buy)
            {
                o.BS = AlsiWebService.BuySell.Buy;
            }
            if (t.BuyorSell == Trade.BuySell.Sell)
            {
                o.BS = AlsiWebService.BuySell.Sell;
            }
            o.Timestamp = t.TimeStamp;
            o.Volume    = t.TradeVolume;
            o.Price     = t.TradedPrice;
            o.Contract  = t.InstrumentName;
            o.Status    = Matched ? AlsiWebService.orderStatus.Completed : AlsiWebService.orderStatus.Ready;
            service.InsertNewOrder(o);
            var s = new AlsiWebService.AlsiNotifyService();

            s.InsertNewOrder(o);

            if (t.Reason == Trade.Trigger.CloseLong || t.Reason == Trade.Trigger.CloseLong)
            {
                WebSettings.General.MANUAL_CLOSE_TRIGGER = true;
            }
        }
 public static void SendSms(Trade trade, SmsMsg Msg, bool AdminEmail)
 {
     _Msg = Msg;
     _SMSTrade = trade;
     _Admin = AdminEmail;
     BW = new BackgroundWorker();
     BW.DoWork += new DoWorkEventHandler(BW_DoWork);
     BW.RunWorkerAsync();
 }
        private void LoadTradeLog(bool todayOnly, bool onlyTrades, int periods)
        {
            AlsiDBDataContext dc = new AlsiDBDataContext();
            liveTradeListView.Items.Clear();

            IQueryable<AlsiUtils.TradeLog> tl = null;
            int count = dc.TradeLogs.Count();
            if (periods > count) return;

            if (todayOnly)
            {
                if (onlyTrades)
                {
                    tl = from x in dc.TradeLogs
                         where x.Time == DateTime.Now.Date
                         where x.BuySell != "None"
                         select x;
                }
                else
                {
                    tl = from x in dc.TradeLogs
                         where x.Time == DateTime.Now.Date
                         select x;
                }
            }
            else
            {
                if (onlyTrades)
                {
                    tl = (from x in dc.TradeLogs
                          select x).Skip(count - periods);

                    tl = tl.Where(z => z.BuySell != "None");
                }
                else
                {
                    tl = (from x in dc.TradeLogs
                          select x).Skip(count - periods);
                }
            }



            liveTradeListView.BeginUpdate();
            Cursor = Cursors.WaitCursor;
            foreach (var v in tl)
            {
                Trade t = new Trade()
                {
                    TimeStamp = v.Time,
                    TradeVolume = (int)v.Volume,
                    BuyorSell = Trade.BuySellFromString(v.BuySell),
                    IndicatorNotes = v.Notes,
                    CurrentPrice = (double)v.Price,
                    ForeColor = Color.FromName(v.ForeColor),
                    BackColor = Color.FromName(v.BackColor),
                    Reason = GetTradeReasonFromString(v.Reason),
                };
                UpdateTradeLog(t, false);
            }
            liveTradeListView.EndUpdate();
            Cursor = Cursors.Default;
        }
        private Trade SetTradeLogColor(Trade t)
        {

            switch (t.Reason)
            {
                case Trade.Trigger.None:
                    t.ForeColor = Color.DarkGray;
                    t.BackColor = Color.White;
                    break;

                case Trade.Trigger.OpenLong:
                    t.ForeColor = Color.Green;
                    t.BackColor = Color.White;
                    break;

                case Trade.Trigger.CloseLong:
                    t.ForeColor = Color.Green;
                    t.BackColor = Color.WhiteSmoke;
                    break;

                case Trade.Trigger.OpenShort:
                    t.ForeColor = Color.Red;
                    t.BackColor = Color.White;
                    break;

                case Trade.Trigger.CloseShort:
                    t.ForeColor = Color.Red;
                    t.BackColor = Color.WhiteSmoke;
                    break;


            }
            return t;
        }
        private void UpdateTradeLog(Trade t, bool WritetoDB)
        {

            string time = WritetoDB ? DateTime.UtcNow.AddHours(2).ToString() : t.TimeStamp.ToString();
            ListViewItem lvi = new ListViewItem(time);
            lvi.SubItems.Add(t.BuyorSell.ToString());
            lvi.SubItems.Add(t.Reason.ToString());
            lvi.SubItems.Add(t.CurrentPrice.ToString());
            lvi.SubItems.Add(t.TradeVolume.ToString());
            lvi.SubItems.Add(t.IndicatorNotes.ToString());
            lvi.ForeColor = t.ForeColor;
            lvi.BackColor = t.BackColor;
            liveTradeListView.Items.Add(lvi);


            if (WritetoDB) DataBase.InsertTradeLog(t);
            var Bid = AlsiTrade_Backend.HiSat.LivePrice.Bid;
            var Offer = AlsiTrade_Backend.HiSat.LivePrice.Offer;
            var Last = AlsiTrade_Backend.HiSat.LivePrice.Last;





        }
        private AlsiUtils.Trade BuildTrade(RuleModelCalc.CalcDataObject.FinalTrigger t)
        {
            ////Test
            //t.Trade.TradeTrigger_Open = objTrade.TriggerOpen.ReverseLong;
            //t.Trade.TradeTrigger_Close = objTrade.TriggerClose.CloseShort;

            var vol = 0;
            var T = new AlsiUtils.Trade();
            T.BackColor = Color.Black;
            T.InstrumentName = WebSettings.General.OTS_INST;
            T.BuyorSell = GetBS(t.Trade, out vol);
            T.TradeVolume = vol;
            return T;
        }
 private static bool CheckDbCount(AlsiWebDataContext dc, Trade trade)
 {
     int c = dc.WebTradeLogs.Count();
     if (c > 0)
     {
         //var  last = dc.WebTradeLogs.Skip(c - 10).Take(10);
     }
     else
     {//create new if db is empty
         WebTradeLog wtl = new WebTradeLog()
         {
             Time = trade.TimeStamp,
             BuySell = trade.BuyorSell.ToString(),
             Price = (int)trade.TradedPrice,
             Reason = trade.Reason.ToString(),
             Volume = trade.TradeVolume,
             PriceMatched = (int)trade.TradedPrice,
             Matched = false,
         };
         dc.WebTradeLogs.InsertOnSubmit(wtl);
         dc.SubmitChanges();
         return false;
     }
     return true;
 }
 public CompletedTrade()
 {
     OpenTrade = new Trade();
     CloseTrade = new Trade();
 }
        void e_onMatch(object sender, OrderMatchEventArgs e)
        {
            var et = e.LastOrder;


            Trade t = new Trade()
            {
                TimeStamp = e.Matched ? e.LastOrder.TimeStamp : DateTime.UtcNow.AddHours(2),
                TradedPrice = et.Price,
                TradeVolume = et.Volume,
                BuyorSell = et.BS,
                InstrumentName = et.Contract,
                xlRef = et.Reference,
                xlMatched = e.Matched,

            };
            OrderMatchEvent ome = new OrderMatchEvent();
            ome.Success = true;
            ome.Trade = t;
            onOrderMatch(this, ome);
        }
        static public List<Trade> GetTradeLogFromDatabase(int numberofLogs)
        {
            List<Trade> logdata = new List<Trade>();
            try
            {


                AlsiDBDataContext dc = new AlsiDBDataContext();
                dc.Connection.ConnectionString = AlsiUtils.Data_Objects.GlobalObjects.CustomConnectionString;

                var count = (from p in dc.TradeLogs
                             select (p.Time)).Count();


                var result = (from q in dc.TradeLogs
                              orderby q.Time ascending
                              select new { q.Time, q.BuySell, q.Reason, q.Price, q.Volume, q.ForeColor, q.BackColor })
                                            .Skip(count - numberofLogs)
                                         .Take(numberofLogs);

                foreach (var v in result)
                {
                    Trade l = new Trade();
                    l.TimeStamp = (DateTime)v.Time;
                    // l.BuyorSell = v.BuySell;
                    // l.TradeReason = v.Reason;
                    l.TradedPrice = (double)v.Price;
                    l.TradeVolume = (int)v.Volume;
                    l.ForeColor = Color.FromName(v.ForeColor);
                    l.BackColor = Color.FromName(v.BackColor);
                    logdata.Add(l);
                }

                return logdata;
            }
            catch (Exception ex)
            {

                return logdata;
            }

        }
        public static void SendOrder(Trade t, bool Matched)
        {
            var o = new AlsiWebService.xlTradeOrder();
            if (t.BuyorSell == Trade.BuySell.Buy) o.BS = AlsiWebService.BuySell.Buy;
            if (t.BuyorSell == Trade.BuySell.Sell) o.BS = AlsiWebService.BuySell.Sell;
            o.Timestamp = t.TimeStamp;
            o.Volume = t.TradeVolume;
            o.Price = t.TradedPrice;
            o.Contract = t.InstrumentName;
            o.Status = Matched ? AlsiWebService.orderStatus.Completed : AlsiWebService.orderStatus.Ready;
            service.InsertNewOrder(o);
            var s = new AlsiWebService.AlsiNotifyService();
            s.InsertNewOrder(o);

            if (t.Reason == Trade.Trigger.CloseLong || t.Reason == Trade.Trigger.CloseLong) WebSettings.General.MANUAL_CLOSE_TRIGGER = true;
        }
            public static double AdjustPriceToStrategy(Trade trade,double bid,double offer)
            {
                double last = trade.CurrentPrice;             
                double spread = WebSettings.TradeApproach.Spread;

                if (trade.BuyorSell == Trade.BuySell.Buy)
                {
                    if (Mode == TradeMode.Hit) return offer;
                    if (Mode == TradeMode.Aggressive) return last + spread;
                    if (Mode == TradeMode.BestBidOffer) return bid + 1;
                    if (Mode == TradeMode.BestAgressive) return bid + spread;

                }


                if (trade.BuyorSell == Trade.BuySell.Sell)
                {
                    if (Mode == TradeMode.Hit) return bid;
                    if (Mode == TradeMode.Aggressive) return last - spread;
                    if (Mode == TradeMode.BestBidOffer) return offer- 1;
                    if (Mode == TradeMode.BestAgressive) return offer - spread;
                }

                return last;
            }
        //Step 5
        private void GetTrades()
        {
            TradeList = new List<Trade>();
            var Count = SSPOP.Count;
            for (int x = 1; x < Count; x++)
            {
                if (SSPOP[x].Position && !SSPOP[x - 1].Position && SSPOP[x].Trigger != SS_Price.TradeTrigger.CloseEndOfDay)
                {

                    var Opentrade = SSPOP[x];

                    for (int q = x + 1; q < Count; q++)
                    {
                        if (SSPOP[q].Position && SSPOP[q].Trigger != SS_Price.TradeTrigger.None
                            || (!SSPOP[q].Position && SSPOP[q].Trigger == SS_Price.TradeTrigger.CloseLong || !SSPOP[q].Position && SSPOP[q].Trigger == SS_Price.TradeTrigger.CloseShort)
                            || (!SSPOP[q].Position && SSPOP[q].Trigger == SS_Price.TradeTrigger.CloseEndOfDay)
                            )
                        {
                            var Closetrade = SSPOP[q];
                            Trade t = new Trade()
                            {
                                OpenTrade = Opentrade,
                                CloseTrade = Closetrade,
                            };
                            TradeList.Add(t);
                            x = q;
                            break;
                        }



                    }
                }

            }


        }
 public static TrailTrade GetTrailTrade(Trade trade)
 {
     var tl = new TrailTrade();
     CopyProperties(tl, trade);
     return tl;
 }
            public TakeProfitTrade(Trade trade)
            {
                this.BuyorSell = trade.BuyorSell;
                this.CurrentDirection = trade.CurrentDirection;
                this.CurrentPrice = trade.CurrentPrice;
                this.Extention = trade.Extention;
                this.IndicatorNotes = trade.IndicatorNotes;
                this.InstrumentName = trade.InstrumentName;
                this.Notes = trade.Notes;
                this.OHLC = trade.OHLC;
                this.Position = trade.Position;
                this.Reason = trade.Reason;
                this.RunningProfit = trade.RunningProfit;
                this.TimeStamp = trade.TimeStamp;
                this.TotalPL = trade.TotalPL;
                this.TotalRunningProfit = trade.TotalRunningProfit;
                this.TradedPrice = trade.TradedPrice;
                this.TradeVolume = trade.TradeVolume;

            }
        public static Trade.BuySell GetBuySell(Trade.Trigger trigger)
        {
            switch (trigger)
            {
                case Trade.Trigger.CloseLong:
                    return Trade.BuySell.Sell;
                    break;

                case Trade.Trigger.CloseShort:
                    return Trade.BuySell.Buy;
                    break;

                case Trade.Trigger.OpenLong:
                    return Trade.BuySell.Buy;
                    break;

                case Trade.Trigger.OpenShort:
                    return Trade.BuySell.Sell;
                    break;

                case Trade.Trigger.EndOfDayCloseLong:
                    return Trade.BuySell.Sell;
                    break;

                case Trade.Trigger.EndOfDayCloseShort:
                    return Trade.BuySell.Buy;
                    break;

            }
            return Trade.BuySell.None;
        }
        private void SendTEST()
        {
            bool Matched = true;
            var t = new AlsiUtils.Trade()
            {
                TimeStamp = DateTime.Now,
                BuyorSell = Trade.BuySell.Buy,
                InstrumentName = "Test",
                TradedPrice = 34465,
                TradeVolume = 1,

            };


            var o = new AlsiWebService.xlTradeOrder();
            if (t.BuyorSell == Trade.BuySell.Buy) o.BS = AlsiWebService.BuySell.Buy;
            if (t.BuyorSell == Trade.BuySell.Sell) o.BS = AlsiWebService.BuySell.Sell;
            o.Timestamp = t.TimeStamp;
            o.Volume = t.TradeVolume;
            o.Price = t.TradedPrice;
            o.Contract = t.InstrumentName;
            o.Status = Matched ? AlsiWebService.orderStatus.Completed : AlsiWebService.orderStatus.Ready;
            service.InsertNewOrder(o);
            var s = new AlsiWebService.AlsiNotifyService();
            s.InsertNewOrder(o);

            if (t.Reason == Trade.Trigger.CloseLong || t.Reason == Trade.Trigger.CloseLong) WebSettings.General.MANUAL_CLOSE_TRIGGER = true;
        }
        public static void SendOrderToWebDB(Trade trade)
        {
            var dc = new AlsiWebDataContext();
            string bs = "none";
            if (trade.BuyorSell == Trade.BuySell.Buy) bs = "Buy";
            if (trade.BuyorSell == Trade.BuySell.Sell) bs = "Sell";
            if (!CheckDbCount(dc, trade)) return;

            if (!trade.xlMatched)
            {
                WebTradeLog wtl = new WebTradeLog()
                {
                    Time = trade.TimeStamp,
                    BuySell = trade.BuyorSell.ToString(),
                    Price = (int)trade.TradedPrice,
                    Reason = trade.Reason.ToString(),
                    Volume = trade.TradeVolume,
                    PriceMatched = 0,
                    Matched = false,
                };
               // dc.WebTradeLogs.InsertOnSubmit(wtl);
                //dc.SubmitChanges();
            }
            else
            {
                int c = dc.WebTradeLogs.Count();
                var last = dc.WebTradeLogs.Skip(c - 1).Take(1).Single();
                last.Time = trade.TimeStamp;
                last.Matched = true;
                dc.SubmitChanges();
            }

        }