protected override void StrategyExecute() { DataSeries sma20 = Indicators.SMA.Series(data.Close, parameters[0], ""); Indicators.MACD macd = Indicators.MACD.Series(data.Close, parameters[1], parameters[2], parameters[3], ""); Indicators.Stoch stoch = Indicators.Stoch.Series(data.Bars, parameters[4], parameters[5], parameters[6], ""); DataSeries line1 = stoch.SlowKSeries; DataSeries line2 = stoch.SlowDSeries; double delta = 0, lastDelta = 0; bool upTrend = false; AppTypes.MarketTrend stochasticTrend = AppTypes.MarketTrend.Unspecified; for (int idx = 1; idx < macd.Values.Length; idx++) { delta = (macd.HistSeries[idx] - macd.HistSeries[idx - 1]); stochasticTrend = ((line1[idx] > line2[idx]) ? AppTypes.MarketTrend.Upward : AppTypes.MarketTrend.Downward); upTrend = (data.Close[idx] > sma20[idx] ? true : false); if (upTrend && delta > 0 && lastDelta < 0 && stochasticTrend == AppTypes.MarketTrend.Upward) { BuyAtClose(idx); } if (is_bought && stochasticTrend == AppTypes.MarketTrend.Downward) { SellAtClose(idx); } lastDelta = delta; } }
override protected void StrategyExecute() { int sma_short_period = (int)parameters[0]; int sma_long_period = (int)parameters[1]; int fastK = (int)parameters[2]; int slowK = (int)parameters[3]; int slowD = (int)parameters[4]; DataSeries short_sma = Indicators.SMA.Series(data.Close, sma_short_period, ""); DataSeries long_sma = Indicators.SMA.Series(data.Close, sma_long_period, ""); Indicators.Stoch stoch = Indicators.Stoch.Series(data.Bars, fastK, slowK, slowD, ""); DataSeries stoch1 = stoch.SlowKSeries; DataSeries stoch2 = stoch.SlowDSeries; AppTypes.MarketTrend lastTrend = AppTypes.MarketTrend.Unspecified; AppTypes.MarketTrend currentTrend = AppTypes.MarketTrend.Unspecified; AppTypes.MarketTrend stochTrend = AppTypes.MarketTrend.Unspecified; for (int idx = 0; idx < short_sma.Count; idx++) { stochTrend = (stoch1[idx] > stoch2[idx] ? AppTypes.MarketTrend.Upward : AppTypes.MarketTrend.Downward); currentTrend = ((data.Close[idx] > short_sma[idx]) && (short_sma[idx] > long_sma[idx]) ? AppTypes.MarketTrend.Upward : AppTypes.MarketTrend.Downward); if (lastTrend == AppTypes.MarketTrend.Downward && currentTrend == AppTypes.MarketTrend.Upward && stochTrend == AppTypes.MarketTrend.Upward) { BuyAtClose(idx); } if (lastTrend == AppTypes.MarketTrend.Upward && currentTrend == AppTypes.MarketTrend.Downward) { SellAtClose(idx); } lastTrend = currentTrend; } }
override protected void StrategyExecute() { DataSeries sma20 = Indicators.SMA.Series(data.Close, parameters[0], ""); Indicators.MACD macd = Indicators.MACD.Series(data.Close, parameters[1], parameters[2], parameters[3], ""); DataSeries hist = macd.HistSeries; Indicators.Stoch stoch = Indicators.Stoch.Series(data.Bars, parameters[4], parameters[5], parameters[6], ""); DataSeries line1 = stoch.SlowKSeries; DataSeries line2 = stoch.SlowDSeries; double delta = 0, lastDelta = 0; bool macd_swicth_to_up_trend = false, macd_swicth_to_down_trend = false, sto_switch_to_up_trend = false, sto_switch_to_down_trend = false; AppTypes.MarketTrend stochasticTrend = AppTypes.MarketTrend.Unspecified; for (int idx = 1; idx < macd.Count; idx++) { delta = (hist[idx] - hist[idx - 1]); macd_swicth_to_up_trend = (delta > 0 && lastDelta < 0 ? true : false); macd_swicth_to_down_trend = (delta < 0 && lastDelta > 0 ? true : false); stochasticTrend = (line1[idx] > line2[idx] ? AppTypes.MarketTrend.Upward : AppTypes.MarketTrend.Downward); sto_switch_to_up_trend = (line1[idx] > line2[idx]) && (line1[idx - 1] < line2[idx - 1]) ? true : false; sto_switch_to_down_trend = (line1[idx] < line2[idx]) && (line1[idx - 1] > line2[idx - 1]) ? true : false; if (!is_bought && data.Close[idx] > sma20[idx]) { if (macd_swicth_to_up_trend && stochasticTrend == AppTypes.MarketTrend.Downward) { BuyAtClose(idx); } } if (is_bought) { if (sto_switch_to_down_trend || macd_swicth_to_down_trend) { SellAtClose(idx); } } lastDelta = delta; } }
public StochSlowRule(DataBars db, double fastK, double slowK, double slowD) { stoch = Indicators.Stoch.Series(db, fastK, slowK, slowD, "stoch"); line1 = stoch.SlowKSeries; line2 = stoch.SlowDSeries; }