- ago
Hello...I'm trying to create a test that buys the S&P 500 when an external symbol (a custom model that can cycle from 0 to 100) crosses above 80 then crosses back below 70.

I can get it to buy at a cross above 80, then sell when it crosses below 70, but that's not what I'm looking for. I want the buy conditions to be a cross above 80 THEN a cross below 70, and sell x number of days later.

I've spent a lot of time searching the discussion and can't seem to find what I'm looking for...is it even possible?

Thanks for any help.
0
155
Solved
10 Replies

Reply

Bookmark

Sort
Glitch8
 ( 11.81% )
- ago
#1
Sure it's possible, here's a simplified example:

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) {          rsi = RSI.Series(bars.Close, 14);          PlotIndicator(rsi); } //execute the strategy rules here, this is executed once for each bar in the backtest history public override void Execute(BarHistory bars, int idx) { if (!HasOpenPosition(bars, PositionType.Long)) {             //code your buy conditions here             if (!crossedBelow80)                if (rsi.CrossesUnder(80, idx))                   crossedBelow80 = true;     if (crossedBelow80)                if (rsi.CrossesUnder(70, idx))                   PlaceTrade(bars, TransactionType.Buy, OrderType.Market); } else { //code your sell conditions here     if (idx - LastPosition.EntryBar > 20)                PlaceTrade(bars, TransactionType.Sell, OrderType.Market); } }       //declare private variables below       private RSI rsi;       private bool crossedBelow80 = false; } }
0
Cone8
 ( 25.44% )
- ago
#2
Here's how you might do it with the blocks - use a Multi-Condition and drag the Qualifier "Within the Past N Bars" onto the crossover 80 rule.

0
Glitch8
 ( 11.81% )
- ago
#3
There's no need to use a MultiCondition group here, if you use two conditions they are already ANDed together.

And this approach won't work if the indicator crossed above 80 but remained above 70 for more than 10 bars.
0
Cone8
 ( 25.44% )
- ago
#4
Of course you're right about the MC Group.
And maybe you're not interested in a trade that RSI has been pegged over 80 for 100 bars.
But I did say, "here's how you might do it".
0
Cone8
 ( 25.44% )
- ago
#5
Actually, the requirement specified, "when an external symbol ... crosses above 80.. ".
In that case, it wouldn't be passible with these blocks because you can't have 2 qualifies on the same block.

Glitch's code modified for the external symbol, specified at the bottom -

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)       {          _extBars = GetHistory(bars, _extSymbol, null);                   rsi = RSI.Series(_extBars.Close, 14);          PlotIndicator(rsi);          DrawHorzLine(80, WLColor.Gray, 1, LineStyle.Dashed, rsi.PaneTag);          DrawHorzLine(70, WLColor.Green, 1, LineStyle.Dashed, rsi.PaneTag);       }       //execute the strategy rules here, this is executed once for each bar in the backtest history       public override void Execute(BarHistory bars, int idx)       {          if (!HasOpenPosition(bars, PositionType.Long))          {             //code your buy conditions here             if (!crossedBelow80)                if (rsi.CrossesUnder(80, idx))                   crossedBelow80 = true;             if (crossedBelow80)                if (rsi.CrossesUnder(70, idx))                   PlaceTrade(bars, TransactionType.Buy, OrderType.Market);          }          else          {             //code your sell conditions here             crossedBelow80 = false;             if (idx - LastPosition.EntryBar > 20)                PlaceTrade(bars, TransactionType.Sell, OrderType.Market);          }       }       //declare private variables below       private RSI rsi;       private bool crossedBelow80 = false;       private BarHistory _extBars;       private string _extSymbol = "SPY";    } }

0
Best Answer
- ago
#6
Hmm, based on the code and chart after running the test, it's not what I'm looking to do, though I thank you greatly for your help.

I was hoping there was a function such as THEN instead of AND or OR. Using this example, I want to see what happens when the RSI crosses *above* 80 and THEN (no matter how many days later) crosses back *below* 70. It can't trigger another trade until RSI re-crosses above 80 and then falls back below 70.
0
Cone8
 ( 25.44% )
- ago
#7
Just add this below // code your sell conditions here
crossedBelow80 = false;

I added to the code above.
0
Glitch8
 ( 11.81% )
- ago
#8
Thanks Cone, corrected code is in Post #5. I neglected to reset that boolean variable back to false. With this in place it is EXACTLY what you're looking for :)
0
- ago
#9
Ahh, that was it. Thank you both so much!
1
- ago
#10
Sigh, really sorry about this. Trying to get back into the C# methodology but struggling. The script as updated is perfect...but I need it to allow multiple positions. I've done everything I can do but cannot get it to work. For a single position, it's perfect, I just need it to allow multiple open positions.
0

Reply

Bookmark

Sort