Please check my usage of trading methods
Author: Darrell__
Creation Date: 1/1/2017 7:14 PM
profile picture

Darrell__

#1
I am trying to create a generic template that will allow me to use various orders in a trading strategy, i.e. BuyAtMarket (ShortAtMarket), BuyAtStop (ShortAtStop), BuyAtLimit (ShortAtLimit), etc.. In addition, I want the template to be able to use RiskStop and AutoProfit levels. I have gone through a lot of the sample code; however; I want to make sure that I structured the Trading System Loop (below) correctly. The code for the trading system loop portion appears to work correctly, but I'd like someone with much more experience with Wealth-Lab to review the "Trading system loop" logic and see if I programmed it correctly, and/or if there's anything I need to add, delete, or change to make the code work more efficiently. Thanks in advance.

Also, the code uses a trading signal, i.e. "MySignals[bar]," which is calculated by an external function at the end of each bar. Also, the buyAtStop and shortAtStop are DataSeries objects created prior to the loop, i.e. "DataSeries buyAtStop = Bars.Close + ( boPctATR * atrBarsBO );", where DataSeries atrBarsBO = ATR.Series( Bars, boBarATR );".

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

Eugene

#2
Hi Darrell,

Looks OK on the surface.

P.S. You might want to use the boolean value returned by Sell/Cover* methods like this:
CODE:
Please log in to see this code.
profile picture

Darrell__

#3
Eugene,

Thank you for taking the time to review my code. Since I'm rather new to Wealth-Lab, I don't want to make a mistake in what is probably the most critical portion of the trading strategy. Based on your suggestion, I changed the template for the RiskStopLevel and AutoProtectLevel exits as follows:

CODE:
Please log in to see this code.

I do have two additional questions. Can I set the RiskStopLevel following SellAtStop, or SellAtLimit entries (as below), or do I need to set the RiskStopLevel prior to issuing the SellAtStop, or SellAtLimit entries? I think the advantage of setting the RiskStopLevel afterwards, is that I could use the actual entry price when calculating the stop level.

CODE:
Please log in to see this code.

And finally, could I also set the AutoProfitLevel afterwards? For example,

CODE:
Please log in to see this code.

Thanks again.

profile picture

Eugene

#4
Darrell,

The idea of using RiskStopLevel and AutoProfitLevel is to put them on top of an entry. This way Wealth-Lab recognizes the Position which properties the values are being assigned to. Sure you can use the actual entry price when calculating the stop level. For instance:

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

Cone

#5
@Eugene: That's not precisely correct. It's also important to know that if you want to use Max Percent Risk-based sizing, you need to set RiskStopLevel before you create the position. AutoProfitLevel should also be assigned before creating the position, and then both of these values can be used for same bar exits in auto trading. See Preferences > Trading > Allow Same Bar Exits for Auto-Trading.

Darrell's code was correct. You can't really know the precise entry price a priori, but you need to set these values before creating the position and never change them. If you change their values after creating the position, it's peeking. Of course after the entry bar, you can use a different value based on the precise entry price in your stop / limit exit orders.
profile picture

Darrell__

#6
Thanks again to Eugene and Cone for reviewing my "Trading system loop" code template.

Not to beat a dead horse, but since this portion of the trading strategy is critically important, I want to make sure I've incorporated your suggestions correctly. Specifically, since I do want to use same bar exits for auto trading, could I set both the RiskStopLevel and AutoProfitLevel on top of an entry, and then adjust these values (one time) after the entry using the precise entry price in my stop / limit exit orders as follows:
CODE:
Please log in to see this code.


Thanks again... and hopefully other people, like myself, who are just getting started building strategy code will benefit from this template/discussion.