public StockFilteredStrategyBase(IStockEvent filterEvent, IStockEvent triggerEvent, string buyTriggerEventName, string shortTriggerEventName, string okToBuyFilterEventName, string okToShortFilterEventName)
 {
     TriggerIndicator = triggerEvent;
      FilterIndicator = filterEvent;
      BuyTriggerEventName = buyTriggerEventName;
      ShortTriggerEventName = shortTriggerEventName;
      OkToBuyFilterEventName = okToBuyFilterEventName;
      OkToShortFilterEventName = okToShortFilterEventName;
 }
예제 #2
0
 private bool TryExecute(IStockEvent stockEvent)
 {
     try
     {
         stockEvent.Apply();
         return(true);
     }
     catch (Exception e)
     {
         _errorsLogger.LogMessage(e.Message);
         return(false);
     }
 }
예제 #3
0
파일: stock_intf.cs 프로젝트: hol353/ApsimX
        /// <summary>
        /// The main stock management function that handles a number of events.
        /// </summary>
        /// <param name="Model"></param>
        /// <param name="stockEvent">The event parameters</param>
        /// <param name="dtToday"></param>
        /// <param name="fLatitude"></param>
        public void doStockManagement(TStockList Model, IStockEvent stockEvent, int dtToday = 0, double fLatitude = -35.0)
        {
            TCohortsInfo CohortsInfo = new TCohortsInfo();
            TPurchaseInfo PurchaseInfo = new TPurchaseInfo();
            List<string> sClosed;
            string sParam;
            int iParam1;
            int iParam3;
            double fValue;
            int iTag;
            int iGroups;

            if (stockEvent != null)
            {
                if (stockEvent.GetType() == typeof(TStockAdd))          // add_animals
                {
                    TStockAdd stockInfo = (TStockAdd)stockEvent;
                    CohortsInfo.sGenotype = stockInfo.genotype;
                    CohortsInfo.iNumber = Math.Max(0, stockInfo.number);
                    if (!ParseRepro(stockInfo.sex, ref CohortsInfo.ReproClass))
                        throw new Exception("Event ADD does not support sex='" + stockInfo.sex + "'");
                    if (dtToday > 0)
                        CohortsInfo.iAgeOffsetDays = DaysFromDOY365(stockInfo.birth_day, dtToday);
                    else
                        CohortsInfo.iAgeOffsetDays = 0;
                    CohortsInfo.iMinYears = stockInfo.min_years;
                    CohortsInfo.iMaxYears = stockInfo.max_years;
                    CohortsInfo.fMeanLiveWt = stockInfo.mean_weight;
                    CohortsInfo.fCondScore = stockInfo.cond_score;
                    CohortsInfo.fMeanGFW = stockInfo.mean_fleece_wt;
                    if (dtToday > 0)
                        CohortsInfo.iFleeceDays = DaysFromDOY365(stockInfo.shear_day, dtToday);
                    else
                        CohortsInfo.iFleeceDays = 0;
                    CohortsInfo.sMatedTo = stockInfo.mated_to;
                    CohortsInfo.iDaysPreg = stockInfo.pregnant;
                    CohortsInfo.fFoetuses = stockInfo.foetuses;
                    CohortsInfo.iDaysLact = stockInfo.lactating;
                    CohortsInfo.fOffspring = stockInfo.offspring;
                    CohortsInfo.fOffspringWt = stockInfo.young_wt;
                    CohortsInfo.fOffspringCS = stockInfo.young_cond_score;
                    CohortsInfo.fLambGFW = stockInfo.young_fleece_wt;

                    if (CohortsInfo.iNumber > 0)
                        Model.AddCohorts(CohortsInfo, 1 + DaysFromDOY365(1, dtToday), fLatitude, null);
                }

                else if (stockEvent.GetType() == typeof(TStockBuy))
                {
                    TStockBuy stockInfo = (TStockBuy)stockEvent;
                    PurchaseInfo.sGenotype = stockInfo.genotype;
                    PurchaseInfo.Number = Math.Max(0, stockInfo.number);
                    if (!ParseRepro(stockInfo.sex, ref PurchaseInfo.Repro))
                        throw new Exception("Event BUY does not support sex='" + stockInfo.sex + "'");
                    PurchaseInfo.AgeDays = Convert.ToInt32(Math.Round(MONTH2DAY * stockInfo.age));  // Age in months
                    PurchaseInfo.LiveWt = stockInfo.weight;
                    PurchaseInfo.GFW = stockInfo.fleece_wt;
                    PurchaseInfo.fCondScore = stockInfo.cond_score;
                    PurchaseInfo.sMatedTo = stockInfo.mated_to;
                    PurchaseInfo.Preg = stockInfo.pregnant;
                    PurchaseInfo.Lact = stockInfo.lactating;
                    PurchaseInfo.NYoung = stockInfo.no_young;
                    if ((PurchaseInfo.Preg > 0) || (PurchaseInfo.Lact > 0))
                        PurchaseInfo.NYoung = Math.Max(1, PurchaseInfo.NYoung);
                    PurchaseInfo.YoungWt = stockInfo.young_wt;
                    if ((PurchaseInfo.Lact == 0) || (PurchaseInfo.YoungWt == 0.0))                              // Can't use MISSING as default owing
                        PurchaseInfo.YoungWt = StdMath.DMISSING;                                                //   to double-to-single conversion
                    PurchaseInfo.YoungGFW = stockInfo.young_fleece_wt;
                    iTag = stockInfo.usetag;

                    if (PurchaseInfo.Number > 0)
                    {
                        Model.Buy(PurchaseInfo);
                        if (iTag > 0)
                            Model.setTag(Model.Count(), iTag);
                    }
                } //_ buy _

                //sell a number from a group of animals
                else if (stockEvent.GetType() == typeof(TStockSell))
                {
                    TStockSell stockInfo = (TStockSell)stockEvent;
                    Model.Sell(stockInfo.group, stockInfo.number);
                }
                //sell a number of animals tagged with a specific tag
                else if (stockEvent.GetType() == typeof(TStockSellTag))
                {
                    TStockSellTag stockInfo = (TStockSellTag)stockEvent;
                    Model.SellTag(stockInfo.tag, stockInfo.number);
                }

                else if (stockEvent.GetType() == typeof(TStockShear))
                {
                    TStockShear stockInfo = (TStockShear)stockEvent;
                    sParam = stockInfo.sub_group.ToLower();
                    Model.Shear(stockInfo.group, ((sParam == "adults") || (sParam == "both") || (sParam == "")), ((sParam == "lambs") || (sParam == "both")));
                }

                else if (stockEvent.GetType() == typeof(TStockMove))
                {
                    TStockMove stockInfo = (TStockMove)stockEvent;
                    iParam1 = stockInfo.group;
                    if ((iParam1 >= 1) && (iParam1 <= Model.Count()))
                        Model.setInPadd(iParam1, stockInfo.paddock);
                    else
                        throw new Exception("Invalid group number in MOVE event");
                }

                else if (stockEvent.GetType() == typeof(TStockJoin))
                {
                    TStockJoin stockInfo = (TStockJoin)stockEvent;
                    Model.Join(stockInfo.group, stockInfo.mate_to, stockInfo.mate_days);
                }
                else if (stockEvent.GetType() == typeof(TStockCastrate))
                {
                    TStockCastrate stockInfo = (TStockCastrate)stockEvent;
                    Model.Castrate(stockInfo.group, stockInfo.number);
                }
                else if (stockEvent.GetType() == typeof(TStockWean))
                {
                    TStockWean stockInfo = (TStockWean)stockEvent;
                    iParam1 = stockInfo.group;
                    sParam = stockInfo.sex.ToLower();
                    iParam3 = stockInfo.number;

                    if (sParam == "males")
                        Model.Wean(iParam1, iParam3, false, true);
                    else if (sParam == "females")
                        Model.Wean(iParam1, iParam3, true, false);
                    else if ((sParam == "all") || (sParam == ""))
                        Model.Wean(iParam1, iParam3, true, true);
                    else
                        throw new Exception("Invalid offspring type \"" + sParam + "\" in WEAN event");
                }

                else if (stockEvent.GetType() == typeof(TStockDryoff))
                {
                    TStockDryoff stockInfo = (TStockDryoff)stockEvent;
                    Model.DryOff(stockInfo.group, stockInfo.number);
                }
                //split off the requested animals from all groups
                else if (stockEvent.GetType() == typeof(TStockSplitAll))
                {
                    TStockSplitAll stockInfo = (TStockSplitAll)stockEvent;
                    iGroups = Model.Count(); //get pre-split count of groups
                    for (iParam1 = 1; iParam1 <= iGroups; iParam1++)
                    {
                        sParam = stockInfo.type.ToLower();
                        fValue = stockInfo.value;
                        iTag = stockInfo.othertag;

                        if (sParam == "age")
                            Model.SplitAge(iParam1, Convert.ToInt32(Math.Round(fValue)));
                        else if (sParam == "weight")
                            Model.SplitWeight(iParam1, fValue);
                        else if (sParam == "young")
                            Model.SplitYoung(iParam1);
                        else if (sParam == "number")
                            Model.Split(iParam1, Convert.ToInt32(Math.Round(fValue)));
                        else
                            throw new Exception("Stock: invalid keyword (" + sParam + ") in \"split\" event");
                        if ((iTag > 0) && (Model.Count() > iGroups))     //if a tag for any new group is given
                            Model.setTag(Model.Count(), iTag);
                    }
                }

                //split off the requested animals from one group
                else if (stockEvent.GetType() == typeof(TStockSplit))
                {
                    TStockSplit stockInfo = (TStockSplit)stockEvent;
                    iGroups = Model.Count(); //get pre-split count of groups
                    iParam1 = stockInfo.group;
                    sParam = stockInfo.type.ToLower();
                    fValue = stockInfo.value;
                    iTag = stockInfo.othertag;

                    if ((iParam1 < 1) && (iParam1 > Model.Count()))
                        throw new Exception("Invalid group number in SPLIT event");
                    else if (sParam == "age")
                        Model.SplitAge(iParam1, Convert.ToInt32(Math.Round(fValue)));
                    else if (sParam == "weight")
                        Model.SplitWeight(iParam1, fValue);
                    else if (sParam == "young")
                        Model.SplitYoung(iParam1);
                    else if (sParam == "number")
                        Model.Split(iParam1, Convert.ToInt32(Math.Round(fValue)));
                    else
                        throw new Exception("Stock: invalid keyword (" + sParam + ") in \"split\" event");
                    if ((iTag > 0) && (Model.Count() > iGroups))     //if a tag for the new group is given
                        Model.setTag(Model.Count(), iTag);
                }

                else if (stockEvent.GetType() == typeof(TStockTag))
                {
                    TStockTag stockInfo = (TStockTag)stockEvent;
                    iParam1 = stockInfo.group;
                    if ((iParam1 >= 1) && (iParam1 <= Model.Count()))
                        Model.setTag(iParam1, stockInfo.value);
                    else
                        throw new Exception("Invalid group number in TAG event");
                }

                else if (stockEvent.GetType() == typeof(TStockSort))
                {
                    Model.Sort();
                }

                else if (stockEvent.GetType() == typeof(TStockPrioritise))
                {
                    TStockPrioritise stockInfo = (TStockPrioritise)stockEvent;
                    iParam1 = stockInfo.group;
                    if ((iParam1 >= 1) && (iParam1 <= Model.Count()))
                        Model.setPriority(iParam1, stockInfo.value);
                    else
                        throw new Exception("Invalid group number in PRIORITISE event");
                }

                else if (stockEvent.GetType() == typeof(TStockDraft))
                {
                    TStockDraft stockInfo = (TStockDraft)stockEvent;
                    sClosed = new List<string>(stockInfo.closed);

                    Model.Draft(sClosed);
                }

                else
                    throw new Exception("Event not recognised in STOCK");
            }
        }
        private void triggerIndicatorTextBox_TextChanged(object sender, EventArgs e)
        {
            if (triggerIndicatorTextBox.Text == string.Empty) return;
            if (previousTriggerIndicator == triggerIndicatorTextBox.Text) return;
            previousTriggerIndicator = triggerIndicatorTextBox.Text;

            IStockViewableSeries triggerIndicator = null;
            if (StockIndicatorManager.Supports(this.triggerIndicatorTextBox.Text))
            {
                triggerIndicator = StockIndicatorManager.CreateIndicator(this.triggerIndicatorTextBox.Text);
            }
            else
            {
                if (StockTrailStopManager.Supports(this.triggerIndicatorTextBox.Text))
                {
                    triggerIndicator = StockTrailStopManager.CreateTrailStop(this.triggerIndicatorTextBox.Text);
                }
                else
                {
                    if (StockPaintBarManager.Supports(this.triggerIndicatorTextBox.Text))
                    {
                        triggerIndicator = StockPaintBarManager.CreatePaintBar(this.triggerIndicatorTextBox.Text);
                    }
                    else
                    {
                        if (StockDecoratorManager.Supports(this.triggerIndicatorTextBox.Text))
                        {
                            string[] fields = this.triggerIndicatorTextBox.Text.Split('|');
                            if (fields.Length == 2)
                            {
                                triggerIndicator = StockDecoratorManager.CreateDecorator(fields[0], fields[1]);
                            }
                        }
                    }
                }
            }
            if (triggerIndicator != null)
            {
                triggerEvents = triggerIndicator as IStockEvent;

                buyTriggerComboBox.Items.Clear();
                buyTriggerComboBox.Items.AddRange(triggerEvents.EventNames.Cast<object>().ToArray());
                if (buyTriggerComboBox.Items.Contains(buyTriggerComboBox.Text))
                {
                    buyTriggerComboBox.SelectedItem = buyTriggerComboBox.Text;
                }
                else
                {
                    buyTriggerComboBox.SelectedIndex = 0;
                }

                shortTriggerComboBox.Items.Clear();
                shortTriggerComboBox.Items.AddRange(triggerEvents.EventNames.Cast<object>().ToArray());
                if (shortTriggerComboBox.Items.Contains(shortTriggerComboBox.Text))
                {
                    shortTriggerComboBox.SelectedItem = shortTriggerComboBox.Text;
                }
                else
                {
                    shortTriggerComboBox.SelectedIndex = 0;
                }
            }
            else
            {
                MessageBox.Show("Cannot create indicator1Name, check syntax please");
            }
        }
        public virtual void Initialise(StockSerie stockSerie, StockOrder lastBuyOrder, bool supportShortSelling)
        {
            this.Serie = stockSerie;
             this.LastBuyOrder = lastBuyOrder;
             this.SupportShortSelling = supportShortSelling;

             // Initialise trigger indicator
             IStockViewableSeries triggerSerie = this.TriggerIndicator as IStockViewableSeries;
             if (triggerSerie != null)
             {
            if (stockSerie.HasVolume || !triggerSerie.RequiresVolumeData)
            {
               this.triggerIndicator = stockSerie.GetStockEvents(triggerSerie);
            }
            else
            {
               throw new System.Exception("This serie has no volume information but is required for " + triggerSerie.Name);
            }
            try
            {
               BuyTriggerEventIndex = this.TriggerIndicator.EventNames.ToList().IndexOf(BuyTriggerEventName);
            }
            catch
            {
               throw new System.Exception("This indicator has no triggering event" + triggerSerie.Name + "(" + OkToBuyFilterEventName + ")");
            }
            try
            {
               ShortTriggerEventIndex = this.TriggerIndicator.EventNames.ToList().IndexOf(ShortTriggerEventName);
            }
            catch
            {
               throw new System.Exception("This indicator has no triggering event" + triggerSerie.Name + "(" + OkToShortFilterEventName + ")");
            }
             }

             // Initialise filter indicator
             IStockViewableSeries filterSerie = this.FilterIndicator as IStockViewableSeries;
             if (filterSerie != null)
             {
            if (stockSerie.HasVolume || !filterSerie.RequiresVolumeData)
            {
               this.filterIndicator = stockSerie.GetStockEvents(filterSerie);
            }
            else
            {
               throw new System.Exception("This serie has no volume information but is required for " + filterSerie.Name);
            }

            // Get event indexes
            try
            {
               OkToBuyFilterEventIndex = this.FilterIndicator.EventNames.ToList().IndexOf(OkToBuyFilterEventName);
            }
            catch
            {
               throw new System.Exception("This indicator has no filtering event" + filterSerie.Name + "(" + OkToBuyFilterEventName + ")");
            }
            try
            {

               OkToShortFilterEventIndex = this.FilterIndicator.EventNames.ToList().IndexOf(OkToShortFilterEventName);
            }
            catch
            {
               throw new System.Exception("This indicator has no filtering event" + filterSerie.Name + "(" + OkToShortFilterEventName + ")");
            }
             }
        }