
//+------------------------------------------------------------------+
//| Hedge.mq4 |
//| Copyright 2016, AM2 |
//| http://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, AM2."
#property link "http://www.forexsystems.biz"
#property version "1.00"
#property strict
//--- Inputs
extern double Lot = 0.1; // Trade volume
extern double KLot = 2; // Lot increase
extern int StopLoss = 0; // Order sroploss
extern int TakeProfit = 0; // Order takeprofit
extern int TrailingStop = 100; // Trailing stop
extern int TrailingStep = 20; // Trailing step
extern int BuySell = 0; // 0-OFF. 1-Buy. 2-Sell.
extern int BULevel = 0; // Zero Level
extern int BUPoint = 30; // Zero Points
extern int Delta = 100; // Distance from the price
extern int Slip = 3; // Slippage
extern int Magic = 123; // Magic number
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void PutOrder(int type,double price)
{
int r=0;
color clr=Green;
double sl=0,tp=0;
if(type==1 || type==3 || type==5)
{
clr=Red;
if(StopLoss>0) sl=NormalizeDouble(price+StopLoss*Point,Digits);
if(TakeProfit>0) tp=NormalizeDouble(price-TakeProfit*Point,Digits);
}
if(type==0 || type==2 || type==4)
{
clr=Blue;
if(StopLoss>0) sl=NormalizeDouble(price-StopLoss*Point,Digits);
if(TakeProfit>0) tp=NormalizeDouble(price+TakeProfit*Point,Digits);
}
r=OrderSend(NULL,type,Lots(),NormalizeDouble(price,Digits),Slip,sl,tp,"",Magic,0,clr);
return;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int FindOrderType()
{
int oticket,ticketNumber=0,type=0;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
oticket=OrderTicket();
if(oticket>ticketNumber)
{
ticketNumber=oticket;
type=OrderType();
}
}
}
}
return(type);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double FindLastBuyPrice()
{
int oticket,ticketNumber=0;
double oprice=0;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic && OrderType()==OP_BUY)
{
oticket=OrderTicket();
if(oticket>ticketNumber)
{
ticketNumber=oticket;
oprice=OrderOpenPrice();
}
}
}
}
return(oprice);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double FindLastSellPrice()
{
int oticket,ticketNumber=0;
double oprice=0;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic && OrderType()==OP_SELL)
{
oticket=OrderTicket();
if(oticket>ticketNumber)
{
ticketNumber=oticket;
oprice=OrderOpenPrice();
}
}
}
}
return(oprice);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int CountTrades()
{
int count=0;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if(OrderType()<2) count++;
}
}
}
return(count);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void Trailing()
{
bool mod;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderType()==OP_BUY)
{
if(Bid-OrderOpenPrice()>TrailingStop*Point)
{
if(OrderStopLoss()<Bid-(TrailingStop+TrailingStep-1)*Point)
{
mod=OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*Point,OrderTakeProfit(),0,Yellow);
return;
}
}
}
if(OrderType()==OP_SELL)
{
if((OrderOpenPrice()-Ask)>TrailingStop*Point)
{
if(OrderStopLoss()>Ask+(TrailingStop+TrailingStep-1)*Point || OrderStopLoss()==0)
{
mod=OrderModify(OrderTicket(),OrderOpenPrice(),Ask+TrailingStop*Point,OrderTakeProfit(),0,Yellow);
return;
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void BU()
{
bool m;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
if(OrderType()==OP_BUY)
{
if(OrderOpenPrice()<=(Bid-(BULevel+BUPoint)*Point) && OrderOpenPrice()>OrderStopLoss())
{
m=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+BUPoint*Point,OrderTakeProfit(),0,Yellow);
return;
}
}
if(OrderType()==OP_SELL)
{
if(OrderOpenPrice()>=(Ask+(BULevel+BUPoint)*Point) && (OrderOpenPrice()<OrderStopLoss() || OrderStopLoss()==0))
{
m=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-BUPoint*Point,OrderTakeProfit(),0,Yellow);
return;
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double Lots()
{
double lot=Lot;
if(CountTrades()>0)
{
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
{
lot=OrderLots()*KLot;
}
}
}
}
return(lot);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
if(BULevel>0) BU();
if(TrailingStop>0) Trailing();
// первая поза
if(BuySell==1)
{
if(CountTrades()<1) PutOrder(0,Ask);
}
if(BuySell==2)
{
if(CountTrades()<1) PutOrder(1,Bid);
}
// 2-я поза
if((FindLastBuyPrice()-Ask)/Point>=Delta && FindOrderType()==0)
{
if(CountTrades()==1) PutOrder(1,Bid);
}
if((Bid-FindLastSellPrice())/Point>=Delta && FindOrderType()==1)
{
if(CountTrades()==1) PutOrder(0,Ask);
}
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Change.mq4 |
//| Copyright 2016, AM2 |
//| http://www.forexsystems.biz |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, AM2."
#property link "http://www.forexsystems.biz"
#property version "1.00"
#property strict
extern int StopLoss = 0; // Order sroploss
extern int TakeProfit = 0; // Order takeprofit
extern int Magic = 123; // Magic number
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void PutOrder(int type,double price)
{
int r=0,err=0;
color clr=Green;
double sl=0,tp=0;
if(type==1 || type==3 || type==5)
{
clr=Red;
if(StopLoss>0) sl=NormalizeDouble(price+StopLoss*Point,Digits);
if(TakeProfit>0) tp=NormalizeDouble(price-TakeProfit*Point,Digits);
}
if(type==0 || type==2 || type==4)
{
clr=Blue;
if(StopLoss>0) sl=NormalizeDouble(price-StopLoss*Point,Digits);
if(TakeProfit>0) tp=NormalizeDouble(price+TakeProfit*Point,Digits);
}
r=OrderSend(NULL,type,0.1,NormalizeDouble(price,Digits),3,sl,tp,"",Magic,0,clr);
return;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double LastSL()
{
double sl=0;
if(OrderSelect(OrdersTotal()-1,SELECT_BY_POS,MODE_TRADES))
{
if(OrderType()<2) sl=OrderStopLoss();
}
return(sl);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double LastTP()
{
double tp=0;
if(OrderSelect(OrdersTotal()-1,SELECT_BY_POS,MODE_TRADES))
{
if(OrderType()<2) tp=OrderTakeProfit();
}
return(tp);
}
//+------------------------------------------------------------------+
//| Модификация стопов |
//+------------------------------------------------------------------+
void StopMode(double sl,double tp)
{
bool m;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if(OrderType()==OP_BUY)
{
if(OrderStopLoss()!=sl && OrderTakeProfit()!=tp)
{
m=OrderModify(OrderTicket(),OrderOpenPrice(),sl,tp,0,Yellow);
return;
}
}
if(OrderType()==OP_SELL)
{
if(OrderStopLoss()!=sl && OrderTakeProfit()!=tp)
{
m=OrderModify(OrderTicket(),OrderOpenPrice(),sl,tp,0,Yellow);
return;
}
}
}
}
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
if(OrdersTotal()<1)
{
for(int i=1;i<=2;i++)
{
PutOrder(2,Bid-222*Point*i);
PutOrder(5,Bid-666*Point);
}
}
StopMode(LastSL(),LastTP());
Comment("\n SL: ",LastSL(),
"\n TP: ",LastTP());
}
//+------------------------------------------------------------------+
Открывает неплохо, но тупо держит ордера.
Индикатор 10 поменялся, а он не закрывает или не переворачивается.
риск к профиту от 1 к 3.
Здесь закрылись селы по профиту, остался ордер бай.
Иногда заходит в поизцию и не переворачивается, хотя по инди катору должен быть переворот.
AM2