- ago
Hi, guys.

How can I write a code that does the following:

1. Checks if the high of the current candle is lower than the high of the previous candle;

2. If yes, checks how many percent it has retreated from the last highest high (peak) to the high of the current candle, as long as the peak was within 10 candles (pullback). * note: It is NOT the highest high of the last 10 candles, if there is a peak from 7 candles ago and a new peak from 2 candles ago that is less than 7 candles ago, it should be considered the most recent (2 candles ago).

3. If the retracement is less than 10%, place an order on the break of the high of the current bar.

Thanks in advance!
0
697
Solved
3 Replies

Reply

Bookmark

Sort
- ago
#1
Hi Emilio,

Did you know that such pullback can be identified as a pattern as a Price Grid? You could use the PriceGrid Drawing Tool for that:

https://www.youtube.com/watch?v=AlVNWGc2LoI&t=92s

Then it can be used in Building Blocks and as an Indicator:

https://www.youtube.com/watch?v=AlVNWGc2LoI&t=323s
1
Glitch8
 ( 11.08% )
- ago
#2
Here's some code I came up with for these rules, I put the retracement into the SIgnal Name so you can see it when you view the Positions ...

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Indicators; using System.Collections.Generic; using ScottPlot; 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) {          StartIndex = 11; } //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))          {             //entry logic - #1 high is lower than previous high             if (bars.High[idx] < bars.High[idx - 1])             {                //#2 - get most recent peak within the past 10 candles                for (int n = 1; n < 11; n++)                {                   int checkIdx = idx - n;                   if (bars.High[checkIdx] > bars.High[checkIdx - 1] && bars.High[checkIdx] > bars.High[checkIdx + 1])                   {                      //retracement must be less than 10% from this peak                      double retracement = bars.Close[idx] - bars.High[checkIdx];                      retracement = retracement * 100.0 / bars.High[checkIdx];                      if (retracement > -10.0)                      {                         PlaceTrade(bars, TransactionType.Buy, OrderType.Stop, bars.High[idx], retracement.ToString("N2"));                      }                      return;                   }                }             }          }          else          {             //sell after 10 bars             if (idx - LastOpenPosition.EntryBar >= 10)                PlaceTrade(bars, TransactionType.Sell, OrderType.Market);          } } //declare private variables below } }
1
Best Answer
- ago
#3
Thank you both, I will try the code.

P.S.: very nice tool, the price grid!
0

Reply

Bookmark

Sort