Enter after second consecutive bar that closes above or below SMA
Author: tassod
Creation Date: 12/29/2009 11:57 PM
profile picture

tassod

#1
Hello,

Can someone fix this strategy to where it will buy or go short on the close of a second consecutive bar that closes above or below the specified SMA series? I am totally new to this and have no programming experience. This is a rule based strategy that I converted to code. Thanks is advance.

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

Eugene

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

tassod

#3
Thank you very much Eugene!
profile picture

tassod

#4
For Eugene or anyone else that wants to take a stab at this, how would i go about adding a "Midline" series. This moving average would be the mid point of the current SMA high and SMA low in this code. Basically its (maH + maL) /2. After that is plotted, I would like to change my exits to when the bar closes below this midpoint line instead of using the maH or maL lines. Thanks in advance.
profile picture

Eugene

#5
Add this new line right after the "DataSeries maL...":
CODE:
Please log in to see this code.

Then, refer to mid in the code above instead of maH/maL.
profile picture

tassod

#6
I could have sworn I tried this yesterday and got errors, but now it works! Oh well, that's what counts! Thanks again Eugene!
profile picture

Eugene

#7
You're welcome. The errors in this case could have been caused by failing to append "[bar]" where needed (e.g. Close[bar] < mid - error) and/or referring to a value of the series at a bar where the program doesn't expect it (e.g. CrossOver( bar, Close, mid[bar] - error).
profile picture

tassod

#8
Eugene, I would like to implement Position sizing based on Volatility to this strategy (for futures). How would I go about doing this with this code? When I go to portfolio simulation mode and Position Sizer I get an error "Strategy code must set RiskStopLevel in order to use Maximum Risk position size". I've done a lot of searching in these forums and the user guide and found references to this but my lack of programing knowledge stops me from even knowing where to begin coding this.
profile picture

Eugene

#9
Look up RiskStopLevel in the QuickRef (hit F11) for a code snippet showing how to add a stop level to your Strategy. For example, in your code above a 5% RiskStopLevel would translate to:
CODE:
Please log in to see this code.


Percent volatility position sizing is implemented in our upcoming PosSizer pack.
profile picture

tassod

#10
If I were to add this in my dataseries,
CODE:
Please log in to see this code.


Can I use the ATR to set my stop level? for example:
CODE:
Please log in to see this code.


I seem to get an error when i do this..
profile picture

Eugene

#11
This syntax is incorrect.

See DataSeries > Accessing a Single Value from a DataSeries in the WealthScript Programming Guide.
profile picture

tassod

#12
After looking this up like you suggested I changed it to the following:
CODE:
Please log in to see this code.


Can you explain to me though what this line is doing? Is it saying that it is setting the risk to 2.5 times the ATR of that given commodity?
profile picture

Eugene

#13
Sorry, but this line doesn't make sense.

My line above (e.g. Close[bar] * 0.95) was setting the risk stop level to 5% past the security's closing price.

To make sense, correct your line to subtract (for long positions) or add (for short) 2.5 * current ATR from the closing price - vs. multiplying:
CODE:
Please log in to see this code.
profile picture

tassod

#14
Thank you
profile picture

tassod

#15
I must not be applying what you wrote correctly. Wealth Lab is understanding it but its not doing exactly what I thought it would do. I would like to risk a specific amount (ie 2% of $100,000 = $2,000) per trade. I would like to buy as many contracts risking a specific multiplier of that commodities ATR. (ie if Sugar's ATR is .5 over the last 5 days, and my risk is set @ $1,500. it would give me a 2 lot for that volatility). Here is the code:
CODE:
Please log in to see this code.


Using this code, it wants to buy 16 contracts of Sugar at this time when i only want to risk $2000.
profile picture

Eugene

#16
Have you enabled Futures mode in Symbol Info Manager?
profile picture

Cone

#17
RiskStopLevel is the level at which if you exit the trade you will lose the amount risked. (The amount can be more if prices gap through that level.) Consequently, the number of contracts depends on the the difference between the entry price, the RiskStopLevel, and the contract's Point value; it doesn't have anything directly to do with the value of ATR except in the setting of the RiskStopLevel.

See the User Guide for an example, see the QuickRef for an example, and make sure that you have a statement in your exit logic to exit at a StopLoss at the Position p's p.RiskStopLevel. If you do, then you should see that your loss is approximately 2% of your equity at that time.
profile picture

tassod

#18
Yes Futures mode is enabled. To be a little more clear, all I want to do is for Wealth Lab to tell me how many contracts I should be buying based on the amount I want to risk. I want it to do it by taking into consideration the volatility (ATR) of the given commodity. I do not want to set any stoploss in my code. My exits are already defined.
profile picture

Eugene

#19
Once we release the Percent Volatility PosSizer, which dialog box you can see below, this will be a breeze:

You can create a support ticket now to request our PosSizer pack.
profile picture

tassod

#20
I have created the ticket. When will this be available?
profile picture

Eugene

#21
Once I finish testing and debugging it which may happen this month.
profile picture

tassod

#22
Eugene,

Understood about being a current customer in order to receive new Posizer Pack. I have another question though. If I were to use the WealthScript overide radio button and include "SetShareSize" in the code, would I be able to achieve what I am looking to do? For example:
CODE:
Please log in to see this code.
profile picture

Eugene

#23
No.
profile picture

tassod

#24
Ok, thanks for all your help.
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).