291 lines
19 KiB
MQL5
291 lines
19 KiB
MQL5
//+------------------------------------------------------------------+
|
|
//| SPVIX.mq5 |
|
|
//| Copyright 2024, MetaQuotes Ltd. |
|
|
//| https://www.mql5.com |
|
|
//+------------------------------------------------------------------+
|
|
#property copyright "Copyright 2024, MetaQuotes Ltd."
|
|
#property link "https://www.mql5.com"
|
|
#property version "1.00"
|
|
#include <Math\Stat\Math.mqh>
|
|
#include <Trade\Trade.mqh>
|
|
CTrade ExtTrade; // オブジェクト生成
|
|
/*
|
|
input group "***時間設定***"
|
|
input int Jisa = 7; //日本時間と時差
|
|
input int ho =9; //時間
|
|
input int mi =5; //分
|
|
input int oho =5; //時間
|
|
input double PIP =250; //300
|
|
input group "***時間設定2***"
|
|
input int ho2 =16; //時間
|
|
input int mi2 =5; //分
|
|
*/
|
|
|
|
|
|
|
|
input double zmax = 2.0;
|
|
|
|
|
|
|
|
sinput ulong Magic = 550055; // マジックナンバー
|
|
sinput ulong Slippage = 20; // スリッページ
|
|
sinput double Lots = 0.1; // ロット数
|
|
input double MaxSpread = 50; // エントリー見送りスプレッド
|
|
input group "***利幅損切り設定***"
|
|
input double in_TP = 1000.0; //TP
|
|
input double in_SL =1000.0; //SL
|
|
/*input double in_Trail = 2000.0; //Trail
|
|
input double in_TrailS = 2000.0; //TrailStart
|
|
input double PIP2 =250; //300
|
|
input double in_TP2 = 14000.0; //TP
|
|
input double in_SL2 =2000.0; //SL
|
|
input double in_Trail2 = 2200.0; //Trail
|
|
input double in_TrailS2 = 1400.0; //TrailStart*/
|
|
input group "***閾値***"
|
|
|
|
input group "***通貨***"
|
|
input string NO1 ="USDCHF"; //USDCHF
|
|
input string NO2 ="AUDCHF"; //XAUUSD
|
|
|
|
string symbols[9] = {"AUDCHF", "EURAUD", "GBPAUD", "CADCHF", "EURCAD", "EURNZD", "GBPCAD", "GBPNZD", "NZDCHF"};
|
|
|
|
|
|
int RSI_hNO1[9]; // RSIオブジェクト
|
|
int RSI_hNO2; // RSIオブジェクト
|
|
double RSI0[]; // RSIオブジェクト
|
|
double RSI1[]; // RSIオブジェクト
|
|
double RSI2[]; // RSIオブジェクト
|
|
double RSI3[]; // RSIオブジェクト
|
|
double RSI4[]; // RSIオブジェクト
|
|
double RSI5[]; // RSIオブジェクト
|
|
double RSI6[]; // RSIオブジェクト
|
|
double RSI7[]; // RSIオブジェクト
|
|
double RSI8[]; // RSIオブジェクト
|
|
double RSINO2[]; // RSIオブジェクト
|
|
int L1,L2;
|
|
double dSpread;
|
|
datetime dflag,dflag2;
|
|
//+------------------------------------------------------------------+
|
|
//| Expert initialization function |
|
|
//+------------------------------------------------------------------+
|
|
int OnInit() {
|
|
//---
|
|
// EAの初期設定
|
|
ExtTrade.SetExpertMagicNumber(Magic); // マジックナンバーの設定
|
|
ExtTrade.SetDeviationInPoints(Slippage); // スリッページの設定
|
|
for(int i=0; i<=8; i++) {
|
|
RSI_hNO1[i]= iRSI(symbols[i],PERIOD_CURRENT, 14,PRICE_CLOSE); // RSIの初期設定
|
|
}
|
|
|
|
|
|
RSI_hNO2= iRSI(NO2,PERIOD_CURRENT, 44, PRICE_CLOSE); // RSIの初期設定
|
|
//---
|
|
|
|
return(INIT_SUCCEEDED);
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Expert deinitialization function |
|
|
//+------------------------------------------------------------------+
|
|
void OnDeinit(const int reason) {
|
|
//---
|
|
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| Expert tick function |
|
|
//+------------------------------------------------------------------+
|
|
void OnTick() {
|
|
//---
|
|
/*
|
|
double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
|
|
double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
|
|
dSpread = (Ask - Bid) / (Point()*10);
|
|
*/
|
|
double rsi[9];
|
|
double zscore[9];
|
|
string symbol;
|
|
ulong Tiket;
|
|
ArrayInitialize(rsi,0);
|
|
ArraySetAsSeries(RSI1,true); //配列を時系列に(中身が入っていない状態でもOK)
|
|
|
|
for(int i=0; i<=8; i++) {
|
|
CopyBuffer(RSI_hNO1[i],0,0,3,RSI1);
|
|
rsi[i]=RSI1[0];
|
|
}
|
|
|
|
double heikin=MathMean(rsi);
|
|
double hensa= MathStandardDeviation(rsi);
|
|
heikin=MathRound(heikin,2);
|
|
hensa=MathRound(hensa,2);
|
|
|
|
double max=MathMax(rsi);
|
|
double min=MathMin(rsi);
|
|
max=(max-heikin)/hensa;
|
|
min=(min-heikin)/hensa;
|
|
string symMAX=symbols[ArrayBsearch(rsi,MathMax(rsi))];
|
|
string symMIN=symbols[ArrayBsearch(rsi,MathMin(rsi))];
|
|
|
|
for(int i=0; i<=8; i++) {
|
|
zscore[i]= Zscore(rsi[i],heikin,hensa);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//このEAがポジションを持っているかチェック
|
|
for(int i = 0; i < PositionsTotal(); i++) {
|
|
Tiket = PositionGetTicket(i);
|
|
symbol=PositionGetString(POSITION_SYMBOL);
|
|
if(PositionGetInteger(POSITION_MAGIC) == Magic) break;
|
|
|
|
else {
|
|
Tiket = 0;
|
|
symbol="";
|
|
}
|
|
}
|
|
int num=0;
|
|
int total=ArraySize(symbols);
|
|
for(int i=0; i<total; i++) {
|
|
if(symbols[i]==symbol) {
|
|
num=i;
|
|
break;
|
|
}
|
|
}if(symbols[num]==symbol)
|
|
{
|
|
Print("/////////////////////////////////////////////");
|
|
}
|
|
|
|
if(!(symbol=="")&&MathAbs(zscore[num]) < 0.5&& rsi[num]<30 &&1==SP(symbol)||rsi[num]>60 &&1==LP(symbol) ) {
|
|
ExtTrade.PositionClose(Tiket);
|
|
Print("close");
|
|
}
|
|
if(max>zmax&&rsi[ArrayBsearch(rsi,MathMax(rsi))]>70&&0==SP(symMAX)) {
|
|
SELL("SELL",symMAX,Lots,in_SL,in_TP);
|
|
|
|
}
|
|
|
|
if(min<-zmax&&rsi[ArrayBsearch(rsi,MathMax(rsi))]<30&&0==LP(symMIN)) {
|
|
BUY("BUY",symMIN,Lots,in_SL,in_TP);
|
|
}
|
|
|
|
/* //ハンドルでバッファを指定して、バッファからMA値を配列にコピー
|
|
Print(RSI1[0],"//////////",RSINO2[0]);
|
|
double XAUUSD =MathLog( iClose(NO2,PERIOD_H1,0));
|
|
|
|
double USDCHF =MathLog( iClose(NO1,PERIOD_H1,0));
|
|
double lo3= MathLog( iClose(NO1,PERIOD_H1,3) ); // 対数を取る値
|
|
|
|
double loPER= (USDCHF-lo3)*100;
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*
|
|
Comment(USDCHF,"\n",loPER);
|
|
if(RSINO1[1]>90&&LP()==0&&SP()==0 ) {
|
|
BUY("BUY","USDCHF",Lots,in_SL,in_TP);
|
|
// SELL("SELL","XAUUSD",Lots,in_SL,in_TP);
|
|
}
|
|
|
|
|
|
if(RSINO1[1]<50&&SP() ==0) {
|
|
//BUY("BUY","US500");
|
|
SELL("SELL","USDCHF",Lots,in_SL,in_TP);
|
|
}*/
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
double Zscore(double num, double mean, double std) {
|
|
return (num - mean) / std;
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
void SELL(string COM,string SYM,double l,double s,double L) {
|
|
double Ask=NormalizeDouble(SymbolInfoDouble(SYM,SYMBOL_ASK),_Digits);
|
|
double Bid=NormalizeDouble(SymbolInfoDouble(SYM,SYMBOL_BID),_Digits);
|
|
double SL = NormalizeDouble((Ask + s*_Point), _Digits);
|
|
double TP = NormalizeDouble((Bid -L*_Point), _Digits);
|
|
if(!ExtTrade.Sell(l,SYM,Bid,SL,TP,COM))Print("//////////////");
|
|
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
void BUY(string COM,string SYM,double l,double s,double L) {
|
|
|
|
double Ask=NormalizeDouble(SymbolInfoDouble(SYM,SYMBOL_ASK),_Digits);
|
|
double Bid=NormalizeDouble(SymbolInfoDouble(SYM,SYMBOL_BID),_Digits);
|
|
double SL = NormalizeDouble((Bid -s*_Point), _Digits);
|
|
double TP = NormalizeDouble((Ask +L*_Point), _Digits);
|
|
if(!ExtTrade.Buy(l,SYM,Ask,SL,TP,COM))Print("//////////////");
|
|
|
|
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
//+------------------------------------------------------------------+
|
|
int LP(string s) { //ロングポジション数を取得
|
|
int buys=0;
|
|
int total=PositionsTotal();
|
|
ulong ticket=0;
|
|
ulong PositionMagic=0;
|
|
long type;
|
|
string symbol;
|
|
for(int i=total-1; i>=0; i--) {
|
|
ticket=PositionGetTicket(i);
|
|
symbol=PositionGetString(POSITION_SYMBOL);
|
|
PositionMagic=PositionGetInteger(POSITION_MAGIC);
|
|
type =PositionGetInteger(POSITION_TYPE);
|
|
if(symbol==s && PositionMagic==Magic && type==POSITION_TYPE_BUY) {
|
|
buys++;
|
|
}
|
|
}
|
|
return(buys);
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
int SP(string s) { //ショートポジション数を取得
|
|
int sells=0;
|
|
int total=PositionsTotal();
|
|
ulong ticket=0;
|
|
ulong PositionMagic=0;
|
|
long type;
|
|
string symbol;
|
|
for(int i=total-1; i>=0; i--) {
|
|
ticket=PositionGetTicket(i);
|
|
symbol=PositionGetString(POSITION_SYMBOL);
|
|
PositionMagic=PositionGetInteger(POSITION_MAGIC);
|
|
type =PositionGetInteger(POSITION_TYPE);
|
|
if(symbol==s && PositionMagic==Magic && type==POSITION_TYPE_SELL) {
|
|
sells++;
|
|
}
|
|
}
|
|
return(sells);
|
|
}
|
|
//+------------------------------------------------------------------+
|
|
ulong Ti() { //ロングポジション数を取得
|
|
int buys=0;
|
|
int total=PositionsTotal();
|
|
ulong ticket=0;
|
|
ulong PositionMagic=0;
|
|
long type;
|
|
string symbol;
|
|
for(int i=total-1; i>=0; i--) {
|
|
ticket=PositionGetTicket(i);
|
|
symbol=PositionGetString(POSITION_SYMBOL);
|
|
PositionMagic=PositionGetInteger(POSITION_MAGIC);
|
|
type =PositionGetInteger(POSITION_TYPE);
|
|
if(/*symbol==_Symbol && */PositionMagic==Magic && type==POSITION_TYPE_BUY) {
|
|
buys++;
|
|
}
|
|
}
|
|
return(buys);
|
|
}
|
|
|
|
//+------------------------------------------------------------------+
|