Hi everyone! First, sorry for my english. My expert advisor don't make transactions in the way that I want. I've tried many things, but no one was working.
My ea should make transactions when actual price is higher/lower than the highest/lowest price from last x bars. The next transaction should be in the distance of x pips, when first condition is still valid. Please for help.
//+------------------------------------------------------------------+//| R.A.J. spread.mq4 |//| Copyright 2015, R.A.J. |//| https://www.mql5.com |//+------------------------------------------------------------------+#property copyright"Copyright 2015, R.A.J."#property link"https://www.mql5.com"#property version"1.00"#property strictexterndouble step_of_pips=2;
externdouble TakeProfit=900;
double Change_LotSize;
externdouble LotSize=0.1;
double bid_price;
double pips;
double wartosc;
externint lastBar=4;
externint firstBar=1;
externint StopBars=2;
datetime now;
externdouble nad=4;
intOnInit()
{
double ticksize=MarketInfo(Symbol(),MODE_TICKSIZE);
if(ticksize==0.00001 || ticksize==0.001)// Jeżeli format ceny ma 5 cyfr po przecinku lub tylko 3 to
pips = ticksize*10; // wartość pips zdefiniowana w zmiennych globalnych mnoży się razy 10 aby pips miała wartość rzeczywistego pipsa,else pips= ticksize; // w innym wypadku pips ma wartość 1, PS dla EUR/DOl u tego brokera warość pips mnoży się przez 10
bid_price=Bid;
return(INIT_SUCCEEDED);
}
voidOnTick()
{
{
if( now != Time[0] )
{
now = Time[0];
//...once per bar code goes here...//------------------------------------------------------------------------------------// Trailing distance//------------------------------------------------------------------------------- 1 --// Special function 'start'
{
string Symb=Symbol(); // Symbol//------------------------------------------------------------------------------- 2 --for(int i=1; i<=OrdersTotal(); i++) // Cycle searching in orders
{
if (OrderSelect(i-1,SELECT_BY_POS)==true) // If the next is available
{ // Analysis of orders:int Tip=OrderType(); // Order typeif(OrderSymbol()!=Symb||Tip>1)continue;// The order is not "ours"double SL=OrderStopLoss(); // SL of the selected order//---------------------------------------------------------------------- 3 --while(true) // Modification cycle
{
//------------------------------------------------------------------- 4 --bool Modify=false; // Not to be modifiedswitch(Tip) // By order type
{
case0 : // Order Buy
{
SL=Low[iLowest(NULL,0,MODE_LOW,StopBars,1)]-Point; // then modify itstring Text="Buy "; // Text for Buy
Modify=true; // To be modified
}
break; // Exit 'switch'case1 : // Order Sell
{
SL=High[iHighest(NULL,0,MODE_HIGH,StopBars,1)]+Point; // then modify it// Text for Sell
Modify=true; // To be modified
}
} // End of 'switch'if (Modify==false) // If it is not modifiedbreak; // Exit 'while'//------------------------------------------------------------------- 5 --double TP =OrderTakeProfit(); // TP of the selected orderdouble Price =OrderOpenPrice(); // Price of the selected orderint Ticket=OrderTicket(); // Ticket of the selected orderAlert ("Modification ",Ticket,". Awaiting response..");
bool Ans=OrderModify(Ticket,Price,SL,TP,0);//Modify it!//------------------------------------------------------------------- 6 --if (Ans==true) // Got it! :)
{
Alert ("Order ",Ticket," is modified:)");
break; // From modification cycle.
}
//------------------------------------------------------------------- 7 --int Error=GetLastError(); // Failed :(switch(Error) // Overcomable errors
{
// At the next iterationcase136:Alert("No prices. Waiting for a new tick..");
while(RefreshRates()==false) // To the new tickSleep(1); // Cycle delaycontinue; // At the next iterationcase146:Alert("Trading subsystem is busy. Retrying ");
Sleep(500); // Simple solution
RefreshRates(); // Update datacontinue; // At the next iteration// Critical errorscase2 : Alert("Common error.");
break; // Exit 'switch'case5 : Alert("Old version of the client terminal.");
break; // Exit 'switch'case64: Alert("Account is blocked.");
break; // Exit 'switch'case133:Alert("Trading is prohibited");
break; // Exit 'switch'default: Alert("Occurred error ",Error);//Other errors
}
break; // From modification cycle
}
} // End of modification cycle//---------------------------------------------------------------------- 8 --
} // End of order analysis
} // End of order search//------------------------------------------------------------------------------- 9 --// Exit start()
}
//...once per tick code goes here...
{
//Warunek Kup if (Ask+(nad*pips)>NewHigh && Ask>bid_price+step_of_pips*pips) //jezeli cena ask jest wieksza niż cena bid + step of pips(10) * 0.0001
buy();
}
{
//Warunek Sprzedaj if (Bid-(nad*pips)<NewLow && Bid<bid_price-step_of_pips*pips)//jeżeli cena bid jest mniejsza niż cena bid - step of pips(10) * 0.0001
sell();
}
}
}
double NewHigh=High[iHighest(NULL,0,MODE_HIGH,lastBar,firstBar)];
double NewLow=Low[iLowest(NULL,0,MODE_LOW,lastBar,firstBar)];
void sell()
{
OrderSend(Symbol(),OP_SELL,LotSize+Change_LotSize,Bid,1,High[iHighest(NULL,0,MODE_HIGH,2,0)]+Point,Bid-(TakeProfit*pips),NULL,0,0,Red);
bid_price=bid_price-step_of_pips*pips;
}
void buy()
{
OrderSend(Symbol(),OP_BUY,LotSize+Change_LotSize,Ask,1,Low[iLowest(NULL,0,MODE_LOW,2,0)]-Point,Ask+(TakeProfit*pips),NULL,0,0,Green);
bid_price=bid_price+step_of_pips*pips;
}
Hi everyone! First, sorry for my english. My expert advisor don't make transactions in the way that I want. I've tried many things, but no one was working.
My ea should make transactions when actual price is higher/lower than the highest/lowest price from last x bars. The next transaction should be in the distance of x pips, when first condition is still valid. Please for help.