private void PopulatePositions() { // First, remove the existing positions. This is easier // than trying to figure out which ones need to change. // Also, simply allocating a new collection object (which // will be empty) doesn't work; the RaisePropertyChanged // doesn't go through for some reason. if (MyPositions != null) { while (MyPositions.Count > 0) { MyPositions.RemoveAt(0); } } else { MyPositions = new ObservableCollection <Position_VM>(); } // Now put each position in the collection. foreach (var p in TheAccount.Positions) { Position_VM pvm = new Position_VM(); pvm.ThePosition = p; MyPositions.Add(pvm); } // Tell the View to redraw the table. RaisePropertyChanged("MyPosition"); }
/// <summary> /// To get open positions on a symbol. /// </summary> /// <param name="symbol">The contract for this position.</param> /// <param name="count">Number of results to fetch.</param> /// <returns></returns> public async ValueTask <MyPositions> GetPositions(string symbol, long count) { var _result = new MyPositions(); var _params = new Dictionary <string, object>(); { if (count > 0) { _params.Add("count", count); } _params.Add("filter", new CArgument { isJson = true, value = new Dictionary <string, object> { { "symbol", symbol } } }); } var _response = await privateClient.CallApiGet2Async("/api/v1/position", _params); if (_response != null) { #if RAWJSON _result.rawJson = _response.Content; #endif if (_response.IsSuccessful == true) { var _positions = privateClient.DeserializeObject <List <BMyPositionItem> >(_response.Content); if (_positions != null) { foreach (var _p in _positions) { _p.orderType = OrderType.Position; _p.orderStatus = _p.isOpen ? OrderStatus.Open : OrderStatus.Closed; _p.sideType = _p.quantity > 0 ? SideType.Bid : _p.quantity < 0 ? SideType.Ask : SideType.Unknown; _p.quantity = Math.Abs(_p.quantity); _p.amount = _p.price * _p.quantity; } _result.result = _positions.ToList <IMyPositionItem>(); _result.SetSuccess(); } } else { var _message = privateClient.GetResponseMessage(_response); _result.SetFailure(_message.message); } } return(_result); }
// [TestMethod] public void Execute_WealthScript(Bars bars) { this.executor.Execute(this.script, bars); this.Positions = this.script.PositionsCreated; this.AlertsCreated = this.script.AlertsCreated; foreach (Position p in this.Positions) { PositionsCreated.Add(MyPositions.PositionAdd(p)); } }
/// <summary> /// Get Open Interest Summary /// </summary> /// <param name="args">Add additional attributes for each exchange</param> /// <returns></returns> public override async Task <MyPositions> FetchAllOpenPositions(Dictionary <string, object> args = null) { var _result = new MyPositions(); var _markets = await publicApi.LoadMarkets(); if (_markets.success == true) { tradeClient.ExchangeInfo.ApiCallWait(TradeType.Trade); var _params = tradeClient.MergeParamsAndArgs(args); var _json_value = await tradeClient.CallApiGet1Async("/v1/me/getpositions", _params); #if DEBUG _result.rawJson = _json_value.Content; #endif var _json_result = tradeClient.GetResponseMessage(_json_value.Response); if (_json_result.success == true) { var _json_data = tradeClient.DeserializeObject <List <BMyPositionItem> >(_json_value.Content); { var _positions = _json_data .OrderByDescending(o => o.timestamp); foreach (var _p in _positions) { var _market = publicApi.publicClient.ExchangeInfo.Markets.GetMarketBySymbol(_p.symbol); if (_market == null) { continue; } _p.positionId = _p.timestamp.ToString(); // positionId 제공 안함 _p.orderStatus = OrderStatus.Open; _p.orderType = OrderType.Position; _p.amount = Math.Abs(_p.quantity) * _p.price; _result.result.Add(_p); } } } _result.SetResult(_json_result); } else { _result.SetResult(_markets); } return(_result); }
private void AITowerBeforeMove() { var potentialTargets = MyPositions.Where(p => IsMyBorder(p) && !IsDefended(p)).ToList(); var potentialTargetsExtended = new List <Position>(); foreach (var target in potentialTargets) { potentialTargetsExtended.AddRange(target.Arounds(true).Where(p => !IsDefended(p) && Map[p.X, p.Y].IsOwned).ToList()); } // Make them distinct potentialTargets = potentialTargetsExtended.GroupBy(i => (i.X, i.Y)).Select(x => x.First()).ToList(); potentialTargets.RemoveAll(p => p.Arounds(true).Where(pA => IsMyBorder(pA) && Map[pA.X, pA.Y].IsOccupied && !IsDefended(pA)).ToList().Count < 2); potentialTargets.RemoveAll(p => Map[p.X, p.Y].HasMineSpot); while (MyGold > BUILD_COST_TOWER && potentialTargets.Count > 0) { potentialTargets.Sort(delegate(Position p1, Position p2) { int count1 = p1.Arounds(true).Where(p => IsMyBorder(p) && Map[p.X, p.Y].IsOccupied && !IsDefended(p)).ToList().Count; int count2 = p2.Arounds(true).Where(p => IsMyBorder(p) && Map[p.X, p.Y].IsOccupied && !IsDefended(p)).ToList().Count; if (count1 != count2) { return(count2 - count1); } else { return((int)(Math.Abs(11 - (p1.X + p1.Y)) - Math.Abs(11 - (p2.X + p2.Y)))); } // Or closer to our Hq ?? }); var target = potentialTargets[0]; int soldierCount = target.Arounds(true).Where(p => IsMyBorder(p) && Map[p.X, p.Y].IsOccupied && !IsDefended(p)).ToList().Count; if (soldierCount < 2) { break; } else { List <Unit> onTheWayUnits = MyUnits.FindAll(u => u.Position.Arounds(true).Exists(p => p == target)); FreePlace(target); Build("TOWER", target); potentialTargets.RemoveAt(0); } //potentialTargets.RemoveAll(p => IsDefended(p)); potentialTargets.RemoveAll(p => p.Arounds(true).Where(pA => IsMyBorder(pA) && Map[pA.X, pA.Y].IsOccupied && !IsDefended(pA)).ToList().Count < 2); } }
/// <summary> /// Get open positions /// </summary> /// <param name="args">Add additional attributes for each exchange</param> /// <returns></returns> public override async ValueTask <MyPositions> FetchAllOpenPositionsAsync(Dictionary <string, object> args = null) { var _result = new MyPositions(); var _markets = await publicApi.LoadMarketsAsync(); if (_markets.success == true) { tradeClient.ExchangeInfo.ApiCallWait(TradeType.Trade); var _params = tradeClient.MergeParamsAndArgs(args); var _json_value = await tradeClient.CallApiGet1Async("/api/v1/position", _params); #if RAWJSON _result.rawJson = _json_value.Content; #endif var _json_result = tradeClient.GetResponseMessage(_json_value.Response); if (_json_result.success == true) { var _json_data = tradeClient.DeserializeObject <List <BMyPositionItem> >(_json_value.Content); { var _positions = _json_data .OrderByDescending(p => p.timestamp); foreach (var _p in _positions) { _p.orderType = OrderType.Position; _p.orderStatus = _p.isOpen ? OrderStatus.Open : OrderStatus.Closed; _p.sideType = _p.quantity > 0 ? SideType.Bid : _p.quantity < 0 ? SideType.Ask : SideType.Unknown; _p.quantity = Math.Abs(_p.quantity); _p.amount = _p.price * _p.quantity; _result.result.Add(_p); } } } _result.SetResult(_json_result); } else { _result.SetResult(_markets); } return(_result); }
/// <summary> /// Get open positions /// </summary> /// <param name="args">Add additional attributes for each exchange</param> /// <returns></returns> public override async ValueTask <MyPositions> FetchAllOpenPositionsAsync(Dictionary <string, object> args = null) { var _result = new MyPositions(); var _markets = await publicApi.LoadMarketsAsync(); if (_markets.success == true) { tradeClient.ExchangeInfo.ApiCallWait(TradeType.Trade); var _params = tradeClient.MergeParamsAndArgs(args); var _json_value = await tradeClient.CallApiPost1Async("/0/private/OpenPositions", _params); #if RAWJSON _result.rawJson = _json_value.Content; #endif var _json_result = tradeClient.GetResponseMessage(_json_value.Response); if (_json_result.success == true) { var _json_data = tradeClient.DeserializeObject <KResponse <Dictionary <string, KMyPositionItem> > >(_json_value.Content); { var _positions = _json_data.result .OrderByDescending(o => o.Value.timestamp); foreach (var _p in _positions) { _p.Value.positionId = _p.Key; _p.Value.orderType = OrderType.Position; _p.Value.amount = _p.Value.cost + _p.Value.fee; _p.Value.price = _p.Value.amount / Math.Abs(_p.Value.quantity); _result.result.Add(_p.Value); } } } _result.SetResult(_json_result); } else { _result.SetResult(_markets); } return(_result); }
/// <summary> /// Get position /// </summary> /// <param name="args">Add additional attributes for each exchange</param> /// <returns></returns> public override async Task <MyPositions> FetchAllOpenPositions(Dictionary <string, object> args) { var _result = new MyPositions(); var _markets = await publicApi.LoadMarkets(); if (_markets.success == true) { tradeClient.ExchangeInfo.ApiCallWait(TradeType.Trade); var _params = tradeClient.MergeParamsAndArgs(args); var _json_value = await tradeClient.CallApiPost1Async($"/get_position", _params); #if DEBUG _result.rawJson = _json_value.Content; #endif var _json_result = tradeClient.GetResponseMessage(_json_value.Response); if (_json_result.success == true) { var _json_data = tradeClient.DeserializeObject <CMyPositions>(_json_value.Content); { var _positions = _json_data.result .OrderByDescending(o => o.timestamp); foreach (var _p in _positions) { _p.orderType = OrderType.Position; _p.amount = Math.Abs(_p.quantity) * _p.price; _result.result.Add(_p); } } } _result.SetResult(_json_result); } else { _result.SetResult(_markets); } return(_result); }
/// <summary> /// View your active positions. /// </summary> /// <param name="args">Add additional attributes for each exchange</param> /// <returns></returns> public override async ValueTask <MyPositions> FetchAllOpenPositions(Dictionary <string, object> args = null) { var _result = new MyPositions(); var _markets = await publicApi.LoadMarkets(); if (_markets.success == true) { tradeClient.ExchangeInfo.ApiCallWait(TradeType.Trade); var _params = tradeClient.MergeParamsAndArgs(args); var _json_value = await tradeClient.CallApiPost1Async("/v1/positions", _params); #if RAWJSON _result.rawJson = _json_value.Content; #endif var _json_result = tradeClient.GetResponseMessage(_json_value.Response); if (_json_result.success == true) { var _json_data = tradeClient.DeserializeObject <List <BMyPositionItem> >(_json_value.Content); { foreach (var _p in _json_data) { _p.orderType = OrderType.Position; _p.sideType = _p.quantity < 0 ? SideType.Ask : SideType.Bid; _p.amount = Math.Abs(_p.quantity) * _p.price; _result.result.Add(_p); } } } _result.SetResult(_json_result); } else { _result.SetResult(_markets); } return(_result); }
/// <summary> /// Get open positions /// </summary> /// <returns></returns> public async ValueTask <MyPositions> GetAllPositions() { var _result = new MyPositions(); var _response = await privateClient.CallApiGet2Async("/api/v1/position"); if (_response != null) { #if RAWJSON _result.rawJson = _response.Content; #endif if (_response.IsSuccessful == true) { var _positions = privateClient.DeserializeObject <List <BMyPositionItem> >(_response.Content); if (_positions != null) { foreach (var _p in _positions) { _p.orderType = OrderType.Position; _p.orderStatus = _p.isOpen ? OrderStatus.Open : OrderStatus.Closed; _p.sideType = _p.quantity > 0 ? SideType.Bid : _p.quantity < 0 ? SideType.Ask : SideType.Unknown; _p.quantity = Math.Abs(_p.quantity); _p.amount = _p.price * _p.quantity; } _result.result = _positions.ToList <IMyPositionItem>(); _result.SetSuccess(); } } else { var _message = privateClient.GetResponseMessage(_response); _result.SetFailure(_message.message); } } return(_result); }
public bool Build(string type, Position position) { // Check if we can reach the position if (!MyPositions.Exists(p => p == position)) { return(false); } if (Map[position.X, position.Y].IsOccupied) { return(false); } if (!Map[position.X, position.Y].IsOwned) { return(false); } if (type == "MINE") { if (MyGold < BUILD_COST_MINE) { return(false); } if (!Map[position.X, position.Y].HasMineSpot) { return(false); } } if (type == "TOWER") { if (MyGold < BUILD_COST_TOWER) { return(false); } if (Map[position.X, position.Y].HasMineSpot) { return(false); } } Output.Append($"BUILD {type} {position.X} {position.Y};"); if (type == "TOWER") { MyGold -= BUILD_COST_TOWER; Buildings.Add(new Building { Owner = ME, Position = position, Type = type == "TOWER" ? BuildingType.Tower : BuildingType.Mine, }); } if (type == "MINE") { MyGold -= BUILD_COST_MINE; Buildings.Add(new Building { Owner = ME, Position = position, Type = type == "TOWER" ? BuildingType.Tower : BuildingType.Mine, }); } Map[position.X, position.Y].OccupiedBy = Buildings[Buildings.Count - 1]; return(true); }
// Start new AI ======================================================================= private bool TryToWin() { var myBorders = MyPositions.Where(p => IsMyBorder(p)).ToList(); foreach (var border in myBorders) { var steps = PathFinding(border, OpponentHq); steps.RemoveAt(0); if (steps.Exists(p => Map[p.X, p.Y].IsOwned && Map[p.X, p.Y].Active)) { continue; } if (steps.Count * TRAIN_COST_LEVEL_1 > MyGold) { continue; } int cost = 0; steps.ForEach(delegate(Position p) { if (Map[p.X, p.Y].IsOwned) { cost += 0; } else if (IsDefended(p)) { int index = steps.IndexOf(p); if (index == 0) { cost += TRAIN_COST_LEVEL_3; } else { var arounds = p.Arounds(true); arounds.Remove(steps[index - 1]); arounds.RemoveAll(pA => !(Map[pA.X, pA.Y].OccupiedBy is Building)); arounds.RemoveAll(pA => !(Map[pA.X, pA.Y].OccupiedBy as Building).IsTower); if (arounds.Count > 0) { cost += TRAIN_COST_LEVEL_3; } else { cost += TRAIN_COST_LEVEL_1; } } } else if (!Map[p.X, p.Y].IsOccupied) { cost += TRAIN_COST_LEVEL_1; } else if (Map[p.X, p.Y].OccupiedBy is Unit) { switch ((Map[p.X, p.Y].OccupiedBy as Unit).Level) { case 1: cost += TRAIN_COST_LEVEL_2; break; case 2: cost += TRAIN_COST_LEVEL_3; break; case 3: cost += TRAIN_COST_LEVEL_3; break; } } else { if ((Map[p.X, p.Y].OccupiedBy as Building).IsTower) { cost += TRAIN_COST_LEVEL_3; } else { cost += TRAIN_COST_LEVEL_1; } } }); if (cost <= MyGold) { Output.Clear(); Wait(); foreach (var p in steps) { Console.Error.Write(p + " - "); } ApplyCut(in steps); Console.Error.WriteLine(Output); return(true); } } return(false); }
/// <summary> /// To get open positions on a symbol. /// </summary> /// <param name="base_name">The type of trading base-currency of which information you want to query for.</param> /// <param name="quote_name">The type of trading quote-currency of which information you want to query for.</param> /// <param name="args">Add additional attributes for each exchange</param> /// <returns></returns> public override async Task <MyPositions> FetchOpenPositions(string base_name, string quote_name, Dictionary <string, object> args = null) { var _result = new MyPositions(base_name, quote_name); var _market = await publicApi.LoadMarket(_result.marketId); if (_market.success == true) { tradeClient.ExchangeInfo.ApiCallWait(TradeType.Trade); var _params = new Dictionary <string, object>(); { _params.Add("filter", new CArgument { isJson = true, value = new Dictionary <string, object> { { "symbol", _market.result.symbol } } }); tradeClient.MergeParamsAndArgs(_params, args); } var _json_value = await tradeClient.CallApiGet1Async("/api/v1/position", _params); #if DEBUG _result.rawJson = _json_value.Content; #endif var _json_result = tradeClient.GetResponseMessage(_json_value.Response); if (_json_result.success == true) { var _json_data = tradeClient.DeserializeObject <List <BMyPositionItem> >(_json_value.Content); { var _positions = _json_data .OrderByDescending(p => p.timestamp); foreach (var _p in _json_data) { _p.orderType = OrderType.Position; _p.orderStatus = _p.isOpen ? OrderStatus.Open : OrderStatus.Closed; _p.sideType = _p.quantity > 0 ? SideType.Bid : _p.quantity < 0 ? SideType.Ask : SideType.Unknown; _p.quantity = Math.Abs(_p.quantity); _p.amount = _p.price * _p.quantity; _result.result.Add(_p); } } } _result.SetResult(_json_result); } else { _result.SetResult(_market); } return(_result); }