코드 수정

This commit is contained in:
2018-10-15 15:48:04 +09:00
parent 9140673315
commit 0a3bc6b77d
7 changed files with 1295 additions and 655 deletions

View File

@@ -2,6 +2,8 @@
using MaterialSkin.Controls;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
@@ -33,6 +35,11 @@ namespace AutoSellerNS
public long m_iProfitUnitPrice;
public int m_iCurPrice;
public override string ToString()
{
return $"[{m_strCodeName}:{m_strCode}] {m_iCurPrice}";
}
}
public class NCITEM
@@ -46,6 +53,11 @@ namespace AutoSellerNS
public int m_iOrderNo;
public int m_iOrgOrderNo;
public DateTime m_Time;
public override string ToString()
{
return $"[{m_strCodeName}:{m_strCode}] {m_iOrderNo}";
}
}
@@ -56,8 +68,6 @@ namespace AutoSellerNS
string m_strSelectedCode = "";
bool m_bSell = false;
SemaphoreSlim m_UpdateSemaphore = new SemaphoreSlim(1, 1);
SemaphoreSlim m_NCUpdateSemaphore = new SemaphoreSlim(1, 1);
System.Timers.Timer m_Timer = new System.Timers.Timer();
public AutoSeller()
@@ -71,7 +81,6 @@ namespace AutoSellerNS
btUpdate.Enabled = false;
btSell.Enabled = false;
for (int i = 0; i < 21; i++)
{
lvCurPrice.Items.Add(new ListViewItem(new string[] { "", "", "", "" }));
@@ -125,16 +134,23 @@ namespace AutoSellerNS
public void SetAccountList(string[] aAccountList)
{
cbAccount.Items.Clear();
cbAccount.Items.AddRange(aAccountList);
foreach (var account in aAccountList)
{
if (account == Config.GetAccount())
Action update = new Action(delegate {
cbAccount.Items.Clear();
cbAccount.Items.AddRange(aAccountList);
foreach (var account in aAccountList)
{
cbAccount.SelectedItem = account;
break;
if (account == Config.GetAccount())
{
cbAccount.SelectedItem = account;
break;
}
}
}
});
if (cbAccount.InvokeRequired)
cbAccount.Invoke(update);
else
update();
}
private void splitContainer2_SizeChanged(object sender, EventArgs e)
@@ -153,8 +169,6 @@ namespace AutoSellerNS
btUpdate.Enabled = true;
btSell.Enabled = true;
UpdateItem();
}
void SyncItems(List<ITEM> OldItems, List<ITEM> NewItems)
@@ -188,7 +202,13 @@ namespace AutoSellerNS
void SyncListViewItems(List<ITEM> Items)
{
lvItems.Items.Cast<ListViewItem>().ToList().RemoveAll(s => (Items.Any(t => t.m_strCode == s.SubItems[chCode.Index].Text) == false));
foreach (ListViewItem item in lvItems.Items)
{
if (Items.Any(t => t.m_strCode == item.SubItems[chCode.Index].Text) == false)
item.Remove();
}
//lvItems.Items.Cast<ListViewItem>().ToList().RemoveAll(s => (Items.Any(t => t.m_strCode == s.SubItems[chCode.Index].Text) == false));
foreach(ITEM item in Items)
{
@@ -204,10 +224,10 @@ namespace AutoSellerNS
}
// update
CybosHelper.STOCK_CUR_ITEM ItemCur = m_CybosHelper.GetItem(item.m_strCode);
STOCK_CUR_ITEM ItemCur = m_CybosHelper.GetItem(item.m_strCode);
row.SubItems[chCurPrice.Index].Text = string.Format("{0:n0}", item.m_iCurPrice);
row.SubItems[chCount.Index].Text = string.Format("{0:n0}", item.m_iAvailableQuantity);
row.SubItems[chCount.Index].Text = string.Format("{0:n0}", item.m_iItemCnt);
row.SubItems[chBookValue.Index].Text = string.Format("{0:n2}", item.m_dBookUnitPrice);
row.SubItems[chCapitalGains.Index].Text = string.Format("{0:n0}", item.m_iProfitUnitPrice);
row.SubItems[chAssessedValue.Index].Text = string.Format("{0:n0}", item.m_iAssessedValue / 1000);
@@ -222,13 +242,15 @@ namespace AutoSellerNS
}
}
public async void UpdateItem()
public void UpdateItem()
{
m_CybosHelper.UpdateItems();
}
public void UpdateItemCallback(List<ITEM> Items)
{
await m_UpdateSemaphore.WaitAsync();
List<ITEM> Items = await m_CybosHelper.UpdateItemsAsync();
SyncItems(m_Items, Items);
SyncListViewItems(m_Items);
m_UpdateSemaphore.Release();
}
void SyncNCItems(List<NCITEM> oldList, List<NCITEM> newList)
@@ -246,8 +268,13 @@ namespace AutoSellerNS
void SyncNCListVIewItems(List<NCITEM> NCItems)
{
lvNCItem.Items.Cast<ListViewItem>().ToList().RemoveAll(r =>
NCItems.Any(t => (t.m_iOrgOrderNo.ToString() == r.SubItems[chNCOrgOrderNo.Index].Text) == false));
foreach(ListViewItem item in lvNCItem.Items)
{
if (NCItems.Any(t => t.m_iOrgOrderNo.ToString() == item.SubItems[chNCOrgOrderNo.Index].Text) == false)
item.Remove();
}
//lvNCItem.Items.Cast<ListViewItem>().ToList().RemoveAll(r =>
// NCItems.Any(t => (t.m_iOrgOrderNo.ToString() == r.SubItems[chNCOrgOrderNo.Index].Text) == false));
foreach (var NCItem in NCItems)
{
@@ -273,30 +300,38 @@ namespace AutoSellerNS
}
}
public async void UpdateNCItem()
public void UpdateNCItem()
{
await m_NCUpdateSemaphore.WaitAsync();
List<NCITEM> NCItems = await m_CybosHelper.UpdateNCAsync();
SyncNCItems(m_NCItems, NCItems);
SyncNCListVIewItems(m_NCItems);
m_NCUpdateSemaphore.Release();
m_CybosHelper.UpdateNC();
}
async void CorrectItems()
public void UpdateNCItemCallback(List<AutoSeller.NCITEM> NCItems)
{
SyncNCItems(m_NCItems, NCItems);
SyncNCListVIewItems(m_NCItems);
}
async Task CorrectItems()
{
if (m_bSell == false)
return;
var ClonedList = m_NCItems.ConvertAll(s => s);
foreach (var NCItem in ClonedList)
List<NCITEM> cloned;
lock (m_NCItems)
cloned = m_NCItems.ConvertAll(s => s);
foreach (var NCItem in cloned)
{
if (NCItem.m_bAsk == true && DateTime.Now - NCItem.m_Time >= TimeSpan.FromSeconds(3))
if (NCItem.m_bAsk == true && DateTime.Now - NCItem.m_Time >= TimeSpan.FromSeconds(5))
{
var CurItem = m_CybosHelper.GetItem(NCItem.m_strCode);
if (CurItem == null)
continue;
int iPrice = CurItem.m_aiBidPrice[0];
if (iPrice == NCItem.m_iOrderPrice)
continue;
await m_CybosHelper.CorrectionItem(NCItem.m_strCode, NCItem.m_iOrderNo, NCItem.m_iRemainCnt, iPrice);
NCItem.m_Time = DateTime.Now;
Util.Log(Util.LOG_TYPE.SELL, string.Format("[{0}] 정정 주문 (주문번호: {1})", NCItem.m_strCodeName, NCItem.m_iOrgOrderNo));
@@ -304,9 +339,19 @@ namespace AutoSellerNS
}
}
private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
private async void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
CorrectItems();
await CorrectItems();
lbRQCnt.Invoke(new Action(delegate {
lbRQCnt.Text = $"{m_CybosHelper.GetLimitRemainCountRQ(), 3}";
}));
lbTRCnt.Invoke(new Action(delegate {
lbTRCnt.Text = $"{m_CybosHelper.GetLimitRemainCountTrade(), 3}";
}));
lbSBCnt.Invoke(new Action(delegate {
lbSBCnt.Text = $"{m_CybosHelper.GetLimitRemainCountSB(), 3}";
}));
}
private void btUpdate_Click(object sender, EventArgs e)
@@ -495,7 +540,7 @@ namespace AutoSellerNS
return;
string strCode = lvItems.SelectedItems[0].SubItems[chCode.Index].Text;
CybosHelper.STOCK_CUR_ITEM Item = m_CybosHelper.GetItem(strCode);
STOCK_CUR_ITEM Item = m_CybosHelper.GetItem(strCode);
if (Item == null)
return;
@@ -546,6 +591,42 @@ namespace AutoSellerNS
Config.SetMockTrading(cbMockTrading.Checked);
}
class ListViewItemComparer : IComparer
{
int m_iColumn = 0;
SortOrder m_Order = SortOrder.Descending;
public ListViewItemComparer(int column, SortOrder Order)
{
m_iColumn = column;
m_Order = Order;
}
public int Compare(object x, object y)
{
ListViewItem item1 = (ListViewItem)x;
ListViewItem item2 = (ListViewItem)y;
double num1;
double num2;
if (double.TryParse(item1.SubItems[m_iColumn].Text, out num1) &&
double.TryParse(item2.SubItems[m_iColumn].Text, out num2))
{
if (m_Order == SortOrder.Ascending)
return (num1 > num2) ? 1 : -1;
else
return (num1 > num2) ? -1 : 1;
}
else
{
if (m_Order == SortOrder.Ascending)
return string.Compare(item1.SubItems[m_iColumn].Text, item2.SubItems[m_iColumn].Text);
else
return string.Compare(item2.SubItems[m_iColumn].Text, item1.SubItems[m_iColumn].Text);
}
}
}
#region Simulation
private void btLoad_Click(object sender, EventArgs e)
{
@@ -558,7 +639,84 @@ namespace AutoSellerNS
SimulationHelper helper = new SimulationHelper(this, m_CybosHelper, tbSimulationLog);
helper.StartSimuation2();
}
private void btUpdateNC_Click(object sender, EventArgs e)
{
UpdateNCItem();
}
#endregion
private void lvItems_ColumnClick(object sender, ColumnClickEventArgs e)
{
SortOrder Order = (lvItems.Sorting == SortOrder.Ascending || lvItems.Sorting == SortOrder.None) ? SortOrder.Descending : SortOrder.Ascending;
lvItems.ListViewItemSorter = new ListViewItemComparer(e.Column, Order);
lvItems.Sorting = Order;
lvItems.Sort();
}
private void lvNCItem_ColumnClick(object sender, ColumnClickEventArgs e)
{
SortOrder Order = (lvNCItem.Sorting == SortOrder.Ascending || lvNCItem.Sorting == SortOrder.None) ? SortOrder.Descending : SortOrder.Ascending;
lvNCItem.ListViewItemSorter = new ListViewItemComparer(e.Column, Order);
lvNCItem.Sorting = Order;
lvNCItem.Sort();
}
private async void btClearAll_Click(object sender, EventArgs e)
{
List<ITEM> cloned;
lock (m_Items)
cloned = m_Items.ConvertAll(s => s);
foreach(var item in cloned)
{
await m_CybosHelper.SellItem(item.m_strCode, item.m_iItemCnt);
}
}
private void btUpdateConclusion_Click(object sender, EventArgs e)
{
m_CybosHelper.GetConclusion();
}
public void UpdateConclusionCallback(List<CybosHelper.CONCLUSION> data)
{
List<ListViewItem> items = new List<ListViewItem>();
string lastItemCode = "";
foreach(var conclusion in data)
{
bool bSameItem = (conclusion.strCode == lastItemCode);
var listItem = new ListViewItem(new string[] {
bSameItem ? "" : conclusion.strCode,
bSameItem ? "" : conclusion.strCodeName,
conclusion.strSellBuy,
conclusion.iCommitedAmount.ToString("n0"),
conclusion.iSettlementAmount.ToString("n0"),
conclusion.iFee.ToString("n0"),
conclusion.iTransactionTax.ToString("n0"),
conclusion.iSpecialTax.ToString("n0"),
conclusion.strDate
});
if (conclusion.strSellBuy == "매수")
listItem.SubItems[2].ForeColor = Color.Red;
else
listItem.SubItems[2].ForeColor = Color.Blue;
listItem.UseItemStyleForSubItems = false;
items.Add(listItem);
}
lvConclusion.Invoke(new Action(() => {
lvConclusion.BeginUpdate();
lvConclusion.Items.Clear();
lvConclusion.Items.AddRange(items.ToArray());
foreach (ColumnHeader col in lvConclusion.Columns)
col.Width = -2;
lvConclusion.EndUpdate();
}));
}
}
}