|
Dave Walton
2013-07-13 07:45:38
|
|
I've been working on different position size methods. In the OnNewPosition code of my MM script, I check the available cash of the portfolio and reject the new position if the margin used is more than available. I also store information about rejected positions in various ArrayLists. However, I find that no matter how large I increase the position size, my report continues to say 0 rejected positions even though the number of trades reduces as I increase position size. Clearly the simulator is rejecting signals before my MM script code ever executes. How can I view the signals rejected by the simulator?
|
|
|
|
QuantShare
2013-07-13 15:21:41
0
|
|
You said: My report continues to say 0 rejected positions
How do you get the number of rejected positions and where are they displayed?
|
|
|
|
Dave Walton
2013-07-13 15:35:25
0
|
|
Under OnNewPosition:
double margin = (Portfolio.GetAvailableCash("long")) + Portfolio.GetAvailableCash("short");
if(margin > risk)
{
Functions.UpdateNumberOfShares(nbShares);
}
else
{
RejectedSymbols.Add(NewPosition.Symbol);
RejectedNbshares.Add(nbShares);
RejectedPrice.Add(NewPosition.Price);
RejectedMarginA.Add(margin);
Functions.RejectPosition();
}
Under OnEndSimulation:
Functions.AddMetric("# Rejected Positions", RejectedSymbols.Count);
for(int i=0; i < RejectedSymbols.Count;i++)
{
string rs = (string)RejectedSymbols[i];
int rn = (int)RejectedNbshares[i];
double rp = Math.Round((double)RejectedPrice[i],2);
double rm = (double)RejectedMarginA[i];
Functions.AddMetric("Reject Order " + i + ": " + rn + " " + rs + " @ $" + rp + " short by:", (rn * rp) - rm);
}
|
|
|
|
Dave Walton
2013-07-13 16:48:20
0
|
|
BTW, if I simulate a trading system at 1% risk, the report shows 242 trades. If I increase that to 3% risk, the report only shows 129 trades yet my code indicates 0 rejected positions. Clearly rejections are happening before my code executes.
|
|
|
|
QuantShare
2013-07-13 20:02:00
0
|
|
Are you entering positions using the MM script or using the strategy formula (QS language) ?
Note that "OnNewPosition" event is not executed when a position is initiated from the MM script.
Also, maybe the "nbShares" variable is depending on the "risk" value (I don't know how you calculate these variables). In that case, increasing the rsik could explain why you get fewer trades (Each trade will get a bigger number of shares).
Last question, where are you initializing the "RejectedSymbols" variable? And how?
|
|
|
|
Dave Walton
2013-07-13 22:42:23
0
|
|
I am entering positions using a strategy formula. What I think is happening (please confirm) is that positions are rejected by the simulator because I there is not sufficient margin to enter based on the default strategy of Total Equity/ number of positions. The OnNewPositon never executes because there is no position to pass to my code. Is that correct? Ideally what I'd like to do is have a way to set the number of shares to 1 share coming into the MM script and let the script set the correct number of shares. Then My code would pick up the rejects. Is there a way to do this?
Here is the whole script:
OnStartSimulation:
Functions.SetNumericInput("$Risk per Trade", 1000, "Risk per Trade in Dollars");
OnClosePosition:
empty
OnNewPosition:
double risk = (double)Variables.GetVariable("$Risk per Trade");
int nbShares = 0;
if (NewPosition.StopSettings.StopLossOption == 1) // percentage stop used
{
nbShares = (int)Math.Floor(risk / (NewPosition.Price * (NewPosition.StopSettings.StopLoss / 100)));
}
else if (NewPosition.StopSettings.TrailingStopOption == 1) // percentage stop used
{
nbShares = (int)Math.Floor(risk / (NewPosition.Price * (NewPosition.StopSettings.TrailingStop / 100)));
}
else if (NewPosition.StopSettings.StopLossOption == 2) // point stop used
{
nbShares = (int)Math.Floor(risk / NewPosition.StopSettings.StopLoss);
}
else if (NewPosition.StopSettings.TrailingStopOption == 2) // point stop used
{
nbShares = (int)Math.Floor(risk / NewPosition.StopSettings.TrailingStop);
}
else
{
nbShares = (int)Math.Floor(risk / NewPosition.Price);
}
double margin = (Portfolio.GetAvailableCash("long")) + Portfolio.GetAvailableCash("short");
if(margin > risk)
{
Functions.UpdateNumberOfShares(nbShares);
}
else
{
RejectedSymbols.Add(NewPosition.Symbol);
RejectedNbshares.Add(nbShares);
RejectedPrice.Add(NewPosition.Price);
RejectedMarginA.Add(margin);
Functions.RejectPosition();
}
OnEndPeriod:
empty
OnEndSimulation:
Functions.AddMetric("# Rejected Positions", RejectedSymbols.Count);
for(int i=0; i < RejectedSymbols.Count;i++)
{
string rs = (string)RejectedSymbols[i];
int rn = (int)RejectedNbshares[i];
double rp = Math.Round((double)RejectedPrice[i],2);
double rm = (double)RejectedMarginA[i];
Functions.AddMetric("Reject Order " + i + ": " + rn + " " + rs + " @ $" + rp + " short by:", (rn * rp) - rm);
}
Global:
ArrayList RejectedSymbols = new ArrayList(); // array that holds symbol information for rejected positions
ArrayList RejectedNbshares = new ArrayList(); // array that holds number of shares information for rejected positions
ArrayList RejectedPrice = new ArrayList(); // array that holds symbol price information for rejected positions
ArrayList RejectedMarginA = new ArrayList(); // array that holds portfolio margin available at time of rejected positions
|
|
|
|
QuantShare
2013-07-14 04:25:19
0
|
|
The "OnNewPositon" event should be executed for any valid order initiated by the strategy formula.
I think the ignore list stays empty because the following code always return TRUE:
if(margin > risk)
What is the number of positions allowed for your trading system?
Try increasing it to a very high value (1000 for example). In that case you will get a very low number of shares for each new position.
|
|
|
|
Dave Walton
2013-07-14 22:42:50
0
|
|
OK I will try that. Just so I understand... But what happens if Total equity / 1000 is not enough to buy 1 share of one of the stocks (like goog)? I assume the simulator would reject a buy signal in that case as not a valid order, right? It seems better to change to allow 10x the number of intended max allowed positions.
But, if I want to limit my number of open positions to 4 simultaneous ultimately-- if I reject any new positions when there are 4 open, will the same positions be chosen as otherwise the simulator would have used, i.e. the top 4 ranked symbols?
|
|
|
|
QuantShare
2013-07-15 03:07:12
0
|
|
If is just an example, you can put 100 positions if you want.
I do not understand your second question. Please clarify.
|
|
|
|
Dave Walton
2013-07-16 04:18:38
0
|
|
OK, I made it work. I set my # of positions to 10x my actual intent and increased my percent invested to 1000%. I also had to subtract the values of pending orders from portfolio equity. I then rejected the positions I intended to in my script. I will share it soon.
|
|
|
|