Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

simulating order types TWAP and or VWAP #203

Unanswered
eervin123 asked this question in Q&A
Discussion options

Is there a simple way to create an order type that will simulate a "fill price" based on average prices in the future? eg. TWAP (time weighted average price) VWAP (volume weighted average price). I was thinking about an instance variable in Order like 'limit' or 'stop' in the meantime, I was thinking I might modify the data creating additional columns VWAP and TWAP in addition to {open, high, low, close}. VWAP and TWAP would contain future information for trade purposes but not for signal purposes.

The justification is obviously to simulate more real world hypotheticals. If managing meaningful capital, orders would need to be spread out over time and volume once the signal recommends a trade.

I realize this is meant to be a lightweight simple to use package, so maybe this is too much to think of from a feature perspective, but perhaps there is a simple work around that I can put together as an example for the docs.

You must be logged in to vote

Replies: 2 comments 2 replies

Comment options

I might modify the data creating additional columns VWAP and TWAP in addition to {open, high, low, close}

This is the supported and recommended approach. Anything you pass in as data will be available the same way as computed indicators and OHLC price points are.

You may then save the computed statistics (and such, whatever you're after) into an object and process it after backtest run.

For rolling VWAP, I'd do:

class Example(Strategy):
 def init(self):
 df = self.data.df
 def VWAP(prices, volume, n):
 return (prices * volume).rolling(n).sum() / volume.rolling(n).sum()
 self.vwap = self.I(VWAP, df.Close, df.Volume, 20)
 ...

Have it plotted and all.

You must be logged in to vote
0 replies
Comment options

Is there a simple way to create an order type that will simulate a "fill price"

On second read, I don't quite get it ...

If managing meaningful capital, orders would need to be spread out over time and volume once the signal recommends a trade.

Why not just place multiple orders? 🤨 Something like:

signal = signal.rolling(n).max() # spread the signal to n bars
order_size = order_size / n
You must be logged in to vote
2 replies
Comment options

eervin123 Jan 1, 2021
Author Sponsor

I think this answered my questions but I haven't marked it as answered yet, because I haven't had a chance to play with it and wanted to try it so I can understand it. I'm sure you are right, and I'm just being dense but, I just don't understand how what you wrote looks at the future not the past. Reading your responses here it seems to me that you are looking at the previous n bars of the data, but I wan't my orders to be based on the future which the next() function doesn't know yet. Is it possible that my lack of understanding comes from not using the SignalStrategy class yet? My questions were for the base Strategy class.

Like I said, I haven't had a chance to roll up my sleeves yet to dig in. I'm sure I'll solve my own ignorance once I tinker with your answer. I just didn't want you to think that your answer wasn't appreciated so I chimed in here.

Comment options

kernc Jan 2, 2021
Maintainer

Yeah, what I meant with the snippet above is that you ought to spread the "signal" to several consecutive bars and have orders executed at each, either via SignalStrategy or plain if self.signal: self.buy(...). This way, your average price / trade performance will reflect the average price / dynamics of the market.

If that's not it, and if passing extra data columns doesn't help, I guess I might not have understood your intent completely. What's an example of pseudocode you'd ideally expect?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /