MaSto8
 ( -0.81% )
- ago
Hello,
Is it possible to implement this using building blocks? When one indicator crosses another. Buy after a 3% increase within 4 days

Thanks
0
212
13 Replies

Reply

Bookmark

Sort
Cone8
 ( 12.18% )
- ago
#1
We have a BarsSince indicator, but there isn't a way to apply it properly to a condition and compare it to a number of bars. That would be a handy condition and I'll put it on the "To Do" list.

Anyway, as an example, here's how you'd use BarSince to find how many bars since the Close crossed above a moving average.

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Data; using WealthLab.Indicators; using System.Collections.Generic; namespace WealthScript12 { public class MyStrategy : UserStrategyBase {       IndicatorBase ind2;       IndicatorBase barsSince;        public override void Initialize(BarHistory bars) {          ind2 = SMA.Series(bars.Close,50);          PlotIndicator(ind2, WLColor.Blue);          // bars since SMA crossed over ind2          barsSince = BarsSince.Series(bars.Close > ind2);          PlotIndicator(barsSince); } public override void Execute(BarHistory bars, int idx) { } } }
0
- ago
#2
There's always more than one way to skin a cat. If you drop the 4-day requirement and simply take the slope of the price (converted to percentages with ROC) over 4 bars, you will get something "sort of" similar. Some might even argue this linear regression approach, which takes an average over 4-bars, may give you a more stable solution in some situations.

The pctChgOver4days[idx] > 3.0 is looking for a 3% increase over a 4-bar period, which is different than what you're asking for. Do appreciate that. (I'm not sure the 3.0 number is correct, but the units for slope should be in percentage/bar where the ROC indicator puts price into percentages.)

And this solution is not looking at the SMA of the price, although you could add that.

I don't do Blocks, but someone should be able to convert the approach below into Blocks for you.
CODE:
using WealthLab.Backtest; using WealthLab.Core; using WealthLab.Indicators; namespace WealthScript1 {    public class MyStrategy : UserStrategyBase    {       IndicatorBase pctChgOver4days;       public override void Initialize(BarHistory bars)       {          pctChgOver4days = LRSlope.Series(ROC.Series(bars.Close, 1), 4);       }       public override void Execute(BarHistory bars, int idx)       {          if (HasOpenPosition(bars, PositionType.Long))          {             //sell conditions below                       }          else          {             //buy conditions below             if (pctChgOver4days[idx] > 3.0)                PlaceTrade(bars, TransactionType.Buy, OrderType.Market);          }       }    } }
0
Glitch8
 ( 9.17% )
- ago
#3
We could easily whip up that custom building block for you in a few hours work. If you’re interested email concierge@wealth-lab.com
1
- ago
#4
Just to expand on the alternative linear regression approach, you can add a goodness-of-fit condition to your Buy operation with the WL RSquared indicator. The rSquared value is depicting how well the price gain over 4 bars follows the regression line; if it's perfect, 3% per day over 4 days, it will have a value of 1.0. I would let the WL parameter optimizer set the rSquared threshold to maximize your profit. Below, I have set it to 0.7 minimum in this example.
CODE:
         {             //buy conditions below             if (pctChgOver4days[idx] > 3.0 && rSquared[idx] > 0.7)                PlaceTrade(bars, TransactionType.Buy, OrderType.Market);          }
If you don't know statistics (and the rSquared behavior), get someone locally to help you that does. The more you know the more profit you can make. And it's more fun working with a local partner.
0
MaSto8
 ( -0.81% )
- ago
#5
Hello,
thanks for the support and the solutions. Unfurtunatelly i can´t coding. So i have to do this with blocks.
@ superticker. I havn´t someone to interact with strategies. I learn a lot by thomas vittners billions college. But the most things i do it by myself.
So i hope, glitch will create a new building block.
And who knows, one day i will learn coding.

Regads
MaSto
0
- ago
#6
QUOTE:
Unfortunately I can´t coding. So I have to do this with blocks.

The coded solutions I've given in Post #2 and Post #4 can be done with existing Blocks. There's nothing special about them. Ask someone to convert them to Blocks for you. (Or, since you're a Blocks programmer, you may be able to do it yourself.)

But I wouldn't apply methodology you don't understand. For example, the RSquared (regression correlation coefficient) is about how well the price model follows the linear regression line (LRSlope indicator). If you're not familiar with how that works, either look it up in a statistics book or don't use it. Never treat any methodology as a black box because every black box comes with certain assumptions that may not apply in some cases. Happy computing to you.
1
Cone8
 ( 12.18% )
- ago
#7
We should have a block for BarsSince. We'll get it done eventually, but as Glitch mentioned, the Concierge method is faster.
0
Glitch8
 ( 9.17% )
- ago
#8
How do you envision a block for BarsSince would work? i think it would be easy to craft a customized block for this special case but I can’t see how a generalized BarsSince block would fit within the Blocks framework.
0
Cone8
 ( 12.18% )
- ago
#9
It's actually a simple block:
BarsSince (Indicator1 [comparer1] Indicator2) [comparer2] N bars

Example for above:
BarsSince (Close > MathIndOp (Close * 1.03)) <= 4 bars

Well, that's not exactly it, but if a delay were added here and there, we can get it.
0
- ago
#10
QUOTE:
BarsSince Close > MathIndOp (Close * 1.03) <= 4 bars

Doing this with C# code would be more intuitive and easier. :-)

I think there should be a Block to inject C# code in the Block framework whenever necessary. That would handle all the awkward cases.
0
MaSto8
 ( -0.81% )
- ago
#11
Hello,
a friend of mine created a code. How can i import this code as a block.

Thanks
Markus
0
Glitch8
 ( 9.17% )
- ago
#12
They can create a Buidling Block extension library, here is the API documentation:

https://www.wealth-lab.com/Support/ExtensionApi/BuildingBlock
0
MaSto8
 ( -0.81% )
- ago
#13
Ok, thanks. We will try it.
0

Reply

Bookmark

Sort