I specify the author of the idea and the author of a MQL5 code in a code.
The coding services are provided "as is" and you use them at your own risk.
Hi everyone,
this is a brilliant initiative to share ideas, and I'm sure it'll be very helpful to anyone like me who is trying to put together his/her first EA.
The strategy that I'd like (and I'm trying) to translate into code is very simple:
1. Open a trade on a random position (Long or Short);
2. After 60 seconds check if this trade has reached TP or SL:
- if it did: close the current trade and open a new one of the same value of the first one but in the opposite direction (if the first one was Long, open a Short one and viceversa);
- if it didn't: let it run for 60 more seconds and perform the same check;
And so on. Keep it running like this. Once a trade is open, again point 2. . And again and again.
No index or any chart to take into account. I'd like to try it as they ask me to do it (I'm not asking myself if this is a good strategy, I'm just trying to code it).
Below I'm posting the code that I've been able to write so far.
Thank you to anyone who will take part in this.
E.
//+------------------------------------------------------------------+//| cheduecoglioni.mq5 |//| Gianni Esperti |//| https://www.gianniesperti.come? |//+------------------------------------------------------------------+#property copyright"Gianni Esperti"#property link"https://www.gianniesperti.come?"#property version"1.00"//+------------------------------------------------------------------+//| Expert initialization function |//+------------------------------------------------------------------+#include <Trade\Trade.mqh> CTrade trade; uint s; int r; intOnInit() { MathSrand( GetTickCount() ); r = MathRand(); EventSetTimer(60); doubleAsk = NormalizeDouble ( SymbolInfoDouble (_Symbol, SYMBOL_ASK), _Digits ); if (r <= 16383) { Print(r, " bitches in my Lex"); trade.Buy (0.01, NULL, Ask, 0, ( Ask + 100 * _Point ), NULL); } else { Print(r, " of my beats in yo stereo"); trade.Sell (0.01, NULL, Ask, 0, ( Ask - 100 * _Point ), NULL); } return (INIT_SUCCEEDED); } voidOnTimer() { }
2. After 60 seconds check if this trade has reached TP or SL:
- if it did: close the current trade and open a new one of the same value of the first one but in the opposite direction (if the first one was Long, open a Short one and viceversa);
...
I do not quite understand: we do not care what worked - TP or Sl?
Added: if TP or SL works, it means that the position has already closed - that is, the position is no longer there.
Capturing trading events is very convenient in function OnTradeTransaction.
In OnTradeTransaction, we can determine the deal direction (ENUM_DEAL_ENTRY - we will only be interested in DEAL_ENTRY_OUT) and the deal type (ENUM_DEAL_TYPE - we will only be interested in DEAL_TYPE_BUY and DEAL_TYPE_SELL).
this is a brilliant initiative to share ideas, and I'm sure it'll be very helpful to anyone like me who is trying to put together his/her first EA.
The strategy that I'd like (and I'm trying) to translate into code is very simple:
1. Open a trade on a random position (Long or Short);
2. After 60 seconds check if this trade has reached TP or SL:
- if it did: close the current trade and open a new one of the same value of the first one but in the opposite direction (if the first one was Long, open a Short one and viceversa);
- if it didn't: let it run for 60 more seconds and perform the same check;
And so on. Keep it running like this. Once a trade is open, again point 2. . And again and again.
No index or any chart to take into account. I'd like to try it as they ask me to do it (I'm not asking myself if this is a good strategy, I'm just trying to code it).
Below I'm posting the code that I've been able to write so far.
Thank you to anyone who will take part in this.
E.
Here is the preliminary code:
//+------------------------------------------------------------------+//| cheduecoglioni.mq5 |//| Gianni Esperti |//| https://www.gianniesperti.come? |//+------------------------------------------------------------------+#property copyright"Gianni Esperti"#property link"https://www.gianniesperti.come?"#property version"1.000"//+------------------------------------------------------------------+//| Expert initialization function |//+------------------------------------------------------------------+#include <Trade\PositionInfo.mqh> #include <Trade\Trade.mqh> CPositionInfo m_position; // trade position object CTrade m_trade; // trading object//---ulong m_magic=15489; // magic numberENUM_POSITION_TYPE m_close_pos_type=POSITION_TYPE_BUY; datetime m_time_last_trade=0; bool m_is_trade=true; //+------------------------------------------------------------------+//| |//+------------------------------------------------------------------+intOnInit() { m_trade.SetExpertMagicNumber(m_magic); m_close_pos_type=POSITION_TYPE_BUY; m_time_last_trade=0; m_is_trade=true; //---return (INIT_SUCCEEDED); } //+------------------------------------------------------------------+//| Expert tick function |//+------------------------------------------------------------------+voidOnTick() { if(TimeCurrent()-m_time_last_trade>60 && m_is_trade) { MqlTick last_tick; if(!SymbolInfoTick(Symbol(),last_tick)) return; if(last_tick.ask==0.0 || last_tick.bid==0.0) return; Print(__FUNCTION__", TimeCurrent()-m_time_last_trade=",(long)(TimeCurrent()-m_time_last_trade)); if(m_close_pos_type==POSITION_TYPE_SELL) { Print("Open Buy"); m_trade.Buy(0.01,NULL,last_tick.ask, last_tick.bid-100*Point(), last_tick.bid+100*Point()); } elseif(m_close_pos_type==POSITION_TYPE_BUY) { Print("Open Sell"); m_trade.Sell(0.01,NULL,last_tick.bid, last_tick.ask+100*Point(), last_tick.ask-100*Point()); } } } //+------------------------------------------------------------------+//| TradeTransaction function |//+------------------------------------------------------------------+voidOnTradeTransaction(constMqlTradeTransaction &trans, constMqlTradeRequest &request, constMqlTradeResult &result) { //--- get transaction type as enumeration value ENUM_TRADE_TRANSACTION_TYPE type=trans.type; //--- if transaction is result of addition of the transaction in historyif(type==TRADE_TRANSACTION_DEAL_ADD) { long deal_entry =0; long deal_type =0; string deal_symbol =""; long deal_magic =0; long deal_time =0; if(HistoryDealSelect(trans.deal)) { deal_entry=HistoryDealGetInteger(trans.deal,DEAL_ENTRY); deal_type=HistoryDealGetInteger(trans.deal,DEAL_TYPE); deal_symbol=HistoryDealGetString(trans.deal,DEAL_SYMBOL); deal_magic=HistoryDealGetInteger(trans.deal,DEAL_MAGIC); deal_time=HistoryDealGetInteger(trans.deal,DEAL_TIME); } elsereturn; if(deal_symbol==Symbol() && deal_magic==m_magic) { if(deal_entry==DEAL_ENTRY_OUT) { if(deal_type==DEAL_TYPE_BUY || deal_type==DEAL_TYPE_SELL) { m_close_pos_type=(deal_type==DEAL_TYPE_BUY)?POSITION_TYPE_SELL:POSITION_TYPE_BUY; m_time_last_trade=(datetime)deal_time; m_is_trade=true; Print(__FUNCTION__", DEAL_ENTRY_OUT, m_time_last_trade=",TimeToString(m_time_last_trade)); } } elseif(deal_entry==DEAL_ENTRY_IN) { m_time_last_trade=(datetime)deal_time/*TimeCurrent()*/; m_is_trade=false; } } } } //+------------------------------------------------------------------+
I do not quite understand: we do not care what worked - TP or Sl?
Added: if TP or SL works, it means that the position has already closed - that is, the position is no longer there.
Exactly, either way (TP or SL) just close it and open a new one.
But if you tell me that once I set TP or SL, the buy function automatically closes it, it should open straight away the second trade: same value opposite direction.
Does it make sense? Sorry for not knowing these basic rules, but I'm new to all of this.
Exactly, either way (TP or SL) just close it and open a new one.
But if you tell me that once I set TP or SL, the buy function automatically closes it, it should open straight away the second trade: same value opposite direction.
Does it make sense? Sorry for not knowing these basic rules, but I'm new to all of this.
Thank you for helping out
Then I remove the time interval check (60 seconds)?
Crossing of two iMA 1.007
The option of lot selection is included: manual lot or percentage of risk
Thanks, that option is working perfectly fine
Terms and Conditions:
Hi everyone,
this is a brilliant initiative to share ideas, and I'm sure it'll be very helpful to anyone like me who is trying to put together his/her first EA.
The strategy that I'd like (and I'm trying) to translate into code is very simple:
1. Open a trade on a random position (Long or Short);
2. After 60 seconds check if this trade has reached TP or SL:
- if it did: close the current trade and open a new one of the same value of the first one but in the opposite direction (if the first one was Long, open a Short one and viceversa);
- if it didn't: let it run for 60 more seconds and perform the same check;
And so on. Keep it running like this. Once a trade is open, again point 2. . And again and again.
No index or any chart to take into account. I'd like to try it as they ask me to do it (I'm not asking myself if this is a good strategy, I'm just trying to code it).
Below I'm posting the code that I've been able to write so far.
Thank you to anyone who will take part in this.
E.
...
2. After 60 seconds check if this trade has reached TP or SL:
- if it did: close the current trade and open a new one of the same value of the first one but in the opposite direction (if the first one was Long, open a Short one and viceversa);
...
I do not quite understand: we do not care what worked - TP or Sl?
Added: if TP or SL works, it means that the position has already closed - that is, the position is no longer there.
Capturing trading events is very convenient in function OnTradeTransaction.
In OnTradeTransaction, we can determine the deal direction (ENUM_DEAL_ENTRY - we will only be interested in DEAL_ENTRY_OUT) and the deal type (ENUM_DEAL_TYPE - we will only be interested in DEAL_TYPE_BUY and DEAL_TYPE_SELL).
Hi everyone,
this is a brilliant initiative to share ideas, and I'm sure it'll be very helpful to anyone like me who is trying to put together his/her first EA.
The strategy that I'd like (and I'm trying) to translate into code is very simple:
1. Open a trade on a random position (Long or Short);
2. After 60 seconds check if this trade has reached TP or SL:
- if it did: close the current trade and open a new one of the same value of the first one but in the opposite direction (if the first one was Long, open a Short one and viceversa);
- if it didn't: let it run for 60 more seconds and perform the same check;
And so on. Keep it running like this. Once a trade is open, again point 2. . And again and again.
No index or any chart to take into account. I'd like to try it as they ask me to do it (I'm not asking myself if this is a good strategy, I'm just trying to code it).
Below I'm posting the code that I've been able to write so far.
Thank you to anyone who will take part in this.
E.
Here is the preliminary code:
I do not quite understand: we do not care what worked - TP or Sl?
Added: if TP or SL works, it means that the position has already closed - that is, the position is no longer there.
Exactly, either way (TP or SL) just close it and open a new one.
But if you tell me that once I set TP or SL, the buy function automatically closes it, it should open straight away the second trade: same value opposite direction.
Does it make sense? Sorry for not knowing these basic rules, but I'm new to all of this.
Thank you for helping out
Exactly, either way (TP or SL) just close it and open a new one.
But if you tell me that once I set TP or SL, the buy function automatically closes it, it should open straight away the second trade: same value opposite direction.
Does it make sense? Sorry for not knowing these basic rules, but I'm new to all of this.
Thank you for helping out
Then I remove the time interval check (60 seconds)?
Then I remove the time interval check (60 seconds)?
Actually yes. Cause at the 60 sec mark (if it didn't reach tp or sl) it'd keep it alive anyway.
The first version of the code includes the 60 sec check, right?
Actually yes. Cause at the 60 sec mark (if it didn't reach tp or sl) it'd keep it alive anyway.
The first version of the code includes the 60 sec check, right?
Yes, in post #66 code includes the 60 sec check.