Restart Strategy after STOPLOSS or TAKEPROFIT [SOLVED]
I run a strategy with order. In this order i set STOPLOSS and TAKEPROFIT. I would like if my strategy touch STOPLOSS or TAKEPROFIT, restart strategy on the Init() procedure. I set Init() in the Complete() procedure but do not work (restart).
Is possible restart auto, the strategy if STOPPLOSS or TAKEPROFIT executed
Thanks
Answer - No, we haven't this functional.
Note:
Do not do this ever!
I set Init() in the Complete() procedure but do not work (restart).
The Complete() method is dedicated to free resources before the strategy will be removed. Doing by this occurs a recursion processes which prevents the normal closing of the strategy and raises a memory consumption.
If you want to restart strategy you only need to reset an initial stage of it. Thereby, just create some method - InitStartContidions() (sets all variables to the initial values) and put in in the Init() method and in the OnQuote() for example.
Option 1 (event-based)
void InitStartContidions()
{
/// sets all variables to the initial values
}
/// <summary>
/// This function will be called after creating
/// </summary>
public override void Init()
{
InitStartContidions();
Positions.PositionRemoved += Positions_PositionRemoved;
}
private void Positions_PositionRemoved(Position obj)
{
if (obj.StopLossOrder != null || obj.TakeProfitOrder != null)
{
InitStartContidions();
}
}
/// <summary>
/// This function will be called before removing
/// </summary>
public override void Complete()
{
// unsubscribing from event
Positions.PositionAdded -= Positions_PositionAdded;
}
Option 2 (simplified for one-position mode and cases when positions are used only SL/TP as exit condition)
void InitStartContidions()
{
/// sets all variables to the initial values
}
/// <summary>
/// This function will be called after creating
/// </summary>
public override void Init()
{
InitStartContidions();
}
/// <summary>
/// Entry point. This function is called when new quote comes
/// </summary>
public override void OnQuote()
{
/// for the one positions mode
/// if you open only one position you can catch the state when the position is closed
var pos = Positions.GetPositions().Where(p => p.MagicNumber == 1233 && p.Account == Accounts.Current && p.Instrument == Instruments.Current);
/// no opened positions
if (!pos.Any())
{
InitStartContidions();
}
}
Good Luck!