上午布伦特冲100 下午缩回97 写了个价差监控
今天上午布伦特一度摸到100美元,我还在想这波能追,结果下午4点40再看只剩97.4,WTI 是89.6,两个油差着7.8。一天之内价差从上午的十来块收窄到这个位置,我原来只盯单一品种、还拿固定点数止损去扛,来回被穿。索性写了个只监控不下单的小脚本,同时取两个品种的报价,算价差或者比价,超过上限下限就弹窗加写日志,同一方向30分钟才提醒一次,免得手机响个不停。金银比也一样,今天金价4056、白银52,比值大概78,这种比值天生爱往回走,拉太开往往不是方向问题而是节奏问题。脚本不替我下单,只提醒我抬眼看一下。
//+------------------------------------------------------------------+
//| _spread.mq5 跨品种价差/比价旁路监控(只报警不下单) |
//+------------------------------------------------------------------+
#property script_show_inputs
input string InpSymA = "XBRUSD"; // 品种A(布伦特)
input string InpSymB = "XTIUSD"; // 品种B(WTI)
input bool InpUseRatio = false; // true=算比价 A/B,false=算价差 A-B
input double InpUpper = 9.5; // 价差/比价 上限
input double InpLower = 6.0; // 价差/比价 下限
input bool InpAlert = true; // 超阈值弹窗
input bool InpLog = true; // 超阈值写日志
input int InpCooldown = 30; // 同一方向重复提醒间隔(分钟)
datetime g_lastUp = 0, g_lastDn = 0;
double GetVal()
{
double a = SymbolInfoDouble(InpSymA, SYMBOL_BID);
double b = SymbolInfoDouble(InpSymB, SYMBOL_BID);
if(a <= 0 || b <= 0) return -1;
if(InpUseRatio) return (b > 0 ? a / b : -1);
return a - b;
}
void LogIt(string tag, double v)
{
if(!InpLog) return;
int h = FileOpen("spread_log.csv", FILE_READ|FILE_WRITE|FILE_CSV|FILE_ANSI);
if(h == INVALID_HANDLE) return;
FileSeek(h, 0, SEEK_END);
FileWrite(h, TimeToString(TimeCurrent(), TIME_DATE|TIME_SECONDS), tag, DoubleToString(v, 3));
FileClose(h);
}
void OnStart()
{
double v = GetVal();
if(v < 0) { Print("品种报价取不到,检查 InpSymA/InpSymB 名称"); return; }
datetime now = TimeCurrent();
string tag = "";
if(v > InpUpper && (now - g_lastUp) > InpCooldown * 60)
{
tag = "ABOVE_UPPER";
g_lastUp = now;
}
else if(v < InpLower && (now - g_lastDn) > InpCooldown * 60)
{
tag = "BELOW_LOWER";
g_lastDn = now;
}
Print(InpSymA, "/", InpSymB, " ", (InpUseRatio ? "比值=" : "价差="), DoubleToString(v, 3));
if(tag != "")
{
LogIt(tag, v);
if(InpAlert) Alert(InpSymA, " 与 ", InpSymB, " ", tag, " 当前 ", DoubleToString(v, 3));
}
}全部回复(15)
发表回复
请先登录后再回复。
