here's some code to get you thinking that does work. i can give you part but not all of the strategy obviously. My target exits are predicted automatically for me and are dynamic based on realtime calculations. Each target is then stored in its own variable. it'll be up to you to calculate targets on your own however you want.
1 big position is entered when conditions are met. It'll stop out if the function that monitors risk indicate it should exit. thats a separate function all together and never places hard stops or trails etc (why show your hand to the MMs and level 3 only to have them sniff it out and trigegr it?) as a result i use mkt orders for everything and to get fills right way. Just keep in mind this is a completely separate function of the strategy
I'm not a coder so this may be the most inefficient thing in the world, but it works.
public class Scaleout: Strategy
{
#region Variables
private double target1 = 0;
private double target2 = 0;
private double target3 = 0;
private double basket = 0;
private double exitsize = 0;
private int counter = 0;
#endregion
protected override void Initialize()
{
counter = 0;
}
protected override void OnBarUpdate()
{
target1 = /* your mathmatics here to calculate in realtime, or enter a number in the strat properties if you want. its up to you*/;
target2 = /* your mathmatics here to calculate in realtime, or enter a number in the strat properties if you want. its up to you*/;
target3 = /* your mathmatics here to calculate in realtime, or enter a number in the strat properties if you want. its up to you*/;
//See what the current position size is
if (Position.MarketPosition == MarketPosition.Long)
{
basket = Position.Quantity;
}
//scale out 1/3 on target 1 hit
if (High[0] >= target1 && counter == 0 && Position.MarketPosition == MarketPosition.Long)
{
exitsize = basket * 0.33;
exitsize = Math.Round(exitsize);
ExitLong((int)exitsize);
counter = 1;
}
//scale out next 2/3 on target 2 hit
if (High[0] >= target2 && counter == 1 && Position.MarketPosition == MarketPosition.Long)
{
exitsize = Position.Quantity / 2; /* "basket / 2" didnt work for some reason */
exitsize = Math.Round(exitsize);
ExitLong((int)exitsize);
counter = 2;
}
//Exit remainder and quit on target high hit
if (High[0] >= target3 && counter == 2 && Position.MarketPosition == MarketPosition.Long)
{
counter = 3;
StopStrategy();
return;
}
}
private void StopStrategy()
{
if (Position.MarketPosition == MarketPosition.Long)
ExitLong();
else if (Position.MarketPosition == MarketPosition.Short)
ExitShort();
}
"stopping out" is handled completely separately and you can build that however you want. If it does get stopped out after it has already started scaling out, and winds up entering again later, the counter is reset to zero and the scaling out begins again with target 1, 2 etc. Hopefully you found this to be worthwhile t least for idea generation. If you improve upon the general concept, feel free to post your revisions.

Comment