ww58
- ago
I did not find the ability to see the history of trades, and this can be very useful. At least in some basic form, so that it would not be necessary to get into the broker interface, which is not always convenient.
5
766
15 Replies

Reply

Bookmark

Sort
- ago
#1
As mentioned on the forum days ago by @Glitch, Broker APIs usually do not return a history of trades.

If it's useful for you, what if you export it from Positions tab and import the trade history from file using New strategy > "Trade history strategy"?
0
Cone8
 ( 7.89% )
- ago
#2
I think what ww5 is asking is that we cache the live trade fills to disk file. Pretty reasonable idea. In WL6 we did it.

But it's true that about the Trade History backtests. If you have an account with IB, TDA, or similar, you can export the broker trades, point the Trade History backtest to the trade file.
0
ww58
- ago
#3
Cone, right, that's what I mean. Save the trades made instead of getting the whole history from the broker.
0
ww58
- ago
#4
Maybe until this saving functionality is implemented, there is some hack to catch position closures from the C# code? Broker reports are good if it is a test of 1 strategy, but when there are several of them they are of no use
0
Cone8
 ( 7.89% )
- ago
#5
From a strategy perspective, you could query an account's positions at the end of a bar. You could discover and save changes in symbol's quantity, for example, but not necessarily a fill price.

For example, if at 10:05 you had 100 AAPL, and you found that there was "no AAPL" at 10:15, you only know that the position was exited, but can't tell the price at which it was sold... though you could reasonably assume that it occurred at (or near) the open for a market order, or at the limit price for a limit order.
0
MIH8
- ago
#6
I like that. Simplifies things for the user. Thumbs up.
0
ww58
- ago
#8
I have tried to save information about open trades in AssignAutoStopTargetPrices. Simply I've added "File.AppendAllText(...)" in this function and and it led me to an incomprehensible result. In this file, every time I run the strategy, I get information on all possible trades that could have been in the last 1000 bars, as indicated in my strategy monitor. Although the expected result is that the file will record trades only on real signals.

Also, it turns out I need somehow to know whether the strategy is launched from the strategy monitor or in the backtest, so as not to save the information from the backtest in vain.
0
Glitch8
 ( 11.29% )
- ago
#9
No problem, use code like this:

CODE:
if (Backtester.ExecutionMode == StrategyExecutionMode.Strategy) { }
0
ww58
- ago
#10
StrategyExecutionMode.StrategyMonitor helped to get signals only when running in Strategy Monitor, however I still can't understand why the strategy is executed like it was in a backtest.

For example such a strategy
CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using System.IO; namespace WealthScript12 { public class MyStrategy : UserStrategyBase { public override void Initialize(BarHistory bars){ } public override void Execute(BarHistory bars, int idx) {          Position foundPosition0 = FindOpenPosition(0);          if (foundPosition0 == null)          {             PlaceTrade(bars, TransactionType.Buy, OrderType.Market, 0, 0, "Buy At Market (1)");          }          else          {             ClosePosition(foundPosition0, OrderType.Limit, foundPosition0.EntryPrice * ((10.00 / 100.0) + 1.0), "Sell at 10% profit target");             ClosePosition(foundPosition0, OrderType.Stop, foundPosition0.EntryPrice * (1.0 - (10.00 / 100.0)), "Sell at 10% stop loss");          } }       public override void AssignAutoStopTargetPrices(Transaction t, double basisPrice, double executionPrice)       {          if (Backtester.ExecutionMode == StrategyExecutionMode.StrategyMonitor) File.AppendAllText(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\trades.txt", t.Symbol + " " + t.ExecutionDate.ToShortDateTimeString() + " " + t.PositionType.ToString() + Environment.NewLine);       } } }
on a 50 symbols dataset and one-time launch via run now gives a file with 151 entries.
0
Glitch8
 ( 11.29% )
- ago
#11
You might be misunderstanding how the strategy monitor works. It runs a backtest just like the strategy window, with some optimizations like not processing cosmetic chart methods.
0
ww58
- ago
#12
Yeah, I thought it only processes the last bar. How can I change this strategy to record only actual trades in the present time?
0
Cone8
 ( 7.89% )
- ago
#13
This should work, but keep in mind that this method is used for Entries only.

CODE:
public override void AssignAutoStopTargetPrices(Transaction t, double basisPrice, double executionPrice) {          if (Backtester.ExecutionMode == StrategyExecutionMode.StrategyMonitor)             if (t.EntryDate == t.Bars.DateTimes[t.Bars.Count - 1])                File.AppendAllText(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)                   + @"\trades.txt", t.Symbol + " " + t.ExecutionDate.ToShortDateTimeString()              + " " + t.PositionType.ToString() + Environment.NewLine);       }
1
ww58
- ago
#14
CODE:
t.ExecutionDate.ToShortDateTimeString()
returns 01/01/0001 00:00 while live in strategy monitor. What can I replace it with?
0
Glitch8
 ( 11.29% )
- ago
#15
Try replacing it with Backtester.CurrentDate.
1

Reply

Bookmark

Sort