public Tactics(string instrument_id, BaseTaticsHelper helper) { V_Instrument_id = instrument_id; m_TaticsHelper = helper; V_TacticsState = EM_TacticsState.Start; V_OrderState = EM_OrderOperation.Normal; orderPercent = float.Parse(AppSetting.Ins.GetValue("OrderValue")); Start(); }
/// <summary> /// 平仓处理 /// </summary> /// <returns></returns> public async Task CloseHandle() { if (V_TacticsState != EM_TacticsState.CloseAll && V_TacticsState != EM_TacticsState.CloseLong && V_TacticsState != EM_TacticsState.CloseShort) { V_TacticsState = EM_TacticsState.Normal; return; } if (V_AccountInfo.V_Positions != null || V_AccountInfo.V_Positions.Count > 0) { if (V_TacticsState == EM_TacticsState.CloseAll) { await V_AccountInfo.ClearPositions(0); return; } else if (V_TacticsState == EM_TacticsState.CloseLong) { foreach (var item in V_AccountInfo.V_Positions) { if (item.V_Dir > 0) { await V_AccountInfo.ClearPositions(1); return; } } } else { foreach (var item in V_AccountInfo.V_Positions) { if (item.V_Dir < 0) { await V_AccountInfo.ClearPositions(-1); return; } } } } V_TacticsState = EM_TacticsState.Normal; }
/// <summary> /// 对冲处理 /// </summary> /// <returns></returns> public async Task HedgeHandle() { if (V_TacticsState != EM_TacticsState.Hedge) { V_TacticsState = EM_TacticsState.Normal; return; } if (V_AccountInfo.V_Positions != null || V_AccountInfo.V_Positions.Count > 0) { //只有一单 if (V_AccountInfo.V_Positions.Count == 1) { await V_AccountInfo.MakeOrder(-V_AccountInfo.V_Position.V_Dir, V_AccountInfo.V_Position.V_AllVol); return; } else { //有两单,检查下量能不能对上 float vol_1 = V_AccountInfo.V_Positions[0].V_AllVol; float vol_2 = V_AccountInfo.V_Positions[1].V_AllVol; float vol = vol_1 - vol_2; float maxVol = MathF.Max(vol_1, vol_2); if (MathF.Abs(vol) > maxVol * 0.2f) { //算不上对冲,继续开单 await V_AccountInfo.MakeOrder(vol > 0?V_AccountInfo.V_Positions[1].V_Dir : V_AccountInfo.V_Positions[0].V_Dir, MathF.Abs(vol)); return; } } } V_TacticsState = EM_TacticsState.Pause; }
/// <summary> /// 下单处理 /// </summary> /// <returns></returns> public async Task OrderHandle() { if (V_TacticsState != EM_TacticsState.Short && V_TacticsState != EM_TacticsState.Long) { V_TacticsState = EM_TacticsState.Normal; return; } if (V_AccountInfo.V_Positions == null || V_AccountInfo.V_Positions.Count == 0) { //什么单都没有 可以开 tempVol = V_AccountInfo.GetAvailMoney() * orderPercent; await V_AccountInfo.MakeOrder(V_TacticsState == EM_TacticsState.Short?-1 : 1, tempVol); return; } else { if (V_AccountInfo.V_Positions.Count == 1) { //只有多单或者空单 检查开的量是否够(因为有可能挂单没吃完,被撤了) if ((V_AccountInfo.V_Position.V_Dir > 0 && V_TacticsState == EM_TacticsState.Long) || (V_AccountInfo.V_Position.V_Dir < 0 && V_TacticsState == EM_TacticsState.Short)) { if ((tempVol - V_AccountInfo.V_Position.V_AllVol) >= tempVol * 0.2f) { await V_AccountInfo.MakeOrder(V_TacticsState == EM_TacticsState.Short? -1 : 1, tempVol - V_AccountInfo.V_Position.V_AllVol); return; } } } } tempVol = 0; V_TacticsState = EM_TacticsState.Normal; }
/// <summary> /// 初始化 /// </summary> /// <returns></returns> public virtual async void Start() { try { await m_TaticsHelper.RunHistory(); if (V_TacticsState == EM_TacticsState.Stop) { return; } m_LastRefreshTime = DateTime.Now; V_AccountInfo = new AccountInfo(); V_AccountInfo.V_Leverage = m_TaticsHelper.V_Leverage; //获取一下合约面值 JContainer con = await CommonData.Ins.V_SwapApi.getInstrumentsAsync(); if (V_TacticsState == EM_TacticsState.Stop) { return; } DataTable t = JsonConvert.DeserializeObject <DataTable>(con.ToString()); foreach (DataRow dr in t.Rows) { if (dr["instrument_id"].Equals(V_Instrument_id)) { V_AccountInfo.V_Contract_val = float.Parse(dr["contract_val"].ToString()); break; } } //设置合约倍数 await CommonData.Ins.V_SwapApi.setLeverageByInstrumentAsync(V_Instrument_id, (int)m_TaticsHelper.V_Leverage, "3"); } catch (Exception ex) { Console.WriteLine(V_Instrument_id + " " + ex.ToString()); Debugger.Error(V_Instrument_id + " " + ex.ToString()); Console.WriteLine(V_Instrument_id + " ReStart"); Debugger.Error(V_Instrument_id + " ReStart"); Start(); } if (V_TacticsState == EM_TacticsState.Stop) { return; } cache = new KLineCache(); if (V_TacticsState == EM_TacticsState.Start) { V_TacticsState = EM_TacticsState.Normal; } Console.WriteLine("start {0}", V_Instrument_id); Debugger.Warn(string.Format("start {0}", V_Instrument_id)); Update(); }
public void SetTacticsState(EM_TacticsState state) { V_TacticsState = state; }
///// <summary> ///// 客户端请求运行策略 ///// </summary> ///// <param name="coin"></param> ///// <returns> ///// 1 成功 ///// 2 已在运行 ///// </returns> //public int F_ReqRunTactics(string coin) { // foreach (var item in m_TacticsDic.Keys) // { // if (item.Contains(coin)) { // return 2; // } // } // RunTactics(coin); // return 1; //} /// <summary> /// 请求操作 ( 1:开始 2 停止 3 暂停 4 开空 5 平空 6 开多 7 平多 8 全平 9 对冲) /// </summary> /// <param name="coin"></param> /// <param name="state"></param> /// <returns> /// 1 成功 /// 2 策略没运行 /// 3 平空失败 /// 4 平多失败 /// 5 全平失败 /// 6 对冲失败 /// </returns> public int F_ReqOperation(string coin, int state) { EM_TacticsState em_state = (EM_TacticsState)state; bool success = false; switch (em_state) { case EM_TacticsState.Start: success = StartTatics(coin); return(success ? 1 : -1); break; case EM_TacticsState.Stop: success = StopTatics(coin); return(success ? 2 : -2); break; case EM_TacticsState.Pause: success = PauseTatics(coin); return(success ? 3 : -3); break; case EM_TacticsState.Short: break; case EM_TacticsState.CloseShort: break; case EM_TacticsState.Long: break; case EM_TacticsState.CloseLong: break; case EM_TacticsState.CloseAll: break; case EM_TacticsState.Hedge: break; default: break; } string instrument_id = ""; if (!IsTacticsRunning(coin, out instrument_id)) { return(-1); } else { m_TacticsDic[instrument_id].SetTacticsState(em_state); return(1); } return(0); }