extern double TakeProfit = 30; extern double StopLoss = 20; extern double Lots = 0.1; //mia aggiunta int NumOrder ; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int start() { double Macd_1, Macd_2, Macd_3, Macd_4, Ma_Quick, Ma_Slow; bool Long_1, Long_2, Long, Short_1, Short_2, Short; int cnt, ticket, total; if(Bars<100) { Print("bars less than 100"); return(0); } if( isNewBar()){ NumOrder = 0;} if(TakeProfit<10) { Print("TakeProfit less than 10"); return(0); // check TakeProfit } // to simplify the coding and speed up access // data are put into internal variables Macd_1 = iMACD(NULL,0,3,20,9,PRICE_CLOSE,MODE_MAIN,1); Macd_2 = iMACD(NULL,0,3,20,9,PRICE_CLOSE,MODE_MAIN,2); Macd_3 = iMACD(NULL,0,3,20,9,PRICE_CLOSE,MODE_MAIN,3); Macd_4 = iMACD(NULL,0,3,20,9,PRICE_CLOSE,MODE_MAIN,4); Ma_Quick = iMA(NULL,0,20,0,MODE_EMA,PRICE_CLOSE,0); Ma_Slow = iMA(NULL,0,65,0,MODE_EMA,PRICE_CLOSE,0); Long_1 = Macd_1 > Macd_2 && Macd_2 < Macd_3 && Ma_Quick > Ma_Slow && Macd_2 < 0 && Ask > High[1]; Long_2 = Macd_2 > Macd_3 && Macd_3 < Macd_4 && Ma_Quick > Ma_Slow && Macd_3 < 0 && Ask > High[2] && High[1] < High[2]; Long = Long_1 || Long_2; Short_1 = Macd_1 < Macd_2 && Macd_2 > Macd_3 && Ma_Quick < Ma_Slow && Macd_2 > 0 && Bid < Low[1]; Short_2 = Macd_2 < Macd_3 && Macd_3 > Macd_4 && Ma_Quick < Ma_Slow && Macd_3 > 0 && Bid < Low[2] && Low[1] > Low[2]; Short = Short_1 || Short_2; // no opened orders identified if(AccountFreeMargin()<(1000*Lots)) { Print("We have no money. Free Margin = ", AccountFreeMargin()); return(0); } // check for long position (BUY) possibility if(Long) { if(NumOrder == 0) { ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,NULL,Ask-StopLoss*Point,Ask+TakeProfit*Point,"macd sample",16384,0,Green); if(ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) { Print("BUY order opened : ",OrderOpenPrice()); NumOrder = 1; } } else Print("Error opening BUY order : ",GetLastError()); return(0); } } // check for short position (SELL) possibility if(Short) { if(NumOrder == 0) { ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,0,Bid+StopLoss*Point,Bid-TakeProfit*Point,"macd sample",16384,0,Red); if(ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) { Print("SELL order opened : ",OrderOpenPrice()); NumOrder = 1; } } else Print("Error opening SELL order : ",GetLastError()); return(0); } return(0); } } // the end. bool isNewBar() { static datetime lastbar=0; datetime curbar = Time[0]; if(lastbar!=curbar) { lastbar=curbar; return (true); } else { return(false); } }
Sample
Analysis
Market Information Used:
Series array that contains the highest prices of each bar
Series array that contains the lowest prices of each bar
Series array that contains open time of each bar
Indicator Curves created:
Indicators Used:
MACD Histogram
Moving average indicator
Custom Indicators Used:
Order Management characteristics:
It automatically opens orders when conditions are reached
Other Features: