Rank: Member
Groups: Registered, Registered Users Joined: 11/24/2005(UTC) Posts: 17 Location: Chagrin Falls, Ohio USA
|
You're welcome, finquant. Glad the code snippet worked. I don't think I can help you with your signal conflicts, though to me it sounds like you're getting an exit and buy/sell signals on the same day. If that happens, then I think the exit takes precedence in MS. This would argue for latching long entries/exits and short entries/exits together so that wouldn't happen, but that is a feature that is currently missing in MS.
As for position sizing, etc, with some help from others in the forum, I was able to sort thru some of the quirks of MS. Here's a working code for a simple moving average system, complete with price pyramiding a la' the Turtles (i.e., 1/2 ATR(20) steps) and an ATR-based dynamic position sizing using 1% risk increments. This code takes into account that a straight-EOD system should emphasize buying at the close. These rules can be relaxed, of course, to enable entries to trigger during the day (just remove the [color=blue:5e5e623eb3]If[/color] statement with [color=blue:5e5e623eb3]PyramidPrice[/color]), but I digress...
So, finally, here's the promised sample code...you'll want to replace the entry buy signal with your own signal(s).
Buy Order
[color=blue:5e5e623eb3]{* Long Entry *}
BuyMAF:= Mov(C,OPT1,E);
BuyQ:= C > Ref(BuyMAF,-1);
BuyQ[/color]
Stop Price
[color=blue:5e5e623eb3]{* Long Entry *}
BuyMAF:= Mov(C,OPT1,E);
BuyQ:= Cross(C,Ref(BuyMAF,-1)); {* Obtains original entry price *}
{* Pyramid into position *}
BuyEntry:= ValueWhen(1,BuyQ,C);
ATRF:= Mov(ATR(1),20,E);
ATREntry:= ValueWhen(1,BuyQ,ATRF);
PyramidPrice:= BuyEntry + 0.5*(Simulation.PositionCount-1)*ATREntry;
If(C>PyramidPrice,C-0.01,1000000)[/color]
Entry Size: % of Available Equity
[color=blue:5e5e623eb3]SimAC:= Simulation.AccountCash;
TotalEquity:= SimAC +
Simulation.AccountBorrowed +
Simulation.PortfolioValue +
Simulation.AccountReserved;
NumShares:=(0.01*TotalEquity)/Mov(ATR(1),20,E);
TotalBuy:= C*NumShares;
(Min(TotalBuy,SimAC)/SimAC)-0.001
{* End of Code Snippet *}[/color]
This should give you an idea of how to do both pyramiding and dynamic position sizing. The Turtles' pyramiding required one to use the ATR and the price of the original entry and then jump in increments of 1/2 ATR(20). This code tries to maintain that flavor. Note also a particular quirk of MS: When you specify % of Available Equity and use a function, it means decimal fraction--the range from 0 to 1, not 0 to 100.
Oh, and another thing---this code is REALLY SLOW because of the need to access the simulation global variables. Unfortunately, you can't get around this one if you want your sizing to change with your system test.
Hope this helps.
Craig
|