Topics
Forum Topics not found
Replies
ap11
29 Jul 2020, 15:11
Run following code in Backtesting using Tick Data from Server
You will get .csv file with tick data history.
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FileSystem)]
public class NewcBot : Robot
{
private StringBuilder sb = new StringBuilder();
protected override void OnTick()
{
sb.AppendLine(string.Format("{0},{1},{2}", Time.ToString("dd.MM.yyyy HH:mm:ss.fff"), Bid, Ask));
}
protected override void OnStop()
{
var filePath = string.Format("c:\\temp\\{0}.csv", SymbolName);
File.WriteAllText(filePath, sb.ToString());
}
}
@ap11
ap11
20 Apr 2019, 11:29
( Updated at: 21 Dec 2023, 09:21 )
In cTrader version 3.6 we will add a new feature called Chart Controls. Developers will be able to add UI elements on a chart like text labels, buttons, input fields, etc.
This version will be released to Spotware Public Beta in a couple of weeks.
Using these UI controls it will be possible to create trading panels that will work in Visual Backtesting:
You can see this example in action here:
https://www.dropbox.com/s/tg7oje9lc6bo5r9/ChartControls.mp4?dl=0
@ap11
ap11
20 Apr 2019, 11:17
Hi,
Testing manual strategies on historical data is not supported out of the box. But you can use Visual Backtesting for this. cBot can use user inputs to open/close positions or place pending orders.
cBot can get user input from UI controls created in another window as in product Ahmad suggested. Or use some built-in cTrader events.
For now, cTrader support mouse events on the chart so you can create a cBot with trading commands using the mouse clicks. Below is an example of a cBot that uses
Shift + Left Mouse Button to open a buy position.
Ctrl + LMB to open a sell position.
Alt + LMB to close all positions.
[Parameter(DefaultValue = 0.1)] public double Lots { get; set; } [Parameter("StopLoss (pips)", DefaultValue = 20)] public double StopLossPips { get; set; } [Parameter("StopLoss (pips)", DefaultValue = 20)] public double TakeProfitPips { get; set; } protected override void OnStart() { var instructionsText = "Shift + LMB = Buy\nCtrl + LMB = Sell\nAlt + LMB = Close All"; Chart.DrawStaticText("instructions", instructionsText, VerticalAlignment.Top, HorizontalAlignment.Left, Chart.ColorSettings.ForegroundColor); Chart.MouseDown += OnChartMouseDown; } private void OnChartMouseDown(ChartMouseEventArgs obj) { if (obj.ShiftKey && !obj.CtrlKey && !obj.AltKey) ExecuteMarketOrder(TradeType.Buy, Symbol, Lots * Symbol.LotSize, "MouseTrading", StopLossPips, TakeProfitPips); else if (!obj.ShiftKey && obj.CtrlKey && !obj.AltKey) ExecuteMarketOrder(TradeType.Sell, Symbol, Lots * Symbol.LotSize, "MouseTrading", StopLossPips, TakeProfitPips); else if (!obj.ShiftKey && !obj.CtrlKey && obj.AltKey) Positions.ToList().ForEach(p => ClosePositionAsync(p)); }
You can see it in action on this video:
https://www.dropbox.com/s/asdijotjnr7tl02/MouseTrading.mp4?dl=0
@ap11
ap11
22 Feb 2019, 17:49
Dear Mario and Conno123,
cTrader works in different threads. The main UI thread coordinates all the work.
If you have a lot of UI objects, like a deal map with a lot of historical trades, a lot of positions and pending orders on the chart, a lot chart objects drawn by indicators or cBots, it all might cause UI freezing and unresponsive behavior.
All of these objects are controlled by a user. Items on the chart can be hidden using the Viewing Options of a chart. If you experience any issues you can experiment with that.
cBots and indicators work in different threads than the UI thread but still coordinated by the main thread.
If you want to use multithreading in a single cBot or Indicator, you can do it. cAlgo API is not thread safe, but you can execute your code in another thread. You only have to remember that API method calls should be made from the main cBot/indicator thread. You can do this by wrapping a call into BeginInvokeOnMainThread method.
We are constantly working on increasing cTrader performance. Last year we have seriously modified our in house performance testing procedure. Many scenarios involving heavy usage of cTrader were not included before but now they are a part of the testing process. Version 3.3 and all upcoming versions include some performance improvements.
If you have a specific scenario of poor performance with steps to reproduce, it would be nice if you share it with us.
Best Regards,
Andrey
@ap11
ap11
13 Sep 2018, 09:57
( Updated at: 21 Dec 2023, 09:20 )
Hi .ics,
Timeout error happens when you send a request and client app doesn't receive a response from the server. It can be a connection issue or might be caused by other errors.
Can you please check the details for this position:
22/08/2018 20:03:10.945 | Closing position PID47436055
22/08/2018 20:04:51.273 | → Closing position PID47436055 FAILED with error "Timeout", Position PID47436055
Go to History tab and find the closing deal for this position. Open details using the info button and click Positions Details button.
There you will see a history of this position.
When a position is opened, you will have: Order -> Opening Deal -> Position
When it's closed: Order -> Closing Deal -> Position Closed
You can compare closing order time with your logs. And see when it was actually closed.
Next time when you see it, please send troubleshooting information using Ctrl+Shift+Alt+T. And add a link to this post in the description.
Kind Regards,
Andrey
@ap11
ap11
11 Sep 2018, 18:03
RE:
ctid612239 said:
Hi,
Thanks for the answer, is there any other way to get the information without using the cTrader interface?
Thanks!
You can use Open API. Here you can find documentation and examples:
https://connect.spotware.com/
Kind Regards,
Andrey
@ap11
ap11
27 Jul 2018, 11:10
Hi Lavio,
This reason for the error is the conversion between robot time zone and UTC. When a robot has a time zone with daylight saving time, conversion to UTC might fail.
We are investigating this issue. The easy fix, for now, is to switch robot to UTC time zone:
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class NewcBot : Robot
You can help us with some details:
- Symbol and timeframe
- Backtesting start and end dates
- Source data (ticks, m1 bars or selected timeframe bars)
- If you can provide cBot, please send it to community@spotware.com. If no, it would be useful to hear from you what cBot uses (timer, order types, indicators etc.)
Thank you in advance,
Andrey
@ap11
ap11
25 Jul 2018, 12:55
( Updated at: 21 Dec 2023, 09:20 )
Hi ,
There is a LinesTrader cBot demonstrates new API added in version 3.01. Currently, you can use it only in Spotware cTrader (Public beta).
https://ctrader.com/algos/cbots/show/1774
Regards,
Andrei
@ap11
ap11
12 Jul 2018, 16:12
Hi Ceakuk,
In ModifyPosition you should specify desired volume you wish to have after position modification.
Here is an example on how to change volume from 20k to 10k:
var position = ExecuteMarketOrder(TradeType.Buy, Symbol, 20000).Position; ModifyPosition(position, 10000);
Regards,
Andrey
@ap11
ap11
12 Jul 2018, 16:03
Hi Julioc1010,
Pending order Take Profit is set in pips. And in your case, it is equal to 200 pips.
Probably order was executed at 0.9849 and Take Profit was placed at 0.9649 - 200 pips away from the entry price.
Please check if it is the case.
P.S.
If you don't want your order to be executed on gaps, away from target price, you can use StopLimit orders.
Regards,
Andrey
@ap11
ap11
12 Jul 2018, 15:43
Hi Thongbaogiaodich,
You can use PendingOrders.Cancelled event. Event arguments have a property called Reason. And in case it will be 'Rejected'
Here is the example:
protected override void OnStart() { PendingOrders.Cancelled += PendingOrders_Cancelled; PlaceStopOrder(TradeType.Buy, Symbol, Symbol.VolumeInUnitsMax, Symbol.Ask); } private void PendingOrders_Cancelled(PendingOrderCancelledEventArgs obj) { var ord = obj.PendingOrder; Print("{0} order {1} {2} at {3} was cancelled, reason: {4}", ord.OrderType, ord.TradeType, ord.SymbolCode, ord.TargetPrice, obj.Reason); }
It will output following log entry:
Stop order Buy EURUSD at 1.16634 was cancelled, reason: Rejected
Regards,
Andrey
@ap11
ap11
09 Jul 2018, 15:57
Hi Zedodia,
It seems like a very old code. Here is the list of changes required to switch code from deprecated API
1. Move from OnPositionOpened and OnPositionClosed overrides to Positions events
2. Replace Trade.CreateBuyStopOrder and Trade.CreateSellStopOrder to PlaceStopOrder methods. You will need to add TradeType as first parameter and label before StopLoss
3. Replace Trade.ModifyPosition to ModifyPosition
4. Change Vol parameter type from int to double
5. Replace usage of Symbol.PointSize to Symbol.TickSize
See results below:
[Parameter("Vol", DefaultValue = 10000)] public double Vol { get; set; } [Parameter("SL", DefaultValue = 200)] public int SL { get; set; } [Parameter("TP", DefaultValue = 200)] public int TP { get; set; } [Parameter("Dev", DefaultValue = 200)] public int Dev { get; set; } [Parameter("N", DefaultValue = 5)] public int N { get; set; } private Position position1, pos1; protected override void OnStart() { Print("Welcome to the world of infinite financial possibilities!"); Positions.Opened += Positions_Opened; Positions.Closed += Positions_Closed; double Bid = Symbol.Bid; double Ask = Symbol.Ask; double Point = Symbol.TickSize; for (int i = 1; i <= N; i++) PlaceStopOrder(TradeType.Buy, Symbol, Vol, Ask + Dev * i * Point, null, 0, 0); for (int i = 1; i <= N; i++) PlaceStopOrder(TradeType.Sell, Symbol, Vol, Bid - Dev * i * Point, null, 0, 0); } private void Positions_Opened(PositionOpenedEventArgs obj) { Position openedPosition = obj.Position; position1 = openedPosition; if (position1.TradeType == TradeType.Buy) { PlaceStopOrder(TradeType.Buy, Symbol, Vol, position1.EntryPrice + Dev * N * Symbol.TickSize, null, 0, 0); ModifyPosition(openedPosition, position1.EntryPrice - SL * Symbol.TickSize, position1.EntryPrice + TP * Symbol.TickSize); Print("StopLoss and TakeProfit were successfully established"); } if (position1.TradeType == TradeType.Sell) { PlaceStopOrder(TradeType.Sell, Symbol, Vol, position1.EntryPrice - Dev * N * Symbol.TickSize, null, 0, 0); ModifyPosition(openedPosition, position1.EntryPrice + SL * Symbol.TickSize, position1.EntryPrice - TP * Symbol.TickSize); Print("StopLoss and TakeProfit were successfully established"); } } private void Positions_Closed(PositionClosedEventArgs obj) { pos1 = obj.Position; if (pos1.TradeType == TradeType.Buy) { if (pos1.GrossProfit > 0) PlaceStopOrder(TradeType.Sell, Symbol, Vol, pos1.EntryPrice, null, 0, 0); if (pos1.GrossProfit < 0) PlaceStopOrder(TradeType.Buy, Symbol, Vol, pos1.EntryPrice, null, 0, 0); } if (pos1.TradeType == TradeType.Sell) { if (pos1.GrossProfit > 0) PlaceStopOrder(TradeType.Buy, Symbol, Vol, pos1.EntryPrice, null, 0, 0); if (pos1.GrossProfit < 0) PlaceStopOrder(TradeType.Sell, Symbol, Vol, pos1.EntryPrice, null, 0, 0); } } protected override void OnStop() { Print("Successful day!"); }
Regards,
Andrey
@ap11
ap11
30 Jul 2020, 16:22
Hi Koktos632,
Can you please confirm that these are cTrader apps from the same broker. Which broker are you using?
@ap11