384 lines
7.2 KiB
Plaintext
384 lines
7.2 KiB
Plaintext
///////////////////////////////////////////////////////
|
|
//
|
|
// SaherElm IT Center XNNTest MQL5 Expert Advisor
|
|
// -------------------------------------------------
|
|
// Name: XNNTest
|
|
// Description: an Exper Advisor which used RSI and MA
|
|
// to Analyse Market ...
|
|
//
|
|
// Maintainer:
|
|
// ------------
|
|
// Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com)
|
|
//
|
|
//////////////////////////////////////////////////////x
|
|
//
|
|
// Global Properties ...
|
|
#property copyright "Copyright 2023, SaherElm IT Center"
|
|
#property link "https://saherelm.ir"
|
|
#property version "1.00"
|
|
#property description "SaherElm XNNTest"
|
|
#property strict
|
|
|
|
//
|
|
// Imports ...
|
|
#include "../Classes/x-saherelm.xtrade.class.mq5"
|
|
|
|
//
|
|
#define ShortName "XNNTest"
|
|
|
|
//
|
|
// Inputs ...
|
|
input group "Weights";
|
|
input double w0 = 0.5;
|
|
input double w1 = 0.5;
|
|
input double w2 = 0.5;
|
|
input double w3 = 0.5;
|
|
input double w4 = 0.5;
|
|
input double w5 = 0.5;
|
|
input double w6 = 0.5;
|
|
input double w7 = 0.5;
|
|
input double w8 = 0.5;
|
|
input double w9 = 0.5;
|
|
|
|
//
|
|
input double eaVolume = 0.01;
|
|
|
|
//
|
|
// Variables ...
|
|
int barsTotal;
|
|
|
|
//
|
|
string eaSymbol;
|
|
ENUM_TIMEFRAMES eaPeriod;
|
|
|
|
//
|
|
// This is our Data Provider ...
|
|
int rsiHandler = INVALID_HANDLE;
|
|
double rsi[];
|
|
|
|
//
|
|
double nnInputs[]; // Storing Inputs
|
|
double nnWeights[]; // Storing Weights;
|
|
|
|
//
|
|
double nnOutput; // Output Neuron Value
|
|
|
|
//
|
|
XSCTrade *mTrader;
|
|
|
|
//
|
|
// Initialization ...
|
|
int OnInit()
|
|
{
|
|
//
|
|
if (!InitialEA())
|
|
{
|
|
return INIT_PARAMETERS_INCORRECT;
|
|
}
|
|
|
|
//
|
|
// Init Succeed ...
|
|
return INIT_SUCCEEDED;
|
|
}
|
|
|
|
//
|
|
// DeInitialization ...
|
|
void OnDeinit(const int reason)
|
|
{
|
|
//
|
|
// REASON_PROGRAM 0 The EA has stopped working calling the ExpertRemove() function
|
|
// REASON_REMOVE 1 Program removed from a chart
|
|
// REASON_RECOMPILE 2 Program recompiled
|
|
// REASON_CHARTCHANGE 3 A symbol or a chart period is changed
|
|
// REASON_CHARTCLOSE 4 Chart closed
|
|
// REASON_PARAMETERS 5 Inputs changed by a user
|
|
// REASON_ACCOUNT 6 Another account has been activated or reconnection to the trade server has occurred due to changes in the account settings
|
|
// REASON_TEMPLATE 7 Another chart template applied
|
|
// REASON_INITFAILED 8 The OnInit() handler returned a non-zero value
|
|
// REASON_CLOSE 9 Terminal closed
|
|
|
|
//
|
|
// De Initialize XSampleEA Providers ...
|
|
IndicatorRelease(rsiHandler);
|
|
ArrayFree(rsi);
|
|
ArrayFree(nnInputs);
|
|
ArrayFree(nnWeights);
|
|
}
|
|
|
|
//
|
|
// On Tick Handler ...
|
|
void OnTick()
|
|
{
|
|
//
|
|
XPosition positions[];
|
|
|
|
//
|
|
XPosition longs[];
|
|
int longsCount = 0;
|
|
|
|
//
|
|
XPosition shorts[];
|
|
int shortsCount = 0;
|
|
|
|
//
|
|
int positionsCount = mTrader.GetPositions(
|
|
positions //
|
|
);
|
|
if (IsValidSize(positionsCount))
|
|
{
|
|
//
|
|
ExtractPositions(
|
|
positions,
|
|
longs,
|
|
shorts //
|
|
);
|
|
|
|
//
|
|
longsCount = ArraySize(longs);
|
|
shortsCount = ArraySize(shorts);
|
|
}
|
|
|
|
//
|
|
if (longsCount == 0 && shortsCount == 0)
|
|
{
|
|
//
|
|
double longEntry = GetEntry(eaSymbol, POSITION_TYPE_BUY);
|
|
mTrader.Buy(
|
|
eaSymbol,
|
|
eaPeriod,
|
|
eaVolume,
|
|
longEntry //
|
|
);
|
|
|
|
//
|
|
double shortEntry = GetEntry(eaSymbol, POSITION_TYPE_SELL);
|
|
mTrader.Sell(
|
|
eaSymbol,
|
|
eaPeriod,
|
|
eaVolume,
|
|
shortEntry //
|
|
);
|
|
}
|
|
|
|
//
|
|
return;
|
|
// int rsiCopiedData = CopyBuffer(
|
|
// rsiHandler,
|
|
// 0, // Line Index
|
|
// 1, // BarIndex
|
|
// 10, // Count
|
|
// rsi // Buffer
|
|
// );
|
|
// if (rsiCopiedData < 0)
|
|
// {
|
|
// //
|
|
// // Ignore Moving Forward when there isn't any Copied Data ...
|
|
// return;
|
|
// }
|
|
|
|
// //
|
|
// // Normalize Input Data ...
|
|
// double lower = 0;
|
|
// double upper = 1;
|
|
|
|
// //
|
|
// double max = rsi[ArrayMaximum(rsi)];
|
|
// double min = rsi[ArrayMinimum(rsi)];
|
|
|
|
// //
|
|
// ArrayFree(nnInputs);
|
|
// ArrayResize(nnInputs, 10);
|
|
|
|
// //
|
|
// for (int i = 0; i < ArraySize(rsi); i++)
|
|
// {
|
|
// nnInputs[i] = ((rsi[i] - min) * (upper - lower) / (max - min)) + lower;
|
|
// }
|
|
|
|
// //
|
|
// // Calculating Output ...
|
|
// nnOutput = CalculateNeuron(
|
|
// nnInputs,
|
|
// nnWeights //
|
|
// );
|
|
|
|
// //
|
|
// // Now we Can Use NN Output for Placing Trades ...
|
|
|
|
// //
|
|
// bool canBuy = nnOutput < 0.5;
|
|
// bool canSell = nnOutput >= 0.5;
|
|
|
|
// //
|
|
// XPosition positions[];
|
|
// XPosition longs[];
|
|
// XPosition shorts[];
|
|
// int positionsCount = mTrader.GetPositions(
|
|
// positions //
|
|
// );
|
|
// if (IsValidSize(positionsCount))
|
|
// {
|
|
// //
|
|
// ExtractPositions(
|
|
// positions,
|
|
// longs,
|
|
// shorts //
|
|
// );
|
|
|
|
// //
|
|
// if (canBuy)
|
|
// {
|
|
// //
|
|
// mTrader.Close(shorts, "Opposit");
|
|
|
|
// //
|
|
// if (ArraySize(longs) > 0)
|
|
// {
|
|
// return;
|
|
// }
|
|
// }
|
|
// else if (canSell)
|
|
// {
|
|
// //
|
|
// mTrader.Close(longs, "Opposit");
|
|
|
|
// //
|
|
// if (ArraySize(shorts) > 0)
|
|
// {
|
|
// return;
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
// //
|
|
// double mEntry =
|
|
// canBuy
|
|
// ? GetEntry(eaSymbol, POSITION_TYPE_BUY)
|
|
// : GetEntry(eaSymbol, POSITION_TYPE_SELL);
|
|
|
|
// //
|
|
// if (canBuy)
|
|
// {
|
|
// //
|
|
// mTrader.Buy(
|
|
// eaSymbol,
|
|
// eaPeriod,
|
|
// eaVolume,
|
|
// mEntry //
|
|
// );
|
|
// }
|
|
// else if (canSell)
|
|
// {
|
|
// //
|
|
// mTrader.Sell(
|
|
// eaSymbol,
|
|
// eaPeriod,
|
|
// eaVolume,
|
|
// mEntry //
|
|
// );
|
|
// }
|
|
}
|
|
|
|
//
|
|
// Custom Functions ...
|
|
|
|
//
|
|
bool InitialEA()
|
|
{
|
|
//
|
|
bool result = false;
|
|
|
|
//
|
|
eaSymbol = _Symbol;
|
|
eaPeriod = _Period;
|
|
|
|
//
|
|
// Initialize RSI Handler ...
|
|
rsiHandler = iRSI(
|
|
eaSymbol,
|
|
eaPeriod,
|
|
14,
|
|
PRICE_CLOSE //
|
|
);
|
|
result = rsiHandler != INVALID_HANDLE;
|
|
if (!result)
|
|
{
|
|
return result;
|
|
}
|
|
|
|
//
|
|
mTrader = new XSCTrade(
|
|
1,
|
|
78692110 //
|
|
);
|
|
|
|
//
|
|
ArraySetAsSeries(rsi, true);
|
|
|
|
//
|
|
ArrayResize(nnWeights, 10);
|
|
|
|
//
|
|
// Filling Weights Buffer ...
|
|
nnWeights[0] = w0;
|
|
nnWeights[1] = w1;
|
|
nnWeights[2] = w2;
|
|
nnWeights[3] = w3;
|
|
nnWeights[4] = w4;
|
|
nnWeights[5] = w5;
|
|
nnWeights[6] = w6;
|
|
nnWeights[7] = w7;
|
|
nnWeights[8] = w8;
|
|
nnWeights[9] = w9;
|
|
|
|
//
|
|
result = true;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Activation Function ...
|
|
double Activate(double neuron)
|
|
{
|
|
//
|
|
double result = 0;
|
|
|
|
//
|
|
result = 1 / (1 + exp(-neuron));
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Calculating Neuron ...
|
|
// NET Inputs ...
|
|
double CalculateNeuron(
|
|
double &x[], // Inputs
|
|
double &w[] // Weights
|
|
)
|
|
{
|
|
//
|
|
double result = 0;
|
|
|
|
//
|
|
double netInput = 0;
|
|
for (int i = 0; i < ArraySize(x); i++)
|
|
{
|
|
//
|
|
netInput += x[i] * w[i];
|
|
}
|
|
|
|
//
|
|
// Change The Shape of Sigmoid Graph for Smoothing ...
|
|
netInput *= 0.4;
|
|
|
|
//
|
|
// Calculate Activated Result ...
|
|
result = Activate(netInput);
|
|
|
|
//
|
|
return result;
|
|
} |