Author: Copyright � 2005, Nick Bilak
Orders Execution
Checks for the total of open ordersIt automatically opens orders when conditions are reachedIt Closes Orders by itself
Indicators Used
Moving average indicatorMoving average indicator
0 Views
0 Downloads
0 Favorites

Profitability Reports

AUD/USD Oct 2024 - Jan 2025
23.00 %
Total Trades 75
Won Trades 0
Lost trades 0
Win Rate 0.00 %
Expected payoff -14.48
Gross Profit 329.10
Gross Loss -1415.40
Total Net Profit -1086.30
-100%
-50%
0%
50%
100%
GBP/USD Oct 2024 - Jan 2025
57.00 %
Total Trades 85
Won Trades 28
Lost trades 57
Win Rate 0.33 %
Expected payoff -5.85
Gross Profit 668.40
Gross Loss -1165.80
Total Net Profit -497.40
-100%
-50%
0%
50%
100%
mandarine
//+------------------------------------------------------------------+
//|                                                    mandarine.mq4 |
//|                                     Copyright © 2005, Nick Bilak |
//|                                        http://www.forex-tsd.com/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, Nick Bilak"
#property link      "http://www.forex-tsd.com/"

#include <stdlib.mqh>

extern double StopLoss = 100;  
extern double TakeProfit = 200;  
extern double TrailingStop = 100;  
extern int    T3Periods = 5;  
extern int    FastPeriod = 5;  
extern int    SlowPeriod = 20;  
extern double Lots = 0.1;
extern double MaximumRisk = 3;
extern bool   FixedLot = false;


extern int    slippage=2;   	//slippage for market order processing
extern int    shift=1;			//shift to current bar, 
extern int    MAGICNUM=187233;


bool   buysig,sellsig; 
int    ttime,total;
string TradeSymbol;

//+------------------------------------------------------------------+
//| Calculate open positions                                         |
//+------------------------------------------------------------------+
int CalculateCurrentOrders()
  {
   int ord;
//----
   total=OrdersTotal();
   for(int i=0;i<total;i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MAGICNUM)
        {
         ord++;
        }
     }
//---- return orders volume
	return(ord);
  }

double LotsRisk(int StopLoss)  { 
   double lot=Lots;
//---- select lot size
   if (!FixedLot)
      lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk*0.001/StopLoss,1);
   else
      lot=Lots;
//---- return lot size
   if(lot<0.1) lot=0.1;
   return(lot);
}

//+------------------------------------------------------------------+
//| Check for open order conditions                                  |
//+------------------------------------------------------------------+
void CheckForSignals(string symbol) {
      //double buy =iCustom(NULL,0,"signals-mandarine",T3Periods,FastPeriod,SlowPeriod,shift,0,0);
      //double sell=iCustom(NULL,0,"signals-mandarine",T3Periods,FastPeriod,SlowPeriod,shift,1,0);

      double t3=t3ma(T3Periods);
      double emaf11=iMA(symbol,0,FastPeriod,0,MODE_EMA,PRICE_CLOSE,shift);
      double emaf12=iMA(symbol,0,FastPeriod,0,MODE_EMA,PRICE_CLOSE,shift+1);
      double emas21=iMA(symbol,0,SlowPeriod,0,MODE_EMA,PRICE_OPEN,shift);
      double emas22=iMA(symbol,0,SlowPeriod,0,MODE_EMA,PRICE_OPEN,shift+1);
		
      sellsig=false;
      buysig=false;
      //if (t3>0) Print(t3);
      //if (sell>0) {
      if (emaf11<emas21 && emaf12>=emas22 && emaf11<t3) {
         sellsig=true;
      }
      //if (buy>0) {
      if (emaf11>emas21 && emaf12<=emas22 && emaf11>t3) {
         buysig=true;
      }
}

void CheckForOpen(string symbol) {
   int    res;
//---- sell conditions
   if(sellsig && ttime!=Time[0])  {
      res=OrderSend(symbol,OP_SELL,LotsRisk(StopLoss),Bid,slippage,Bid+StopLoss*Point,Bid-TakeProfit*Point,"t3",MAGICNUM,0,Red);
   	if (res<0) Print("Error opening SELL order : ",ErrorDescription(GetLastError()));
      ttime=Time[0];
      return;
   }
//---- buy conditions
   if(buysig && ttime!=Time[0])  {
      res=OrderSend(symbol,OP_BUY,LotsRisk(StopLoss),Ask,slippage,Ask-StopLoss*Point,Ask+TakeProfit*Point,"t3",MAGICNUM,0,Blue);
   	if (res<0) Print("Error opening BUY order : ",ErrorDescription(GetLastError()));
      ttime=Time[0];
      return;
   }
}
  
  
//+------------------------------------------------------------------+
//| Check for close order conditions                                 |
//+------------------------------------------------------------------+
void CheckForClose(string symbol)  {
	total=OrdersTotal();
   for(int i=0;i<total;i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)  break;
      if(OrderMagicNumber()!=MAGICNUM || OrderSymbol()!=symbol) continue;
      //---- check order type 
      if(OrderType()==OP_BUY)
        {
         if (sellsig) {
            OrderClose(OrderTicket(),OrderLots(),Bid,slippage,White);
         }
         break;
        }
      if(OrderType()==OP_SELL)
        {
         if (buysig) {
            OrderClose(OrderTicket(),OrderLots(),Ask,slippage,White);
         }
         break;
        }
     }
}


//+------------------------------------------------------------------+
//| Start function                                                   |
//+------------------------------------------------------------------+
void start()  {


   //---- check for history and trading
   if(Bars<100 || IsTradeAllowed()==false) return;

	TradeSymbol=Symbol();

   //---- check for signals
   CheckForSignals(TradeSymbol);

   //---- calculate open orders by current symbol
   if (CalculateCurrentOrders()==0) 
      CheckForOpen(TradeSymbol);
   else
      CheckForClose(TradeSymbol);
	
	TrailStop(TradeSymbol);
}
//+------------------------------------------------------------------+

void TrailStop(string mySymbol) {
   double StopLoss,trailing;
   if ( TrailingStop>=8 ) {
   	trailing=TrailingStop*Point;
      for (int i = 0; i < OrdersTotal(); i++) {
         if ( OrderSelect (i, SELECT_BY_POS) == false )  continue;
         if ( OrderSymbol() != mySymbol || OrderMagicNumber() != MAGICNUM )  continue;
         if ( OrderType() == OP_BUY ) {
            StopLoss = Bid-trailing;
            if ( StopLoss > OrderStopLoss() ) {
               OrderModify (OrderTicket(), OrderOpenPrice(), StopLoss, OrderTakeProfit(), 0, White);
            }
         }
   
         if ( OrderType() == OP_SELL ) {
            StopLoss = Ask+trailing;
            if ( StopLoss < OrderStopLoss() ) {
               OrderModify (OrderTicket(), OrderOpenPrice(), StopLoss, OrderTakeProfit(), 0, Gold);
            }
         }
      }
   }
   return;
}


double t3ma(int Periods) {
	int i,limit=Periods*5;
	double e1[1],e2[1],e3[1],e4[1],e5[1],e6[1],e7[1];
	ArrayResize(e1,limit*4);
	ArrayResize(e2,limit*4);
	ArrayResize(e3,limit*4);
	ArrayResize(e4,limit*4);
	ArrayResize(e5,limit*4);
	ArrayResize(e6,limit*4);
	ArrayResize(e7,limit*4);
	ArraySetAsSeries(e1,true);
	ArraySetAsSeries(e2,true);
	ArraySetAsSeries(e3,true);
	ArraySetAsSeries(e4,true);
	ArraySetAsSeries(e5,true);
	ArraySetAsSeries(e6,true);
	ArraySetAsSeries(e7,true);
   for(i=limit+Periods*5; i>=0; i--) {
   	e1[i]=iMA(NULL,0,Periods,0,MODE_EMA,PRICE_CLOSE,i);
   }
   for(i=limit+Periods*4; i>=0; i--) {
   	e2[i]=iMAOnArray(e1,0,Periods,0,MODE_EMA,i);
   }
   for(i=limit+Periods*3; i>=0; i--) {
   	e3[i]=iMAOnArray(e2,0,Periods,0,MODE_EMA,i);
   }
   for(i=limit+Periods*2; i>=0; i--) {
   	e4[i]=iMAOnArray(e3,0,Periods,0,MODE_EMA,i);
   }
   for(i=limit+Periods; i>=0; i--) {
   	e5[i]=iMAOnArray(e4,0,Periods,0,MODE_EMA,i);
   }
	double a=0.8;
	double c1=-a*a*a;
	double c2=3*a*a+3*a*a*a;
	double c3=-6*a*a-3*a-3*a*a*a;
	double c4=1+3*a+a*a*a+3*a*a;
   for(i=limit; i>=0; i--) {
   	e6[i]=iMAOnArray(e5,0,Periods,0,MODE_EMA,i);
   	e7[i]=c1*e6[i]+c2*e5[i]+c3*e4[i]+c4*e3[i];
   }
   return(e7[0]);
}

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---