update EA

This commit is contained in:
Kunthawat Greethong
2026-01-08 13:21:40 +07:00
parent 92391f9d18
commit 2d2a6d30a0
2 changed files with 237 additions and 245 deletions

View File

@@ -38,6 +38,7 @@ enum ENUM_OI_SOURCE {
};
input ENUM_OI_SOURCE InpOISource = OI_SOURCE_MANUAL;
input string InpOICsvPath = "\\Files\\oi_data.csv"; // Path to CSV file
input int InpCSVReloadInterval = 60; // CSV reload interval (minutes, 0=disabled)
input double InpManualFuturePrice = 0.0; // Manual future price input
// OI Levels (Manual Input - สามารถใส่ได้ถึง 3 ระดับ)
@@ -170,6 +171,12 @@ bool NewsBlockActive = false;
datetime NewsBlockStart = 0;
datetime NewsBlockEnd = 0;
// CSV Cache Variables
double CachedFuturePrice = -1;
string LoadedCSVPath = "";
bool CSVLoadLogged = false;
datetime LastCSVReloadTime = 0;
//=== NEW: Control Panel Variables ===
// Control Panel Position - Moved below dashboard
int ControlPanelX = 10;
@@ -357,21 +364,31 @@ void OnTick()
//+------------------------------------------------------------------+
void OnTimer()
{
//--- Update dashboard periodically
if(InpEnableDashboard)
{
UpdateDashboard();
UpdateControlPanel();
}
//--- Check news events
if(InpAvoidHighImpactNews)
{
CheckNewsEvents();
}
//--- Reset daily statistics if new day
CheckDailyReset();
//--- Update dashboard periodically
if(InpEnableDashboard)
{
UpdateDashboard();
UpdateControlPanel();
}
//--- Check news events
if(InpAvoidHighImpactNews)
{
CheckNewsEvents();
}
//--- Reset daily statistics if new day
CheckDailyReset();
//--- CSV reload check
if(InpOISource == OI_SOURCE_CSV_FILE && InpCSVReloadInterval > 0) {
if(TimeCurrent() - LastCSVReloadTime >= InpCSVReloadInterval * 60) {
Print("CSV Reload: Scheduled reload triggered");
LastCSVReloadTime = TimeCurrent();
CachedFuturePrice = -1;
LoadOIFromCSV();
}
}
}
//+------------------------------------------------------------------+
@@ -1094,64 +1111,91 @@ void InitializeOILevels()
//+------------------------------------------------------------------+
void LoadOIFromCSV()
{
string filename = InpOICsvPath;
int filehandle = FileOpen(filename, FILE_READ|FILE_CSV|FILE_ANSI, ',');
if(filehandle == INVALID_HANDLE)
{
Print("Error opening CSV file: ", filename);
// Fall back to manual levels
InitializeOILevels();
return;
}
// Skip header if exists
FileReadString(filehandle);
int callIndex = 0;
int putIndex = 0;
while(!FileIsEnding(filehandle) && (callIndex < 3 || putIndex < 3))
{
string row = FileReadString(filehandle);
string data[];
int count = StringSplit(row, ',', data);
if(count >= 3)
{
string type = data[0];
double strike = StringToDouble(data[1]);
int oi = (int)StringToInteger(data[2]);
if(type == "CALL" && callIndex < 3)
{
CallLevels[callIndex] = strike;
CallOI[callIndex] = oi;
callIndex++;
}
else if(type == "PUT" && putIndex < 3)
{
PutLevels[putIndex] = strike;
PutOI[putIndex] = oi;
putIndex++;
}
}
}
FileClose(filehandle);
// Fill any missing levels with zeros
for(int i = callIndex; i < 3; i++)
{
CallLevels[i] = 0;
CallOI[i] = 0;
}
for(int i = putIndex; i < 3; i++)
{
PutLevels[i] = 0;
PutOI[i] = 0;
}
string filename = InpOICsvPath;
int filehandle = FileOpen(filename, FILE_READ|FILE_CSV|FILE_ANSI, ',');
if(filehandle == INVALID_HANDLE)
{
Print("Error opening CSV file: ", filename);
InitializeOILevels();
return;
}
int callIndex = 0;
int putIndex = 0;
double futurePrice = 0.0;
bool isFirstLine = true;
while(!FileIsEnding(filehandle) && (callIndex < 3 || putIndex < 3))
{
string row = FileReadString(filehandle);
if(isFirstLine) {
isFirstLine = false;
if(StringFind(row, "Type") >= 0 || StringFind(row, "Strike") >= 0 || StringFind(row, "OI") >= 0) {
continue;
}
}
string data[];
int count = StringSplit(row, ',', data);
if(count >= 3)
{
string type = data[0];
double strike = StringToDouble(data[1]);
int oi = (int)StringToInteger(data[2]);
if(StringFind(type, "Future") >= 0)
{
futurePrice = strike;
if(!CSVLoadLogged) {
Print("DEBUG: Parsed Future price: ", futurePrice);
}
}
else if(type == "CALL" && callIndex < 3)
{
CallLevels[callIndex] = strike;
CallOI[callIndex] = oi;
callIndex++;
}
else if(type == "PUT" && putIndex < 3)
{
PutLevels[putIndex] = strike;
PutOI[putIndex] = oi;
putIndex++;
}
}
}
FileClose(filehandle);
for(int i = callIndex; i < 3; i++)
{
CallLevels[i] = 0;
CallOI[i] = 0;
}
for(int i = putIndex; i < 3; i++)
{
PutLevels[i] = 0;
PutOI[i] = 0;
}
if(futurePrice > 0) {
CachedFuturePrice = futurePrice;
DynamicFuturePrice = futurePrice;
FuturePrice = futurePrice;
LoadedCSVPath = filename;
CSVLoadLogged = true;
Print("CSV SUCCESS: FuturePrice=", futurePrice, ", CALL=[", CallLevels[0], ",", CallLevels[1], ",", CallLevels[2], "], PUT=[", PutLevels[0], ",", PutLevels[1], ",", PutLevels[2], "] loaded from ", filename);
} else {
if(!CSVLoadLogged) {
Print("CSV ERROR: No valid price found in ", filename);
CSVLoadLogged = true;
}
CachedFuturePrice = 0;
}
}
//+------------------------------------------------------------------+
@@ -1214,32 +1258,36 @@ bool InitializeIndicators()
//+------------------------------------------------------------------+
bool UpdateMarketData()
{
// Get current prices
SpotPrice = SymbolInfo.Bid();
SymbolInfo.RefreshRates();
// Get future price (manual or from symbol)
if(DynamicFuturePrice > 0)
{
FuturePrice = DynamicFuturePrice;
}
else if(InpManualFuturePrice > 0)
{
FuturePrice = InpManualFuturePrice;
}
else
{
// In real implementation, you might get this from a different symbol
// For now, use spot price with a small offset
FuturePrice = SpotPrice;
}
// Calculate Delta
double previousDelta = DeltaPrice;
DeltaPrice = FuturePrice - SpotPrice;
// Update Delta EMA
UpdateDeltaEMA();
// Get current prices
SpotPrice = SymbolInfo.Bid();
SymbolInfo.RefreshRates();
// Get future price (CSV cache, manual or from symbol)
if(CachedFuturePrice > 0)
{
FuturePrice = CachedFuturePrice;
}
else if(DynamicFuturePrice > 0)
{
FuturePrice = DynamicFuturePrice;
}
else if(InpManualFuturePrice > 0)
{
FuturePrice = InpManualFuturePrice;
}
else
{
// In real implementation, you might get this from a different symbol
// For now, use spot price with a small offset
FuturePrice = SpotPrice;
}
// Calculate Delta
double previousDelta = DeltaPrice;
DeltaPrice = FuturePrice - SpotPrice;
// Update Delta EMA
UpdateDeltaEMA();
// Calculate Deviation
DeltaDeviation = DeltaPrice - DeltaEMA;