Orders Execution
Indicators Used
0
Views
0
Downloads
0
Favorites
PURIA-M30
//+------------------------------------------------------------------+
//| Puaria-m15.mq4 |
//| Copyright © 2011, Serg Deev |
//| http://www.work2it.ru |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, Serg Deev"
#property link "http://www.work2it.ru"
#define MAGICMA 20050610
extern string Trades = "Íàñòðîéêè òîðãîâûõ îïåðàöèé";
extern double MaxRisk = 0.2;
extern double Lots = 0.1;
extern int TakeProfit = 150;
extern int MinProfit = 100; // ìèíèìàëüíûé ïðîôèò
extern double MinProfitPercent = 0.5; // ïðîöåíò âçÿòèÿ ïðè ìèíèìàëüíîì ïðîôèòå
extern int ProfitStep = 75; // øàã ïðîôèòà
extern double ProfitPercent = 0.1; // ïðîöåíò ïðîôèòà áîëüøå ìèíèìàëüíîãî
extern int TrailingStop = 90;
extern int StopLost = 80;
extern bool UseAllTicks = true;
extern int ma0_period = 69;
int ma0_shift = 0;
int ma0_method = 3; // 0-MODE_SMA; 1-MODE_EMA; 2-MODE_SMMA; 3-MODE_LWMA;
int ma0_price = 3; // 0-PRICE_CLOSE, 1-PRICE_OPEN, 2-PRICE_HIGH, 3-PRICE_LOW, 4-PRICE_MEDIAN, 5-PRICE_TYPICAL, 6-PRICE_WEIGHTED
extern int ma1_period = 74;
int ma1_shift = 0;
int ma1_method = 3; // 0-MODE_SMA; 1-MODE_EMA; 2-MODE_SMMA; 3-MODE_LWMA;
int ma1_price = 3; // 0-PRICE_CLOSE, 1-PRICE_OPEN, 2-PRICE_HIGH, 3-PRICE_LOW, 4-PRICE_MEDIAN, 5-PRICE_TYPICAL, 6-PRICE_WEIGHTED
extern int ma2_period = 19;
int ma2_shift = 0;
int ma2_method = 1; // 0-MODE_SMA; 1-MODE_EMA; 2-MODE_SMMA; 3-MODE_LWMA;
int ma2_price = 1; // 0-PRICE_CLOSE, 1-PRICE_OPEN, 2-PRICE_HIGH, 3-PRICE_LOW, 4-PRICE_MEDIAN, 5-PRICE_TYPICAL, 6-PRICE_WEIGHTED
extern int macd_fast = 17;
extern int macd_slow = 38;
int macd_signal = 1;
int macd_price = 1; // 0-PRICE_CLOSE, 1-PRICE_OPEN, 2-PRICE_HIGH, 3-PRICE_LOW, 4-PRICE_MEDIAN, 5-PRICE_TYPICAL, 6-PRICE_WEIGHTED
extern int macd_open = 8;
double NextProfit = 0;
double StartLots = 0;
double MinLot;
double MaxLot;
//+------------------------------------------------------------------+
int init() {
MinLot = MarketInfo(Symbol(),MODE_MINLOT);
MaxLot = MarketInfo(Symbol(),MODE_MAXLOT);
return(0);
}
//+------------------------------------------------------------------+
int deinit() {
return(0);
}
//+------------------------------------------------------------------+
double Get_Lots() {
double lot=Lots;
if (MaxRisk > 0) {
double RiskSumm = AccountFreeMargin()*MaxRisk;
lot=RiskSumm/StopLost/100;
}
lot=MathFloor(lot*100)/100;
if (lot > MaxLot) lot = MaxLot;
if (lot < MinLot) lot = MinLot;
return(lot);
}
//+------------------------------------------------------------------+
bool Signal_Stop_Buy() {
double ma0 = iMA(NULL,0,ma0_period,ma0_shift,ma0_method,ma0_price,0);
double ma1 = iMA(NULL,0,ma1_period,ma1_shift,ma1_method,ma1_price,0);
double ma2 = iMA(NULL,0,ma2_period,ma2_shift,ma2_method,ma2_price,0);
if (ma0 > ma2) return(true);
return(false);
}
//+------------------------------------------------------------------+
bool Signal_Stop_Sell() {
double ma0 = iMA(NULL,0,ma0_period,ma0_shift,ma0_method,ma0_price,0);
double ma1 = iMA(NULL,0,ma1_period,ma1_shift,ma1_method,ma1_price,0);
double ma2 = iMA(NULL,0,ma2_period,ma2_shift,ma2_method,ma2_price,0);
if (ma0 < ma2) return(true);
return(false);
}
//+------------------------------------------------------------------+
bool macd_up(int timeframe, int fast, int slow, int signal, int price, int num) {
double y;
double x = iMACD(NULL,timeframe,fast,slow,signal,price,MODE_MAIN,0)*100000;
for (int i=1; i<num; i++) {
y = iMACD(NULL,timeframe,fast,slow,signal,price,MODE_MAIN,i)*100000;
if (y > x) return(false);
else x = y;
}
return(true);
}
//+------------------------------------------------------------------+
bool macd_down(int timeframe, int fast, int slow, int signal, int price, int num) {
double y;
double x = iMACD(NULL,timeframe,fast,slow,signal,price,MODE_MAIN,0)*100000;
for (int i=1; i<num; i++) {
y = iMACD(NULL,timeframe,fast,slow,signal,price,MODE_MAIN,i)*100000;
if (y < x) return(false);
else x = y;
}
return(true);
}
//+------------------------------------------------------------------+
bool Signal_Buy() {
double ma0 = iMA(NULL,0,ma0_period,ma0_shift,ma0_method,ma0_price,0);
double ma1 = iMA(NULL,0,ma1_period,ma1_shift,ma1_method,ma1_price,0);
double ma2 = iMA(NULL,0,ma2_period,ma2_shift,ma2_method,ma2_price,0);
double macd = iMACD(NULL,0,macd_fast,macd_slow,macd_signal,macd_price,MODE_MAIN,0);
if (((ma1-ma0)/Point > 0.5) && ((ma2-ma0)/Point > 0.5)) {
if (macd > 0) {
if (macd_up(0,macd_fast,macd_slow,macd_signal,macd_price,macd_open)) return(true);
}
}
return(false);
}
//+------------------------------------------------------------------+
bool Signal_Sell() {
double ma0 = iMA(NULL,0,ma0_period,ma0_shift,ma0_method,ma0_price,0);
double ma1 = iMA(NULL,0,ma1_period,ma1_shift,ma1_method,ma1_price,0);
double ma2 = iMA(NULL,0,ma2_period,ma2_shift,ma2_method,ma2_price,0);
double macd = iMACD(NULL,0,macd_fast,macd_slow,macd_signal,macd_price,MODE_MAIN,0);
if (((ma0-ma1)/Point > 0.5) && ((ma0-ma2)/Point > 0.5)) {
if (macd < 0) {
if (macd_down(0,macd_fast,macd_slow,macd_signal,macd_price,macd_open)) return(true);
}
}
return(false);
}
//+------------------------------------------------------------------+
void CheckForOpen() {
int res;
if ((!UseAllTicks) && (Volume[0]>1)) return;
if (Signal_Sell()) {
StartLots = Get_Lots();
NextProfit = Bid - MinProfit*Point;
res=OrderSend(Symbol(),OP_SELL,StartLots,Ask,3,0,0,"",MAGICMA,0,Red);
return;
}
if (Signal_Buy()) {
StartLots = Get_Lots();
NextProfit = Ask + MinProfit*Point;
res=OrderSend(Symbol(),OP_BUY,StartLots,Bid,3,0,0,"",MAGICMA,0,Blue);
return;
}
}
//+------------------------------------------------------------------+
void CheckForClose()
{
double SL,TP,lx;
int profit;
if ((!UseAllTicks) && (Volume[0]>1)) return;
for(int i=0;i<OrdersTotal();i++) {
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderMagicNumber()!=MAGICMA || OrderSymbol()!=Symbol()) continue;
//---- check order type
if(OrderType()==OP_BUY) {
if (Signal_Stop_Buy()) OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
else if (OrderStopLoss() == 0.0) {
SL = Bid - StopLost*Point;
TP = Ask + TakeProfit*Point;
OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,Blue);
}
else {
if (Bid > NextProfit) {
if (Bid < (OrderOpenPrice()+(MinProfit+ProfitStep/2)*Point)) lx = NormalizeDouble(StartLots*MinProfitPercent,2);
else lx = NormalizeDouble(StartLots*ProfitPercent,2);
OrderClose(OrderTicket(),lx,Bid,3,White);
NextProfit = Bid + ProfitStep*Point;
}
else if (NextProfit > (OrderOpenPrice()+MinProfit*Point)) {
SL = NormalizeDouble((Bid - TrailingStop*Point),Digits);
TP = NormalizeDouble((Ask + TakeProfit*Point),Digits);
if (OrderStopLoss() < SL) OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,Blue);
}
}
break;
}
if(OrderType()==OP_SELL) {
if (Signal_Stop_Sell()) OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
else if (OrderStopLoss() == 0.0) {
SL = Bid + StopLost*Point;
TP = Ask - TakeProfit*Point;
OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,Blue);
}
else {
if (Ask < NextProfit) {
if (Ask > (OrderOpenPrice()-(MinProfit+ProfitStep/2)*Point)) lx = NormalizeDouble(StartLots*MinProfitPercent,2);
else lx = NormalizeDouble(StartLots*ProfitPercent,2);
OrderClose(OrderTicket(),lx,Ask,3,White);
NextProfit = Bid - ProfitStep*Point;
}
else if (NextProfit < (OrderOpenPrice()-MinProfit*Point)) {
SL = NormalizeDouble(Ask + TrailingStop*Point,Digits);
TP = NormalizeDouble(Bid - TakeProfit*Point,Digits);
if (OrderStopLoss() > SL) OrderModify(OrderTicket(),OrderOpenPrice(),SL,TP,0,Blue);
}
}
break;
}
}
}
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
{
int buys=0,sells=0;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICMA)
{
if(OrderType()==OP_BUY) buys++;
if(OrderType()==OP_SELL) sells++;
}
}
if(buys>0) return(buys);
else return(-sells);
}
//+------------------------------------------------------------------+
void start()
{
if(Bars<100 || IsTradeAllowed()==false) return;
if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
else CheckForClose();
}
//+------------------------------------------------------------------+
Comments
Markdown Formatting Guide
# H1
## H2
### H3
**bold text**
*italicized text*
[title](https://www.example.com)

`code`
```
code block
```
> blockquote
- Item 1
- Item 2
1. First item
2. Second item
---