Возьмем простейший советник торгующий с фиксированными значениями стопов.
//+------------------------------------------------------------------+
//| WellX.mq4 |
//| Copyright © 2011, AM2 |
//| http://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, AM2"
#property link "http://www.forexsystems.biz"
#define MAGIC 20111010
extern double StopLoss = 400;
extern double TakeProfit = 800;
extern double ADXPeriod = 14;
extern double BBPeriod = 20;
extern double BBDev = 2;
extern int Level = 30;
extern double Lots = 1;
bool b=true, s=true;
//+------------------------------------------------------------------+
int start()
{
//---- go trading only for first tiks of new bar
if(Volume[0]>1) return;
//----
int p=0;
//---- get Indicatorrs
double bbh=iBands(NULL,0,BBPeriod,BBDev,0,PRICE_CLOSE,MODE_UPPER,0);
double bbl=iBands(NULL,0,BBPeriod,BBDev,0,PRICE_CLOSE,MODE_LOWER,0);
double adx=iADX(NULL,0,ADXPeriod,PRICE_CLOSE,MODE_MAIN,0);
//----
for (int i=0; i<OrdersTotal(); i++)
{
if (OrderSelect(i, SELECT_BY_POS)==true)
{
if (OrderSymbol()!=Symbol() || OrderMagicNumber()!=MAGIC) continue;
if (OrderType()==OP_BUY || OrderType()==OP_SELL) p++;
}
}
//---- buy
if(adx<Level && Ask<bbl && b && p<1)
{
OrderSend(Symbol(),OP_BUY,Lots,Ask,30,Ask-StopLoss*Point,Ask+TakeProfit*Point,"",MAGIC,0,Blue);
b=false;
s=true;
}
//---- sell
if(adx<Level && Bid>bbh && s && p<1)
{
OrderSend(Symbol(),OP_SELL,Lots,Bid,30,Bid+StopLoss*Point,Bid-TakeProfit*Point,"",MAGIC,0,Red );
b=true;
s=false;
}
//----
return(0);
}
//+------------------------------------------------------------------+
Результат оптимизации эксперта с 2000-го года ниже:
Функция Мартингейла может иметь следующий вид:
//+------------------------------------------------------------------+
int LossCount = 0;
double LotsArray[]={0.1,0.2,0.4,0.8,1.6,3.2};
...
double Lots()
{
double Lot = Lots;
int total = OrdersHistoryTotal();
for (int i = 0; i < total; i++)
{
OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);
if (OrderSymbol() == Symbol() && OrderMagicNumber() == MAGIC)
{
if (OrderProfit() > 0)
{
Lot=LotsArray[0];
LossCount = 0;
}
else
{
Lot=LotsArray[LossCount+1];
LossCount++;
}
}
}
return(Lot);
}
//+------------------------------------------------------------------+
Остается только собрать все воедино:
//+------------------------------------------------------------------+
//| WellX.mq4 |
//| Copyright © 2011, AM2 |
//| http://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, AM2"
#property link "http://www.forexsystems.biz"
#define MAGIC 20111010
extern double StopLoss = 400;
extern double TakeProfit = 800;
extern double ADXPeriod = 14;
extern double BBPeriod = 20;
extern double BBDev = 2;
extern int Level = 30;
extern double Lots = 0.1;
int LossCount = 0;
double LotsArray[]={0.1,0.2,0.4,0.8,1.6,3.2};
bool b=true, s=true;
//+------------------------------------------------------------------+
int start()
{
//---- go trading only for first tiks of new bar
if(Volume[0]>1) return;
//----
int p=0;
//---- get Indicatorrs
double bbh=iBands(NULL,0,BBPeriod,BBDev,0,PRICE_CLOSE,MODE_UPPER,0);
double bbl=iBands(NULL,0,BBPeriod,BBDev,0,PRICE_CLOSE,MODE_LOWER,0);
double adx=iADX(NULL,0,ADXPeriod,PRICE_CLOSE,MODE_MAIN,0);
//----
for (int i=0; i<OrdersTotal(); i++)
{
if (OrderSelect(i, SELECT_BY_POS)==true)
{
if (OrderSymbol()!=Symbol() || OrderMagicNumber()!=MAGIC) continue;
if (OrderType()==OP_BUY || OrderType()==OP_SELL) p++;
}
}
//---- buy
if(adx<Level && Ask<bbl && b && p<1)
{
OrderSend(Symbol(),OP_BUY,Lots(),Ask,30,Ask-StopLoss*Point,Ask+TakeProfit*Point,"",MAGIC,0,Blue);
b=false;
s=true;
}
//---- sell
if(adx<Level && Bid>bbh && s && p<1)
{
OrderSend(Symbol(),OP_SELL,Lots(),Bid,30,Bid+StopLoss*Point,Bid-TakeProfit*Point,"",MAGIC,0,Red );
b=true;
s=false;
}
//----
return(0);
}
//+------------------------------------------------------------------+
double Lots()
{
double Lot = Lots;
int total = OrdersHistoryTotal();
for (int i = 0; i < total; i++)
{
OrderSelect(i, SELECT_BY_POS, MODE_HISTORY);
if (OrderSymbol() == Symbol() && OrderMagicNumber() == MAGIC)
{
if (OrderProfit() > 0)
{
Lot=LotsArray[0];
LossCount = 0;
}
else
{
Lot=LotsArray[LossCount+1];
LossCount++;
}
}
}
return(Lot);
}
//+------------------------------------------------------------------+
Прибыль эксперта после оптимизации конечно же куда скромнее, но для наглядности вполне подходит.
Результат с того же 2000-го года.
Дерзайте, юные умы!