using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using TSLab.Script; using TSLab.Script.Handlers; using TSLab.Script.Realtime; namespace TSLab.Helper { public class CPosition { private ISecurity sec; private IContext ctx; /// /// позиция /// private IPosition position; //стоплосс позиции private IPosition stoplossPosition; /// /// имя позиции /// private string sPositionName; /// /// имя стоплосса /// private string sStoplossName; /// /// номер бара работы с позициями /// private int _i; public IPosition Position { get { return position; } } public IPosition StoplossPosition { get { return stoplossPosition; } } /// /// конструктор /// /// /// public CPosition(int i, ISecurity sec, IContext ctx, string sPositionName) { this.sec = sec; this.ctx = ctx; this.sPositionName = sPositionName; this.sStoplossName = sPositionName + " sl2"; position = sec.Positions.GetLastActiveForSignal(sPositionName, i - 1); stoplossPosition = sec.Positions.GetLastActiveForSignal(sStoplossName, i - 1); this._i = i; //если отложенный ордер сработал if (stoplossPosition != null) { //то закрываем обе позиции if (position != null) position.CloseAtMarket(_i, "111"); stoplossPosition.CloseAtMarket(_i, "111"); } } /// /// открытие длинной позиции /// /// /// /// /// public bool OpenLong(double stoploss, int shares) { if (position == null) { sec.Positions.BuyAtMarket(_i, shares, sPositionName); sec.Positions.SellIfLess(_i, shares, stoploss, sStoplossName); ctx.StoreObject(sPositionName + "_stoploss", stoploss); return true; } return false; } /// /// открытие короткой позиции /// /// /// /// /// public bool OpenShort( double stoploss, int shares) { if (position == null) { sec.Positions.SellAtMarket(_i, shares, sPositionName); sec.Positions.BuyIfGreater(_i, shares, stoploss, sStoplossName); ctx.StoreObject(sPositionName + "_stoploss", stoploss); return true; } return false; } /// /// выставление стоплосса /// /// /// /// /// public bool setStoploss(double stoploss) { if (position == null && stoplossPosition == null) { return false; } if (position != null) { position.CloseAtStop(_i, stoploss, sStoplossName); ctx.StoreObject(sPositionName + "_stoploss", stoploss); return true; } return false; } public bool setStoploss() { if (position == null && stoplossPosition == null) { return false; } if (position != null) { object o = ctx.LoadObject(sPositionName + "_stoploss"); double sl = (double)o; position.CloseAtStop(_i, sl, sStoplossName); return true; } return false; } public bool Close() { if (position == null) { return false; } position.CloseAtMarket(_i, sPositionName + " Close"); return true; } } }