public PlotCollectionSet GetLastData(int nLookahead = 0, bool bRemove = false) { if (m_data.Count == 0) { return(null); } if (nLookahead > 0 && bRemove) { throw new Exception("Removing data is not supported when retrieving data with a lookahead."); } PlotCollectionSet lastData = new PlotCollectionSet(); List <PlotCollection> rgPlots = new List <PlotCollection>(); for (int i = 0; i < m_data.Count; i++) { PlotCollectionSet dataFrame = m_data[i]; if (dataFrame.Count == 0) { return(null); } PlotCollection plots = new PlotCollection("Frame " + i.ToString()); for (int j = 0; j < dataFrame.Count; j++) { PlotCollection framePlots = dataFrame[j]; if (framePlots.Count == 0) { return(null); } Plot last = framePlots[framePlots.Count - (1 + nLookahead)]; if (last.Name == null) { last.Name = framePlots.Name; } plots.Add(last); if (bRemove) { framePlots.RemoveAt(framePlots.Count - 1); } } lastData.Add(plots); } if (bRemove) { m_output = m_surface.BuildGraph(m_config, m_data); SimpleGraphingControl_Resize(this, EventArgs.Empty); ScrollToEnd(false); } return(lastData); }
private void lineToolStripMenuItem_Click(object sender, EventArgs e) { timerData.Enabled = false; toolStrip1.Visible = false; List <PlotCollectionSet> rgSet = new List <PlotCollectionSet>(); int nCount = m_nDataCount; for (int i = 0; i < 4; i++) { PlotCollectionSet set = new PlotCollectionSet(); PlotCollection plots; plots = new PlotCollection("plot_1 - " + i.ToString()); for (int j = 0; j < nCount; j++) { plots.Add(new Plot(j, j * Math.Sin(j))); } set.Add(plots); plots = new PlotCollection("plot_2 - " + i.ToString()); for (int j = 0; j < nCount; j++) { plots.Add(new Plot(j, j * Math.Sin(j) * Math.Cos(j))); } set.Add(plots); rgSet.Add(set); } configureLineCharts(); updateGraph(rgSet); }
private PlotCollection saveData(string strName, PlotCollection price, Histogram rgHistogram, List <Tuple <float, float, int> > rgTopRanges, float fTop, float fBottom) { PlotCollection col = new PlotCollection(strName); col.Add(rgHistogram.Count); for (int i = 0; i < rgHistogram.Count; i++) { col.Add(rgHistogram[i].ToList()); } col.Add(rgTopRanges.Count); for (int i = 0; i < rgTopRanges.Count; i++) { List <double> rgdf = new List <double>(); rgdf.Add(rgTopRanges[i].Item1); rgdf.Add(rgTopRanges[i].Item2); rgdf.Add(rgTopRanges[i].Item3); col.Add(rgdf); } col.Add(price.AbsoluteMinYVal); col.Add(price.AbsoluteMinYVal); col.Add(fTop); col.Add(fBottom); return(col); }
public EmaData Pre(PlotCollectionSet dataset, int nDataIdx) { PlotCollection dataSrc = dataset[nDataIdx]; PlotCollection dataDst = new PlotCollection(dataSrc.Name + " EMA" + m_config.Interval.ToString()); return(new EmaData(dataSrc, dataDst, m_config.Interval)); }
public static Image QuickRender(PlotCollection plots, int nWidth = -1, int nHeight = -1, bool bConvertToEastern = false, ConfigurationAxis.VALUE_RESOLUTION?timeResolution = null, string strCfgXmlFile = null, bool bIncludeTitle = true, List <ConfigurationTargetLine> rgTargetLines = null, bool bUseTimeResolutionForValueType = false) { PlotCollectionSet set = new PlotCollectionSet(); set.Add(plots); return(QuickRender(set, nWidth, nHeight, bConvertToEastern, timeResolution, strCfgXmlFile, bIncludeTitle, rgTargetLines, bUseTimeResolutionForValueType)); }
public PlotCollectionSet GetData(PlotCollectionSet dataset, int nDataIdx, int nLookahead, Guid?guid = null, bool bAddToParams = false) { PlotCollection data = dataset[nDataIdx]; List <PlotCollection> rgPlots = new List <PlotCollection>(); PlotCollection plotHigh1 = getHighPoints(data, 1, nLookahead); PlotCollection plotLow1 = getLowPoints(data, 1, nLookahead); PlotCollection plotHigh2 = getHighPoints(plotHigh1, 2, nLookahead); PlotCollection plotLow2 = getLowPoints(plotLow1, 2, nLookahead); PlotCollection plotHigh3 = getHighPoints(plotHigh2, 3, nLookahead); PlotCollection plotLow3 = getLowPoints(plotLow2, 3, nLookahead); if (bAddToParams) { data = dataset[nDataIdx]; for (int i = 0; i < data.Count; i++) { if (plotLow1 != null && plotLow1[i].Active) { data[i].SetParameter(plotLow1.Name, plotLow1[i].Y); } if (plotLow2 != null && plotLow2[i].Active) { data[i].SetParameter(plotLow2.Name, plotLow2[i].Y); } if (plotLow3 != null && plotLow3[i].Active) { data[i].SetParameter(plotLow3.Name, plotLow3[i].Y); } if (plotHigh1 != null && plotHigh1[i].Active) { data[i].SetParameter(plotHigh1.Name, plotHigh1[i].Y); } if (plotHigh2 != null && plotHigh2[i].Active) { data[i].SetParameter(plotHigh2.Name, plotHigh2[i].Y); } if (plotHigh3 != null && plotHigh3[i].Active) { data[i].SetParameter(plotHigh3.Name, plotHigh3[i].Y); } } } rgPlots.Add(plotLow1); rgPlots.Add(plotHigh1); rgPlots.Add(plotLow2); rgPlots.Add(plotHigh2); rgPlots.Add(plotLow3); rgPlots.Add(plotHigh3); return(new PlotCollectionSet(rgPlots)); }
public RsiData(PlotCollection src, PlotCollection dst, uint nInterval) { m_src = src; m_dst = dst; m_nCount = 0; m_nInterval = (int)nInterval; m_dfRsi = 0; }
public SmaData(PlotCollection src, PlotCollection dst, uint nInterval) { m_src = src; m_dst = dst; m_nCount = 0; m_dfSma = 0; m_dfInc = 1.0 / nInterval; }
public EmaData(PlotCollection src, PlotCollection dst, uint nInterval) { m_src = src; m_dst = dst; m_dfEma = 0; m_dfTotal = 0; m_dfMult = 2.0 / (nInterval + 1); }
public void Render(Graphics g, PlotCollectionSet dataset, int nLookahead) { PlotCollection plots = dataset[m_config.DataIndexOnRender]; List <int> rgX = m_gx.TickPositions; int nStartIdx = m_gx.StartPosition; for (int i = 0; i < rgX.Count; i++) { int nIdx = nStartIdx + i; if (nIdx < plots.Count) { Plot plot = plots[nIdx]; float fX = rgX[i]; if (plot.Active && plot.Count.HasValue) { float fVol = plot.Count.Value; float fOpen = (float)plot.Y_values[0]; float fClose = (float)plot.Y_values[3]; bool bPositive = (fClose > fOpen) ? true : false; Color clrFill = (bPositive) ? Color.FromArgb(128, Color.Green) : Color.FromArgb(128, Color.Maroon); Color clrLine = Color.FromArgb(128, Color.Black); float fHspace = m_gx.Configuration.PlotSpacing / 2; float fX1 = fX - fHspace; float fX2 = fX + fHspace; float fWid = m_gx.Configuration.PlotSpacing; float fTop = m_gy.ScaleValue(fVol, true); float fBottom = m_gy.ScaleValue(0.0, true); float fHt = Math.Abs(fBottom - fTop); if (!m_rgPens.ContainsKey(clrLine)) { m_rgPens.Add(clrLine, new Pen(clrLine, 1.0f)); } if (!m_rgBrushes.ContainsKey(clrFill)) { m_rgBrushes.Add(clrFill, new SolidBrush(clrFill)); } Pen pLine = m_rgPens[clrLine]; Brush brFill = m_rgBrushes[clrFill]; g.FillRectangle(brFill, fX1, fTop, fWid, fHt); g.DrawRectangle(pLine, fX1, fTop, fWid, fHt); if (plot.Clipped) { g.DrawLine(Pens.Red, fX1, fTop, fX1 + fWid, fTop); g.DrawLine(Pens.Red, fX1, fTop + 1, fX1 + fWid, fTop + 1); g.DrawLine(Pens.Red, fX1, fTop + 2, fX1 + fWid, fTop + 2); } } } } }
private void stepNext() { try { if (m_rgLastData.Count > 0) { PlotCollectionSet lastData1 = m_rgLastData[m_rgLastData.Count - 1]; m_rgLastData.RemoveAt(m_rgLastData.Count - 1); simpleGraphingControl1.AddData(lastData1, true); return; } PlotCollectionSet newData = new PlotCollectionSet(); PlotCollectionSet lastData = simpleGraphingControl1.GetLastData(); if (lastData == null) { return; } for (int i = 0; i < lastData.Count; i++) { PlotCollection frameData = lastData[i]; PlotCollection frameNewData = new PlotCollection(frameData.Name); for (int j = 0; j < frameData.Count; j++) { double dfTime = frameData[j].X; DateTime dtStart = DateTime.FromFileTime((long)dfTime); dtStart += TimeSpan.FromDays(1); dfTime = dtStart.ToFileTime(); double dfVal = frameData[j].Y + (-1 + (2 * m_random.NextDouble())); double dfO = dfVal; double dfC = dfVal + (-1 + (2 * m_random.NextDouble())); double dfH = Math.Max(dfO, dfC) + (Math.Abs(dfC - dfO) * m_random.NextDouble()); double dfL = Math.Min(dfO, dfC) - (Math.Abs(dfC - dfO) * m_random.NextDouble()); List <double> rgdfVal = new List <double>() { dfO, dfH, dfL, dfC }; Plot p = new Plot(dfTime, rgdfVal); p.Action1Active = enableActionStripMenuItem.Checked; p.Action2Active = enableActionStripMenuItem.Checked; frameNewData.Add(p); } newData.Add(frameNewData); } simpleGraphingControl1.AddData(newData, true, true); } finally { } }
public double Process(SmaData data, int i, MinMax minmax = null, int nLookahead = 0, bool bAddToParams = false) { bool bActive = data.SrcData[i].Active; PlotCollection dataSrc = data.SrcData; PlotCollection dataDst = data.DstData; double dfInc = data.Increment; if (bActive) { if (data.Count < m_config.Interval) { data.SMA += dataSrc[i].Y * dfInc; if (dataDst != null) { dataDst.Add(dataSrc[i].X, dataSrc[i].Y, false, dataSrc[i].Index); } } else { if (i < dataSrc.Count - nLookahead) { data.SMA = (data.SMA * (1 - dfInc)) + dataSrc[i].Y * dfInc; } if (dataDst != null) { dataDst.Add(dataSrc[i].X, data.SMA, true, dataSrc[i].Index); } if (bAddToParams) { dataSrc[i].SetParameter(dataDst.Name, (float)data.SMA); } } if (minmax != null) { minmax.Add(data.SMA); } data.Count++; } else { if (dataDst != null) { dataDst.Add(dataSrc[i].X, dataSrc[i].Y, false, dataSrc[i].Index); } } return(data.SMA); }
public RsiData Pre(PlotCollectionSet dataset, int nDataIdx, PlotCollection dataDst = null) { PlotCollection dataSrc = dataset[nDataIdx]; if (dataDst == null) { dataDst = new PlotCollection(dataSrc.Name + " RSI" + m_config.Interval.ToString()); } return(new RsiData(dataSrc, dataDst, m_config.Interval)); }
public BbData(PlotCollection src, PlotCollection dst, uint nInterval) { m_src = src; m_dst = dst; m_nCount = 0; m_nInterval = (int)nInterval; m_dfAve = 0; m_dfBbAbove = 0; m_dfBbBelow = 0; m_dfPctB = 0; m_dfBbWid = 0; }
public List <PlotCollectionSet> GetLastOutput(int nSequenceLength = 1) { List <PlotCollectionSet> rgOutput = new List <PlotCollectionSet>(); if (m_output == null || m_output.Count == 0) { return(rgOutput); } int nCount = m_output[0][0].Count; int nStart = nCount - nSequenceLength; if (nStart < 0) { nStart = 0; nSequenceLength = nCount; } for (int k = nStart; k < nStart + nSequenceLength; k++) { PlotCollectionSet lastData = new PlotCollectionSet(); List <PlotCollection> rgPlots = new List <PlotCollection>(); for (int i = 0; i < m_output.Count; i++) { PlotCollectionSet dataFrame = m_output[i]; if (dataFrame.Count > 0) { PlotCollection plots = new PlotCollection("Frame " + i.ToString()); for (int j = 0; j < dataFrame.Count; j++) { PlotCollection framePlots = dataFrame[j]; if (framePlots.Count == nCount) { Plot last = framePlots[k]; last.Name = framePlots.Name; plots.Add(last); } } lastData.Add(plots); } } rgOutput.Add(lastData); } return(rgOutput); }
public override void Resize(int nX, int nY, int nWidth, int nHeight) { m_rcBounds = new Rectangle(nX, nY, nWidth, nHeight); m_rgTickPositions = new List <int>(); for (int x = m_rcBounds.Right - m_config.PlotSpacing; x >= m_rcBounds.Left; x -= m_config.PlotSpacing) { if (x >= m_rcBounds.Left) { m_rgTickPositions.Insert(0, x); } } m_rgTickValues = new List <TickValue>(); if (m_data.Count == 0) { return; } PlotCollection primaryPlot = m_data[m_config.DataIndexForAxisLabel]; if (m_rgTickPositions.Count < primaryPlot.Count) { m_nStartPosition = primaryPlot.Count - m_rgTickPositions.Count; } for (int i = StartPosition; i < primaryPlot.Count; i++) { double dfLast = (i == 0) ? 0 : primaryPlot[i - 1].X; TickValue tv = new TickValue(primaryPlot[i], TickValue.TYPE.X, m_config, dfLast, ref m_nDayCount, ref m_nDayLast); m_rgTickValues.Add(tv); if (m_rgTickValues.Count == m_rgTickPositions.Count) { break; } } if (m_data.Count > 0) { int nCount = m_rgTickPositions.Count; if (nCount == 0) { nCount = m_data[0].Count; } m_data.GetMinMaxOverWindow(StartPosition, m_rgTickPositions.Count, out m_dfMin, out m_dfMinY, out m_dfMax, out m_dfMaxY, out m_dfAbsMinY, out m_dfAbsMaxY); } }
public void AddData(PlotCollectionSet data, bool bMaintainCount, bool bRender = false) { if (data.Count != m_data.Count) { throw new Exception("The number of plot collections must match the number of plot sets used by the graph."); } List <string> rgUpdated = new List <string>(); for (int i = 0; i < data.Count; i++) { PlotCollectionSet dataFrame = m_data[i]; if (rgUpdated.Contains(dataFrame[0].Name)) { continue; } PlotCollection dataToAdd = data[i]; if (dataFrame.Count != dataToAdd.Count) { throw new Exception("The number of data items to add must match the number of plot collections in the frame!"); } for (int j = 0; j < dataFrame.Count; j++) { PlotCollection dataFrameItems = dataFrame[j]; Plot plot = dataToAdd[j]; dataFrameItems.Add(plot); if (bMaintainCount) { dataFrameItems.RemoveAt(0); } } rgUpdated.Add(dataFrame[0].Name); } m_output = m_surface.BuildGraph(m_config, m_data); SimpleGraphingControl_Resize(this, EventArgs.Empty); ScrollToEnd(bRender); }
public BbData Pre(PlotCollection dataset) { m_caVal = new CalculationArray((int)m_config.Interval); m_caValExt = new CalculationArray((int)m_config.Interval); PlotCollection dataSrc = dataset; PlotCollection dataDst = new PlotCollection(dataSrc.Name + " BB" + m_config.Interval.ToString()); if (m_config.GetExtraSetting("BbTarget:BarRange", 0) == 1) { m_target = TARGET.BAR; } else if (m_config.GetExtraSetting("BbTarget:TotalRange", 0) == 1) { m_target = TARGET.RANGE; } return(new BbData(dataSrc, dataDst, m_config.Interval)); }
protected override void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider) { if (windowService == null) { throw new ArgumentNullException("windowService"); } if (objectProvider == null) { throw new ArgumentNullException("objectProvider"); } object data = (object)objectProvider.GetObject(); PlotCollection col = data as PlotCollection; FormPlotCollection dlg = new FormPlotCollection(col); windowService.ShowDialog(dlg); }
public void Render(Graphics g, PlotCollectionSet dataset, int nLookahead) { PlotCollection rgPrice = dataset[m_config.DataIndexOnRender]; PlotCollection rgVolume = (m_bEnableVolumeScale) ? rgPrice : null; double dfMin = m_dfMin; double dfMax = m_dfMax; float fTop = m_fTop; float fBottom = m_fBottom; // Fill the background Color clr = Color.FromArgb(128, Color.LightCyan); if (!m_rgBrushes.ContainsKey(clr)) { m_rgBrushes.Add(clr, new SolidBrush(clr)); } Brush br = m_rgBrushes[clr]; g.FillRectangle(br, 0, fTop, m_nWidth, fBottom - fTop); // Draw the price zones float fY = fTop; float fHt = (fBottom - fTop) / m_nResolution; for (int i = m_rgHistogram.Count - 1; i >= 0; i--) { float fWid = (float)(m_rgHistogram[i].NormalizedCount * (m_nWidth - 5)); clr = m_clrMap.GetColor(m_rgHistogram[i].NormalizedCount); if (!m_rgBrushes.ContainsKey(clr)) { m_rgBrushes.Add(clr, new SolidBrush(clr)); } br = m_rgBrushes[clr]; g.FillRectangle(br, 2, fY, fWid, fHt); fY += fHt; } }
public override void Scroll(double dfPct) { if (m_data.Count == 0) { return; } PlotCollection data = m_data[0]; int nVisibleCount = m_rgTickPositions.Count; int nTotalCount = data.Count; int nInvisibleCount = nTotalCount - nVisibleCount; if (nInvisibleCount < 0) { m_nScrollOffset = m_nStartPosition; return; } m_nScrollOffset = (int)Math.Round(nInvisibleCount * (1.0 - dfPct)); }
public void PreRender(Graphics g, PlotCollectionSet dataset, int nLookahead) { PlotCollection rgData = dataset[m_config.DataIndexOnRender]; List <Tuple <float, float, int> > rgTopRanges; m_rgHistogram = GraphDataZones.LoadData(rgData, out rgTopRanges, out m_dfMin, out m_dfMax, out m_fTop, out m_fBottom); // Draw the background zones for (int i = 0; i < rgTopRanges.Count && i < m_nMaxPeakCount; i++) { float fBtm1 = rgTopRanges[i].Item1; float fTop1 = rgTopRanges[i].Item2; int nIdx = rgTopRanges[i].Item3; RectangleF rc = new RectangleF(2.0f, m_fBottom - fTop1, m_gx.TickPositions.Last(), fTop1 - fBtm1); Color clr = Color.FromArgb(m_nPeakRenderAlpha, m_clrMap.GetColor(m_rgHistogram[nIdx].NormalizedCount)); Brush br = new SolidBrush(clr); g.FillRectangle(br, rc); br.Dispose(); } }
/// <summary> /// Create the PlotCollection for display on the graph. /// </summary> /// <param name="strName">Specifies the name of the plot.</param> /// <param name="rgT">Specifies the X axis data (e.g. the time sequence)</param> /// <param name="rgrgY">Specifies the Y data.</param> /// <param name="nYIdx">Specifies which of the Y data items are to be activated in the plot.</param> /// <returns>The filled PlotCollection is returned.</returns> private PlotCollection createPlots(string strName, float[] rgT, List <float[]> rgrgY, int nYIdx) { PlotCollection plots = new PlotCollection(); plots.Name = strName; int nTidx = 0; for (int i = 0; i < rgrgY.Count; i++) { for (int j = 0; j < rgrgY[i].Length; j++) { float fX = rgT[nTidx] * 100; float fY = rgrgY[i][j]; bool bActive = (i == nYIdx) ? true : false; plots.Add(fX, fY, bActive); nTidx++; } } return(plots); }
public static Histogram LoadData(PlotCollection col, out List <Tuple <float, float, int> > rgTopRanges, out double dfMin, out double dfMax, out float fTop, out float fBottom) { Histogram rgHistogram = new Histogram(); int nIdx = 0; int nCount = (int)col[nIdx].Y; nIdx++; for (int i = 0; i < nCount; i++) { HistogramItem item = HistogramItem.FromList(col[nIdx].Y_values); nIdx++; rgHistogram.Add(item); } nCount = (int)col[nIdx].Y; nIdx++; rgTopRanges = new List <Tuple <float, float, int> >(); for (int i = 0; i < nCount; i++) { float fItem1 = col[nIdx].Y_values[0]; float fItem2 = col[nIdx].Y_values[1]; int nItem3 = (int)col[nIdx].Y_values[2]; nIdx++; rgTopRanges.Add(new Tuple <float, float, int>(fItem1, fItem2, nItem3)); } dfMin = col[nIdx].Y; nIdx++; dfMax = col[nIdx].Y; nIdx++; fTop = col[nIdx].Y; nIdx++; fBottom = col[nIdx].Y; return(rgHistogram); }
public DateTime?GetDateAtLocation(Point pt) { if (!m_plotArea.Bounds.Contains(pt)) { return(null); } PlotCollection col = m_data[0]; int nXSpacing = m_config.XAxis.PlotSpacing; int nX1 = m_plotArea.Bounds.Right; int nX0 = nX1 - nXSpacing; for (int i = col.Count - 1; i >= 0; i--) { if (nX1 <= m_plotArea.Bounds.Left) { return(null); } Plot plot = col[i]; if (plot.Tag == null) { return(null); } DateTime dt = (DateTime)plot.Tag; if (nX0 <= pt.X && nX1 > pt.X) { return(dt); } nX1 = nX0; nX0 -= nXSpacing; } return(null); }
private PlotCollection combine(string strName, PlotCollection p1, PlotCollection p2) { PlotCollection p = new PlotCollection(strName + " high/low"); if (p1.Count != p2.Count) { throw new Exception("The two plot collections must have the same number of items!"); } for (int i = 0; i < p1.Count; i++) { if (p1[i].Active) { p.Add(p1[i]); } else if (p2[i].Active) { p.Add(p2[i]); } } return(p); }
/// <summary> /// Create a simple moving average of the input plot. /// </summary> /// <param name="strName">Specifies the name of the new plot.</param> /// <param name="plotsSrc">Specifies the source plots.</param> /// <param name="nIterations">Specifies the iterations over which to average.</param> /// <returns>The plot of average values is returned.</returns> private PlotCollection createPlotsAve(string strName, PlotCollection plotsSrc, int nIterations) { PlotCollection plots = new PlotCollection(); plots.Name = strName + nIterations.ToString(); List <float> rgfVal = new List <float>(); for (int i = 0; i < plotsSrc.Count; i++) { rgfVal.Add(plotsSrc[i].Y); if (rgfVal.Count > nIterations) { rgfVal.RemoveAt(0); } bool bActive = (rgfVal.Count == nIterations) ? true : false; double fAve = rgfVal.Sum() / nIterations; plots.Add(plotsSrc[i].X, fAve, bActive); } return(plots); }
public void Render(Graphics g, PlotCollectionSet dataset, int nLookahead) { PlotCollection plots = dataset[m_config.DataIndexOnRender]; List <int> rgX = m_gx.TickPositions; int nStartIdx = m_gx.StartPosition; Plot plotLast = null; float fXLast = 0; float fYLast = 0; double dfMinX = 0; double dfMaxX = 0; double dfMinY = 0; double dfMaxY = 1; double dfParamMin = 0; double dfParamMax = 0; string strDataParam = null; bool bNative = false; if (!string.IsNullOrEmpty(m_config.DataParam)) { string[] rgstr = m_config.DataParam.Split(':'); strDataParam = rgstr[0]; if (rgstr.Length > 1 && rgstr[1] == "native") { bNative = true; } else { plots.GetParamMinMax(strDataParam, out dfParamMin, out dfParamMax); } if (rgstr.Length > 1 && rgstr[1] == "r") { plots.GetMinMaxOverWindow(0, plots.Count, out dfMinX, out dfMinY, out dfMaxX, out dfMaxY); } } double dfScaleHigh = m_config.GetExtraSetting("ScaleHigh", 70); double dfScaleLow = m_config.GetExtraSetting("ScaleLow", 30); float fLevel70 = m_gy.ScaleValue(dfScaleHigh, true); float fLevel30 = m_gy.ScaleValue(dfScaleLow, true); for (int i = 0; i < rgX.Count; i++) { int nIdx = nStartIdx + i; if (nIdx < plots.Count) { Plot plot = plots[nStartIdx + i]; float fX = rgX[i]; float?fY1 = getYValue(plot, dfMinY, dfMaxY, dfParamMin, dfParamMax, strDataParam, bNative); if (!fY1.HasValue) { continue; } float fY = fY1.Value; if (float.IsNaN(fY) || float.IsInfinity(fY)) { fY = fYLast; } if (m_config.LineColor != Color.Transparent) { if (plotLast != null && plotLast.Active && plot.Active && ((plot.LookaheadActive && m_config.LookaheadActive) || i < rgX.Count - nLookahead)) { g.DrawLine(m_style.LinePen, fXLast, fYLast, fX, fY); Color clr = Color.Transparent; m_rgpt.Clear(); if (fY < fLevel70) { m_rgpt.Add(new PointF(fXLast, fLevel70)); if (fYLast < fLevel70) { m_rgpt.Add(new PointF(fXLast, fYLast)); } if (fY < fLevel70) { m_rgpt.Add(new PointF(fX, fY)); } m_rgpt.Add(new PointF(fX, fLevel70)); m_rgpt.Add(m_rgpt[0]); clr = Color.FromArgb(64, Color.Green); } else if (fY > fLevel30) { m_rgpt.Add(new PointF(fXLast, fLevel30)); if (fYLast > fLevel30) { m_rgpt.Add(new PointF(fXLast, fYLast)); } if (fY > fLevel30) { m_rgpt.Add(new PointF(fX, fY)); } m_rgpt.Add(new PointF(fX, fLevel30)); m_rgpt.Add(m_rgpt[0]); clr = Color.FromArgb(64, Color.Red); } if (clr != Color.Transparent && m_rgpt.Count > 0) { if (!m_style.Brushes.ContainsKey(clr)) { m_style.Brushes.Add(clr, new SolidBrush(clr)); } g.FillPolygon(m_style.Brushes[clr], m_rgpt.ToArray()); } } } plotLast = plot; fXLast = fX; if (!float.IsNaN(fY) && !float.IsInfinity(fY)) { fYLast = fY; } } } for (int i = 0; i < rgX.Count; i++) { int nIdx = nStartIdx + i; if (nIdx < plots.Count) { Plot plot = plots[nStartIdx + i]; float fX = rgX[i]; float?fY1 = getYValue(plot, dfMinY, dfMaxY, dfParamMin, dfParamMax, strDataParam, bNative); if (!fY1.HasValue) { continue; } float fY = fY1.Value; RectangleF rcPlot = new RectangleF(fX - 2.0f, fY - 2.0f, 4.0f, 4.0f); if (isValid(rcPlot)) { if (m_config.PlotFillColor != Color.Transparent) { Brush brFill = (plot.Active) ? m_style.PlotFillBrush : Brushes.Transparent; g.FillEllipse(brFill, rcPlot); } if (m_config.PlotLineColor != Color.Transparent) { Pen pLine = (plot.Active) ? m_style.PlotLinePen : Pens.Transparent; g.DrawEllipse(pLine, rcPlot); } } } } }
private PlotCollection getLowPoints(PlotCollection data, int nLevel, int nLookahead) { if (data == null || data.Count < 3) { return(null); } PlotCollection dataLow = new PlotCollection(data.Name + " L" + nLevel.ToString()); int nOpen = data[0].PrimaryIndexY; int nHigh = data[0].PrimaryIndexY; int nLow = data[0].PrimaryIndexY; int nClose = data[0].PrimaryIndexY; if (data[0].Y_values.Length == 4) { nHigh = 1; nLow = 2; nClose = 3; } List <Tuple <int, Plot> > rgActive = new List <Tuple <int, Plot> >(); for (int i = 0; i < data.Count - nLookahead; i++) { if (data[i].Active) { rgActive.Add(new Tuple <int, Plot>(i, data[i])); } } if (rgActive.Count < 3) { return(null); } MinMax minmax = new MinMax(); int nIdx = 1; for (int i = 0; i < data.Count; i++) { int nIdxCurrent = rgActive[nIdx].Item1; if (i == nIdxCurrent && nIdx < rgActive.Count - 1) { Plot plotCurrent = data[nIdxCurrent]; int nIdxPast = rgActive[nIdx - 1].Item1; Plot plotPast = data[nIdxPast]; int nIdxFuture = rgActive[nIdx + 1].Item1; Plot plotFuture = data[nIdxFuture]; double dfOpen = (plotCurrent.Y_values.Length == 1) ? plotCurrent.Y : plotCurrent.Y_values[nOpen]; double dfClose = (plotCurrent.Y_values.Length == 1) ? plotCurrent.Y : plotCurrent.Y_values[nClose]; double dfHigh1 = (plotCurrent.Y_values.Length == 1) ? plotCurrent.Y : plotCurrent.Y_values[nHigh]; double dfLow1 = (plotCurrent.Y_values.Length == 1) ? plotCurrent.Y : plotCurrent.Y_values[nLow]; double dfHigh0 = (plotPast.Y_values.Length == 1) ? plotPast.Y : plotPast.Y_values[nHigh]; double dfLow0 = (plotPast.Y_values.Length == 1) ? plotPast.Y : plotPast.Y_values[nLow]; double dfHigh2 = (plotFuture.Y_values.Length == 1) ? plotFuture.Y : plotFuture.Y_values[nHigh]; double dfLow2 = (plotFuture.Y_values.Length == 1) ? plotFuture.Y : plotFuture.Y_values[nLow]; bool bLow = false; if (dfLow1 < dfLow0 && dfLow1 < dfLow2) { bLow = true; } minmax.Add(dfLow1); dataLow.Add(new Plot(data[nIdxCurrent].X, dfLow1, null, bLow, data[nIdxCurrent].Index)); nIdx++; } else { double dfLow = (data[i].Y_values.Length == 1) ? data[i].Y : data[i].Y_values[nLow]; dataLow.Add(new Plot(data[i].X, dfLow, null, false, data[i].Index)); } } dataLow.SetMinMax(minmax); return(dataLow); }
/// <summary> /// The DoWork thread is the main tread used to train or run the model depending on the operation selected. /// </summary> /// <param name="sender">Specifies the sender</param> /// <param name="e">specifies the arguments.</param> private void m_bw_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker bw = sender as BackgroundWorker; m_input = e.Argument as InputData; SettingsCaffe s = new SettingsCaffe(); s.ImageDbLoadMethod = IMAGEDB_LOAD_METHOD.LOAD_ALL; try { m_model.Batch = m_input.Batch; m_mycaffe = new MyCaffeControl <float>(s, m_log, m_evtCancel); // Train the model. if (m_input.Operation == InputData.OPERATION.TRAIN) { m_model.Iterations = (int)((m_input.Epochs * 7000) / m_model.Batch); m_log.WriteLine("Training for " + m_input.Epochs.ToString() + " epochs (" + m_model.Iterations.ToString("N0") + " iterations).", true); m_log.WriteLine("INFO: " + m_model.Iterations.ToString("N0") + " iterations.", true); m_log.WriteLine("Using hidden = " + m_input.HiddenSize.ToString() + ", and word size = " + m_input.WordSize.ToString() + ".", true); // Load the Seq2Seq training model. NetParameter netParam = m_model.CreateModel(m_input.InputFileName, m_input.TargetFileName, m_input.HiddenSize, m_input.WordSize, m_input.UseSoftmax, m_input.UseExternalIp); string strModel = netParam.ToProto("root").ToString(); SolverParameter solverParam = m_model.CreateSolver(m_input.LearningRate); string strSolver = solverParam.ToProto("root").ToString(); byte[] rgWts = loadWeights("sequence"); m_strModel = strModel; m_strSolver = strSolver; m_mycaffe.OnTrainingIteration += m_mycaffe_OnTrainingIteration; m_mycaffe.OnTestingIteration += m_mycaffe_OnTestingIteration; m_mycaffe.LoadLite(Phase.TRAIN, strSolver, strModel, rgWts, false, false); if (!m_input.UseSoftmax) { MemoryLossLayer <float> lossLayerTraining = m_mycaffe.GetInternalNet(Phase.TRAIN).FindLayer(LayerParameter.LayerType.MEMORY_LOSS, "loss") as MemoryLossLayer <float>; if (lossLayerTraining != null) { lossLayerTraining.OnGetLoss += LossLayer_OnGetLossTraining; } MemoryLossLayer <float> lossLayerTesting = m_mycaffe.GetInternalNet(Phase.TEST).FindLayer(LayerParameter.LayerType.MEMORY_LOSS, "loss") as MemoryLossLayer <float>; if (lossLayerTesting != null) { lossLayerTesting.OnGetLoss += LossLayer_OnGetLossTesting; } } m_blobProbs = new Blob <float>(m_mycaffe.Cuda, m_mycaffe.Log); m_blobScale = new Blob <float>(m_mycaffe.Cuda, m_mycaffe.Log); TextDataLayer <float> dataLayerTraining = m_mycaffe.GetInternalNet(Phase.TRAIN).FindLayer(LayerParameter.LayerType.TEXT_DATA, "data") as TextDataLayer <float>; if (dataLayerTraining != null) { dataLayerTraining.OnGetData += DataLayerTraining_OnGetDataTraining; } // Train the Seq2Seq model. m_plotsSequenceLoss = new PlotCollection("Sequence Loss"); m_plotsSequenceAccuracyTest = new PlotCollection("Sequence Accuracy Test"); m_plotsSequenceAccuracyTrain = new PlotCollection("Sequence Accuracy Train"); m_mycaffe.Train(m_model.Iterations); saveWeights("sequence", m_mycaffe); } // Run a trained model. else { NetParameter netParam = m_model.CreateModel(m_input.InputFileName, m_input.TargetFileName, m_input.HiddenSize, m_input.WordSize, m_input.UseSoftmax, m_input.UseExternalIp, Phase.RUN); string strModel = netParam.ToProto("root").ToString(); byte[] rgWts = loadWeights("sequence"); strModel = m_model.PrependInput(strModel); m_strModelRun = strModel; int nN = m_model.TimeSteps; m_mycaffe.LoadToRun(strModel, rgWts, new BlobShape(new List <int>() { nN, 1, 1, 1 }), null, null, false, false); m_blobProbs = new Blob <float>(m_mycaffe.Cuda, m_mycaffe.Log); m_blobScale = new Blob <float>(m_mycaffe.Cuda, m_mycaffe.Log); runModel(m_mycaffe, bw, m_input.InputText); } } catch (Exception excpt) { throw excpt; } finally { // Cleanup. if (m_mycaffe != null) { m_mycaffe.Dispose(); m_mycaffe = null; } } }