update code
This commit is contained in:
2220
OI_MeanReversion_Pro_XAUUSD.mq5
Normal file
2220
OI_MeanReversion_Pro_XAUUSD.mq5
Normal file
File diff suppressed because it is too large
Load Diff
Binary file not shown.
@@ -111,6 +111,8 @@ double SpotPrice = 0.0;
|
|||||||
|
|
||||||
double CallLevels[3];
|
double CallLevels[3];
|
||||||
double PutLevels[3];
|
double PutLevels[3];
|
||||||
|
int CallOI[3];
|
||||||
|
int PutOI[3];
|
||||||
|
|
||||||
double DynamicFuturePrice = 0.0;
|
double DynamicFuturePrice = 0.0;
|
||||||
double DynamicCallStrike1 = 0.0;
|
double DynamicCallStrike1 = 0.0;
|
||||||
@@ -194,17 +196,9 @@ double CurrentBarHigh = 0.0;
|
|||||||
double CurrentBarLow = 0.0;
|
double CurrentBarLow = 0.0;
|
||||||
double LastPrice = 0.0;
|
double LastPrice = 0.0;
|
||||||
|
|
||||||
double CachedFuturePrice = -1; // -1 = not loaded, 0 = loaded but failed, >0 = success
|
double CachedFuturePrice = -1;
|
||||||
string LoadedCSVPath = ""; // Path from which CSV was successfully loaded
|
string LoadedCSVPath = "";
|
||||||
bool CSVLoadLogged = false; // Track if we've logged the result
|
bool CSVLoadLogged = false;
|
||||||
|
|
||||||
double CSVDynamicCallStrike1 = 0.0;
|
|
||||||
double CSVDynamicCallStrike2 = 0.0;
|
|
||||||
double CSVDynamicCallStrike3 = 0.0;
|
|
||||||
double CSVDynamicPutStrike1 = 0.0;
|
|
||||||
double CSVDynamicPutStrike2 = 0.0;
|
|
||||||
double CSVDynamicPutStrike3 = 0.0;
|
|
||||||
bool CSVStrikesLoaded = false;
|
|
||||||
|
|
||||||
int OnInit() {
|
int OnInit() {
|
||||||
Trade.SetExpertMagicNumber(InpMagicNumber);
|
Trade.SetExpertMagicNumber(InpMagicNumber);
|
||||||
@@ -214,7 +208,7 @@ int OnInit() {
|
|||||||
SymbolInfo.Name(_Symbol);
|
SymbolInfo.Name(_Symbol);
|
||||||
SymbolInfo.RefreshRates();
|
SymbolInfo.RefreshRates();
|
||||||
|
|
||||||
DynamicFuturePrice = InpManualFuturePrice;
|
DynamicFuturePrice = InpManualFuturePrice;
|
||||||
DynamicCallStrike1 = InpCallStrike1;
|
DynamicCallStrike1 = InpCallStrike1;
|
||||||
DynamicCallStrike2 = InpCallStrike2;
|
DynamicCallStrike2 = InpCallStrike2;
|
||||||
DynamicCallStrike3 = InpCallStrike3;
|
DynamicCallStrike3 = InpCallStrike3;
|
||||||
@@ -226,8 +220,7 @@ int OnInit() {
|
|||||||
InitializeKeyLevels();
|
InitializeKeyLevels();
|
||||||
|
|
||||||
if(InpOISource == OI_SOURCE_CSV_FILE) {
|
if(InpOISource == OI_SOURCE_CSV_FILE) {
|
||||||
LoadFuturePriceFromCSV();
|
LoadOIFromCSV();
|
||||||
ApplyCSVStrikeLevels();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!InitializeIndicators()) {
|
if(!InitializeIndicators()) {
|
||||||
@@ -446,15 +439,18 @@ void DetectAbsorptionFromHistory() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void UpdateMarketData() {
|
void UpdateMarketData() {
|
||||||
SpotPrice = SymbolInfo.Bid();
|
SpotPrice = SymbolInfo.Bid();
|
||||||
SymbolInfo.RefreshRates();
|
SymbolInfo.RefreshRates();
|
||||||
|
|
||||||
if(InpOISource == OI_SOURCE_CSV_FILE) {
|
if(InpOISource == OI_SOURCE_CSV_FILE) {
|
||||||
FuturePrice = LoadFuturePriceFromCSV();
|
if(CachedFuturePrice < 0) {
|
||||||
} else {
|
LoadOIFromCSV();
|
||||||
FuturePrice = 0;
|
}
|
||||||
}
|
FuturePrice = CachedFuturePrice;
|
||||||
}
|
} else {
|
||||||
|
FuturePrice = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool IsPriceNearPutStrike() {
|
bool IsPriceNearPutStrike() {
|
||||||
double tolerance = InpOIZonePoints * _Point;
|
double tolerance = InpOIZonePoints * _Point;
|
||||||
@@ -940,14 +936,22 @@ void CloseAllPositions() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void InitializeOILevels() {
|
void InitializeOILevels() {
|
||||||
CallLevels[0] = DynamicCallStrike1;
|
CallLevels[0] = DynamicCallStrike1;
|
||||||
CallLevels[1] = DynamicCallStrike2;
|
CallLevels[1] = DynamicCallStrike2;
|
||||||
CallLevels[2] = DynamicCallStrike3;
|
CallLevels[2] = DynamicCallStrike3;
|
||||||
|
|
||||||
PutLevels[0] = DynamicPutStrike1;
|
PutLevels[0] = DynamicPutStrike1;
|
||||||
PutLevels[1] = DynamicPutStrike2;
|
PutLevels[1] = DynamicPutStrike2;
|
||||||
PutLevels[2] = DynamicPutStrike3;
|
PutLevels[2] = DynamicPutStrike3;
|
||||||
}
|
|
||||||
|
CallOI[0] = 0;
|
||||||
|
CallOI[1] = 0;
|
||||||
|
CallOI[2] = 0;
|
||||||
|
|
||||||
|
PutOI[0] = 0;
|
||||||
|
PutOI[1] = 0;
|
||||||
|
PutOI[2] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
void InitializeKeyLevels() {
|
void InitializeKeyLevels() {
|
||||||
double allLevels[];
|
double allLevels[];
|
||||||
@@ -1001,180 +1005,114 @@ bool InitializeIndicators() {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
double LoadFuturePriceFromCSV() {
|
void LoadOIFromCSV()
|
||||||
if(CachedFuturePrice >= 0 && CachedFuturePrice != 0) {
|
{
|
||||||
return CachedFuturePrice;
|
string paths[];
|
||||||
}
|
int pathCount = 0;
|
||||||
|
|
||||||
string paths[];
|
ArrayResize(paths, 5);
|
||||||
int pathCount = 0;
|
paths[pathCount++] = InpOICsvPath;
|
||||||
|
paths[pathCount++] = "oi_data.csv";
|
||||||
|
paths[pathCount++] = "\\Files\\oi_data.csv";
|
||||||
|
paths[pathCount++] = "..\\oi_scraper\\oi_data.csv";
|
||||||
|
paths[pathCount++] = "../oi_scraper/oi_data.csv";
|
||||||
|
|
||||||
ArrayResize(paths, 5);
|
int filehandle = INVALID_HANDLE;
|
||||||
paths[pathCount++] = InpOICsvPath;
|
string foundPath = "";
|
||||||
paths[pathCount++] = "oi_data.csv";
|
|
||||||
paths[pathCount++] = "\\Files\\oi_data.csv";
|
|
||||||
paths[pathCount++] = "..\\oi_scraper\\oi_data.csv";
|
|
||||||
paths[pathCount++] = "../oi_scraper/oi_data.csv";
|
|
||||||
|
|
||||||
int filehandle = INVALID_HANDLE;
|
for(int i = 0; i < pathCount; i++) {
|
||||||
string foundPath = "";
|
if(!CSVLoadLogged) {
|
||||||
|
Print("Trying CSV path: ", paths[i]);
|
||||||
|
}
|
||||||
|
filehandle = FileOpen(paths[i], FILE_READ | FILE_CSV | FILE_ANSI, ',');
|
||||||
|
if(filehandle != INVALID_HANDLE) {
|
||||||
|
foundPath = paths[i];
|
||||||
|
if(!CSVLoadLogged) {
|
||||||
|
Print("Found CSV file at: ", foundPath);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for(int i = 0; i < pathCount; i++) {
|
if(filehandle == INVALID_HANDLE) {
|
||||||
if(!CSVLoadLogged) {
|
if(!CSVLoadLogged) {
|
||||||
Print("Trying CSV path: ", paths[i]);
|
Print("CSV ERROR: File not found. Searched paths:");
|
||||||
}
|
for(int i = 0; i < pathCount; i++) {
|
||||||
filehandle = FileOpen(paths[i], FILE_READ | FILE_CSV | FILE_ANSI, ',');
|
Print(" - ", paths[i]);
|
||||||
if(filehandle != INVALID_HANDLE) {
|
}
|
||||||
foundPath = paths[i];
|
CSVLoadLogged = true;
|
||||||
if(!CSVLoadLogged) {
|
}
|
||||||
Print("Found CSV file at: ", foundPath);
|
return;
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(filehandle == INVALID_HANDLE) {
|
int callIndex = 0;
|
||||||
if(!CSVLoadLogged) {
|
int putIndex = 0;
|
||||||
Print("CSV ERROR: File not found. Searched paths:");
|
double futurePrice = 0.0;
|
||||||
for(int i = 0; i < pathCount; i++) {
|
|
||||||
Print(" - ", paths[i]);
|
|
||||||
}
|
|
||||||
CSVLoadLogged = true;
|
|
||||||
}
|
|
||||||
CachedFuturePrice = 0;
|
|
||||||
return 0.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
double futurePrice = 0.0;
|
while(!FileIsEnding(filehandle) && (callIndex < 3 || putIndex < 3))
|
||||||
double callStrikes[];
|
{
|
||||||
double putStrikes[];
|
string row = FileReadString(filehandle);
|
||||||
int callCount = 0;
|
string data[];
|
||||||
int putCount = 0;
|
int count = StringSplit(row, ',', data);
|
||||||
bool inPriceSection = false;
|
|
||||||
int dataLineCount = 0;
|
|
||||||
|
|
||||||
while(!FileIsEnding(filehandle)) {
|
if(count >= 3)
|
||||||
string line = FileReadString(filehandle);
|
{
|
||||||
dataLineCount++;
|
string type = data[0];
|
||||||
|
double strike = StringToDouble(data[1]);
|
||||||
|
int oi = (int)StringToInteger(data[2]);
|
||||||
|
|
||||||
if(line == "") {
|
if(StringFind(type, "Future") >= 0)
|
||||||
inPriceSection = true;
|
{
|
||||||
continue;
|
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++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(dataLineCount == 1 && StringGetCharacter(line, 0) == 0xFEFF) {
|
FileClose(filehandle);
|
||||||
line = StringSubstr(line, 1);
|
|
||||||
if(line == "") {
|
|
||||||
inPriceSection = true;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(StringFind(line, "Type") >= 0 || StringFind(line, "Strike") >= 0 || StringFind(line, "OI") >= 0) {
|
for(int i = callIndex; i < 3; i++)
|
||||||
continue;
|
{
|
||||||
}
|
CallLevels[i] = 0;
|
||||||
|
CallOI[i] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
if(line == "[Price]" || inPriceSection) {
|
for(int i = putIndex; i < 3; i++)
|
||||||
inPriceSection = true;
|
{
|
||||||
string parts[];
|
PutLevels[i] = 0;
|
||||||
int split = StringSplit(line, ',', parts);
|
PutOI[i] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
if(split >= 2 && parts[0] == "FuturePrice") {
|
if(futurePrice > 0) {
|
||||||
string priceStr = parts[1];
|
CachedFuturePrice = futurePrice;
|
||||||
while(StringLen(priceStr) > 0 && (StringGetCharacter(priceStr, 0) == 32 || StringGetCharacter(priceStr, 0) == 9)) {
|
DynamicFuturePrice = futurePrice;
|
||||||
priceStr = StringSubstr(priceStr, 1);
|
LoadedCSVPath = foundPath;
|
||||||
}
|
CSVLoadLogged = true;
|
||||||
while(StringLen(priceStr) > 0 && (StringGetCharacter(priceStr, StringLen(priceStr)-1) == 32 || StringGetCharacter(priceStr, StringLen(priceStr)-1) == 9)) {
|
Print("CSV SUCCESS: FuturePrice=", futurePrice, ", CALL=[", CallLevels[0], ",", CallLevels[1], ",", CallLevels[2], "], PUT=[", PutLevels[0], ",", PutLevels[1], ",", PutLevels[2], "] loaded from ", foundPath);
|
||||||
priceStr = StringSubstr(priceStr, 0, StringLen(priceStr)-1);
|
} else {
|
||||||
}
|
if(!CSVLoadLogged) {
|
||||||
futurePrice = StringToDouble(priceStr);
|
Print("CSV ERROR: No valid price found in ", foundPath);
|
||||||
if(!CSVLoadLogged) {
|
CSVLoadLogged = true;
|
||||||
Print("DEBUG: Parsed FuturePrice: ", futurePrice);
|
}
|
||||||
}
|
CachedFuturePrice = 0;
|
||||||
}
|
}
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
string parts[];
|
InitializeKeyLevels();
|
||||||
int split = StringSplit(line, ',', parts);
|
}
|
||||||
|
|
||||||
if(split >= 3) {
|
|
||||||
string typeStr = parts[0];
|
|
||||||
string strikeStr = parts[1];
|
|
||||||
string oiStr = parts[2];
|
|
||||||
|
|
||||||
while(StringLen(strikeStr) > 0 && (StringGetCharacter(strikeStr, 0) == 32 || StringGetCharacter(strikeStr, 0) == 9)) {
|
|
||||||
strikeStr = StringSubstr(strikeStr, 1);
|
|
||||||
}
|
|
||||||
while(StringLen(strikeStr) > 0 && (StringGetCharacter(strikeStr, StringLen(strikeStr)-1) == 32 || StringGetCharacter(strikeStr, StringLen(strikeStr)-1) == 9)) {
|
|
||||||
strikeStr = StringSubstr(strikeStr, 0, StringLen(strikeStr)-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
double strike = StringToDouble(strikeStr);
|
|
||||||
if(strike <= 0) continue;
|
|
||||||
|
|
||||||
if(typeStr == "CALL") {
|
|
||||||
ArrayResize(callStrikes, callCount + 1);
|
|
||||||
callStrikes[callCount] = strike;
|
|
||||||
callCount++;
|
|
||||||
} else if(typeStr == "PUT") {
|
|
||||||
ArrayResize(putStrikes, putCount + 1);
|
|
||||||
putStrikes[putCount] = strike;
|
|
||||||
putCount++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
FileClose(filehandle);
|
|
||||||
|
|
||||||
if(callCount > 0) {
|
|
||||||
ArraySort(callStrikes);
|
|
||||||
if(callCount >= 1) CSVDynamicCallStrike1 = callStrikes[callCount-1];
|
|
||||||
if(callCount >= 2) CSVDynamicCallStrike2 = callStrikes[callCount-2];
|
|
||||||
if(callCount >= 3) CSVDynamicCallStrike3 = callStrikes[callCount-3];
|
|
||||||
}
|
|
||||||
|
|
||||||
if(putCount > 0) {
|
|
||||||
ArraySort(putStrikes);
|
|
||||||
if(putCount >= 1) CSVDynamicPutStrike1 = putStrikes[putCount-1];
|
|
||||||
if(putCount >= 2) CSVDynamicPutStrike2 = putStrikes[putCount-2];
|
|
||||||
if(putCount >= 3) CSVDynamicPutStrike3 = putStrikes[putCount-3];
|
|
||||||
}
|
|
||||||
|
|
||||||
if(futurePrice > 0) {
|
|
||||||
CachedFuturePrice = futurePrice;
|
|
||||||
LoadedCSVPath = foundPath;
|
|
||||||
CSVStrikesLoaded = true;
|
|
||||||
if(!CSVLoadLogged) {
|
|
||||||
Print("CSV SUCCESS: FuturePrice=", futurePrice, ", CALL=[", CSVDynamicCallStrike1, ",", CSVDynamicCallStrike2, ",", CSVDynamicCallStrike3, "], PUT=[", CSVDynamicPutStrike1, ",", CSVDynamicPutStrike2, ",", CSVDynamicPutStrike3, "] loaded from ", foundPath);
|
|
||||||
CSVLoadLogged = true;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if(!CSVLoadLogged) {
|
|
||||||
Print("CSV ERROR: No valid price found in ", foundPath);
|
|
||||||
Print(" - File exists but contains no parseable price data");
|
|
||||||
Print(" - Total lines read: ", dataLineCount);
|
|
||||||
CSVLoadLogged = true;
|
|
||||||
}
|
|
||||||
CachedFuturePrice = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return CachedFuturePrice;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ApplyCSVStrikeLevels() {
|
|
||||||
if(!CSVStrikesLoaded) return;
|
|
||||||
|
|
||||||
if(DynamicCallStrike1 == 0 && CSVDynamicCallStrike1 > 0) DynamicCallStrike1 = CSVDynamicCallStrike1;
|
|
||||||
if(DynamicCallStrike2 == 0 && CSVDynamicCallStrike2 > 0) DynamicCallStrike2 = CSVDynamicCallStrike2;
|
|
||||||
if(DynamicCallStrike3 == 0 && CSVDynamicCallStrike3 > 0) DynamicCallStrike3 = CSVDynamicCallStrike3;
|
|
||||||
if(DynamicPutStrike1 == 0 && CSVDynamicPutStrike1 > 0) DynamicPutStrike1 = CSVDynamicPutStrike1;
|
|
||||||
if(DynamicPutStrike2 == 0 && CSVDynamicPutStrike2 > 0) DynamicPutStrike2 = CSVDynamicPutStrike2;
|
|
||||||
if(DynamicPutStrike3 == 0 && CSVDynamicPutStrike3 > 0) DynamicPutStrike3 = CSVDynamicPutStrike3;
|
|
||||||
|
|
||||||
InitializeOILevels();
|
|
||||||
InitializeKeyLevels();
|
|
||||||
}
|
|
||||||
|
|
||||||
void CheckExistingPositions() {
|
void CheckExistingPositions() {
|
||||||
for(int i = 0; i < PositionsTotal(); i++) {
|
for(int i = 0; i < PositionsTotal(); i++) {
|
||||||
@@ -1440,7 +1378,34 @@ void UpdateInputValues() {
|
|||||||
InitializeKeyLevels();
|
InitializeKeyLevels();
|
||||||
|
|
||||||
if(InpOISource == OI_SOURCE_CSV_FILE) {
|
if(InpOISource == OI_SOURCE_CSV_FILE) {
|
||||||
LoadFuturePriceFromCSV();
|
CachedFuturePrice = -1;
|
||||||
ApplyCSVStrikeLevels();
|
LoadOIFromCSV();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(ObjectGetString(chart_id, "CP_CallStrike1", OBJPROP_TEXT, 0, value)) {
|
||||||
|
DynamicCallStrike1 = StringToDouble(value);
|
||||||
|
}
|
||||||
|
if(ObjectGetString(chart_id, "CP_CallStrike2", OBJPROP_TEXT, 0, value)) {
|
||||||
|
DynamicCallStrike2 = StringToDouble(value);
|
||||||
|
}
|
||||||
|
if(ObjectGetString(chart_id, "CP_CallStrike3", OBJPROP_TEXT, 0, value)) {
|
||||||
|
DynamicCallStrike3 = StringToDouble(value);
|
||||||
|
}
|
||||||
|
if(ObjectGetString(chart_id, "CP_PutStrike1", OBJPROP_TEXT, 0, value)) {
|
||||||
|
DynamicPutStrike1 = StringToDouble(value);
|
||||||
|
}
|
||||||
|
if(ObjectGetString(chart_id, "CP_PutStrike2", OBJPROP_TEXT, 0, value)) {
|
||||||
|
DynamicPutStrike2 = StringToDouble(value);
|
||||||
|
}
|
||||||
|
if(ObjectGetString(chart_id, "CP_PutStrike3", OBJPROP_TEXT, 0, value)) {
|
||||||
|
DynamicPutStrike3 = StringToDouble(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
InitializeOILevels();
|
||||||
|
InitializeKeyLevels();
|
||||||
|
|
||||||
|
if(InpOISource == OI_SOURCE_CSV_FILE) {
|
||||||
|
LoadOIFromCSV();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -304,8 +304,7 @@ def export_to_csv(df, future_price=0.0):
|
|||||||
for _, row in put_df.iterrows():
|
for _, row in put_df.iterrows():
|
||||||
f.write(f"PUT,{row['Strike']:.1f},{row['OI']}\n")
|
f.write(f"PUT,{row['Strike']:.1f},{row['OI']}\n")
|
||||||
|
|
||||||
f.write("\n[Price]\n")
|
f.write(f"Future,{future_price},0\n")
|
||||||
f.write(f"FuturePrice,{future_price}\n")
|
|
||||||
|
|
||||||
logger.info(f"Exported OI data and price to {output_path}")
|
logger.info(f"Exported OI data and price to {output_path}")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user