Profile Picture

Rule using position type to close a long position

Posted By tdonnelly 3 Years Ago
Message
Posted Thursday November 06 2014
Hi - I'm developing a basic trend-following system (based largely on a demo system by Andreas Clenow) to help me better understand how C# is implemented within RE. But I'm stuck on something. 

The code snippet below details a rule stating that if there's a position already open (this is established higher in the code with a "if(pos.Count==0)" statement), and the current close is <= the lowest close of the previous 25 trading sessions (the parameters for this are referenced in the System Parameters box of the Properties settings), the position is closed at market. This part works fine.

The challenge here is that I need to add another rule that allows for this position-closing action to occur ONLY if the current open position is of Long type. 

This is because I intend to add later a different (but similar) series of position opening and position closing rules just for short positions -- and there's a very good likelihood that a position-opening rule for my short position might unintentionally close an open long position. (Note to Daniel -- this is attempting to do in code what I previously tried to do in DaD.)

I'm familiar with the Position class, as well as member Type of that class -- but couldn't get this to work (either because it's not intended for this purpose, or I'm implementing it incorrectly). 

Can someone please offer some guidance? I'd be grateful, for example if using class solutions, for advice about where and how to declare and/or initialize these, as I'm still trying to figure out proper class and list syntax within RE. 

Many thanks

Terence

}
else //Position already open
{
if(Close.Current <= _lowest.Current)
{
foreach(Position p in pos)
{
p.CloseAtMarket();
}
}
}
phg
Supreme Being (44,837 reputation)Supreme Being (44,837 reputation)Supreme Being (44,837 reputation)Supreme Being (44,837 reputation)Supreme Being (44,837 reputation)Supreme Being (44,837 reputation)Supreme Being (44,837 reputation)Supreme Being (44,837 reputation)Supreme Being (44,837 reputation)
Posted Friday November 07 2014
Does this help:
foreach(Position pos in OpenPositions)
if(pos.Type == PostionType.Long) . . .

I think you want something similar but for a particular symbol.


-Pete

See also Yahoo group about applying RE.

Posted Monday November 10 2014
Thanks, Pete. I tried it, but it didn't work as intended - but this may be because I'm still learning where to position code relative to class declarations, static vs non-static, etc. Will keep at it and report back later on. 

Best,

Terence
phg
Supreme Being (44,837 reputation)Supreme Being (44,837 reputation)Supreme Being (44,837 reputation)Supreme Being (44,837 reputation)Supreme Being (44,837 reputation)Supreme Being (44,837 reputation)Supreme Being (44,837 reputation)Supreme Being (44,837 reputation)Supreme Being (44,837 reputation)
Posted Tuesday November 11 2014
For example, it will work under NewBar(). Or under an event handler like OrderFilled(p,t). There are some examples in the RE manual.


-Pete

See also Yahoo group about applying RE.

Posted Friday November 14 2014
I think I figured it out -- I realized (after reviewing one of Andreas' sample trading systems) that you can construct the if statement this way:

if(pos[0].Type == PositionType.Long)
{
            //code to execute if the current open position is a long position
}
As this references values assembled in an iList, it looks like you always need to reference the index. I was trying to do it without the index, which was likely why it wasn't working. 

-- T



Similar Topics


Reading This Topic


2005-2017 © RightEdge Systems