Profile Picture


Posted By pincheblake 3 Years Ago
Posted Friday August 07 2015

I'm new to C# and RightEdge and have just been learning through trial and eror. I've been working on a basic long only system for stocks and have a couple questions:

  1. Rebalancing - say I set the initial position size to 20%. If I have 5 long positions and get a buy for a 6th, how can I reduce the current positions to make room for the new buy?
  2. Trading frequency - how can I set a particular day to execute trades? For example, only trade on Mondays or only trade on the first day of the month.

Posted Friday August 07 2015

You can get the current date/time of the system with the SystemData.CurrentDate property.  This is a DateTime value (a type provided by .NET), and has properties for DayOfWeek and Day (of the month) which you can use.

To resize a position, you need to submit a new buy or sell order.  The Position class has a SubmitOrder method you can use to do this.  You can use the PositionManager.GetOpenPositions() method to get all open positions for all symbols, and then you could submit orders to resize them.

The resize won't take effect immediately though.  Just as in real trading, the order is submitted to the broker and you have to wait for the broker to fill it for you.  In simulation, with market orders, this should have happened by the next new bar or tick.

Handling all of this for rebalancing will probably be a bit tricky, especially if you are new to programming.  Let us know if you need further help.

Posted Monday August 10 2015
Thanks, Daniel. I will likely need some more help but I'll start with the info you provided and give it a shot.
4 stars.
Posted Tuesday October 06 2015
Here's what I did for my latest book, where both these issues came up.

I go over all symbolscripts in MySystem class. Yes, the names of these classes are horrible, but they get the job done. The code below is of course incomplete and skips many parts, but I'm including what's relevant for this problem.

First, to check for weekday (I'm assuming portfolio type simulation here, else it would be really easy)
foreach(MySymbolScript s in SymbolScripts)
if (s._currentDay == DayOfWeek.Tuesday) rebalanceToday = true; // check each symbolscript for weekday. may seem redundant to check them all, but in case the bars aren't synched, it helps.

//Then of course, if rebalanceToday turns out to be true, we'll need to recalculate the target size and adjust actual position size.
if((s._hasOpenPosition) && rebalanceToday )
long adjustPosition = (s.targetSize - p.CurrentSize); // check the target size and compare to current size. in this case, target size was already calculated in mysymbolscript.

// now we adjust the position size by the difference, as calculated above.
if (adjustPosition > 0)
SystemData.PositionManager.AddToPosition(p.ID,(adjustPosition),OrderType.MarketOnOpen,0,"Rebalance. Rank: " + currentRank + " adjusting by: " + (Math.Abs(adjustPosition) / p.CurrentSize));
SystemData.PositionManager.RemoveFromPosition(p.ID, Math.Abs(adjustPosition),OrderType.MarketOnOpen,0,"Rebalance. Rank: "+ currentRank + " adjusting by: " + (Math.Abs(adjustPosition) / p.CurrentSize));


I hope that can help.

Posted Wednesday October 07 2015
andreas_clenow (10/6/2015)
Yes, the names of these classes are horrible

Any suggestions on what would make better names?


Similar Topics

Reading This Topic

2005-2018 © RightEdge Systems