- ago
In Blocks is there a condition I can use for percent from Previous day close?
Say in an intraday chart (1 minute) I want add a condition to buy if percent from Previous day close > 2%.
Thank you.
0
340
Solved
11 Replies

Reply

Bookmark

Sort
Cone8
 ( 24.80% )
- ago
#1
Use ScaleInd, Daily with ROC(Close, 1).

Edit - no, that won't give you want you want.

I'm afraid this one of those situations that you'd need a Transformer of a Transformer. You'll have to create custom Indicator "PercentFromYesterdaysClose" and use that.
0
Glitch8
 ( 9.89% )
- ago
#2
FYI: Here's how you'd create and plot that TimeSeries in a C# Coded Strategy.

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Data; using WealthLab.Indicators; using System.Collections.Generic; namespace WealthScript1 { public class MyStrategy : UserStrategyBase { //create indicators and other objects here, this is executed prior to the main trading loop public override void Initialize(BarHistory bars) {          TimeSeries prevClose = TimeSeriesCompressor.ToDaily(bars.Close);          prevClose = TimeSeriesSynchronizer.Synchronize(prevClose, bars.Close);          prevClose = prevClose >> 1;          PlotTimeSeries(prevClose, "PrevClose", "Price", WLColor.Aqua);          changeSincePrevClose = (bars.Close - prevClose) / prevClose * 100.0;          PlotTimeSeries(changeSincePrevClose, "ChgSincePrevClose", "PC", WLColor.LightGreen); } //execute the strategy rules here, this is executed once for each bar in the backtest history public override void Execute(BarHistory bars, int idx) { }       //declare private variables below       private TimeSeries changeSincePrevClose; } }
0
Cone8
 ( 24.80% )
- ago
#3
If you want it work in blocks, here's the code you can use for a custom indicator.

Select "+New Indicator",
name it PercentFromYesterdayClose,
enter a BarHistory for the Source parameter,
Generate code,
Replace the code with the code below,
Compile,
Save,
Restart.

CODE:
using WealthLab.Core; using System; using System.Drawing; using WealthLab.Indicators; namespace WealthLab.MyIndicators1 { public class PercentFromYesterdayClose : IndicatorBase { //parameterless constructor public PercentFromYesterdayClose() : base() { } //for code based construction public PercentFromYesterdayClose(BarHistory source) : base() {          Parameters[0].Value = source; Populate(); }       //static Series method       public static PercentFromYesterdayClose Series(BarHistory source)       {          return new PercentFromYesterdayClose(source);       } //name public override string Name { get { return "PercentFromYesterdayClose"; } } //abbreviation public override string Abbreviation { get { return "PercentFromYesterdayClose"; } } //description public override string HelpDescription { get {             return @"Returns the percent change from yesterday's last price for an intraday chart. Use Pre/Post Filter to get yesterday's close"; } } //price pane public override string PaneTag { get { return "PercentFromYesterdayClose"; } }       //default color       public override WLColor DefaultColor       {          get          {             return WLColor.FromArgb(255,0,0,255);          }       }       //default plot style       public override PlotStyle DefaultPlotStyle       {          get          {             return PlotStyle.Line;          }       } //populate public override void Populate() {          BarHistory source = Parameters[0].AsBarHistory; DateTimes = source.Close.DateTimes;          BarHistory dailybars = BarHistoryCompressor.ToDaily(source);          dailybars = BarHistorySynchronizer.Synchronize(dailybars, source);          for (int n = 1; n < source.Close.Count; n++)          {             double yestClose = dailybars.Close[n];             if (source.IsLastBarOfDay(n))             {                // use the value from the previous bar in order to show the Close to Close percent. Otherwise this would be zero.                yestClose = dailybars.Close[n - 1];             }              Values[n] = 100 * ( source.Close[n] / yestClose - 1 );          } } //generate parameters protected override void GenerateParameters() {          AddParameter("Source", ParameterType.BarHistory, null); } } }
0
Best Answer
- ago
#4
Thank you, Both indicators plot correctly. If I wanted to implement it as a Block in a strategy I've tried it like so but it is not giving me the correct answer. See image below.

0
Cone8
 ( 24.80% )
- ago
#5
You should use Compare Indicator to a Value. The idea is to compare the indicator to the number 2(%).
0
- ago
#6
I noticed there are some divergences from the indicator to the price where even though the change was positive the indicator was negative and vice versa ( see chart arrows below).
Not sure why that happens.


0
- ago
#7
Cone's code is designed to be run on intraday data but you're running it on an EOD chart which doesn't make sense to me.
0
- ago
#8
Why not?

0
- ago
#9
@Eugene, thanks I did not realize there would be a difference, it is just easier to visualize.
0
Cone8
 ( 24.80% )
- ago
#10
You asked for something to use for an intraday strategy to compare to the previous day close. I went the extra mile to make sure the last bar of the day was compared too. You can add code to disable that test for daily scales.

But for Daily, just use ROC(bars.Close, 1). (shrugman)
0
- ago
#11
@Cone, I am using it for intraday charts with no problem, It works really good when I apply it with Compare Indicator to a Value block. I was just wondering why it does not work in daily chart as well. Thanks a lot for your help I appreciate it.
0

Reply

Bookmark

Sort