Expert Advisor Programming by Andrew R. Young
Expert Advisor Programming by Andrew R. Young
Expert Advisor Programming by Andrew R. Young
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
EXPERT ADVISOR PROGRAMMING<br />
// Close sell orders<br />
if(SellMarketCount(Symbol(),MagicNumber) > 0)<br />
{<br />
CloseAllSellOrders(Symbol(),MagicNumber,Slippage);<br />
}<br />
// Open buy order<br />
BuyTicket = OpenBuyOrder(Symbol(),LotSize,UseSlippage,MagicNumber);<br />
// Order modification<br />
if(BuyTicket > 0 && (StopLoss > 0 || TakeProfit > 0))<br />
{<br />
OrderSelect(BuyTicket,SELECT_BY_TICKET);<br />
double OpenPrice = OrderOpenPrice();<br />
// Calculate and verify stop loss and take profit<br />
double BuyStopLoss = CalcBuyStopLoss(Symbol(),StopLoss,OpenPrice);<br />
if(BuyStopLoss > 0) BuyStopLoss = AdjustBelowStopLevel(Symbol(),<br />
BuyStopLoss,5);<br />
double BuyTakeProfit = CalcBuyTakeProfit(Symbol(),TakeProfit,<br />
OpenPrice);<br />
if(BuyTakeProfit > 0) BuyTakeProfit = AdjustAboveStopLevel(Symbol(),<br />
BuyTakeProfit,5);<br />
}<br />
}<br />
// Add stop loss and take profit<br />
AddStopProfit(BuyTicket,BuyStopLoss,BuyTakeProfit);<br />
// Sell Order<br />
if(FastMA < SlowMA && LastFastMA >= LastSlowMA<br />
&& SellMarketCount(Symbol(),MagicNumber) == 0)<br />
{<br />
if(BuyMarketCount(Symbol(),MagicNumber) > 0)<br />
{<br />
CloseAllBuyOrders(Symbol(),MagicNumber,Slippage);<br />
}<br />
SellTicket = OpenSellOrder(Symbol(),LotSize,UseSlippage,MagicNumber);<br />
if(SellTicket > 0 && (StopLoss > 0 || TakeProfit > 0))<br />
{<br />
OrderSelect(SellTicket,SELECT_BY_TICKET);<br />
OpenPrice = OrderOpenPrice();<br />
double SellStopLoss = CalcSellStopLoss(Symbol(),StopLoss,OpenPrice);<br />
if(SellStopLoss > 0) SellStopLoss = AdjustAboveStopLevel(Symbol(),<br />
SellStopLoss,5);<br />
double SellTakeProfit = CalcSellTakeProfit(Symbol(),TakeProfit,<br />
OpenPrice);<br />
174