public override void Add(StockOrder stockOrder) { if (stockOrder.State != StockOrder.OrderStatus.Executed) { throw new System.Exception("Cannot add a not executed order to a position"); } if (stockOrder.IsShortOrder ^ this.IsShortPosition) { throw new System.Exception("Cannot add a short order to a long position"); } if (stockOrder.IsBuyOrder()) { this.UnitCost = (this.UnitCost * this.Number + stockOrder.UnitCost * stockOrder.Number) / (float)(this.Number + stockOrder.Number); this.Number += stockOrder.Number; } else { if (this.Number > stockOrder.Number) { this.Number -= stockOrder.Number; } else { if (this.Number == stockOrder.Number) { this.Number = 0; this.Status = PositionStatus.Closed; } else { throw new System.Exception("Cannot sell more stock than owned"); } } } }
public override void Initialise(StockSerie stockSerie, StockOrder lastBuyOrder, bool supportShortSelling) { base.Initialise(stockSerie, lastBuyOrder, supportShortSelling); this.oscSerie = null; this.trailStop = StockTrailStopManager.CreateTrailStop("TRAILHL(3)"); this.trailStop.ApplyTo(stockSerie); }
public static IStockStrategy CreateStrategy(string name, StockSerie stockSerie, StockOrder lastBuyOrder, bool supportShortSelling) { IStockStrategy strategy = null; try { if (name.StartsWith("@")) return CreateFilteredStrategy(name, stockSerie, lastBuyOrder, supportShortSelling); if (strategyList == null) { GetStrategyList(); } if (strategyList.Contains(name)) { StrategyManager sm = new StrategyManager(); strategy = (IStockStrategy) sm.GetType().Assembly.CreateInstance("StockAnalyzer.StockStrategyClasses." + name); strategy.Initialise(stockSerie, lastBuyOrder, supportShortSelling); } } catch (Exception ex) { throw new StockAnalyzerException("Failed to create strategy " + name, ex); } return strategy; }
public override void Initialise(StockSerie stockSerie, StockOrder lastBuyOrder, bool supportShortSelling) { base.Initialise(stockSerie, lastBuyOrder, supportShortSelling); this.SRTrailStop = stockSerie.GetTrailStop(trailName); this.adxDecorator = stockSerie.GetDecorator("DIV(1)", ((IStockIndicator)TriggerIndicator).Name); }
public OrderEditionDlg(string currentStockName, string[] portofolioList, StockOrder stockOrderTemplate) { InitializeComponent(); //Initialise stock name combo list StockDictionary stockDictionary = StockAnalyzerForm.MainFrame.StockDictionary; foreach (string stockName in stockDictionary.Keys) { this.stockNameCombo.Items.Add(stockName); } // Order Type change combo box string[] orderTypeNames = Enum.GetNames(typeof(StockOrder.OrderType)); this.orderTypeComboBox.Enabled = true; this.orderTypeComboBox.Items.Clear(); foreach (string name in orderTypeNames) { this.orderTypeComboBox.Items.Add(name); } this.orderTypeComboBox.SelectedIndex = 0; // UpDownState combo box string[] stateNames = Enum.GetNames(typeof(StockOrder.OrderStatus)); this.stateComboBox.Enabled = true; this.stateComboBox.Items.Clear(); foreach (string name in stateNames) { this.stateComboBox.Items.Add(name); } this.stateComboBox.SelectedIndex = 0; // Portofolio change if (portofolioList == null) { portofolioList = StockAnalyzerForm.MainFrame.StockPortofolioList.GetPortofolioNames(); } this.portofolioComboBox.Enabled = true; this.portofolioComboBox.Items.Clear(); foreach (string name in portofolioList) { this.portofolioComboBox.Items.Add(name); } this.portofolioComboBox.SelectedItem = this.portofolioComboBox.Items[0]; // Create default order if (stockOrderTemplate == null) { this.Order = StockOrder.CreateExecutedOrder(currentStockName, StockOrder.OrderType.BuyAtLimit, false, DateTime.Today, DateTime.Today, 0, 0.0f, 0.0f); } else { this.Order = stockOrderTemplate; } }
public override void Initialise(StockSerie stockSerie, StockOrder lastBuyOrder, bool supportShortSelling) { base.Initialise(stockSerie, lastBuyOrder, supportShortSelling); IStockIndicator indicator = TriggerIndicator as IStockIndicator; middleUpBandSerie = indicator.Series[1]; middleBandSerie = indicator.Series[2]; middleDownBandSerie = indicator.Series[3]; lowSerie = stockSerie.GetSerie(StockDataType.LOW); highSerie = stockSerie.GetSerie(StockDataType.HIGH); closeSerie = stockSerie.GetSerie(StockDataType.CLOSE); }
public StockPosition(StockOrder stockOrder) { if (stockOrder.State != StockOrder.OrderStatus.Executed) { throw new System.Exception("Cannot create a position from a not executed order"); } this.OpenDate = stockOrder.ExecutionDate; this.Number = stockOrder.Number; this.StockName = stockOrder.StockName; this.UnitCost = stockOrder.UnitCost; this.CloseDate = DateTime.MinValue; this.IsShortPosition = stockOrder.IsShortOrder; this.Status = PositionStatus.Opened; }
public override void Initialise(StockSerie stockSerie, StockOrder lastBuyOrder, bool supportShortSelling) { this.Serie = stockSerie; this.LastBuyOrder = lastBuyOrder; this.SupportShortSelling = supportShortSelling; if (StockDictionary.StockDictionarySingleton.ContainsKey("TB." + stockSerie.StockName)) { StockSerie breadthSerie = StockDictionary.StockDictionarySingleton["TB." + stockSerie.StockName]; if (breadthSerie.Initialise()) { IStockIndicator OSCIndicator = breadthSerie.GetIndicator(((IStockIndicator)TriggerIndicator).Name); if (OSCIndicator != null) { OSCSerie = OSCIndicator.Series[0]; } } } }
public OrderEditionDlg(StockOrder stockOrder) { InitializeComponent(); //Initialise stock name combo list this.stockNameCombo.Items.Add(stockOrder.StockName); this.stockNameCombo.SelectedItem = stockOrder.StockName; this.Order = stockOrder; // Disable the portofolio change this.portofolioComboBox.Enabled = false; this.parseOrderBtn.Enabled = false; this.orderText.Enabled = false; this.amountToInvestTextBox.Text = "1000"; }
public virtual void Initialise(StockSerie stockSerie, StockOrder lastBuyOrder, bool supportShortSelling) { this.Serie = stockSerie; this.LastBuyOrder = lastBuyOrder; this.SupportShortSelling = supportShortSelling; IStockViewableSeries triggerSerie = this.TriggerIndicator as IStockViewableSeries; if (triggerSerie != null) { if (stockSerie.HasVolume || !triggerSerie.RequiresVolumeData) { triggerSerie.ApplyTo(stockSerie); } else { throw new System.Exception("This serie has probably no volume"); } } }
public static StockFilteredStrategyBase CreateFilteredStrategy(string name, StockSerie stockSerie, StockOrder lastBuyOrder, bool supportShortSelling) { StockFilteredStrategyBase strategy = null; if (filteredStrategyList == null) { GetFilteredStrategyList(false); } if (filteredStrategyList.Contains(name)) { // Deserialize strategy file string fileName = Settings.Default.RootFolder + @"\FilteredStrategies\" + name.Replace("@", "") + ".xml"; using (FileStream fs = new FileStream(fileName, FileMode.Open)) { System.Xml.XmlReaderSettings settings = new System.Xml.XmlReaderSettings(); settings.IgnoreWhitespace = true; System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(fs, settings); XmlSerializer serializer = new XmlSerializer(typeof(StockFilteredStrategyBase)); strategy = (StockFilteredStrategyBase)serializer.Deserialize(xmlReader); strategy.Initialise(stockSerie, lastBuyOrder, supportShortSelling); } } return strategy; }
void shortMenu_Click(object sender, System.EventArgs e) { if (lastMouseIndex != -1 && this.openCurveType != null && this.dateSerie != null) { StockOrder newOrder = new StockOrder(); newOrder.State = StockOrder.OrderStatus.Executed; newOrder.StockName = this.serieName; newOrder.Type = StockOrder.OrderType.SellAtLimit; newOrder.IsShortOrder = true; newOrder.Value = this.openCurveType.DataSerie[lastMouseIndex]; newOrder.Number = 10; newOrder.CreationDate = this.dateSerie[lastMouseIndex]; newOrder.ExecutionDate = this.dateSerie[lastMouseIndex]; newOrder.ExpiryDate = this.dateSerie[lastMouseIndex].AddMonths(1); OrderEditionDlg dlg = new OrderEditionDlg(newOrder); if (dlg.ShowDialog() == DialogResult.OK) { StockAnalyzerForm.MainFrame.CurrentPortofolio.OrderList.Add(newOrder); this.ForegroundDirty = true; } } }
public override void Initialise(StockSerie stockSerie, StockOrder lastBuyOrder, bool supportShortSelling) { base.Initialise(stockSerie, lastBuyOrder, supportShortSelling); movingAverage.ApplyTo(stockSerie); }
private static int compareDate(StockOrder stockOrder1, StockOrder stockOrder2) { return stockOrder1.ExecutionDate.CompareTo(stockOrder2.ExecutionDate); }
public static StockOrder CreateBuyAtLimitStockOrder(string stockName, DateTime creationDate, DateTime expiryDate, float amountToInvest, float limit, StockDailyValue dailyValue, bool isShortOrder) { StockOrder stockOrder = new StockOrder(); stockOrder.Type = OrderType.BuyAtLimit; stockOrder.StockName = stockName; stockOrder.CreationDate = creationDate; stockOrder.AmountToInvest = amountToInvest; stockOrder.Limit = limit; stockOrder.State = OrderStatus.Pending; stockOrder.ExpiryDate = expiryDate; stockOrder.IsShortOrder = isShortOrder; if (limit > dailyValue.CLOSE) { StockLog.Write("Invalid limit in CreateBuyAtLimitStockOrder"); } return stockOrder; }
public static StockOrder CreateBuyTrailingStockOrder(string stockName, DateTime creationDate, DateTime expiryDate, float amountToInvest, float benchmark, float gapInPoints, StockDailyValue dailyValue) { StockOrder stockOrder = new StockOrder(); stockOrder.Type = OrderType.BuyTrailing; stockOrder.StockName = stockName; stockOrder.CreationDate = creationDate; stockOrder.AmountToInvest = amountToInvest; stockOrder.Benchmark = benchmark; stockOrder.GapInPoints = gapInPoints; stockOrder.State = OrderStatus.Pending; stockOrder.ExpiryDate = expiryDate; return stockOrder; }
public StockPosition AddToPosition(StockOrder stockOrder) { StockPosition stockPosition = null; if (this.StockName == stockOrder.StockName && !(this.IsShortOrder ^ stockOrder.IsShortOrder)) { stockPosition = StockPositionBase.CreatePosition(this); stockPosition.Add(stockOrder); } else { throw new System.ArithmeticException("Cannot add orders for different stock name"); } return stockPosition; }
public static StockOrder CreateSellAtThresholdStockOrder(string stockName, DateTime creationDate, DateTime expiryDate, int number, float threshold, StockDailyValue dailyValue, bool isShortOrder) { StockOrder stockOrder = new StockOrder(); stockOrder.Type = OrderType.SellAtThreshold; stockOrder.StockName = stockName; stockOrder.CreationDate = creationDate; stockOrder.Number = number; stockOrder.Threshold = threshold; stockOrder.State = OrderStatus.Pending; stockOrder.ExpiryDate = expiryDate; stockOrder.IsShortOrder = isShortOrder; if (threshold > dailyValue.CLOSE) // OK Due to dodgy short sell management { // throw new Exception("Invalid threshold"); } return stockOrder; }
public static StockOrder CreateSellTrailingStockOrder(string stockName, DateTime creationDate, DateTime expiryDate, int number, float benchmark, float gapInPoints, StockDailyValue dailyValue) { StockOrder stockOrder = new StockOrder(); stockOrder.Type = OrderType.SellTrailing; stockOrder.StockName = stockName; stockOrder.CreationDate = creationDate; stockOrder.Number = number; stockOrder.Benchmark = benchmark; stockOrder.GapInPoints = gapInPoints; stockOrder.State = OrderStatus.Pending; stockOrder.ExpiryDate = expiryDate; return stockOrder; }
public override StockOrder TryToSell(int index, ref float benchmark) { StockDailyValue dailyValue = this.Serie.Values.ElementAt(index); benchmark = dailyValue.CLOSE; #region Create Sell Order StockOrder stockOrder = null; // Check if we are in profit bool inProfit = false; float limit; if (LastBuyOrder.IsShortOrder) { if (LastBuyOrder.UnitCost > dailyValue.CLOSE) { inProfit = true; if (this.Serie.GetSerie(StockDataType.EMA3)[index] > dailyValue.CLOSE) { limit = this.Serie.GetSerie(StockDataType.EMA3)[index]; } else if (this.Serie.GetSerie(StockDataType.EMA6)[index] > dailyValue.CLOSE) { limit = this.Serie.GetSerie(StockDataType.EMA6)[index]; } else if (this.Serie.GetSerie(StockDataType.EMA12)[index] > dailyValue.CLOSE) { limit = this.Serie.GetSerie(StockDataType.EMA12)[index]; } else if (this.Serie.GetSerie(StockDataType.SAREX_FOLLOWER)[index] > dailyValue.CLOSE) { limit = this.Serie.GetSerie(StockDataType.SAREX_FOLLOWER)[index]; } else { limit = dailyValue.HIGH; } } else { limit = dailyValue.HIGH; } } else { if (LastBuyOrder.UnitCost < dailyValue.CLOSE) { inProfit = true; if (this.Serie.GetSerie(StockDataType.EMA3)[index] < dailyValue.CLOSE) { limit = this.Serie.GetSerie(StockDataType.EMA3)[index]; } else if (this.Serie.GetSerie(StockDataType.EMA6)[index] < dailyValue.CLOSE) { limit = this.Serie.GetSerie(StockDataType.EMA6)[index]; } else if (this.Serie.GetSerie(StockDataType.EMA12)[index] < dailyValue.CLOSE) { limit = this.Serie.GetSerie(StockDataType.EMA12)[index]; } else if (this.Serie.GetSerie(StockDataType.SAREX_FOLLOWER)[index] < dailyValue.CLOSE) { limit = this.Serie.GetSerie(StockDataType.SAREX_FOLLOWER)[index]; } else { limit = dailyValue.LOW; } } else { limit = dailyValue.LOW; } } // Review sell limit according to indicators float buySellRate = this.Serie.GetSerie(StockIndicatorType.BUY_SELL_RATE).Values[index]; if (LastBuyOrder.IsShortOrder) { if (inProfit || buySellRate > 0.0f) { stockOrder = StockOrder.CreateSellAtLimitStockOrder(dailyValue.NAME, dailyValue.DATE, dailyValue.DATE.AddDays(5), this.LastBuyOrder.Number, limit, dailyValue, true); } } else { if (inProfit || buySellRate < 0.0f) { stockOrder = StockOrder.CreateSellAtThresholdStockOrder(dailyValue.NAME, dailyValue.DATE, dailyValue.DATE.AddDays(5), this.LastBuyOrder.Number, limit, dailyValue, false); } } #endregion lastSellOrder = stockOrder; return stockOrder; }
public static StockOrder CreateSellAtMarketOpenStockOrder(string stockName, DateTime creationDate, DateTime expiryDate, int number, bool isShortOrder) { StockOrder stockOrder = new StockOrder(); stockOrder.Type = OrderType.SellAtMarketOpen; stockOrder.StockName = stockName; stockOrder.CreationDate = creationDate; stockOrder.Number = number; stockOrder.State = OrderStatus.Pending; stockOrder.ExpiryDate = expiryDate; stockOrder.IsShortOrder = isShortOrder; return stockOrder; }
public static StockOrder CreateBuyAtMarketOpenStockOrder(string stockName, DateTime creationDate, DateTime expiryDate, float amountToInvest, bool isShortOrder) { StockOrder stockOrder = new StockOrder(); stockOrder.Type = OrderType.BuyAtMarketOpen; stockOrder.StockName = stockName; stockOrder.CreationDate = creationDate; stockOrder.AmountToInvest = amountToInvest; stockOrder.State = OrderStatus.Pending; stockOrder.ExpiryDate = expiryDate; stockOrder.IsShortOrder = isShortOrder; return stockOrder; }
public static StockOrder CreateFromCAPCA(string line, ref string errorMsg) { StockOrder stockOrder = new StockOrder(); try { string[] fields = line.Split('|'); // Find order date stockOrder.ExecutionDate = DateTime.Parse(fields[0]); stockOrder.CreationDate = DateTime.Parse(fields[0]); // Find order type if (fields[1] == "Buy") { stockOrder.Type = StockOrder.OrderType.BuyAtLimit; } else if (fields[1] == "Sell") { stockOrder.Type = StockOrder.OrderType.SellAtLimit; } else { errorMsg = "Invalid order type: " + fields[1]; return null; } // Find number of stocks stockOrder.Number = int.Parse(fields[2].Replace(" ", "")); // Find Stock Name string stockName = fields[3].Substring(fields[3].IndexOf(' ') + 1); int endIndex = stockName.IndexOfAny(new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-' }) - 1; stockOrder.StockName = stockName.Substring(0, endIndex); // Find stock unit value stockOrder.Value = float.Parse(fields[4], StockAnalyzerApp.Global.EnglishCulture); // There'sno fee in the report :-( stockOrder.Fee = 0.0f; } catch (System.Exception e) { errorMsg = e.Message; stockOrder = null; } return stockOrder; }
public static StockOrder CreateExecutedOrder(int id, string stockName, OrderType type, bool isShortOrder, DateTime creationDate, DateTime executionDate, int number, float value, float fee) { StockOrder stockOrder = new StockOrder(id); stockOrder.StockName = stockName; stockOrder.Type = type; stockOrder.CreationDate = creationDate; stockOrder.ExecutionDate = executionDate; stockOrder.Number = number; stockOrder.ExecutedNumber = number; stockOrder.Value = value; stockOrder.Fee = fee; stockOrder.State = OrderStatus.Executed; stockOrder.IsShortOrder = isShortOrder; return stockOrder; }
private PointF GetScreenPointFromOrder(StockOrder stockOrder) { PointF valuePoint2D = PointF.Empty; DateTime orderDate = serieName.StartsWith("INT_") ? stockOrder.ExecutionDate : stockOrder.ExecutionDate.Date; valuePoint2D.X = this.IndexOf(orderDate); valuePoint2D.Y = stockOrder.UnitCost; return this.GetScreenPointFromValuePoint(valuePoint2D); }
/// <summary> /// Return a new order matching new position. THis method should be rewritten using StockPositions. /// </summary> /// <param name="stockOrder"></param> /// <returns></returns> public StockOrder Add(StockOrder stockOrder) { // #### Need to be reviewed due to the Buy at market OPEN or CLOSE difference. if (this.StockName == stockOrder.StockName) { int newNumber = 0; float newValue = 0; if (this.IsBuyOrder()) { if (stockOrder.IsBuyOrder()) { // Add new order to current position newNumber = this.Number + stockOrder.Number; newValue = (this.Value * this.Number + stockOrder.Value * stockOrder.Number) / (float)newNumber; return CreateExecutedOrder(this.StockName, this.Type, false, this.CreationDate, stockOrder.ExecutionDate, newNumber, newValue, this.Fee + stockOrder.Fee); } else // Sell order { newNumber = this.Number - stockOrder.Number; //if (newNumber == 0) //{ // // Everything is sold. // return null; //} //else //{ // Keep same value as initial order when selling it partially. newValue = this.Value; return CreateExecutedOrder(this.StockName, this.Type, false, this.CreationDate, stockOrder.ExecutionDate, newNumber, newValue, this.Fee + stockOrder.Fee); //} } } else // Sell order { if (stockOrder.IsBuyOrder()) { newNumber = stockOrder.Number - this.Number; if (newNumber == 0) { return null; } else { // Keep same value as initial order when covering it partially. newValue = (stockOrder.Value * stockOrder.Number - this.Value * this.Number) / (float)newNumber; return CreateExecutedOrder(this.StockName, this.Type, false, this.CreationDate, stockOrder.ExecutionDate, newNumber, newValue, this.Fee + stockOrder.Fee); } } else // Sell order { // TODO test newNumber = this.Number + stockOrder.Number; newValue = (this.Value * this.Number + stockOrder.Value * stockOrder.Number) / (float)newNumber; return CreateExecutedOrder(this.StockName, this.Type, false, this.CreationDate, stockOrder.ExecutionDate, newNumber, newValue, this.Fee + stockOrder.Fee); } } } else { throw new System.ArithmeticException("Cannot add orders for different stock name"); } }
public static StockOrder CreateFromStockBrokers(string line, ref string errorMsg) { StockOrder stockOrder = new StockOrder(); try { string[] fields = line.Split('|'); // Find order date stockOrder.ExecutionDate = DateTime.Parse(fields[0]); stockOrder.CreationDate = DateTime.Parse(fields[0]); // Find order type if (fields[1] == "Buy") { stockOrder.Type = StockOrder.OrderType.BuyAtLimit; } else if (fields[1] == "Sell") { stockOrder.Type = StockOrder.OrderType.SellAtLimit; } else { errorMsg = "Invalid order type: " + fields[1]; return null; } // Find Stock Name stockOrder.StockName = fields[2]; // Find number of stocks stockOrder.Number = int.Parse(fields[4]); // Find stock unit value stockOrder.Value = float.Parse(fields[5], StockAnalyzerApp.Global.EnglishCulture) / 100.0f; // Find Total order value and fee float totalOrderValue = 0.0f; if (stockOrder.IsBuyOrder()) { totalOrderValue = float.Parse(fields[6], StockAnalyzerApp.Global.EnglishCulture); stockOrder.Fee = totalOrderValue - stockOrder.Number * stockOrder.Value; } else { totalOrderValue = float.Parse(fields[7], StockAnalyzerApp.Global.EnglishCulture); stockOrder.Fee = stockOrder.Number * stockOrder.Value - totalOrderValue; } } catch (System.Exception e) { errorMsg = e.Message; stockOrder = null; } return stockOrder; }
public static StockOrder CreateBuyAtThresholdStockOrder(string stockName, DateTime creationDate, DateTime expiryDate, float amountToInvest, float threshold, StockDailyValue dailyValue, bool isShortOrder) { StockOrder stockOrder = new StockOrder(); stockOrder.Type = OrderType.BuyAtThreshold; stockOrder.StockName = stockName; stockOrder.CreationDate = creationDate; stockOrder.AmountToInvest = amountToInvest; stockOrder.Threshold = threshold; stockOrder.State = OrderStatus.Pending; stockOrder.ExpiryDate = expiryDate; stockOrder.IsShortOrder = isShortOrder; if (threshold < dailyValue.CLOSE) { throw new Exception("Invalid threshold"); // OK Due to dodgy shrt sell management } return stockOrder; }
public StockOrderViewModel(StockOrder stockOrder) { this.order = stockOrder; }
public static StockOrder CreateSellAtLimitStockOrder(string stockName, DateTime creationDate, DateTime expiryDate, int number, float limit, StockDailyValue dailyValue, bool isShortOrder) { StockOrder stockOrder = new StockOrder(); stockOrder.Type = OrderType.SellAtLimit; stockOrder.StockName = stockName; stockOrder.CreationDate = creationDate; stockOrder.Number = number; stockOrder.Limit = limit; stockOrder.State = OrderStatus.Pending; stockOrder.ExpiryDate = expiryDate; stockOrder.IsShortOrder = isShortOrder; if (limit < dailyValue.CLOSE) { StockLog.Write("Invalid limit in CreateSellAtLimitStockOrder"); } return stockOrder; }