public class TLine : Strategy
{
#region Variables
// Wizard generated variables
private int fastEMA = 3; // Default setting for FastEMA
private int mediumEMA = 8; // Default setting for MediumEMA
private int slowSMA = 20; // Default setting for SlowSMA
// User defined variables (add any user defined variables below)
#endregion
/// <summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
/// </summary>
protected override void Initialize()
{
CalculateOnBarClose = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Condition set 1
if (CrossAbove(EMA(FastEMA), EMA(MediumEMA), 1)
&& EMA(MediumEMA)[0] > SMA(SlowSMA)[0])
{
EnterLong(DefaultQuantity, "");
}
// Condition set 2
if (CrossBelow(EMA(FastEMA), EMA(MediumEMA), 1))
{
ExitLong("", "");
}
}
#region Properties
[Description("")]
[GridCategory("Parameters")]
public int FastEMA
{
get { return fastEMA; }
set { fastEMA = Math.Max(1, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int MediumEMA
{
get { return mediumEMA; }
set { mediumEMA = Math.Max(1, value); }
}
[Description("")]
[GridCategory("Parameters")]
public int SlowSMA
{
get { return slowSMA; }
set { slowSMA = Math.Max(1, value); }
}
#endregion
}
}
I am having trouble with coding the account management & position sizing portion into this.
I have the following parameters:
1) Starting Account Size: 10,000
2) % of Account Size at risk on each trade: 1%
3) # of shares to purchase: the lower of current account size, or 1% / (((EMA3-EMA8)/EMA8)*2), or 10% of account size.
I can't figure out how to code this in and have it update dynamically (say i reach the full account limit so no new trades until a position is exited and hence frees up capital.
Any help will be appreciated!
Comment