question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

average_entry_price in Strategy is wrong

See original GitHub issue

Hello, while backtesting a long-only strategy I notice there might be something wrong in computing the average_entry_price Here it a sequence of buy order (1st one made in go_long, the others in update_position) and how the average_entry_price gets updated

buying qty: 3.3426391274712346 price: 150.44
average_entry_price: 150.44
buying 3.1774795305245025 150.33
average_entry_price: 150.33
buying 3.024307255417514 150.03
average_entry_price: 150.03
buying 2.8473431610486517 151.37
average_entry_price: 151.37
buying 2.6811195961259826 152.7
average_entry_price: 152.7
buying 2.5541417738143384 152.26
average_entry_price: 152.26
buying 2.4468974718562766 150.97
average_entry_price: 150.97
buying 2.318153649389877 151.37
average_entry_price: 151.37
buying 2.2468293065477463 148.35
average_entry_price: 148.35
buying 2.1494655325637435 147.3
average_entry_price: 147.3
buying 2.0424598613965914 147.25
average_entry_price: 147.25
buying 1.9870837092479907 143.77
average_entry_price: 143.77

It looks like average_entry_price always takes the last buying price. Am I doing something wrong or is it a real bug?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
lightyear15commented, Jul 31, 2020

Thanks a lot, self.position.entry_price was the bad boy I was looking for. I agree with you then… if self.position is resuming all info about current position, then self.average_entry_price should report only the average entry price of the last order. I am closing the issue. Thanks again

1reaction
saleh-oldcommented, Jul 20, 2020

Nice catch. I’ll fix it. Thank you

Read more comments on GitHub >

github_iconTop Results From Across the Web

Avg Entry price plot is not correct - NinjaTrader Support Forum
I want to plot average entry (breakeven) price throughout the chart at all times reflecting at all times any position I haven to...
Read more >
Get an open order's entry price in TradingView's Pine Script
It's how TradingView strategies get open trade entry prices. ... the average entry price avgEntryPrice = priceSum / strategy.opentrades.
Read more >
TradingView Pine Script : check previous strategy.entry price ...
I am trying to build a strategy that will buy multiple times (pyramiding) to average down before closing but I want to check...
Read more >
Open TradingView trades with strategy.entry() - Kodify.net
To open a market position our TradingView strategy uses the strategy.entry() function. This function can send market, limit, stop, ...
Read more >
Choosing the Wrong Pricing Strategy Can Be a Costly Mistake
Raju and Zhang say devising appropriate pricing strategies is more critical than ever in a world of hyper-competition. Pricing strategies also ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found