//+------------------------------------------------------------------+//| ZS moving average.mq4 |//| Copyright 2021, MetaQuotes Software Corp. |//| https://www.mql5.com |//+------------------------------------------------------------------+#property copyright"Copyright 2021, MetaQuotes Software Corp."#property link"https://www.mql5.com"#property version"1.00"#property strict//+------------------------------------------------------------------+//+ Input |//+------------------------------------------------------------------+inputstring _="//---General Setting ---//";
inputint Magic_Number =10000000;
inputstring Open_Order_Comment ="FirstRSI";
inputint Spread_Filter =20; // Spread Filter (0=Disable)inputstring __="//---Entry Setting ---//";
inputdouble First_Order_Lot_Size =1.00;
inputstring ___="//---Exit Setting ---//";
inputint Take_Profit_in_Points =1000; //Take_Profit_in_Points (0=Disable)inputint Stop_Loss_in_Points =500; //Stop_Loss_in_Points )(0=Disable)//+------------------------------------------------------------------+//+ Variables |//+------------------------------------------------------------------+double Point_Value =0;
int Number_Of_Buy_Order =0;
int Number_Of_Sell_Order =0;
double Open_Price_Of_Buy_Order =0;
double Open_Price_Of_Sell_Order =0;
bool Close_Order_Status =TRUE;
int Open_Order_Status =0;
//+------------------------------------------------------------------+//+ Check_Opened_Orders |//+------------------------------------------------------------------+void Check_Opened_Orders() //Check whether there is open orders
{
Number_Of_Buy_Order =0;
Number_Of_Sell_Order =0;
Open_Price_Of_Buy_Order =0;
Open_Price_Of_Sell_Order =0;
for (int i=0; i<OrdersTotal();i++) // Loop all the order
{
if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) //Choose order
{
if(OrderMagicNumber()==Magic_Number && OrderSymbol()==OrderSymbol()) // Same magic number and currency symbol
{
if(OrderType()==OP_BUY) // if it is buy order
{
Number_Of_Buy_Order++; //Calculate total buy order
Open_Price_Of_Buy_Order += OrderOpenPrice(); // Record buy price
}// End for OP_Buyif(OrderType()==OP_SELL) // if it is sell order
{
Number_Of_Sell_Order++; //Calculate total sell order
Open_Price_Of_Sell_Order += OrderOpenPrice(); // Record sell price
}// End for OP_Sell
}// End for OrderMagicNumber && Order Symbol
}// End for Order Select
}// ENd for loop
}// Check_Open_Orders //+------------------------------------------------------------------+//+ Check_for_Close |//+------------------------------------------------------------------+ void Check_For_Close() //Check whether it fits the criteria of close position
{
double SlowMovingAverage = iMA(NULL,0,100,0,MODE_SMMA,PRICE_CLOSE,0);
double LastSlowMovingAverage =iMA(NULL,0,100,0,MODE_SMMA,PRICE_CLOSE,1);
double FastMovingAverage=iMA(NULL,0,20,0,MODE_SMMA,PRICE_CLOSE,0);
double LastFastMovingAverage =iMA(NULL,0,20,0,MODE_SMMA,PRICE_CLOSE,1);
//+------------------------------------------------------------------check the buy position fits the criteria to close positionif (Number_Of_Buy_Order) // If there is buy order
{
if(Take_Profit_in_Points>0 && Bid>= Open_Price_Of_Buy_Order+Take_Profit_in_Points*Point_Value)// If buy position hits TP
{
Close_Single_Direction_All_Orders(OP_BUY); //Close all buy position
}
if(Stop_Loss_in_Points>0 && Bid<=Open_Price_Of_Buy_Order-Stop_Loss_in_Points*Point_Value) // If buy position hits SL
{
Close_Single_Direction_All_Orders(OP_BUY); //Close all buy position
}
} // End if there is buy order//+------------------------------------------------------------------check the sell position fits the criteria to close positionif (Number_Of_Sell_Order) // If there is sell order
{
if(Take_Profit_in_Points>0 && Ask>= Open_Price_Of_Sell_Order-Take_Profit_in_Points*Point_Value)// If sell position hits TP
{
Close_Single_Direction_All_Orders(OP_SELL); //Close all sell position
}
if(Stop_Loss_in_Points>0 && Ask<=Open_Price_Of_Sell_Order+Stop_Loss_in_Points*Point_Value) // If sell position hits SL
{
Close_Single_Direction_All_Orders(OP_SELL); //Close all sell position
} // End if there is sell order
}
} // Check_For_Close done//+------------------------------------------------------------------+//+ Check_for_Open |//+------------------------------------------------------------------+ void Check_For_Open() // Check whether it fits the criteria to open order
{
string signal="";
double SlowMovingAverage = iMA(NULL,0,100,0,MODE_SMMA,PRICE_CLOSE,0);
double LastSlowMovingAverage =iMA(NULL,0,100,0,MODE_SMMA,PRICE_CLOSE,1);
double FastMovingAverage=iMA(NULL,0,20,0,MODE_SMMA,PRICE_CLOSE,0);
double LastFastMovingAverage =iMA(NULL,0,20,0,MODE_SMMA,PRICE_CLOSE,1);
if((LastFastMovingAverage <LastSlowMovingAverage)&&(FastMovingAverage > SlowMovingAverage))
{
signal="Buy";
}
if((LastFastMovingAverage >LastSlowMovingAverage)&&(FastMovingAverage < SlowMovingAverage))
{
signal="Sell";
}
if (signal=="Buy" && OrdersTotal()==0)
OrderSend (_Symbol,OP_BUY,1.00,Ask,3,Ask-500*Point,Ask+1000*Point,NULL,0,0,Green);
if (signal=="sell" && OrdersTotal()==0)
OrderSend (_Symbol,OP_SELL,1.00,Bid,3,Bid+500*Point,Bid-1000*Point,NULL,0,0,Red);
}
//+------------------------------------------------------------------+//+ |//+------------------------------------------------------------------+ void Close_Single_Direction_All_Orders(int Operation_Type)
{
for(int i=OrdersTotal()-1; i>=0; i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderMagicNumber()==Magic_Number && OrderSymbol()==Symbol() && OrderType()==Operation_Type)
{
Close_Order_Status=OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,Green);
}
}
}
}
//+------------------------------------------------------------------+//| Expert initialization function |//+------------------------------------------------------------------+intOnInit()
{
//---//---return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+//| Expert deinitialization function |//+------------------------------------------------------------------+voidOnDeinit(constint reason)
{
//---
}
//+------------------------------------------------------------------+//| Expert tick function |//+------------------------------------------------------------------+voidOnTick()
{
//---
Check_Opened_Orders();
Check_For_Close();
Check_For_Open();
}
//+------------------------------------------------------------------+
Here's my code. My strategy plan was to enter a buy trade when 20 SMMA crossed 100SMMA. It does executed but how do i code for the exit where the 100 SMMA crossed 20 SMMA. because my current strategy is just enter and close when it hits tp. I wanted to have another option where it's either TP hits or the smma crossed again. Hopefully someone understand and show me the code. It would be a pleasure.
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
Here's my code. My strategy plan was to enter a buy trade when 20 SMMA crossed 100SMMA. It does executed but how do i code for the exit where the 100 SMMA crossed 20 SMMA. because my current strategy is just enter and close when it hits tp. I wanted to have another option where it's either TP hits or the smma crossed again. Hopefully someone understand and show me the code. It would be a pleasure.