- ago
@Eugene, I found this archive post from WL6 regarding the persistence of a Limit Order when using intraday data. It reads like you confirmed that Limit orders persist for one bar, and if limit is not hit, the Limit Order would expire on that bar and would have to be placed once again. E.g., with 30-minute data, a Limit Order would be good for only 30 minutes.

Is this true for WL8 as well? Is there a way to make a limit order good for the day if you are using intraday data? Or would that only work with daily data?

ADDITION
Here is the link for the post I referenced, which I forgot to add
https://wl6.wealth-lab.com/Forum/Posts/Live-Trading-Cancel-amp-Replace-Limit-orders-38001
0
532
Solved
19 Replies

Reply

Bookmark

Sort
Glitch8
 ( 11.64% )
- ago
#1
The way to make the limit order good for more than one bar is to simply place it again the following bar. If you’re auto trading WL will see that the order is really the same and will just leave it alone.
1
- ago
#2
Thank you, but I would like to preserve / retrieve the original Limit price, which is based on market prices at the time of initial placement. The scenario is live trading using SM, not backtesting, and the bars are 30 min apart. So, the strategy reruns every 30 min. Never figured a way to pass data from one strategy run to the next, except saving data to a file. Is there a structure like Pending Order list I could query that would reveal the limit price and then I could re-place it? Or is that data lost once before the strategy runs on the next bar?

I'm assuming the broker wouldn't cancel the limit until end of day, so WL must be issuing a cancellation order to the broker.

0
Glitch8
 ( 11.64% )
- ago
#3
You can save the value in a variable in your strategy. There are actually numerous ways to accomplish this. But in order to keep the order active it needs to be placed each bar. That’s just how WL works at the architectural level.
0
Best Answer
- ago
#4
Sorry I'm unclear. If the strategy runs at 10 AM and I place a limit order for XYZ

CODE:
private static double _limPrice ; _limPrice = bars.Close[idx] * 1.01


and then at 10:30 the strategy runs again, won't _limPrice be null? It's not cached once the strategy completes, right?

This is for live trading, not backtesting, so this code would only be executed on the last bar.
0
- ago
#5
You can save the double value to Position.Tag, WL's global memory, a disk file, database, network resource... plenty of options.
0
Cone8
 ( 6.07% )
- ago
#6
CODE:
//** SHOULD NOT be static ** // private static double _limPrice ;

If you're trading/testing on a DataSet, each symbol that runs get an instance of the strategy class. A static is variable is shared will all instances of the same class. The last strategy that executes would be assigning the limit price to all other symbols that followed it!

There are reasons to use static variables, but this is not one of them.

This example shows how to do it using a private variable to store the price at a specified time of day and use it for the rest of the day.

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using WealthLab.Data; using WealthLab.Indicators; using System.Collections.Generic; namespace WealthScript3 { public class MyStrategy : UserStrategyBase { public override void Initialize(BarHistory bars) {          PlotStopsAndLimits(3);     } public override void Execute(BarHistory bars, int idx) {          // set the limit price for the rest of the day at 1030          if (bars.DateTimes[idx].GetTime() == _time)             _lmtPrice = bars.Close[idx] * 1.01;           if (!HasOpenPosition(bars, PositionType.Long)) {             if (bars.DateTimes[idx].GetTime() >= _time && !bars.IsLastBarOfDay(idx))             {                Transaction t = PlaceTrade(bars, TransactionType.Short, OrderType.Limit, _lmtPrice);             } } else {             Position p = LastPosition;             if (idx - p.EntryBar + 1 > 10 || (idx < bars.Count - 1 && bars.IsLastBarOfDay(idx + 1)))                ClosePosition(p, OrderType.Market); } }       private double _lmtPrice;       private int _time = 1030; } }
3
Glitch8
 ( 11.64% )
- ago
#7
>> and then at 10:30 the strategy runs again, won't _limPrice be null? It's not cached once the strategy completes, right?<<

It’s not clear in this code where you are declaring this variable. Let’s assume you declare it in the private variables section, then assign it a value somewhere in Execute.

Then No it won’t become null. It will keep its value from the prior Execute call or assume some new value if you assign it something else. And like Cone said you should not use static here.
0
- ago
#8
QUOTE:
You can save the double value to Position.Tag, WL's global memory

Wouldn't these values disappear after the strategy completes?

ADDENDUM
After reading @Cone's sample code, I realized that I may be making an incorrect assumption about how intraday (or daily) strategies run in the SM. My assumption has been that once the WL strategy is completes a run on each bar (e.g., 30 min), all variables are cleared. In other words, I was thinking that when it runs on each bar, it is like re-running a strategy in the Strategy Window. This is the basis of all my questions about how to preserve a variable value from one bar to the next.

Do strategies running in the SM preserve all their values from bar to bar (and day to day?) unless the WL app is closed? In effect, is the strategy in a wait mode (for next bar), but not exited, with all values preserved?
0
Glitch8
 ( 11.64% )
- ago
#9
What do you mean completes? The Execute method is called every bar, and the value will remain for each call.
0
- ago
#10
QUOTE:
You can save the double value to Position.Tag, WL's global memory

QUOTE:
Wouldn't these values disappear after the strategy completes?

Ahhhh. The Position.Tag reference will disappear after the strategy completes. Is that a problem?

WL's global memory will remain valid until you quite WL for the day. Is that a problem? After the trading day, it forgets.

Just be careful when using WL's global memory. If you use SetGlobal(...), all executing strategies (and stocks) are going to be able to read and write into that location. Is that what you want?

In contrast, if you place the values in the Bars.Cache, which is unique to a particular BarHistory, then only that particular stock will have access to that value. Is that what you want?

You can declare variable in the MyStrategy {block} which are only accessible to that MyStrategy instance. Just understand you can have 3 to 8 MyStrategy instances running at the same time by the parameter optimizer. Did you read https://www.wealth-lab.com/blog/anatomy-of-a-wl7-strategy to understand the scoping of C# variables?
0
- ago
#11
Thanks @Glitch. I explained this in the Addendum in the post above, that crossed your post in the cloud. I think you just answered the question I was asking there.

One question I still have: does the SM preserve strategy values from one day to the next?

Wow. My questions must have been very confusing to the WL team as you didn't (and I didn't) realize how incorrect my starting premise was. Very sorry for that. This has been highly educational for me. Unfortunately for you, this is the first time I'm trying to run an intraday strategy in the SM, and I didn't fully appreciate how the SM works. I wonder if this topic merits a helpful graphic like https://www.wealth-lab.com/blog/anatomy-of-a-wl7-strategy

Thanks all, for your patience and support.
0
- ago
#12
QUOTE:
Do strategies running in the SM preserve all their values from bar to bar (and day to day?) unless the WL app is closed?

That depends on where you declared them (which sets the "scoping" of those variables in C#).

If you declare them in the MyStrategy {block}, then they are retained between Execute calls. If you declared them in the Execute {block}, then as local variables to Execute, they are not retained between Execute calls.

It's important to note that multiple variables with the same name can exist. So if you declare x in MyStrategy and x in Execute, these are entirely different variables (with different values and ...) with different scoping properties. Re-read the chapter of your C# textbook that talks about declarations and how they are scoped differently. Afterwards, read the https://www.wealth-lab.com/blog/anatomy-of-a-wl7-strategy again to be sure you understand C# scoping. And all modern computer languages are scoped in the same way.

---
Let me add, this is a C# thing, not an SM thing.
0
- ago
#13
Thanks @superticker for the summary. I have to think through your questions. As you can see from my most recent post, I have gotten a whole new perspective on this topic. I will bookmark your post for future reference when I am ready to tackle it.

Yes, I have reviewed the anatomy material multiple times, mainly as reference material and mostly focusing on how the different methods work. I didn't pay enough attention to scoping and the lifetime of variables. That was very helpful material very pertinent to this discussion.
1
- ago
#14
QUOTE:
If you declare them in the MyStrategy {block}, then they are retained between Execute calls. If you declared them in the Execute {block}, then as local variables to Execute, they are not retained between Execute calls.


Is MyStrategy {block} is at the class level of the strategy code (where example WL strategies store private global variables), and Execute {block} is within the Execute() method?
0
- ago
#15
QUOTE:
Let me add, this is a C# thing, not an SM thing.

Meaning there is no difference in variable scoping or lifetime whether a strategy is run in Strategy Monitor or Strategy Window?
0
- ago
#16
QUOTE:
Is MyStrategy {block} is at the class level of the strategy code (where example WL strategies store private global variables), and Execute {block} is within the Execute() method?
That is correct. Study the examples given in the chapter of your C# textbook discussing declarations paying particular attention to how each declaration location scopes the variable that's being declared.

Also, read between the lines of your C# textbook. Why would the architecture of the language allow two independent versions of a variable named x to be "active" in the same code? When you understand the principle of the architecture, then it will all make sense.

QUOTE:
Meaning there is no difference in variable scoping or lifetime whether a strategy is run in Strategy Monitor or Strategy Window?
That is also correct. The variable scoping is a function of the C# language. But appreciate that one part of WL could declare (and scope) its variable differently than another. So be aware of where those declarations are placed in a given WL part.

In addition, the rule of thumb is to narrowly scope all declarations as much as possible. This is to reduce confusion, but it also makes the garbage collector more efficient because tighter code means the garbage collector has to move the variables around less to reclaim heap space, which improves processor cache hit ratio. Read the part of your C# textbook that discusses Principle of Locality.
1
- ago
#17
Hmmm.... Maybe I should get a C# textbook ;)

@superticker Seriously, this is all very helpful. Thanks.
1
Cone8
 ( 6.07% )
- ago
#18
Did you overlook the working example of exactly how to do it in Post #6?
0
- ago
#19
No, in Post #8 I mentioned it was reviewing the code you posted there that made me realize I completely misunderstood how the strategy code works in the SM with regard to the lifespan of variables.

(Please point me to the correct markup syntax for embedded links)
0

Reply

Bookmark

Sort