I want a code that close position 24 after its opening. thanks.
create variable and save OrderOpenTime hour
for example your position was opened at 21:00
then compare current hour with that
if(Hour()==OrderOpenHour)
{
OrderClose(); //not completed
}
I want a code that close position 24 after its opening. thanks.
First convert your 24 hours to seconds i.e. (24*60*60). Add this to your Order open time. So if current time is equal or greater than this time close the trade.
I want a code that close position 24 after its opening. thanks.
Use this code: Close after 24 hours
Look at the code that @Vladimir Karputov linked and just change it to use a Input variable instead of a constant time amount.
I did it. but the positions closed at 24h or 48h etc.
You can use and modify my function below
The function closes any position which matches a time range, from X minutes, to Y minutes. And you can choose the position type to act (Buy or Sell)
To close any position greater than 24h, call it like:
CloseByTimeExtended(1440, 7200, POSITION_TYPE_BUY); CloseByTimeExtended(1440, 7200, POSITION_TYPE_SELL);
Respectively the above commands will:
Close any BUY position which time is between 24hs (1440 min) to 5 days (7200 min)
The second command does the same, for SELL positions.
it uses Ctrade to close. So you need to have it instantiated at the beginning of your code.
CTrade trades; CPositionInfo positions;
And here is the full function. Use it, change it or adapt it to fit your needs .
If you have questions feel free to ask.
// +------------------------------------------------------------------+ // | Close Positions by Time Range | // +------------------------------------------------------------------+ void CloseByTimeExtended(int from_x_minuts, int to_x_minuts, ENUM_POSITION_TYPE pos_type ) { if ( PositionsTotal() > 0) { for (int i = 0; i < PositionsTotal() ; i++) { if(positions.SelectByIndex(i)) { if (positions.Symbol() == _Symbol && PositionGetInteger(POSITION_MAGIC) == Magic1) { if (( ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL || (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY ) { datetime opening = (datetime)PositionGetInteger(POSITION_TIME); int time_minuts = (int)((TimeCurrent() - opening) / 60); if (1 && time_minuts >= from_x_minuts && time_minuts < to_x_minuts && (ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE) == pos_type ) { Print("=== ------- >>>>>>> Closing position ticket#:", DoubleToString((ulong)PositionGetInteger(POSITION_TICKET), 0), " by TIME range! [ position time was ", IntegerToString(time_minuts), " mins]" ); trades.PositionClose((ulong)PositionGetInteger(POSITION_TICKET)); } } } } else { Print ("[" + __FUNCTION__ + "]: Error selecting position to close by Time"); } } return; } }

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use