- ago
Hi guys, im new to wealth lab and im still trying to suit myself.... I need to use a simple moving average, but displaced in 1 period, when i drag the SMA indicator to the strategy, it gives me no option do displace it... How can i do it?

Thanks!
0
452
Solved
12 Answers

Reply

Bookmark

Sort
- ago
#1
If you're willing to create a Code-Based Strategy you can do it. After you paste the code into the Editor, run the Strategy on a single symbol (not a DataSet) and you should see the SMA (red) and the displaced SMA (black). The >> operator is used to displace a TimeSeries in WL7.

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Drawing; using System.Collections.Generic; namespace WealthScript1 { public class MyStrategy : UserStrategyBase { //Initialize public override void Initialize(BarHistory bars) {          ma = SMA.Series(bars.Close, 50);          displaced = ma >> 1;          PlotIndicator(ma, Color.Red);          PlotTimeSeries(displaced, "DisplacedMA(50)", "Price", Color.Black); } //Execute public override void Execute(BarHistory bars, int idx) { if (OpenPositions.Count == 0) { } else { } }       //private members       SMA ma;       TimeSeries displaced; } }
0
- ago
#2
I dont understand coding, i made the strategy on the " Building Block". The strategy is buy when price hits the SMA of 2 low displaced in 1, and sell when the price hit the SMA of 2 highs displaced in 1. Ill paste the code here, where should i write the dispalce thing? Why can´t we just displace it in the Building Blocks while we are configurating it?
Thanks in advance!

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Drawing; using System.Collections.Generic; namespace WealthScript3 { public class MyStrategy : UserStrategyBase {     public MyStrategy() : base() { } public override void Initialize(BarHistory bars) {       source = new SMA(bars.Low,2);       Plot(source,Color.FromArgb(255,0,0,0));       pct = 0.00;       pct = (100.0 - pct) / 100.0;       multSource = source * pct;       source2 = new SMA(bars.High,2);       Plot(source2,Color.FromArgb(255,0,0,255));       pct2 = 0.00;       pct2 = (100.0 + pct2) / 100.0;       multSource2 = source2 * pct2;          StartIndex = 2; } public override void Execute(BarHistory bars, int idx) {          int index = idx;          Position foundPosition0 = FindOpenPosition(0);          bool condition0;          if (foundPosition0 == null)          {             condition0 = false;             {                condition0 = true;             }             if (condition0)             {                val = multSource[idx];                _transaction = PlaceTrade(bars, TransactionType.Buy, OrderType.Limit, val, 0);             }          }          else          {             condition0 = false;             {                condition0 = true;             }             if (condition0)             {                val2 = multSource2[idx];                ClosePosition(foundPosition0, OrderType.Limit, + val2);             }          } }    private double pct;    private double val;    private IndicatorBase source;    private TimeSeries multSource;    private double pct2;    private double val2;    private IndicatorBase source2;    private TimeSeries multSource2;       private Transaction _transaction; } }
0
- ago
#3
Well, you can't specify a displacement in a Building Block because we didn't think to add that feature. I created a to-do item in our wish list to add this ability, along with a few others like using a compressed time scale and an external symbol!
0
- ago
#4
Here's a modification of your Strategy with the two TimeSeries shifted (displaced) by 1 bar.

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Drawing; using System.Collections.Generic; namespace WealthScript3 {    public class MyStrategy : UserStrategyBase    {       public MyStrategy() : base()       {       }       public override void Initialize(BarHistory bars)       {          source = new SMA(bars.Low, 2);          source = source >> 1;          PlotTimeSeries(source, "DisplacedLow", "Price", Color.FromArgb(255, 0, 0, 0));          pct = 0.00;          pct = (100.0 - pct) / 100.0;          multSource = source * pct;          source2 = new SMA(bars.High, 2);          source2 = source2 >> 1;          PlotTimeSeries(source2, "DisplacedHigh", "Price", Color.FromArgb(255, 0, 0, 255));          pct2 = 0.00;          pct2 = (100.0 + pct2) / 100.0;          multSource2 = source2 * pct2;          StartIndex = 2;       }       public override void Execute(BarHistory bars, int idx)       {          int index = idx;          Position foundPosition0 = FindOpenPosition(0);          bool condition0;          if (foundPosition0 == null)          {             condition0 = false;             {                condition0 = true;             }             if (condition0)             {                val = multSource[idx];                _transaction = PlaceTrade(bars, TransactionType.Buy, OrderType.Limit, val, 0);             }          }          else          {             condition0 = false;             {                condition0 = true;             }             if (condition0)             {                val2 = multSource2[idx];                ClosePosition(foundPosition0, OrderType.Limit, +val2);             }          }       }       private double pct;       private double val;       private TimeSeries source;       private TimeSeries multSource;       private double pct2;       private double val2;       private TimeSeries source2;       private TimeSeries multSource2;       private Transaction _transaction;    } }
0
- ago
#5
Thanks!
0
- ago
#7
Hi Glitch,

something is wrong with the code, the SMAs are right in the chart, but the Buy/Sell points are considering +2 displacement instead of 1... Here is a print of the chart, the yellow circles shows the right point of buy/sell.

0
- ago
#8
I think it's correct. Take for example the proposed entry on Feb 3. The displaced SMA value of Feb 2 was 25.91. The High on Feb 3 was only 25.77. No breaching. Same for Feb 23. The Displaced High as of Feb 22 was 25.49, the next day's high was 24.95. Same thing.

Point is, the DSMA value can only be determined after the market close. You need to be looking at it on the adjacent bar. Because you're placing a limit order for next bar.
0
- ago
#9
Eugene's correct, the code is right and is what you asked. Maybe what you really want to do is just visualize the displaced moving averages on the chart? But really issue the signals based on the non-displaced MAs. Try this variation,

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Drawing; using System.Collections.Generic; namespace WealthScript3 {    public class MyStrategy : UserStrategyBase    {       public MyStrategy() : base()       {       }       public override void Initialize(BarHistory bars)       {          source = new SMA(bars.Low, 2);          PlotTimeSeries(source >> 1, "DisplacedLow", "Price", Color.FromArgb(255, 0, 0, 0));          pct = 0.00;          pct = (100.0 - pct) / 100.0;          multSource = source * pct;          source2 = new SMA(bars.High, 2);          PlotTimeSeries(source2 >> 1, "DisplacedHigh", "Price", Color.FromArgb(255, 0, 0, 255));          pct2 = 0.00;          pct2 = (100.0 + pct2) / 100.0;          multSource2 = source2 * pct2;          StartIndex = 2;       }       public override void Execute(BarHistory bars, int idx)       {          int index = idx;          Position foundPosition0 = FindOpenPosition(0);          bool condition0;          if (foundPosition0 == null)          {             condition0 = false;             {                condition0 = true;             }             if (condition0)             {                val = multSource[idx];                _transaction = PlaceTrade(bars, TransactionType.Buy, OrderType.Limit, val, 0);             }          }          else          {             condition0 = false;             {                condition0 = true;             }             if (condition0)             {                val2 = multSource2[idx];                ClosePosition(foundPosition0, OrderType.Limit, +val2);             }          }       }       private double pct;       private double val;       private TimeSeries source;       private TimeSeries multSource;       private double pct2;       private double val2;       private TimeSeries source2;       private TimeSeries multSource2;       private Transaction _transaction;    } }
0
Best Answer
- ago
#10
Yes, that worked, that is what i meant before, sorry for the mistake
0
- ago
#11
As a follow-up, how would we code a moving average to take into account multiple bars? For example, what if I wanted to plot the simplemoving average of (bars.Close[idx] - bars.Close[idx-1) over a period of 10 days?
0
- ago
#12
Doesn't seem to be a follow-up to Displaced Moving Average. Rather a different question on plotting an arbitrary TimeSeries on a few bars only. In this case please start a new discussion, otherwise can you clarify?
0

Reply

Bookmark

Sort