/* * Caveat: if order-placing failed, it won't send again * */ public async void Run(float curPrice) { if (EntryPrice > 0) { string wd = DateTime.Now.DayOfWeek.ToString().Substring(0, 3); DateTime lmt_time = DateTime.Parse(DateTime.Now.ToLongDateString() + " " + FinalTime.AddSeconds(LmtOrderTime * -1).ToShortTimeString()); DateTime final_time = DateTime.Parse(DateTime.Now.ToLongDateString() + " " + FinalTime.ToShortTimeString()); if (Days.Select(x => x.Name).ToList().Contains(wd)) { TypeAccessor accessor = BaseOrderTypeAccessor.GetAccessor(OT_pre); double i = (DateTime.Now - lmt_time).TotalMinutes; if ((DateTime.Now - lmt_time).TotalMinutes < 5 && lmt_time > pre_sent_dt && lmt_time <= DateTime.Now) { if (ActionType == ActionType.Long) { accessor[OT_pre, "LmtPrice"] = (curPrice - (float)Strategy.Symbol.MinTick * Slippage).ToString(); bool r = await Controllers.OrderManager.ProcessSignal(Strategy.Script, Strategy, OrderAction.PreForceExitLong, DateTime.Now, OT_pre, curPrice); if (r) { pre_sent_dt = DateTime.Now; } } if (ActionType == ActionType.Short) { accessor[OT_pre, "LmtPrice"] = (curPrice + (float)Strategy.Symbol.MinTick * Slippage).ToString(); bool r = await Controllers.OrderManager.ProcessSignal(Strategy.Script, Strategy, OrderAction.PreForceExitShort, DateTime.Now, OT_pre, curPrice); if (r) { pre_sent_dt = DateTime.Now; } } } if ((DateTime.Now - final_time).TotalMinutes < 5 && final_time > final_sent_dt && final_time <= DateTime.Now) { if (ActionType == ActionType.Long) { bool r = await Controllers.OrderManager.ProcessSignal(Strategy.Script, Strategy, OrderAction.FinalForceExitLong, DateTime.Now, OT_final, curPrice); if (r) { final_sent_dt = DateTime.Now; } } if (ActionType == ActionType.Short) { bool r = await Controllers.OrderManager.ProcessSignal(Strategy.Script, Strategy, OrderAction.FinalForceExitShort, DateTime.Now, OT_final, curPrice); if (r) { final_sent_dt = DateTime.Now; } } } } } }
public async void Calc(float curPrice, string symbol = null) { CurPrice = curPrice; foreach (var stat in Strategy.AccountStat) { stat.Value.CurPrice = curPrice; } if (EntryPrice > 0) { if (Level > 0) { if (ActionType == ActionType.Long) { HighestProfit = Math.Max(HighestProfit, curPrice - EntryPrice); } else { HighestProfit = Math.Max(HighestProfit, EntryPrice - curPrice); } // ignore the duplicated signal processing if (HighestProfit - _highestProfitSinceLastSent >= (double)(ModifyThreshold * Strategy.Symbol.MinTick)) { int profit_class = HighestProfit / EntryPrice < ProfitTarget / 100 ? 0 : 1 + (int)((HighestProfit / EntryPrice - ProfitTarget / 100) / (ProfitTarget / 100 * TargetIncrement)); profit_class = profit_class > Level ? Level : profit_class; ProfitClass = Math.Max(ProfitClass, profit_class); if (ProfitClass > 0) { TypeAccessor accessor = BaseOrderTypeAccessor.GetAccessor(OT_stopProfit); if (ActionType == ActionType.Long) { StopPrice = EntryPrice + HighestProfit * (BaseLine / 100 + DropIncrement / 100 * (ProfitClass - 1)); if (curPrice <= StopPrice + (float)(SubmitThreshold * Strategy.Symbol.MinTick)) { accessor[OT_stopProfit, "LmtPrice"] = StopPrice.ToString(); accessor[OT_stopProfit, "AuxPrice"] = StopPrice.ToString(); bool r = await Controllers.OrderManager.ProcessSignal(Strategy.Script, Strategy, OrderAction.APSLong, DateTime.Now, OT_stopProfit, curPrice); if (r) { _highestProfitSinceLastSent = HighestProfit; } } } else if (ActionType == ActionType.Short) { StopPrice = EntryPrice - HighestProfit * (BaseLine / 100 + DropIncrement / 100 * (ProfitClass - 1)); if (curPrice >= StopPrice - (float)(SubmitThreshold * Strategy.Symbol.MinTick)) { accessor[OT_stopProfit, "LmtPrice"] = StopPrice.ToString(); accessor[OT_stopProfit, "AuxPrice"] = StopPrice.ToString(); bool r = await Controllers.OrderManager.ProcessSignal(Strategy.Script, Strategy, OrderAction.APSShort, DateTime.Now, OT_stopProfit, curPrice); if (r) { _highestProfitSinceLastSent = HighestProfit; } } } } } } // stop loss if (Stoploss > 0) { // ignore duplicated signal processing if (Stoploss != _stoplossLastSent) { float sp = 0; // stop price TypeAccessor accessor = BaseOrderTypeAccessor.GetAccessor(OT_stopLoss); float stoploss = (float)Stoploss; switch (StoplossSelector) { case 0: // in points/dollars(actual value) // remain unchanged break; case 1: // in ticks stoploss *= (float)Strategy.Symbol.MinTick; break; case 2: // in percentage stoploss = (float)(EntryPrice * Stoploss * 0.01); break; case 3: // from AFL sript; // remain unchanged break; } if (ActionType == ActionType.Long) { StopLossPrice = sp = EntryPrice - stoploss; if (curPrice <= sp + (float)(SubmitThreshold * Strategy.Symbol.MinTick)) { accessor[OT_stopLoss, "LmtPrice"] = sp.ToString(); accessor[OT_stopLoss, "AuxPrice"] = sp.ToString(); bool r = await Controllers.OrderManager.ProcessSignal(Strategy.Script, Strategy, OrderAction.StoplossLong, DateTime.Now, OT_stopLoss, curPrice); if (r) { _stoplossLastSent = Stoploss; } } } else if (ActionType == ActionType.Short) { StopLossPrice = sp = EntryPrice + stoploss; if (curPrice >= sp - (float)(SubmitThreshold * Strategy.Symbol.MinTick)) { accessor[OT_stopLoss, "LmtPrice"] = sp.ToString(); accessor[OT_stopLoss, "AuxPrice"] = sp.ToString(); bool r = await Controllers.OrderManager.ProcessSignal(Strategy.Script, Strategy, OrderAction.StoplossShort, DateTime.Now, OT_stopLoss, curPrice); if (r) { _stoplossLastSent = Stoploss; } } } } } } }