0% found this document useful (0 votes)
25 views3 pages

cTrader Order Sync Bot Documentation

The document describes a cTrader robot named OrderSyncBot that synchronizes orders between cTrader and MT4 by reading order data from a specified text file. It checks for orders from MT4 that are not already open in cTrader and executes them, while also closing any open positions in cTrader that are not present in the MT4 file. The robot uses a timer to update orders every second and includes error handling for file reading and order parsing.

Uploaded by

clalessio
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views3 pages

cTrader Order Sync Bot Documentation

The document describes a cTrader robot named OrderSyncBot that synchronizes orders between cTrader and MT4 by reading order data from a specified text file. It checks for orders from MT4 that are not already open in cTrader and executes them, while also closing any open positions in cTrader that are not present in the MT4 file. The robot uses a timer to update orders every second and includes error handling for file reading and order parsing.

Uploaded by

clalessio
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

CBOT work quasi: using System;

using [Link];
using [Link];
using [Link];
using [Link];

namespace [Link]
{
// Se la tua versione di cTrader supporta 'FullAccess', usa quello:
[Robot(TimeZone = [Link], AccessRights = [Link])]
public class OrderSyncBot : Robot
{
private [Link] _timer;
private readonly string _filePath = @"C:\Users\Admin\AppData\Roaming\
MetaQuotes\Terminal\6FB71F59CF9A5D4AE68C045BD53F56C4\MQL4\Files\[Link]";

protected override void OnStart()


{
_timer = new [Link](UpdateOrders, null, 0, 1000);
}

private void UpdateOrders(object state)


{
BeginInvokeOnMainThread(() =>
{
var ordersFromMT4 = ReadOrdersFromFile(_filePath);
var openPositions = [Link](); // Converte Positions in un array

// Per ogni ordine letto da MT4: se non è già aperto su cTrader, lo apre.
foreach (var mt4Order in ordersFromMT4)
{
if (!IsOrderAlreadyOpen(mt4Order, openPositions))
{
ExecuteOrder(mt4Order);
}
}

// Se un'operazione aperta su cTrader non è presente nel file MT4, la


chiude.
foreach (var position in openPositions)
{
if (![Link](o => [Link] == [Link]))
{
ClosePosition(position);
}
}
});
}
protected override void OnStop()
{
_timer?.Dispose();
}

private OrderData[] ReadOrdersFromFile(string path)


{
try
{
if (![Link](path))
{
Print("File non trovato: " + path);
return [Link]<OrderData>();
}

var lines = [Link](path);

return lines
.Where(line => ![Link](line))
.Select(line =>
{
var parts = [Link](',');
if ([Link] < 6)
{
Print("Riga non valida: " + line);
return null;
}

if (![Link](parts[1], out double volume) ||


![Link](parts[3], out double openPrice) ||
![Link](parts[4], out double stopLoss) ||
![Link](parts[5], out double takeProfit))
{
Print("Errore di parsing: " + line);
return null;
}

return new OrderData


{
Symbol = parts[0],
Volume = volume,
OrderType = parts[2],
OpenPrice = openPrice,
StopLoss = stopLoss,
TakeProfit = takeProfit,
Id = parts[0] + "_" + parts[3]
};
})
.Where(o => o != null)
.ToArray();
}
catch (Exception ex)
{
Print("Eccezione lettura file: " + [Link]);
return [Link]<OrderData>();
}
}

private bool IsOrderAlreadyOpen(OrderData mt4Order, Position[]


openPositions)
{
return [Link](pos => [Link] == [Link]);
}

private void ExecuteOrder(OrderData order)


{
TradeType tradeType = [Link] == "BUY" ? [Link] :
[Link];
ExecuteMarketOrder(tradeType, [Link], [Link], [Link],
[Link], [Link]);
}
}

public class OrderData


{
public string Id { get; set; }
public string Symbol { get; set; }
public double Volume { get; set; }
public string OrderType { get; set; }
public double OpenPrice { get; set; }
public double StopLoss { get; set; }
public double TakeProfit { get; set; }
}
}

You might also like