If this is a new bar - open a BUY position with a minimum volume.
//+------------------------------------------------------------------+//| Expert tick function |//+------------------------------------------------------------------+voidOnTick()
{
//--- we work only at the time of the birth of new bardatetime time_0=iTime(m_symbol.Name(),Period(),0);
if(time_0==m_prev_bars)
return;
m_prev_bars=time_0;//--- m_trade.Buy(m_symbol.LotsMin());
}
Task: there are two iMA indicators. Need to find: how many bars have passed since the last crossing.
Algorithm: if this is the first run - use the first form of the CopyBuffer function
Call by the first position and the number of required elements
intCopyBuffer(
int indicator_handle, // indicator handleint buffer_num, // indicator buffer numberint start_pos, // start positionint count, // amount to copydouble buffer[] // target array to copy
);
copy 100 elements and search for the intersection. We print the number
of the bar and the opening time of the bar with the
intersection
for(int i=1; i<count-1; i++)
{
if((fast[i+1]<slow[i+1] && fast[i]>slow[i]) || (fast[i+1]>slow[i+1] && fast[i]<slow[i]))
{
Comment("Bars after the last iMA crossing: ",IntegerToString(i+1)," (",TimeToString(rates[i].time,TIME_DATE|TIME_MINUTES),")");
m_last_crossing=rates[i+2].time;
return;
}
}
Next time we use the third form
Call by the start and end dates of a required time interval
intCopyBuffer(
int indicator_handle, // indicator handleint buffer_num, // indicator buffer numberdatetime start_time, // start date and timedatetime stop_time, // end date and timedouble buffer[] // target array to copy
);
Counting of elements of copied data (indicator buffer with the index buffer_num) from the starting position is performed from the present to the past, i.e., starting position of 0 means the current bar (indicator value for the current bar). When copying the yet unknown amount of data, it is recommended to use a dynamic array as a buffer[]...
An example of how to create a handle to the MACD indicator. An example of how to get the MACD indicator values on the last three bars. Remember the main rule: you need to create an indicator handle once (the best option is to create a handle in OnInit).
Hi Vladimir, thank you for your great effort, I have learned a lot from you MQL5 and I want to help you with the (PositionClosePartial) function and this code please look at it .. as it sometimes succeeds in closing a part and sometimes it fails and closes all deals in its use in hedging :
voidOnTradeTransaction(constMqlTradeTransaction &trans,
constMqlTradeRequest &request,
constMqlTradeResult &result)
{
//--- get transaction type as enumeration value ENUM_TRADE_TRANSACTION_TYPE type=trans.type;
ENUM_ACCOUNT_MARGIN_MODE AccountMargin = (ENUM_ACCOUNT_MARGIN_MODE)AccountInfoInteger(ACCOUNT_MARGIN_MODE);
if(AccountMargin==ACCOUNT_MARGIN_MODE_RETAIL_HEDGING)accofif=true;
//--- if transaction is result of addition of the transaction in historyif(type==TRADE_TRANSACTION_DEAL_ADD)
{
long deal_type =-1;
long deal_entry =-1;
double deal_volume =0.0;
string deal_symbol ="";
if(HistoryDealSelect(trans.deal))
{
deal_type =HistoryDealGetInteger(trans.deal,DEAL_TYPE);
deal_entry =HistoryDealGetInteger(trans.deal,DEAL_ENTRY);
deal_volume =HistoryDealGetDouble(trans.deal,DEAL_VOLUME);
deal_symbol =HistoryDealGetString(trans.deal,DEAL_SYMBOL);
}
elsereturn;
if(deal_entry==DEAL_ENTRY_OUT)
{
ulong PositionTicket=PositionGetInteger(POSITION_TICKET);
ulong deviation=ULONG_MAX;
switch((int)deal_type)
{
caseDEAL_TYPE_SELL:
if(Total(POSITION_TYPE_SELL) >0&& accofif)
m_trade.PositionClosePartial(PositionTicket,Lot,-1);
break;
caseDEAL_TYPE_BUY:
if(Total(POSITION_TYPE_BUY) >0&& accofif)
m_trade.PositionClosePartial(PositionTicket,Lot,-1);
break;
default:
break;
}
}
}
}
Removing an order from the list of the open ones. An order can be deleted from the open ones as a result of setting an appropriate request or execution (filling) and moving to the history. Updating a deal in the history. There may be cases when a previously executed deal is changed on a server. For example, a deal has been changed in an...
Hi Vladimir, thank you for your great effort, I have learned a lot from you MQL5 and I want to help you with the (PositionClosePartial) function and this code please look at it .. as it sometimes succeeds in closing a part and sometimes it fails and closes all deals in its use in hedging :
thank you very much Note: I searched a lot in the MQL5 library I have not found using PositionClosePartial and I hope you add this functionality to the library ... best wishes
Ahmadahmad654 : thank you very much Note: I searched a lot in the MQL5 library I have not found using PositionClosePartial and I hope you add this functionality to the library ... best wishes
When I need to close part of a position (on a hedge account) I use CTrade.PositionClosePartial
306178
Open position every new candle.
First we define a new bar.
If this is a new bar - open a BUY position with a minimum volume.
Full code:
306178
Bars after the last iMA crossing
Task: there are two iMA indicators. Need to find: how many bars have passed since the last crossing.
Algorithm: if this is the first run - use the first form of the CopyBuffer function
copy 100 elements and search for the intersection. We print the number of the bar and the opening time of the bar with the intersection
Next time we use the third form
306178
iMACD value on chart
An example of how to create a handle to the MACD indicator. An example of how to get the MACD indicator values on the last three bars. Remember the main rule: you need to create an indicator handle once (the best option is to create a handle in OnInit).
Full code:
Result:
306178
An example of get values from the iStochastic indicator
Code: iStochastic get value.mq5
Do not forget the rules: create the indicator handle ONCE in OnInit, use CopyBuffer to get the data.
Result:
306178
Example Type of candle (we use MqlRates)
Hi Vladimir, thank you for your great effort, I have learned a lot from you MQL5 and I want to help you with the (PositionClosePartial) function and this code please look at it .. as it sometimes succeeds in closing a part and sometimes it fails and closes all deals in its use in hedging :
306178
Hi Vladimir, thank you for your great effort, I have learned a lot from you MQL5 and I want to help you with the (PositionClosePartial) function and this code please look at it .. as it sometimes succeeds in closing a part and sometimes it fails and closes all deals in its use in hedging :
Error:
if you caught a transaction of a type 'DEAL_ENTRY_OUT', then there is ALREADY no position.
It is necessary to change the logic.
Note: I searched a lot in the MQL5 library
I have not found using PositionClosePartial and I hope you add this functionality to the library
... best wishes
306178
thank you very much
Note: I searched a lot in the MQL5 library
I have not found using PositionClosePartial and I hope you add this functionality to the library
... best wishes
When I need to close part of a position (on a hedge account) I use CTrade.PositionClosePartial
126161