voglio trasformare questo indicatore in un EA per mt4.
//------------------------------------------------------------------
#property copyright "www.forex-station.com"
#property link "www.forex-station.com"
//------------------------------------------------------------------
#property indicator_separate_window
#property indicator_buffers 10
#property indicator_color1 clrSilver
#property indicator_color2 clrSilver
#property indicator_color3 clrSilver
#property indicator_color4 clrDimGray
#property indicator_color5 clrDimGray
#property indicator_color6 clrLightGray
#property indicator_style2 STYLE_DOT
#property strict
//
//
//
//
//
enum enMaTypes
{
avgSma, // Simple moving average
avgEma, // Exponential moving average
avgSmma, // Smoothed MA
avgLwma // Linear weighted MA
};
enum enColorOn
{
cc_onSlope, // Change color on slope change
cc_onMiddle, // Change color on middle line cross
cc_onLevels // Change color on outer levels cross
};
extern ENUM_TIMEFRAMES TimeFrame = PERIOD_CURRENT; // Time frame
extern string ForSymbol = ""; // For symbol (leave empty for current chart symbol)
input int MomentumPeriod = 25; // Momentum period
input int AvgPeriod = 0; // Momentum average period (0 -> same as momentum period)
input enMaTypes AvgType = avgEma; // Momentum average method
input int SmoPeriod = 0; // Smoothing average period (0 -> same as momentum period)
input double ZoneUp = 70; // Upper zone limit
input double ZoneDown = 30; // Lower zone limit
extern enColorOn ColorOn = cc_onLevels; // Color change :
extern color ColorUp = clrLimeGreen; // Color for up
extern color ColorDown = clrOrangeRed; // Color for down
extern int LineWidth = 3; // Main line width
extern bool Interpolate = true; // Interpolate in multi time frame?
double iCustomMa(int mode, double price, double length, int r, int instanceNo=0)
{
r = Bars-r-1;
switch (mode)
{
case avgSma : return(iSma(price,(int)length,r,instanceNo));
case avgEma : return(iEma(price,length,r,instanceNo));
case avgSmma : return(iSmma(price,(int)length,r,instanceNo));
case avgLwma : return(iLwma(price,(int)length,r,instanceNo));
default : return(price);
}
}
//
//
//
//
//
double workSma[][_maWorkBufferx2];
double iSma(double price, int period, int r, int instanceNo=0)
{
if (period<=1) return(price);
if (ArrayRange(workSma,0)!= Bars) ArrayResize(workSma,Bars); instanceNo *= 2; int k;
double workEma[][_maWorkBufferx1];
double iEma(double price, double period, int r, int instanceNo=0)
{
if (period<=1) return(price);
if (ArrayRange(workEma,0)!= Bars) ArrayResize(workEma,Bars);
double workSmma[][_maWorkBufferx1];
double iSmma(double price, double period, int r, int instanceNo=0)
{
if (period<=1) return(price);
if (ArrayRange(workSmma,0)!= Bars) ArrayResize(workSmma,Bars);
double workLwma[][_maWorkBufferx1];
double iLwma(double price, double period, int r, int instanceNo=0)
{
if (period<=1) return(price);
if (ArrayRange(workLwma,0)!= Bars) ArrayResize(workLwma,Bars);
le condizioni di entrata per il buy è quando il momentum diventa verde, mentre il sell quando diventa rosso. le condizioni di chiusura del trade sono per il buy quando non è più verde, per il sell quando non è più rosso.
impostare lotto da utente
Convertire un indicatore in un Expert Advisor (EA) per MetaTrader 4 (MT4) richiede di ristrutturare il codice per gestire le decisioni di trading in base all'output dell'indicatore. Di seguito è presentata una versione base di un EA basato sul tuo indicatore che apre e chiude trade in base al cambio di colore del momentum:
mql4
//+------------------------------------------------------------------+
//| CustomEA.mq4|
//| Copyright 2019, MetaQuotes Software Corp. |
//| http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net/"
#property version "1.00"
#property strict
// Input parameters
extern double LotSize = 0.1; // Default lot size
// Declaration of global variables
int buyTicket = -1;
int sellTicket = -1;
// Initialization function
int OnInit()
{
// Initialization code here
return(INIT_SUCCEEDED);
}
// Deinitialization function
void OnDeinit(const int reason)
{
// Deinitialization code here
CloseAllTrades(); // Make sure to close all trades when the EA is removed
}
// The main function that is called on every tick
void OnTick()
{
// First, check the conditions for a buy trade
if (CheckBuyCondition())
{
if(buyTicket < 0) // Make sure there is no buy trade already
{
CloseSellTrade(); // Close any existing sell trade
// Open a buy trade
buyTicket = OrderSend(Symbol(), OP_BUY, LotSize, Ask, 2, 0, 0, "Buy Trade", 0, 0, clrGreen);
}
}
else if (CheckSellCondition())
{
if(sellTicket < 0) // Make sure there is no sell trade already
{
CloseBuyTrade(); // Close any existing buy trade
// Open a sell trade
sellTicket = OrderSend(Symbol(), OP_SELL, LotSize, Bid, 2, 0, 0, "Sell Trade", 0, 0, clrRed);
}
}
else // No clear buy or sell condition, might consider closing trades
{
// Check conditions for closing trades here, possibly checking for an opposite signal
// This example simply holds until a trade in the opposite direction is warranted
}
}
// Function to check buy condition based on the indicator
bool CheckBuyCondition()
{
// This function should contain the logic to check for the conditions
// when the momentum becomes green as per your indicator. This is a placeholder.
// You need to implement this logic based on the specific conditions of your indicator.
return false; // Return true if the conditions are met
}
// Function to check sell condition based on the indicator
bool CheckSellCondition()
{
// This function should contain the logic to check for the conditions
// when the momentum becomes red as per your indicator. This is a placeholder.
// You need to implement this logic based on the specific conditions of your indicator.
return false; // Return true if the conditions are met
}
// Function to close all buy trades
void CloseBuyTrade()
{
// Loop through and close all buy trades
if (buyTicket >= 0)
{
OrderClose(buyTicket, LotSize, OrderClosePrice(), 2, clrNONE);
buyTicket = -1;
}
}
// Function to close all sell trades
void CloseSellTrade()
{
// Loop through and close all sell trades
if (sellTicket >= 0)
{
OrderClose(sellTicket, LotSize, OrderClosePrice(), 2, clrNONE);
sellTicket = -1;
}
}
// Utility function to close all trades (used on EA deinitialization)
void CloseAllTrades()
{
CloseBuyTrade();
CloseSellTrade();
}
Nota Importante:
Questo codice serve come base di partenza. È necessario integrare le funzioni CheckBuyCondition() e CheckSellCondition() con le condizioni specifiche del tuo indicatore per identificare quando il momentum diventa verde (per acquisti) o rosso (per vendite) e modificarle per riportare true quando queste condizioni sono soddisfatte. La gestione degli errori per OrderSend() e OrderClose() non è inclusa in questo codice di base ma è importante aggiungerla per garantire la robustezza e l'affidabilità dell'EA.