private void CalcUpSwing(int bar, double high, bool updateHigh) { if (!updateHigh) { lastHigh = curHigh; lastHighBar = curHighBar; lastHighDuration = curHighDuration; lastHighRelation = curHighRelation; swingCounterUp++; swingSlope = 1; trendChangeBar = bar; TrendChangeBarStats upStats = new TrendChangeBarStats(CurrentBar, Open[0], High[0], Low[0], Close[0], Trend.Up); upHistory.Add(upHisIndex, upStats); bothHistory.Add(upHisIndex + dnHisIndex, upStats); upHisIndex++; } else { swingHighs.RemoveAt(swingHighs.Count - 1); } curHighBar = bar; curHigh = Math.Round(high, decimalPlaces, MidpointRounding.AwayFromZero); curHighLength = Convert.ToInt32(Math.Round((curHigh - curLow) / TickSize, 0, MidpointRounding.AwayFromZero)); curHighDuration = curHighBar - curLowBar; double dtbOffset = ATR(14)[CurrentBar - curHighBar] * dtbStrength / 100; if (curHigh > lastHigh - dtbOffset && curHigh < lastHigh + dtbOffset) { curHighRelation = Relation.Double; BackColor = Color.Yellow; doubleBottomBar = CurrentBar; TrendChangeBarStats dtStats = new TrendChangeBarStats(CurrentBar, Open[0], High[0], Low[0], Close[0], Trend.Up); dT.Add(dtIndex, dtStats); //Print(Environment.NewLine + String.Format("{0} [{1}] {2}", Time[0].ToShortDateString(), Bars.BarsSinceSession, bothHistory[bothHistory.Count - 2].ToString())); //Print(String.Format("{0} [{1}] {2} DOUBLETOP ALERT", Time[0].ToShortDateString(), Bars.BarsSinceSession, bothHistory[bothHistory.Count - 1].ToString())); dtIndex++; enableDTOrderEntry = true; } else if (curHigh < lastHigh) { curHighRelation = Relation.Lower; } else { curHighRelation = Relation.Higher; } string swingLabel = null; switch (curHighRelation) { case Relation.Higher: swingLabel = "HH"; break; case Relation.Lower: swingLabel = "LH"; break; case Relation.Double: swingLabel = "DT"; break; } if (lastHigh != 0.0 || lastLow != 0.0) { DrawLine("ZigZagUp" + swingCounterUp, true, CurrentBar - curLowBar, curLow, CurrentBar - curHighBar, curHigh, zigZagColourUp, zigZagStyle, zigZagWidth); string txtOutput = string.Format("{0}: {1} b.{2}", swingLabel, curHighLength.ToString(), curHighDuration.ToString()); DrawText("UpLabel" + swingCounterUp, true, txtOutput, CurrentBar - curHighBar, curHigh, offsetText, Color.Black, textFont, StringAlignment.Center, Color.Transparent, Color.Transparent, 0); } Swings up = new Swings(curHighBar, curHigh, curHighLength, curHighDuration, curHighRelation); swingHighs.Add(up); upCount = swingHighs.Count - 1; }
private void CalcDnSwing(int bar, double low, bool updateLow) { if (!updateLow) { lastLow = curLow; lastLowBar = curLowBar; lastLowDuration = curLowDuration; lastLowLength = curLowLength; lastLowRelation = curLowRelation; swingCounterDn++; swingSlope = -1; trendChangeBar = bar; TrendChangeBarStats dnStats = new TrendChangeBarStats(CurrentBar, Open[0], High[0], Low[0], Close[0], Trend.Down); dnHistory.Add(dnHisIndex, dnStats); bothHistory.Add(dnHisIndex + upHisIndex, dnStats); //Print(String.Format("{0} [{1}] {2}",Time[0].ToShortDateString(),Bars.BarsSinceSession,bothHistory[bothHistory.Count - 1].ToString())); dnHisIndex++; //BackColor = Color.Coral; } else { swingLows.RemoveAt(swingLows.Count - 1); } curLowBar = bar; curLow = Math.Round(low, decimalPlaces, MidpointRounding.AwayFromZero); curLowLength = Convert.ToInt32(Math.Round((curLow - curHigh) / TickSize, 0, MidpointRounding.AwayFromZero)); curLowDuration = curLowBar - curHighBar; double dtbOffset = ATR(14)[CurrentBar - curLowBar] * dtbStrength / 100; if (curLow > lastLow - dtbOffset && curLow < lastLow + dtbOffset) { curLowRelation = Relation.Double; } else if (curLow < lastLow) { curLowRelation = Relation.Lower; } else { curLowRelation = Relation.Higher; } string swingLabel = null; switch (curLowRelation) { case Relation.Higher: swingLabel = "HL"; break; case Relation.Lower: swingLabel = "LL"; break; case Relation.Double: swingLabel = "DB"; break; } if (lastHigh != 0.0 || lastLow != 0.0) { DrawLine("ZigZagDown" + swingCounterDn, true, CurrentBar - curHighBar, curHigh, CurrentBar - curLowBar, curLow, zigZagColourDn, zigZagStyle, zigZagWidth); string txtOutput = string.Format("{0}: {1} b.{2}", swingLabel, curLowLength.ToString(), curLowDuration.ToString()); DrawText("DnLabel" + swingCounterDn, true, txtOutput, CurrentBar - curLowBar, curLow, -offsetText, Color.Black, textFont, StringAlignment.Center, Color.Transparent, Color.Transparent, 0); } // On new low adjust the breakeven stop to higher low. if (swingStopLoss != null) { //Print (String.Format(" Current StopPrice {0} lastHigh {1}",swingStopLoss.StopPrice,lastHigh)); if (swingStopLoss.StopPrice > lastHigh && scalpProfitTarget == null) { // Print(Bars.BarsSinceSession+"MOVING THE STOPLOSS"); try { if (Close[0] < lastHigh) { swingStopLoss = ExitShortStop(0, true, swingStopLoss.Quantity, lastHigh, "SW.HL", "SW.DTEN"); } else if (Close[0] >= lastHigh) { ExitShortStop(0, true, swingStopLoss.Quantity, High[0] + 1 * TickSize, "SW.HL" + " MKT", "SW.DTEN"); } } catch (Exception ex) { Print("Exception caused by moving swing stop loss position" + Environment.NewLine + ex.ToString()); } } } // Print(String.Format("[{0}] {1}", Bars.BarsSinceSession, lastHigh)); Swings dn = new Swings(curLowBar, curLow, curLowLength, curLowDuration, curLowRelation); swingLows.Add(dn); downCount = swingLows.Count - 1; }