- ago
I am writing a custom strategy using a C# coded extension library. I need help on understanding the strategy execution and implementing the needed logic in live trading. Everything works fine in backtesting mode.

My strategy tries to find an entry based on SMA slopes at every bar if there are no open positions at that bar. The scenario is as below

* The market opens at 9 AM. I start WealthLab and the strategy at 11 AM.
* My data provider returns 1-minute candles from 9 AM till the current time.
* My strategy executes on all bars (since 9 AM) and it finds a long entry on the bar at 10:30 AM (which is in the past).
* The trend at 11 AM is still bullish and the strategy thinks that the entered long position (at 10:30 AM) is still good to hold. As a result, it doesn't open a new entry as long the trend is bullish.

This is a problem because the strategy is holding on to a non-existing position and I lose the trend as long as it doesn't find an exit for that position.

How can I implement my strategy logic in live trading such that, it does not hold on to virtual positions (or cancels or squares-off the position)?
0
636
Solved
17 Replies

Reply

Bookmark

Sort
Cone8
 ( 28.32% )
- ago
#1
Your post implies that because the strategy is still in a position, you want it to signal to enter that position at any time in the future. With no conditions?

A strategy could do that if you programmed to do it. Right now, you could add a "sleep in" rule that holds signal triggers that occurred earlier in the session until you wake up. You just need a way to indicate to your strategies that you're "awake and ready".

Probably it's easier though, if you just have the Strategy Monitor running with Auto-Place so that you don't miss the trade in the first place - although I'm not really an advocate for "turn on and forget it" trading.

Also, in Trading Preferences, see "Exit Orphans" - hit F1 and read about the scenarios in the User Guide.

Probably though, you should add your vote to this feature request so that you could more easily program a "missed trade" rule.
https://www.wealth-lab.com/Discussion/Access-Accounts-data-from-broker-programmatically-7914
1
Glitch8
 ( 12.10% )
- ago
#2
If you want to Strategy to not hold these virtual NSF positions, turn the "Retain NSF" off in Strategy Advanced Settings.

0
Cone8
 ( 28.32% )
- ago
#3
Unless I'm missing something, the scenario described is not an NSF scenario.

My understanding is that ravikatha is just starting late and wants to get a signal for the hypothetical position that is already long "and still good to hold".
0
Glitch8
 ( 12.10% )
- ago
#4
Mmmm, these points make me believe that is indeed an NSF that he wants to avoid ... note he starts his strategy at 11:00 am in this scenario:

* My strategy executes on all bars (since 9 AM) and it finds a long entry on the bar at 10:30 AM (which is in the past).

* The trend at 11 AM is still bullish and the strategy thinks that the entered long position (edit: NSF) (at 10:30 AM) is still good to hold. As a result, it doesn't open a new entry as long the trend is bullish.
0
Cone8
 ( 28.32% )
- ago
#5
ravikatha, since Glitch and I disagree on what we think you're seeing, please post a chart of the strategy to remove all doubt.
0
Glitch8
 ( 12.10% )
- ago
#6
Yeah, actually it’s not NSF behavior. It’s a trade it’s just not in the account yet. I think the only way to avoid it would be to ensure you don’t set a data range that would be large enough to include that virtual position.
0
Best Answer
- ago
#7
I am starting late (at 11 AM) and I immediately want to enter a trade (because the trend is bullish). I am not worried and don't care about the signals I missed before 11 AM.

What I want is that my strategy should not hold any positions when I start at 11 AM. That way, I can enter new ones when appropriate at or after 11 AM.

Limiting the data to start at the moment I start the strategy - will work. Also setting the StartIndex in Initialize method to match 11 AM will work. However, how can I implement this in a generic way, so that the implementation will work even if I start at 11:30 AM or 12 PM?

And regarding the NSF positions: from your messages, I understand that the positions opened before 11 AM did not hit the broker account and hence are designated as NSF positions - Is that right? If so, setting
CODE:
BacktestSettings.RetainNSFPositions
to false should discard those positions. If so my problem can be fixed using that setting.
0
Cone8
 ( 28.32% )
- ago
#8
QUOTE:
I understand that the positions opened before 11 AM did not hit the broker account and hence are designated as NSF positions - Is that right?
No. NSF means that there were Not Sufficient Funds for the position to be included in the backtest. A broker account may actually hold positions that the backtest wasn't able to enter.

If you never want to enter a position before 11:00AM, just include that in the condition before your buy or short. For example:

CODE:
public override void Execute(BarHistory bars, int idx) { if (!HasOpenPosition(bars, PositionType.Long)) { if (bars.DateTimes[idx].GetTime() < 1100)                return;         if (bars.Close[idx] > SMA.Value(idx, bars.Close, 20))                PlaceTrade(bars, TransactionType.Buy, OrderType.Market); } else { //code your sell conditions here } }
0
- ago
#9
Can you please tell me where the GetTime() method is documented for the line below? I can't find it in the documentation. Am I missing an extension?
CODE:
if (bars.DateTimes[idx].GetTime() < 1100)
0
- ago
#10
superticker - bars.DateTimes[idx] returns a System.DateTime. Hence, GetTime() is documented by Microsoft for the System.DateTime struct.
0
- ago
#11
I looked earlier on https://docs.microsoft.com/en-us/dotnet/api/system.datetime?view=net-6.0 but didn't find a Method called GetTime(). Exactly where are you looking?
0
- ago
#12
superticker - You are correct. I was simply using Intelisense and incorrectly surmised it was a method on System.DateTime. I guess it is a WL extension method. I too cannot find it documented in WL8 help.
1
- ago
#13
That piece of code (condition) can be written as

CODE:
if (bars.DateTimes[idx].TimeOfDay < TimeSpan.FromHours(11))
0
- ago
#14
Hello Cone

I got your point. But I need a generic solution, rather than hard-coding the "11 AM" in code.

Below are my observations during testing (assuming I open the chart in 1-minute streaming mode and add strategy to the chart at 11:39:05 AM)

1. DataProvider returns the bars till 11:39 AM from as much back date as possible/available. Assuming that the data is only available from 9 AM, it returns 159 bars.
2. Strategy.Initialize is run once.
3. Strategy.Execute is called 159 times - once for each bar.
4. However none of the PlaceTrade calls in the Strategy.Execute are sent to the Broker adapter.
5. Once the bar at 11:40 AM is returned by the data provider, the Strategy.Initialize is called once again with 160 bars.
6. Strategy.Execute is called 160 times - once for each bar.
7. If there is any PlaceTrade call in the Execute method on 160th bar, it is sent to the Broker adapter.

My question is about the logic - how WL8 decides whether to send a PlaceTrade call to the broker adapter or not? Can I check somehow from the returned transaction object (by the PlaceTrade call) to see if it was sent to the broker or not?

0
Cone8
 ( 28.32% )
- ago
#15
Answer: Signals
A "signal" occurs when the strategy executes a PlaceTrade() on the last bar of the chart.

If you go back to my reply #1, the answer you're looking for is there. It sounds to me like you really want the "sleep in" rule.

In other words, you have to let the strategy know, in some way, that you're looking at it. There are a dozen ways you can do it, but it requires some action on your part - probably the simplest would be to use a global variable (see: SetGlobal, GetGlobal) that the strategy sets on its first run of the day.
0
- ago
#16
I cannot come up with a good and foolproof logic for this.

The example you mentioned about using Get/SetGlobal might work for some scenarios, but not for others. For e.g. I am not sure how the logic works in the following scenario
1. Open WL8 and Strategy at 11 AM for symbol ABCD (The Global data gets set as 11 AM)
2. I close the strategy and then open it again at 11:30 AM. (The Global data exists and is 11 AM). At this point, running the strategy will raise the signals between 11 AM to 11:30 AM which is undesirable.

What I need is a way to know, in streaming mode, whether calling PlaceTrade will result in the order being sent to the connected live broker account. I need to know this before calling PlaceTrade.
0
Cone8
 ( 28.32% )
- ago
#17
I don't follow your workflow or the reasons behind it. You want start late, stop, and restart.

If you're worried about Auto-Placing a trade on the first run, just load the Workspace with Auto-Place deselected; or with Auto-Stage.
0

Reply

Bookmark

Sort