- ago
How do I make a variable assume more than one value when optimizing a strategy?

For example, the variable buyprofit in this code bellow:

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Collections.Generic; using WealthLab.MyIndicators; namespace WealthScript5 { public class MyStrategy : UserStrategyBase { public MyStrategy() : base() { } public override void Initialize(BarHistory bars) { indicator1 = new SMA(bars.Close, 7); PlotIndicator(indicator1, new WLColor(0, 0, 0)); indicator1.MassageColors = true; indicator2 = new SMA(bars.Close, 14); PlotIndicator(indicator2, new WLColor(0, 0, 255)); indicator2.MassageColors = true; indicator12 = new SMA(bars.Close, 7); indicator22 = new SMA(bars.Close, 14); StartIndex = 14; } public override void Execute(BarHistory bars, int idx) { int index = idx; Position foundPosition0 = FindOpenPosition(0); bool condition0; bool newposition; TimeSeries ds = bars.Close; Double buyprofit = 1.005; if (foundPosition0 == null) { condition0 = false; { if (index - 0 >= 0 && indicator1[index] > indicator2[index - 0]) { condition0 = true; } } if (condition0) { _transaction = PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0, 0, "Buy At Market (1)"); } } else { condition0 = false; { if (index - 0 >= 0 && indicator12[index] < indicator22[index - 0]) { condition0 = true; } } if (condition0) { foreach (Position pos in OpenPositions) ClosePosition(pos, OrderType.Market); } else { newposition = false; Position open = LastOpenPosition; Double re = open.EntryPrice * buyprofit; // my code add if (ds[idx - 1] > re) { if (index - 0 >= 0 && indicator1[index] > indicator2[index - 0]) { newposition = true; } if (newposition) { _transaction = PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0, 0, "Buy At Market (2)"); } } } } } public override void NewWFOInterval(BarHistory bars) { indicator1 = new SMA(bars.Close, 7); indicator2 = new SMA(bars.Close, 14); indicator1 = new SMA(bars.Close, 7); indicator2 = new SMA(bars.Close, 14); indicator12 = new SMA(bars.Close, 7); indicator22 = new SMA(bars.Close, 14); indicator12 = new SMA(bars.Close, 7); indicator22 = new SMA(bars.Close, 14); } private IndicatorBase indicator1; private IndicatorBase indicator2; private IndicatorBase indicator12; private IndicatorBase indicator22; private Transaction _transaction; } }
0
432
Solved
2 Replies

Reply

Bookmark

Sort
- ago
#1
Forget abou it, I made it work!
0
- ago
#2
Good that you got it working. For other community members, the answer can be found in the Help > "Optimization" chapter. A code snippet there explains how to create Parameters by calling AddParameter method from within your Strategy's constructor.

1
Best Answer

Reply

Bookmark

Sort