Forum on trading, automated trading systems and testing trading strategies
Hello,
Please EDIT your post and use the SRC button when you post code.
|
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Because there are a lot of indicators, We are going to try to create an automatic sales program through the open indicator "HalfTrend".
Buy a 'long position' on arrow from the blue arrow and settlement(liquidation) it to red arrow
and buy a 'short positon' on red arrow, liquidation on blue arrrow on and on ... ...
teach how to use ordersend and iCustom functions to buy and sell again.
//| Half Trend |
//+------------------------------------------------------------------+
int init()
{
IndicatorBuffers(7); // +1 buffer - trend[]
SetIndexBuffer(0,up);
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(1,down);
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(2,atrlo);
SetIndexBuffer(3,atrhi);
SetIndexBuffer(6,trend);
SetIndexBuffer(4,arrup);
SetIndexBuffer(5,arrdwn);
SetIndexEmptyValue(0,0.0);
SetIndexEmptyValue(1,0.0);
SetIndexEmptyValue(6,0.0);
if(ShowBars)
{
SetIndexStyle(2,DRAW_HISTOGRAM, STYLE_SOLID);
SetIndexStyle(3,DRAW_HISTOGRAM, STYLE_SOLID);
}
else
{
SetIndexStyle(2,DRAW_NONE);
SetIndexStyle(3,DRAW_NONE);
}
if(ShowArrows)
{
SetIndexStyle(4,DRAW_ARROW,STYLE_SOLID); SetIndexArrow(4,233);
SetIndexStyle(5,DRAW_ARROW,STYLE_SOLID); SetIndexArrow(5,234);
}
else
{
SetIndexStyle(4,DRAW_NONE);
SetIndexStyle(5,DRAW_NONE);
}
nexttrend=0;
minhighprice= High[Bars-1];
maxlowprice = Low[Bars-1];
return (0);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
double up_signal=iCustom(NULL,0,"HalfTrend",4,0);
double down_signal=iCustom(NULL,0,"HalfTrend",5,0);
if(OrdersTotal()==0)
{
//--- Buy order
Open_MarketOrder(OP_BUY);
}
else if(OrdersTotal()==0)
{
//--- Sell order
Open_MarketOrder(OP_SELL);
}
}
//+------------------------------------------------------------------+
//| Market order function function |
//+------------------------------------------------------------------+
void Open_MarketOrder(int pOrderType)
{
if(UseMartingale==true)
{
double lot_size=Calculate_Martingale();
if(lot_size==-1)//first oder
{
Lot_Size=Lotsize;
}
else //stop_loss
{
Lot_Size=lot_size*MartingaleMultiplier;
//Lot_Size=Verify_LotSize(Lot_Size);
}
}
int ticketNum;
if(pOrderType==OP_BUY)
{
ticketNum=OrderSend(Symbol(),OP_BUY,Lot_Size,Ask,Slippage,0,0,
"EA magic number:"+(string)MagicNumber,MagicNumber,0,clrGreen);
if(ticketNum!=-1)
{
if(OrderSelect(ticketNum,SELECT_BY_TICKET)==true)
{
double stop_loss=0;
double take_profit=0;
if(Stoploss>0) stop_loss=OrderOpenPrice()-Stoploss*_Point;
if(Takeprofit>0) take_profit=OrderOpenPrice()+Takeprofit*_Point;
if(OrderModify(ticketNum,0,stop_loss,take_profit,0,clrGreen)==true)
{
Print("success modify. Ticket="+(string)ticketNum
+", sale loss="+DoubleToStr(stop_loss,_Digits)
+", attainment of profit="+DoubleToStr(take_profit,_Digits));
}
else //OrderModify error on function
{
Handle_Error("OrderModify");
}
}
else //OrderSelect error on function
{
Handle_Error("OrderSelect");
}
}
else //OrderSend error on function
{
Handle_Error("OrderSend");
}
}
else if(pOrderType==OP_SELL)
{
ticketNum=OrderSend(Symbol(),OP_SELL,Lot_Size,Bid,Slippage,0,0,
"EA magic number:"+(string)MagicNumber,MagicNumber,0,clrRed);
if(ticketNum!=-1)
{
if(OrderSelect(ticketNum,SELECT_BY_TICKET)==true)
{
double stop_loss=0;
double take_profit=0;
if(Stoploss>0) stop_loss=OrderOpenPrice()+Stoploss*_Point;
if(Takeprofit>0) take_profit=OrderOpenPrice()-Takeprofit*_Point;
if(OrderModify(ticketNum,0,stop_loss,take_profit,0,clrRed)==true)
{
Print("success modify="+(string)ticketNum
+", sale loss="+DoubleToStr(stop_loss,_Digits)
+", attainment of profit="+DoubleToStr(take_profit,_Digits));
}
else //OrderModify error on function
{
Handle_Error("OrderModify");
}
}
else //OrderSelect error on function
{
Handle_Error("OrderSelect");
}
}
else //OrderSend error on function
{
Handle_Error("OrderSend");
}
}
}