Files
marketdata/MarketDataLib/Helper/InsiderTransactionMarketDataHelper.cs
2024-02-22 14:52:53 -05:00

122 lines
4.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using MarketData.MarketDataModel;
using MarketData.DataAccess;
using MarketData.Utils;
namespace MarketData.Helper
{
public class InsiderTransactionMarketDataHelper
{
private static int MaxThreads = (int)ThreadHelperEnum.MaxThreads;
private static int WAIT_TIME_MS=500; // wait between request
private List<String> symbols;
private int currentIndex = 0;
public InsiderTransactionMarketDataHelper()
{
}
public bool LoadInsiderTransactions(String symbol)
{
Profiler profiler=new Profiler();
try
{
symbols = new List<String>();
symbols.Add(symbol);
currentIndex = 0;
while (true)
{
List<String> queueSymbols = GetQueueSymbols();
if (null == queueSymbols || 0 == queueSymbols.Count) break;
ManualResetEvent[] resetEvents = new ManualResetEvent[queueSymbols.Count];
for (int eventIndex = 0; eventIndex < resetEvents.Length; eventIndex++)
{
resetEvents[eventIndex] = new ManualResetEvent(false);
}
for (int index = 0; index < queueSymbols.Count; index++)
{
ThreadHelper threadHelper = new ThreadHelper(queueSymbols[index], resetEvents[index]);
ThreadPool.QueueUserWorkItem(ThreadPoolCallback, threadHelper);
}
MDTrace.WriteLine(LogLevel.DEBUG,"Insider Transactions, waiting for queued items to complete.");
WaitHandle.WaitAll(resetEvents);
}
return true;
}
finally
{
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("[LoadInsiderTransactions]End, total took {0}(ms)",profiler.End()));
}
}
public bool LoadInsiderTransactions(List<String> symbols)
{
Profiler profiler=new Profiler();
try
{
this.symbols = symbols;
currentIndex = 0;
while (true)
{
List<String> queueSymbols = GetQueueSymbols();
if (null == queueSymbols || 0 == queueSymbols.Count) break;
ManualResetEvent[] resetEvents = new ManualResetEvent[queueSymbols.Count];
for (int eventIndex = 0; eventIndex < resetEvents.Length; eventIndex++)
{
resetEvents[eventIndex] = new ManualResetEvent(false);
}
for (int index = 0; index < queueSymbols.Count; index++)
{
ThreadHelper threadHelper = new ThreadHelper(queueSymbols[index], resetEvents[index]);
ThreadPool.QueueUserWorkItem(ThreadPoolCallback, threadHelper);
try { Thread.Sleep(WAIT_TIME_MS); } catch(Exception) { ;} // wait
}
MDTrace.WriteLine(LogLevel.DEBUG,"Insider Transactions, waiting for queued items to complete.");
WaitHandle.WaitAll(resetEvents);
}
return true;
}
finally
{
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("[LoadInsiderTransactions]End, total took {0}(ms)",profiler.End()));
}
}
private List<String> GetQueueSymbols()
{
List<String> queueSymbols = new List<String>();
int index = currentIndex;
for (; index < currentIndex + MaxThreads && index < symbols.Count; index++)
{
queueSymbols.Add(symbols[index]);
}
currentIndex = index;
return queueSymbols;
}
public void ThreadPoolCallback(Object threadHelperContext)
{
ThreadHelper threadHelper = (ThreadHelper)threadHelperContext;
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Insider Transactions Thread {0} started for {1}...", Thread.CurrentThread.ManagedThreadId, threadHelper.Symbol));
LoadInsiderTransactionsSymbolEx(threadHelper.Symbol);
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Insider Transactions Thread {0} ended for {1}...", Thread.CurrentThread.ManagedThreadId, threadHelper.Symbol));
threadHelper.ResetEvent.Set();
}
public static void LoadInsiderTransactionsSymbolEx(String symbol)
{
symbol = symbol.ToUpper();
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Load insider transactions for {0}", symbol));
InsiderTransactions insiderTransactions = MarketDataHelper.GetInsiderTransactions(symbol);
// public static SECFilings GetSECInsiderTransactions(String symbol,String cik,int timePeriodDays=7)
if (null == insiderTransactions || 0 == insiderTransactions.Count)
{
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("No insider transactions for {0}", symbol));
return;
}
MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Insider Transactions, Saving {0} records for {1}", insiderTransactions.Count, symbol));
InsiderTransactionDA.InsertInsiderTransactions(insiderTransactions);
MDTrace.WriteLine(LogLevel.DEBUG,"Insider Transactions - Done.");
}
}
}