Topics
Replies
cauedotnet
29 Sep 2017, 19:01
( Updated at: 21 Dec 2023, 09:20 )
RE: Here is the info you need
Sure I can.
I build a simple calgo bot, only so i can compare your backtesting with my spreadsheet backtesting. I will post all the code here.
My spreadsheet system produces the same trades, in the same moment (date/time), same comission, but the gross profit is sligly diferent, only by some dolars. But I need to understand why.
Here is the CBOT to test:
using System; using System.Linq; using cAlgo.API; using cAlgo.API.Requests; using cAlgo.Indicators; namespace cAlgo.Robots { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class SimpleBot : Robot { [Parameter("Initial Volume", DefaultValue = 10000, MinValue = 0)] public int InitialVolume { get; set; } [Parameter("Stop Loss", DefaultValue = 40)] public int StopLoss { get; set; } [Parameter("Take Profit", DefaultValue = 40)] public int TakeProfit { get; set; } private Random random = new Random(); private Position position; private string label = "simpleBot"; protected override void OnStart() { ExecuteOrder(InitialVolume, GetDiffTradeCommand()); this.Positions.Opened += Positions_Opened; this.Positions.Closed += Positions_Closed; } void Positions_Closed(PositionClosedEventArgs obj) { OnPositionClosed(obj.Position); } void Positions_Opened(PositionOpenedEventArgs obj) { OnPositionOpened(obj.Position); } protected override void OnStop() { Positions.Where(pos => pos.Label == label).ToList().ForEach(pos => { ClosePosition(pos); }); } private void ExecuteOrder(int volume, TradeType tradeType) { ExecuteMarketOrder(tradeType, Symbol, volume, label, StopLoss, TakeProfit); } protected override void OnPositionOpened(Position openedPosition) { position = openedPosition; } protected override void OnPositionClosed(Position closedPosition) { if (closedPosition.GrossProfit > 0) { ExecuteOrder(InitialVolume, position.TradeType); } else { ExecuteOrder((int)position.Volume * 2, GetDiffTradeCommand(position.TradeType)); } } protected override void OnError(Error error) { if (error.Code == ErrorCode.BadVolume) Stop(); } private TradeType GetDiffTradeCommand(TradeType type = TradeType.Sell) { return type == TradeType.Sell ? TradeType.Buy : TradeType.Sell; } } }
Here are the config and results:
My spreadsheet give me this result for the same test period.
First Trade:
- Entry Price 76.207
- Close Price 76.107
- Entry Time 02/02/2012 00:00
- Close Time 02/02/2012 06:30
- Commission -0.06
- Gross Profit -1.31
- Net Profit -1.37
- Vol 1000
SecondTrade:
- Entry Price 76.107
- Close Price 76.216
- Entry Time 02/02/2012 06:30
- Close Time 02/02/2012 19:46
- Commission -0.12
- Gross Profit -2.86
- Net Profit -2.98
- Vol 2000
Please help me how to figure out the gross profit formula. Me and my friend want to pass a lot of strategies from excel to cbot, but the formula need to match so we can choose the best strategies.
Thank you very much for your help.
@cauedotnet
cauedotnet
18 Sep 2017, 16:58
Sure I can. Here are my backtesting settings:
Data: Tick Data from Server (acurate)
Comission: 30 per million.
Starting Capital: 1000
But the comission is not only calculated on the Net Profit? In my example, the comission is 0.60. The Gross is $12.81, and the Net Profit was $12.21. So what I understand is that the diference between grofit and net is the comission here. But I really don't know how I got the Gross Profit.
Thanks.
@cauedotnet
cauedotnet
15 Jul 2017, 20:49
RE: Convert MQL4 to C#
I have been using http://mq4tocalgo.com/ to convert some experts and indicators. These converters appears to work on code that doesn't import other libraries. If the code is too complex, sometimes could have some errors on the generated c#. So it is good to learn cAlgo so you can fix any issue.
@cauedotnet
cauedotnet
02 Oct 2017, 19:04
My Broker
Hi Panagiotis.
I'm using FxPro.
As I said before, I'm trying to figure out what is the Gross Profit correct formula used by CTrader.
I was thinking that the formula was (entry/exit) * volume * (1/exit) => 76.207/76.107 * 100000 * 1/76.107 => -1.3156... But in CAlgo Results, the Gross was -1.00. I think my formula is wrong, maybe the last part is (1/pairRate).. But I don't know how to calculate this rate. How can I get it? Or maybe there is a deposit rate too. Im really lost in these calculations.
Thanks
@cauedotnet