I have a triple screen crossover strategy. However, I added logic so that I can limit the # of "buy" signals in a portfolio by first ranking on ROC for transaction weight.
Problem is that I am not getting "sel"l signals, just "stop loss" orders.
Your help is appreciated.
Also, on the sell side, is this right for the "<" or ">" signal?
or
Not sure which is correct?
Thank you,
Larry
Here is my code:
Problem is that I am not getting "sel"l signals, just "stop loss" orders.
Your help is appreciated.
Also, on the sell side, is this right for the "<" or ">" signal?
CODE:
if (idx - 0 >= 0 && SMA_short[idx] < SMA_middle[idx - 0])
or
CODE:
if (idx - 0 <= 0 && SMA_short[idx] < SMA_middle[idx - 0])
Not sure which is correct?
Thank you,
Larry
Here is my code:
CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Data; using WealthLab.Indicators; using System.Collections.Generic; namespace WealthScript7 { public class LSBollingerBands : UserStrategyBase { public LSBollingerBands() { AddParameter("Rate of Change Period", ParameterType.Int32, 100, 50, 500, 50); AddParameter("Benchmark SMA Period", ParameterType.Int32, 200, 10, 500, 10); AddParameter("Number of Buy Candidates", ParameterType.Int32, 5, 1, 500, 1); AddParameter("Stop Loss Pct", ParameterType.Int32, 10, 1, 99, 1); AddParameter("SMAShort", ParameterType.Int32, 4, 1, 8, 1); AddParameter("SMAMiddle", ParameterType.Int32, 9, 9, 17, 1); AddParameter("SMALong", ParameterType.Int32, 18, 18, 30, 1); } //create indicators and other objects here, this is executed prior to the main trading loop public override void Initialize(BarHistory bars) { //load the parameters rocPeriod = Parameters[0].AsInt; benchmarkPeriod = Parameters[1].AsInt; numParticipants = Parameters[2].AsInt; stopLossPct = Parameters[3].AsInt; // create and plot Tripple SMA SMA_short = new SMA(bars.Close, Parameters[4].AsInt); SMA_middle = new SMA(bars.Close, Parameters[5].AsInt); SMA_long = new SMA(bars.Close, Parameters[6].AsInt); PlotIndicator(SMA_short, WLColor.Green, PlotStyle.DashedLine); PlotIndicator(SMA_middle, WLColor.Blue, PlotStyle.DashedLine); PlotIndicator(SMA_long, WLColor.Red, PlotStyle.DashedLine); //create other working indicators roc = ROC.Series(bars.Close, rocPeriod); //cache ROC value with each bar bars.Cache["ROC"] = roc; //benchmark creation benchmarkBar = GetHistory(bars, "SPY"); benchmarkSMA = SMA.Series(benchmarkBar.Close, benchmarkPeriod); //plot the benchmark price bars and the SMA in a new plane PlotBarHistory(benchmarkBar, "Benchmark", WLColor.Green, true); PlotIndicator(benchmarkSMA, WLColor.WhiteSmoke, PlotStyle.Line, false, "Benchmark"); } //this is called prior to the Execute loop, determine which symbols have the lowest RSI public override void PreExecute(DateTime dt, List<BarHistory> participants) { //store the symbols ROC value in their BarHistory instances foreach (BarHistory bh in participants) { ROC symbolROC = (ROC)bh.Cache["ROC"]; int idx = GetCurrentIndex(bh); //this returns the index of the BarHistory for the bar currently being processed double rocVal = symbolROC[idx]; //save the current value along with the BarHistory instance bh.UserData = rocVal; } //sort the participants by ROC value (lowest to highest) participants.Sort((a, b) => a.UserDataAsDouble.CompareTo(b.UserDataAsDouble)); participants.Reverse(); //flip the list so the highest ROC are on top //keep the top symbols based on numParticipants - all open positions buys.Clear(); for (int n = 0; n < numParticipants - OpenPositionsAllSymbols.Count; n++) { if (n >= participants.Count - OpenPositionsAllSymbols.Count) break; buys.Add(participants[n]); } } //execute the strategy rules here, this is executed once for each bar in the backtest history public override void Execute(BarHistory bars, int idx) { bool condition0; //check to be sure the longest period indictor contains valid values if (double.IsNaN(benchmarkSMA[idx])) return; if (!HasOpenPosition(bars, PositionType.Long)) { //set up abool for testing if n buy lisr bool inBuyList = buys.Contains(bars); //create the transation for use wiht same say stops Transaction trans = null; //no entry if benchmark close is inder benchmark SMA if (benchmarkBar.Close[idx] < benchmarkSMA[idx]) return; //no entry if not in the buy list if (!inBuyList) return; condition0 = false; if (idx - 0 >= 0 && SMA_short[idx] > SMA_middle[idx - 0]) { if (idx - 0 >= 0 && SMA_middle[idx] > SMA_long[idx - 0]) { trans = PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0, 0, "Buy Tripple SMA"); condition0 = true; } } //transaction weighted for backtestng by ROC if (trans != null) { trans.Weight = roc[idx]; } } else { // If SMA_short < SMA_middle < SMA_long if (idx - 0 >= 0 && SMA_short[idx] < SMA_middle[idx - 0]) { if (idx - 0 >= 0 && SMA_middle[idx] < SMA_long[idx - 0]) { Backtester.CancelationCode = 284; PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0, 0, "Sell Tripple SMA"); condition0 = true; } } else //stop loss exit PlaceTrade(bars, TransactionType.Sell, OrderType.Stop, StopLoss(bars, idx), 0, "Stop Loss Tripple SMA"); } } //same bar exits: profit or loss from execution price public override void AssignAutoStopTargetPrices(Transaction trans, double basisPrice, double executionPrice) { trans.AutoStopLossPrice = Math.Round(executionPrice * (1 - (stopLossPct / 100)), 2); } //calcualte the stoploss price and return it private double StopLoss(BarHistory bars, int idx) { return Math.Round(LastOpenPosition.EntryPrice * (1 - (stopLossPct / 100)), 2); } //the list of symbols that we should buy each bar private static List<BarHistory> buys = new List<BarHistory>(); //private variables int bbPeriod, rocPeriod, benchmarkPeriod, exitBand, numParticipants; Double bbUpperStdDev, bbLowerStdDev, stopLossPct; //indicators IndicatorBase benchmarkSMA, roc; //bar history for benchmark BarHistory benchmarkBar; public override void NewWFOInterval(BarHistory bars) { SMA_short = new SMA(bars.Close, Parameters[8].AsInt); SMA_middle = new SMA(bars.Close, Parameters[9].AsInt); SMA_long = new SMA(bars.Close, Parameters[10].AsInt); } private IndicatorBase SMA_short; private IndicatorBase SMA_middle; private IndicatorBase SMA_long; } }
Rename
Because it's a Buy signal:
CODE:
//PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0, 0, "Sell Tripple SMA"); PlaceTrade(bars, TransactionType.Sell, OrderType.Market, 0, 0, "Sell Tripple SMA");
Eugene,
LOL.
Thank you,
Larry
LOL.
Thank you,
Larry
Your Response
Post
Edit Post
Login is required