Topics
Replies
davidbtosh
29 May 2016, 08:34
RE:
lucian said:
cBot something like this:
using System; using System.Linq; using cAlgo.API; using System.IO; using System.Collections; using System.Collections.Generic; using cAlgo.API.Indicators; using cAlgo.API.Internals; using cAlgo.Indicators; namespace cAlgo { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)] public class msforex : Robot { protected override void OnStart() { } protected override void OnTick() { if (!this.IsBacktesting) DisplayStatusOnChart(); } protected override void OnBar() { } private string GenerateStatusText() { var statusText = ""; var simbol_code = ""; var simbol_profit = ""; var simbol_positions = ""; var simbol_day_profit = ""; var day_profit = ""; simbol_code = Symbol.Code; simbol_positions = "\nSymbol Positions" + simbol_code + " = " + CountOfTrades(); simbol_profit = "\nSymbol Profit = " + profit_symbol(); simbol_day_profit = "\nSymbol Day Profi = " + day_profit_symbol(); day_profit = "\nDay Profit = " + daily_profit(); statusText = simbol_positions + simbol_profit + simbol_day_profit + day_profit; return (statusText); } private int CountOfTrades() { var tradeCount = 0; foreach (var position in Positions) { if (position.SymbolCode == Symbol.Code) { tradeCount++; } } return tradeCount; } private double profit_symbol() { double profit = 0; foreach (var position in Positions) { if (position.SymbolCode == Symbol.Code) { profit += position.NetProfit; } } return profit; } private double day_profit_symbol() { double profit = 0; foreach (HistoricalTrade trade in History) { if ((trade.SymbolCode == Symbol.Code) && (trade.ClosingTime.Date == Server.Time.Date)) { profit += trade.NetProfit; } } return profit; } private double daily_profit() { double profit = 0; foreach (HistoricalTrade trade in History) { if (trade.ClosingTime.Date == Server.Time.Date) { profit += trade.NetProfit; } } return profit; } private void DisplayStatusOnChart() { ChartObjects.DrawText("pan", GenerateStatusText(), StaticPosition.TopRight, Colors.Aqua); } } }You can also use the LINQ library to reduce the amount of code to maintain by doing something like this:
var simbol_profit = string.Format("\nSymbol Profit = {0}", Positions.Where(pos => pos.SymbolCode == Symbol.Code).Sum(s => s.NetProfit));This uses the LINQ where and Sum extension methods to do exactly the same thing as the custom method.
@davidbtosh
davidbtosh
10 Mar 2016, 14:24
RE:
This looks better
private const string botName = "MyBotName"; //these could be parameters private const int positionsLimit = 3; private TradeResult ExecuteMarketOrderLimited(TradeType tt, long vol, double? slPips, double? tpPips) { TradeResult result = null; try { List posList = Positions.FindAll(botName).ToList(); Position pos = null; if (posList.Count < positionsLimit) { result = ExecuteMarketOrder(tt, Symbol, vol, botName, slPips, tpPips); if (result.IsSuccessful) { pos = result.Position; Print("Position entry price is {0}", pos.EntryPrice); } else { Print("Execution unsuccessful."); } } else { Print("Set position limit of {0} reached. Execution unsuccesful.", positionsLimit); } } catch (Exception e) { //handle error //log //exit Print("Error: {0}", e.Message); } return result; }
@davidbtosh
davidbtosh
10 Mar 2016, 14:22
Here is a quick example of how you might do this. You need to put the cAlgo API ExecuteMarketOrder() method in a "wrapper"
You then only ever call the wrapper method. Here is my implementation of a wrapper method for this
private const string botName = "MyBotName"; //these could be parameters
private const int positionsLimit = 3;
private TradeResult ExecuteMarketOrderLimited(TradeType tt, long vol, double? slPips, double? tpPips)
{
TradeResult result = null;
try
{
List<Position> posList = Positions.FindAll(botName).ToList();
Position pos = null;
if (posList.Count < positionsLimit)
{
result = ExecuteMarketOrder(tt, Symbol, vol, botName, slPips, tpPips);
if (result.IsSuccessful)
{
pos = result.Position;
Print("Position entry price is {0}", pos.EntryPrice);
}
else
{
Print("Execution unsuccessful.");
}
}
else
{
Print("Set position limit of {0} reached. Execution unsuccesful.", positionsLimit);
}
}
catch (Exception e)
{
//handle error
//log
//exit
Print("Error: {0}", e.Message);
}
return result;
}
@davidbtosh
davidbtosh
29 May 2016, 08:35
RE: RE:
ctid201011 said:
@davidbtosh