Skip to content
Open
2 changes: 1 addition & 1 deletion MtApi5/MtApi5.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<VersionPrefix>2.0.0</VersionPrefix>
<VersionPrefix>2.0.1</VersionPrefix>
<BaseOutputPath>$(SolutionDir)build\products</BaseOutputPath>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>
Expand Down
Binary file modified mq5/MtApi5.ex5
Binary file not shown.
35 changes: 25 additions & 10 deletions mq5/MtApi5.mq5
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ enum LockTickType
};

input int Port = 8228;
input int CommandPollingIntervalMs = 100; // command polling interval in live trading, ms (10-1000)
input LockTickType BacktestingLockTicks = NO_LOCK;
input group "Disable Events "
input bool Enable_OnBookEvent = true;
Expand Down Expand Up @@ -106,13 +107,14 @@ void OnTick()
(BacktestingLockTicks == LOCK_EVERY_CANDLE && lastbar_time_changed))
{
_is_ticks_locked = true;

MtLockTickEvent lock_tick_event(symbol);
SendMtEvent(ON_LOCK_TICKS_EVENT, lock_tick_event);
}

OnTimer();
}

//--- Process pending commands on every tick (supplements timer in live mode)
OnTimer();
}

void OnTradeTransaction(
Expand Down Expand Up @@ -446,31 +448,44 @@ int init()

//--- Backtesting mode
if (IsTesting())
{
{
Print("Waiting on remote client...");
//wait for command (BacktestingReady) from remote side to be ready for work
while(!IsRemoteReadyForTesting)
{
executeCommand();

//This section uses a while loop to simulate Sleep() during Backtest.
unsigned int viSleepUntilTick = GetTickCount() + 100; //100 milliseconds
while(GetTickCount() < viSleepUntilTick)
while(GetTickCount() < viSleepUntilTick)
{
//Do absolutely nothing. Just loop until the desired tick is reached.
}
}
}
//---
else
{
//--- In live trading, poll for commands via timer (configurable interval)
int polling_interval = CommandPollingIntervalMs;
if (polling_interval < 10) polling_interval = 10;
if (polling_interval > 1000) polling_interval = 1000;
if (polling_interval != CommandPollingIntervalMs)
PrintFormat("[WARNING] CommandPollingIntervalMs=%d out of range, clamped to %d", CommandPollingIntervalMs, polling_interval);
if (!EventSetMillisecondTimer(polling_interval))
Print("[WARNING] Failed to set millisecond timer for command polling");
}
//---

return (0);
}

int deinit()
int deinit()
{
if (isCrashed == 0)
EventKillTimer();

if (isCrashed == 0)
{
if (!deinitExpert(ExpertHandle, _error))
if (!deinitExpert(ExpertHandle, _error))
{
MessageBox(_error, "MtApi", 0);
isCrashed = true;
Expand Down