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
Hi All
During a trading day if my profit or loss for all of my open and closed positions combined on that day exceed a certain amount (either profit or loss) I close all trades. This part of my code works fine. Once all trades have been closed no more trades are opened on that day.
If neither profit target or loss are reached it simply rolls over to the next trading day with all position open and at 7AM my time (market open) the AccountProfit() resets to zero and it starts again and that all works fine.
My problem is that when I have no open trades and the new trading day begins nothing happens, in other words the total profit or loss stays the same as yesterday at say -200 and no new trades occur. I am assuming that as I have no open trades OnTick() is not called so the dates are not reset to today and therefore the AccountProfit() for today which should be zero is not reset. If I re compile my code everything is reset and all trades open.
I guess my question is if I have no open positions does OnTick() get called or what would I need to do to get the AccountProfit() to reset to today's profit which would be zero when the first tick comes in after the market has opened for the new day.
Part of my code in question
void OnTick()
{
datetime StartDate = __DATE__;
int DurationHours = 24;
double total_day_profit = 0.0;
for ( int z = OrdersHistoryTotal()-1; z >= 0; z -- )
{
if ( !OrderSelect( z, SELECT_BY_POS, MODE_HISTORY ) ){ continue;}
if ( OrderType() != OP_BUY && OrderType() != OP_SELL ) {continue;}
if ( OrderCloseTime() < StartDate || OrderCloseTime() >= StartDate+DurationHours*60*60 ) continue;
total_day_profit += (OrderProfit() + OrderSwap() + OrderCommission());
}
double tricky = AccountProfit();
dayprofit = tricky + total_day_profit;
dayprofit = DoubleToStr( dayprofit, 0);
ObjectCreate("ObjName", OBJ_LABEL, 0, 0, 0);
ObjectSetText("ObjName",dayprofit,17, "Verdana", Red);
ObjectSet("ObjName", OBJPROP_CORNER, 1);
ObjectSet("ObjName", OBJPROP_XDISTANCE, 20);
ObjectSet("ObjName", OBJPROP_YDISTANCE, 20);
today = __DATE__;
if (dayprofit > 300)
{
CloseAllOrders();
yesterday = __DATE__;
}
if (dayprofit < -200)
{
CloseAllOrders();
yesterday = __DATE__;
}
if (today > yesterday)
{
OpenAllOrders();
}
.
.
.
.