- ago
Hi,

Sorry if this is a dumb question I cant find the answer in help or discussions.

How do I compare the current price to an indicator. I can see that there is OHLC but if I want to compare the current price within a bar is that possible?

Thanks
0
502
Solved
25 Replies

Reply

Bookmark

Sort
- ago
#1
Indeed the question is not clear. Why would you want to compare price to an indicator? They may have different units. In Blocks, "Indicator Compare to Indicator" is the block to compare OHLC to an indicator.
0
- ago
#2
QUOTE:
compare the current price to an indicator. ... compare the current price within a bar ...

What do you mean, "current price within a bar?" Are we talking about the Bid and Ask prices of the streaming (i.e. current) bar?

Understand, WealthLab is a backtesting product, not a trading platform. We are typically comparing historical bars to indicators, not Bid and Ask prices. So when you say "current price," I'm thinking of the Bid and Ask prices of the streaming bar, which is an odd thing to think about in a backtesting product. I think I must be misunderstanding you.

You can certainly compare historical prices of 1-minute bars, if that is what you mean. Is that what you want to backtest?
0
- ago
#3
Hi @Eugene,

I want an entry defined by high/low compared to the previous bar and a trigger defined by the current price compared to the open/close.

So if I am trading on a 1 day timeframe, when the price goes beyond open/close during the day (in the current bar) , the trade will be triggered.
0
- ago
#4
Hi @Superticker

You are right, I want to be able to backtest a strategy that does that. Perhaps this is possible to use OHLC at a lower timeframe?
0
- ago
#5
@Eugene

I think I have it I can use Scaleind - let me try that.
0
- ago
#6
QUOTE:
You are right, I want to be able to backtest a strategy that does that. Perhaps this is possible to use OHLC at a lower timeframe?

So you want to place a bracket order (i.e. Place a Limit order and Stop order simultaneously, OCO.) based on the previous 10-minute bar?

Usually, I would place either a Limit or Stop order for a Buy operation, but not both. Most buy-low traders place Limit Buy orders so they aren't overpaying for a stock; the goal is to buy the stock as cheaply as possible when there's a momentary dip. You can certainly craft the Limit price based on the OHLC of the previous 10-minute bar and use an indicator to compute that Limit price.
0
Cone8
 ( 25.44% )
- ago
#7
A "Buy side" (entry) OCO bracket order isn't supported. OCO stop/limit for exits only. But I'm not convinced that's what's being requested.

QUOTE:
So if I am trading on a 1 day timeframe, when the price goes beyond open/close during the day (in the current bar) , the trade will be triggered.
Please define which open/close you're referring to. A clear example would help using real symbols and prices.

Example:
On 8/15 AAPL opened at 178.88 and closed at 177.45. On 8/16, I want to...
0
- ago
#8
Im sorry I have confused everyone. Let me try and clarify. I am trying to test a dip buying strategy.

On a daily timeframe, if a gap down occurs between the low of the previous day and the open of the current day and then the price retraces to be greater than the close of the previous day then buy.

I cant see how to do this. How might I do this, please?
0
- ago
#9
Now that your objective is clear, let me rename the topic title to reflect it. To learn how to make a comparison to the current day's open price, please see the ExecuteSessionOpen example in the QuickRef. The idea is to get the unfinished session's open price in this special method to override and place a limit order there.
0
- ago
#10
Hi @Eugene,

Thanks I have looked at that and can see how to use it but it is only part of the requirement.

Once I have identified the gap, am I then able to identy that the price has retraced to the previous close to meet the 'buy' criteria.
0
vk8
 ( 52.35% )
- ago
#11
I think you are trying to do this

0
- ago
#13
QUOTE:
Once I have identified the gap, am I then able to identy that the price has retraced to the previous close to meet the 'buy' criteria.

At first glance one should be accomplish this in C# code. It's a simple comparison if you use TimeSeriesCompressor or ScaleInd, right?
0
- ago
#15
Hi @Eugene

Im sorry I dont have the skills for that. Not possible in building blocks?

Thanks Tim
0
- ago
#16
Tim, no Block I'm aware of does what you need i.e. ExecuteSessionOpen is currently a C# code endeavour.
0
Cone8
 ( 25.44% )
- ago
#17
ExecuteSessionOpen() with C# code is definitely the way to go with this one, but...

ScaleInd(bars.Open) and the Open series result from BarHistorySynchronizer could be made to advance the open to the first intraday bar of the day. It's a special case because the open is known throughout the day and won't change.

I don't know what Dion thinks about it, but maybe we can add a BarHistorySynchronizer.SyncOpen() method that does this without affecting current code. The result would be the same as BarHistorySynchronizer.Synchronize() for the HLC series, but the Open series would sync to the first intraday bar of the day.

Actually, BarHistorySynchronizer.SyncOpen() could return a TimeSeries for just the Open. No need to mess with the other series since the result is the same.
0
vk8
 ( 52.35% )
- ago
#18
I think the screenshot I posted will be good for backtesting the idea?
0
Glitch8
 ( 11.81% )
- ago
#19
I believe he’s trying to identify and act upon a gap at the open price, and act on the same bar, not the following bar like your screen shot would do.
0
Cone8
 ( 25.44% )
- ago
#20
QUOTE:
On a daily timeframe, if a gap down occurs between the low of the previous day and the open of the current day
I was going to quickly code this for you, but what does it mean to "gap between" if you're using the open of the current day as the gap measure?

Did you mean this?
"...if a gap down occurs between the low and close of the previous day..."
or probably
"...if a gap down occurs between the low and open of the previous day..."
0
Cone8
 ( 25.44% )
- ago
#21
Anyway, here's the C# code for the latter to play with. It includes a 5-bar time-based exit and a stop loss 1/2% below the Low of the signal bar.

Just open a New C# Coded strategy, replace the code with this and run it.

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Data; using WealthLab.Indicators; using System.Collections.Generic; namespace WealthScript123 { 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) {          PlotStopsAndLimits(3); } //execute the strategy rules here, this is executed once for each bar in the backtest history public override void Execute(BarHistory bars, int idx) { }               public override void ExecuteSessionOpen(BarHistory bars, int idx, double sessionOpenPrice) { Position foundPosition0 = FindOpenPosition(0);                    if (foundPosition0 == null)          {             if (sessionOpenPrice > bars.Low[idx] && sessionOpenPrice < bars.Open[idx])                PlaceTrade(bars, TransactionType.Buy, OrderType.Stop, bars.Close[idx] + 0.01, 0, "Buy at Stop");                       }          else          {             // sell after 5 bars             if (idx - foundPosition0.EntryBar > 4)                ClosePosition(foundPosition0, OrderType.Market);             // 0.5% below low of signal bar Stop Loss             ClosePosition(foundPosition0, OrderType.Stop, bars.Low[foundPosition0.EntryBar - 1] * 0.995, "SL");          } } } }
0
Best Answer
- ago
#22
Does the exchange allow two simultaneous orders (a Market and Stop order) on the same shares or is there a missing inner Else statement as shown below? (Perhaps the WL backtester automatically assumes an Else condition; I don't know.)
CODE:
      else       {          // sell after 5 bars **** Else ***          if (idx - foundPosition0.EntryBar > 4)             ClosePosition(foundPosition0, OrderType.Market);          // 0.5% below low of signal bar Stop Loss          else // added a missing Else statement             ClosePosition(foundPosition0, OrderType.Stop, bars.Low[foundPosition0.EntryBar-1] * 0.995, "SL");       }
0
Cone8
 ( 25.44% )
- ago
#23
Yes, automatic order pruning takes care of this. Else not required.
1
- ago
#24
Hi @cone and everyone

Thanks for the code, this amended logic locates the correct set up:
CODE:
if (sessionOpenPrice < bars.Low[idx])


i.e. gap down = open < previous day's low

Thanks for your help.
0
- ago
#25
How about if you coded it both ways with a different Signal message as so:
CODE:
   if (foundPosition0 == null)    {       if (sessionOpenPrice > bars.Low[idx] && sessionOpenPrice < bars.Open[idx])          PlaceTrade(bars, TransactionType.Buy, OrderType.Stop, bars.Close[idx] + 0.01, 0, "Swing up overnight");       else if (sessionOpenPrice < bars.Low[idx])          PlaceTrade(bars, TransactionType.Buy, OrderType.Stop, bars.Close[idx] + 0.01, 0, "Swing up during day");    }
Now you could use a Performance Visualizer to compare the "Swing up overnight" case with the "Swing up during day" case. Perhaps some stocks might prefer one case over the other.

I'm not sure if the above Else is required or not, but it shouldn't hurt to include it.
1

Reply

Bookmark

Sort