رمز:
//+------------------------------------------------------------------+
//| Start function الدالة الرئيسية وتنفذ عند كل تغير بالسعر |
//+------------------------------------------------------------------+
int start()
{
//===== Part 1: Check opened positions:
double curTotal=0, BuyRun=0, SellRun=0;
for (int cnt=0; cnt<OrdersTotal(); cnt++)
{
OrderSelect(cnt,SELECT_BY_POS) ;
if(OrderSymbol()!=Symbol()) continue;
if (OrderMagicNumber()!= Magic) continue;
if(OrderType()<=OP_SELL)
{
curTotal+=1 ;
if (OrderType()==OP_SELL) { SellRun=OrderOpenPrice(); }
if (OrderType()==OP_BUY) { BuyRun =OrderOpenPrice(); }
}
}
//=======Part 2: Initialize Parms:
double MA_f, MA_s, MA_f1, MA_s1;
MA_f = iMA(Symbol(), 0, MAF, 0, MODE_EMA, PRICE_CLOSE, 1);
MA_s = iMA(Symbol(), 0, MAS, 0, MODE_EMA, PRICE_CLOSE, 1);
MA_f1 = iMA(Symbol(), 0, MAF, 0, MODE_EMA, PRICE_CLOSE, 2);
MA_s1 = iMA(Symbol(), 0, MAS, 0, MODE_EMA, PRICE_CLOSE, 2);
bool CrossUp = (MA_f1<=MA_s1 && MA_f>MA_s);
bool CrossDn = (MA_f1>=MA_s1 && MA_f<MA_s);
bool Long = (BuyRun==0 && CrossUp );
bool Short= (SellRun==0 && CrossDn );
//===== Part 3: Do Buy Or Sell:
if(Long)
{
OrderSend(Symbol(),OP_BUY,Lots,Ask, 5, Ask-StopLoss*Point, Ask+TakeProfit*Point,"LetsFX Lesson1", Magic, 0, RoyalBlue);
return ;
}
if(Short)
{
OrderSend(Symbol(),OP_SELL,Lots,Bid, 0, Bid+StopLoss*Point, Bid-TakeProfit*Point,"LetsFX Lesson1", Magic, 0,LightPink);
return;
}
//======Part 4: Handle Running Orders:
double OpenP=0,StopL=0,TakeP=0,Lot=0 ;
int Ticket=0, Type=0 ;
for (int cnt1=0; cnt1<OrdersTotal(); cnt1++)
{
if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==false) continue;
if(OrderSymbol()!=Symbol()) continue;
if (OrderMagicNumber()!= Magic) continue;
if (OrderType()<=OP_SELL)
{
Ticket= OrderTicket();
Type = OrderType();
OpenP = OrderOpenPrice();
StopL = OrderStopLoss();
TakeP = OrderTakeProfit();
Lot = OrderLots();
if (Type==OP_BUY)
{
//Handle running Long pos:
}
else
{
//Handle running Short pos:
}
}
}
// the end.
//----
return(0);
}