Can a Strategy get out of sync with live trading?
Author: thetraderman
Creation Date: 6/24/2011 12:59 PM
profile picture

thetraderman

#1
I have a basic question on exits when a price target has been reached. I noticed with paper trading, probably the same with live trading is should the price bar touch my exit price, WL does not get out on that price because we use the bar+1 concept. So what happen is WL exits on the next bar which usually ends up with a worse price (the opening price of the next bar). Yes, sometimes it could be to my benefit but from my experience I get a worse price. I am sure this was discussed many times before and I can't find where I read the answer to a similar question asked by someone else but is there any way I can have WL exit on that bar and at that price? I know there is slippage but I was hoping when trading a highly liquid stock I can chnag ethe code to have it exit right on that point.

Another question similar to the above is that there seems to be a miss-alignment between WL and the behavior of my script. At times but not always when a price is reached, my script shows that the trade is completed with a profit or loss but for some reason, WL continues with the trade not noticing that the price target was reached. What I have to end up doing is close out the position by hand to sync up WL with my script. After that, the two are in sync for a while until another trade sometime later causes the same issue. I did select exit orders should fill all positions in the preference trading tab. I would think WL would communicate with the program using the IsLastPositionActive loop. Can this occur in live automated trading? If yes I guess I have to sit by the machine all the time.

Thanks!
profile picture

Eugene

#2
QUOTE:
but for some reason, WL continues with the trade not noticing that the price target was reached.

Some reason might be having two trading loops?
profile picture

thetraderman

#3
No, there is no two trading loops. That problem was corrected a long time ago.
profile picture

Cone

#4
QUOTE:
WL does not get out on that price because we use the bar+1 concept.
This is a total misconception and not a "concept" that Wealth-Lab invented. A bar is processed when it is complete and you enter/execute orders for/on the next bar, i.e., bar + 1.

Example:
It's Friday and U.S. markets are closed. After you update your data you want to enter a stop or limit order for Monday. The value is calculated today, on "bar", for Monday, bar + 1. This is how everyone in the world enters EOD orders, and the same concept applies to intraday trading.

It sounds to me like you have a peeking script and/or are not properly employing bar+1. You better let us take a look at your script so that we can point out the problem. Use Support if required.
profile picture

thetraderman

#5
I tried an actual trade with real funds and although I got in OK, for some reason WL did not get me out using my stop. I had to manually get out by using the order box within WL. Please take a look at the code and let me know if you see something. To be clearer WL did say I was out but my order did not get executed which means WL and the script was out of sync with fidelity. What I noticed happens is that if my trade is not exited by fidelity even though WL says I'm out, I end up with multiple trades, ones that did not get closed out originally although WL said I was out and then new orders get entered since WL and the script thinks the previous orders have been closed. I only want to have one order opened at a time. Thanks.

CODE:
Please log in to see this code.
profile picture

Cone

#6
QUOTE:
for some reason WL did not get me out using my stop.
What does that mean exactly? Did WLP generate the Alert? Did the Alert become an order in the Orders tool? Was the Alert place on the backend? If "yes" to all of this, it seems impossible to me that an order could have ticked your stop trigger price and not be executed on the Fidelity back end. The reason is that Fidelity will execute your stop when the inside bid crosses a stop trigger. (And if you're filled at a better price and a tick doesn't actually occur at the stop price, WLP will still think you have a position to exit - a very rare case.)

QUOTE:
WL and the script was out of sync with fidelity.
Getting out of sync is at least 100x more probable for a limit order script, but as I indicated above, it's a very rare case for stop orders and even then, it would be the other way around.

QUOTE:
if you see something... I only want to have one order opened at a time.
It's hard to tell with a partial script that doesn't auto-format correctly when copied, but it looks to me like you have entry logic in the "IsLastPositionActive" block. In other words, that "else" into the entry logic is lined up with/modifies if(p.PositionType == PositionType.Long). That would give you the multiple positions that your script is not prepared or designed for.

With everything that you've described, this must be the problem (coding/logic error). It should be obvious to you by inspecting a chart that your script opens multiple Positions. Does it?
profile picture

thetraderman

#7
I never got multiple open positions ever, even in paper trading. The only reason I got multiple positions is because WL/script thought my trades were closed but in reality they were not. So not knowing any different WL/script opened a new position assuming the others were closed. I just got off with technical support who said I should go through the forum which I am.

I am not sure if I got an alert (I'll run the system again). If WL draws an arrow for buy/exit position doesn't that mean that it sent an alert, otherwise what is the point of it? I did a copy/pase and removed excess code and comments which could be why there is a mis-alignment. I'll check again but as I mentioned the script get's in OK, it is the exit logic. Do you see anything weird in the exit logic other than what you mentioned?

Thanks.
profile picture

thetraderman

#8
It just occurred to me that I did get an alert, but it was labeled ACTIVE in the orders screen. In the meantime WL/script thought the position was closed when in fact it did not close. The positions stayed active until I manually closed out the trades many minutes later. Just so you know I was trading QQQ at 500 shares. As you know QQQ is traded heavily and I should have easily been out of the trade.
profile picture

Cone

#9
Getting out of synch is always a possibility, but it's so unlikely in the way you described that I'd say it's nearly impossible if the systems on both ends were running without error and little delay.

If you want to pursue this with me, please create a support ticket and attach your script to it. I need a running script to investigate if there's a problem with it.
profile picture

thetraderman

#10
Thanks. Technical support asked me to rebuild the database by backing everythung up and doing a re-install. Maybe that will help. If not I'll open a ticket. The issue is the script is quite large several thousand lines of code. Will you have time to look at it?
profile picture

Cone

#11
QUOTE:
The issue is the script is quite large several thousand lines of code. Will you have time to look at it?
If I can run it, I'll give it a quick look tomorrow. If there's a problem with it, then we'll see how it goes.

p.s. Don't waste your time re-installing WLP.
profile picture

thetraderman

#12
Another question regarding exits specifically a trailing stop. If WL activates a trailing stop with fidelity and I want to modify that stop, let's say exit out right away. I tried using ticket panel to manually get out but it gave me an error. I guess the trailing stop is already registered with fidelity. Does that mean I need to go to the alert and modify the criteria to make it a market order? This way it would get out at that instance?

A second question I have is I removed my stop logic (SellAtStop/CoverAtStop) and just have a buy/sell with a trailing stop (ExitAtTrailingStop). I noticed 2 alerts are generated. Is that because the trailing stop acts as a stop initially and a trailing stop thereafter assuming the price is going in right direction? Once the position is closed both alerts are still there. Is there anything wrong with having both stop and trailing stop logic if the criteria for each is different? So in reality one can have a sell, stop, and trailing stop in the same loop.

Thanks.
profile picture

Cone

#13
QUOTE:
to modify that stop, let's say exit out right away.
Call Fidelity to be certain, but it's unlikely they'll accept multiple same-type orders for the same shares. If you want out right away, you must change it to a market order, right. (Also, note that placing a stop (or limit) order that would result in immediate execution will "error out" on the Fidelity back end.)

The Orders tool logic will probably discard multiple alerts having the same order type on the same bar, keeping just the first one triggered. It's best to combine multiple stop order prices into a single order so that nothing unexpected occurs.
profile picture

sofia

#14
How to exit all positions end of day ?

Hello,

How to code an exit all positions end of the day. My script is based on intraday scalp, but I need to close all positions (either long or short) before the market close.
CODE:
Please log in to see this code.

//How to code ExitAtMarket before close ?

Thanks,

Sofia
profile picture

Eugene

#15
profile picture

sofia

#16
Hello,

Thanks for the link. One more question.
Is it possible to change the color of bar on exit position only ?

example,
CODE:
Please log in to see this code.


Thanks,

Sofia

}
profile picture

Eugene

#17
Sure. Exit*, Sell*, Buy*, Short* and Cover* all return a boolean value. If they succeeded it's true (and false otherwise):
CODE:
Please log in to see this code.
profile picture

sofia

#18
One last question, how to exit completely for the day once the first risk stop triggers.


CODE:
Please log in to see this code.


Thanks for all your help Eugene.

Regards,

Sofia


profile picture

Eugene

#19
1. Create up a boolean variable, let's call it canEnter.
2. Set it to false just like you're doing now with SetBarColor.
3. Set canEnter = true when Bars.IntradayBarNumber(bar) == 0 i.e. new trading session has begun.
4. Before entering a trade, check if( canEnter ) so to avoid trading if it's false.
profile picture

sofia

#20
Yes I tried as you suggested , but I am getting following warnings.

Here is the code.

///////////////////
CODE:
Please log in to see this code.

///But here is where I start getting warning messages.
//error CS0165 : Use of unassigned local variable canEnter.
//It looks like the loop does not read variable canEnter.

Thanks,

Sofia





profile picture

Eugene

#21
QUOTE:
Here is the code.

When posting code, wrap it inside a pair of CODE tags (hint: click the CODE button).
QUOTE:
//error CS0165 : Use of unassigned local variable canEnter.

Next time you see an unfamiliar compiler error, look them up in MSDN like that:

Compiler Error CS0165
profile picture

sofia

#22
Thanks Eugene. I initialized the variable.

bool canEnter true;

Now the code compiles correctly but does the same thing as before. i.e it still executes on all intraday bars.
does not exit after risk level trigger.
Sorry for asking so many questions. I am not a programmer and I am learning this language on fly.

Regards,

Sofia

profile picture

Eugene

#23
1. canEnter initialization/definition should happen outside of the trading loop.
2. Move the IntradayBarNumber check outside (above) the IsLastPositionActive block.

There's another, easier way of forcing Max. intraday positions and it's the Position Options PosSizer's checkbox "maximum entries per day".
profile picture

sofia

#24
It works now.

Thanks Eugene.

Sofia
profile picture

sofia

#25
Question on Exit revisited.

Hello,

I have a long/short program , i.e the program is either long or short at any time. But if certain condition is met, then I need to exit by end of the day. I tried coding it as follow, but it won't work. What am I doing wrong. Please help.
Here is an example :
CODE:
Please log in to see this code.


Thanks,

Sofia
profile picture

Eugene

#26
CODE:
Please log in to see this code.

First of all, with ExitAtClose (as opposed to CoverAtClose) you're exiting both long and short positions - not just short. Try this:
CODE:
Please log in to see this code.
profile picture

sofia

#27


CODE:
Please log in to see this code.

I updated my code and it works fine now. Thanks again for your prompt help. Always appreciated.
Wish you a very happy 2013.

Regards,
Sofia
profile picture

Eugene

#28
QUOTE:
//Did you mean ( p.PositionType == PositionType.Short)

Yes I did. :)
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).