To mam prosbe, ze jezeli ktos mialby chwilke niech zerknie na ten kod (to jest 1wsza wersja jeszcze przed zmianami od Tig3ra)
Kod: Zaznacz cały
extern double Lots = 0.1;
extern double MovingPeriod = 25;
extern double MovingShift = 15;
extern double TakeProfit = 30;
extern double StopLoss = 30;
//+------------------------------------------------------------------+
//|Oblicza czy są otwarte jakieś pozycje |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
{
int buys=0, sells=0, all=0;
for (int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false || OrderSymbol()!=symbol) break;
{
if(OrderType()==OP_BUY) buys++;
if(OrderType()==OP_SELL) sells++;
}
}
if(buys>0) return (buys);
else return (-sells);
}
//+------------------------------------------------------------------+
//| Warunki Otwarcia |
//+------------------------------------------------------------------+
void CheckForOpen()
{
double ma;
int res;
ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);
if (Close[1]<ma && Open[1]>ma)
{
res=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,"",0,0,Red);
return;
}
if (Close[1]>ma && Open[1]<ma)
{
res=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"",0,0,Blue);
return;
}
}
void CheckForOpen2() // tu sa warunki otwarcia drugiej pozycji (przeciwnej)
{
double ma;
int res2;
ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);
if (OrderType()==OP_SELL && Close[1]>ma && Open[1]<ma)
{
res2=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"",0,0,Blue);
return;
}
if (OrderType()==OP_BUY && Close[1]<ma && Open[1]>ma)
{
res2=OrderSend(Symbol(),OP_SELL,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"",0,0,Blue);
return;
}
}
//+------------------------------------------------------------------+
//|Warunki zamknięcia |
//+------------------------------------------------------------------+
// zamkniecie tylko na TP/SL
void CheckForClose()
{
double ma;
ma=iMA(NULL,0,MovingPeriod,MovingShift,MODE_SMA,PRICE_CLOSE,0);
for(int i=0;i<OrdersTotal();i++)
{
if(OrderType()==OP_BUY)
{
if(OrderTakeProfit()==TakeProfit*Point || OrderStopLoss()==StopLoss*Point) OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
Print(OrderTakeProfit());
Print(OrderStopLoss());
break;
}
if(OrderType()==OP_SELL)
{
if(OrderTakeProfit()==TakeProfit*Point || OrderStopLoss()==StopLoss*Point) OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
Print(OrderTakeProfit());
Print(OrderStopLoss());
break;
}
}
}
//+------------------------------------------------------------------+
//| Start function |
//+------------------------------------------------------------------+
void start()
{
if (OrderType()==OP_BUY || OrderType()==OP_SELL) CheckForOpen2(); // nie wiem czy ten zapis jest poprawny, ale chodzilo mi o to, ze jezeli sa otwarte jakies pozycje, to strategia ma szukac otwarcia przeciwnych pozycji w 'CheckForOpen2()
//---- check for history and trading
if(Bars<100 || IsTradeAllowed()==false) return;
//---- calculate open orders by current symbol
if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
else CheckForClose();
//----
}
//+------------------------------------------------------------------+