using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TIProAutoTradeExtension
{
///
/// This class is meant to manage all locally driven exits where there is no server side order type that can deliver the exit functionality.
/// Currently this implements the Profit Save and Reduce Risk exit type. Each position will have one instance of the AdvancedExitManager
/// class that will keep track of the current state of the advanced exits. Since the exit state could adjust with each tick, the Evaluate
/// function should be called with each tick that is received from the broker.
///
/// Because an exit could operate on time rather than only market data, there should be a timer that periodically calls Evaluate as well
/// since there is a chance that ticks don't come in frequently enough for a time based exit to trigger in a timely fashion.
///
public class AdvancedExitManager
{
public Position Position { get; }
private List _allExits = new List();
public List AllExits { get { return _allExits; } }
private bool _enabled = false;
public bool Enabled
{
get { return _enabled; }
set { _enabled = value; }
}
public AdvancedExitManager(Position position)
{
Position = position;
_allExits = GetAdvancedExits();
}
///
/// Evaluates the current state of each advanced exit and updates the state or send a request to close the position.
///
public void Evaluate()
{
if (_enabled && null != Position)
{
foreach (IAdvancedExit exit in _allExits)
{
if (!exit.Triggered)
exit.Evaluate();
}
}
}
///
/// This retrieves the current list of advanced exits. In the future this may load DLLs.
///
private List GetAdvancedExits()
{
List exits = new List();
exits.Add(new ProfitSaveExit(Position));
exits.Add(new ReduceRiskExit(Position));
return exits;
}
}
///
/// The interface for operating an advanced exit.
///
public interface IAdvancedExit
{
Position Position { get; set; }
bool Triggered { get; }
void Evaluate();
}
}