how to replace public string to enum function?
how to replace public string to enum function?
18 Feb 2020, 10:38
i need help on how to use drop down option as a label or instance name. i tried
public Instance InstanceName {get; set;}
but too many errors appears.
public enum Instance { Fiber_EU, Cable_GU, Aussei_AU, Ninja_UJ, Yuppy_EJ, Guppy_GJ, Swissie_UF, Loonie_UC }
[Parameter("Instance Name")]
public string InstanceName { get; set; }
Replies
traderfxmaster007
18 Feb 2020, 18:12
RE:
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
namespace cAlgo
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class LEXtrend : Robot
{
#region User defined parameters
public enum Instance { Fiber_EU, Cable_GU, Aussei_AU, Ninja_UJ, Yuppy_EJ, Guppy_GJ, Swissie_UF, Loonie_UC }
[Parameter("Instance Name", DefaultValue = " Fiber_EU")]
public Instance InstanceName { get; set; }
[Parameter("LEX Period", DefaultValue = 14, MinValue = 1, MaxValue = 25)]
public int Period { get; set; }
[Parameter("LEX Threshold", DefaultValue = 17, MinValue = 15, MaxValue = 60)]
public int Threshold { get; set; }
[Parameter("Use Money Management ?", Group = "Money Management", DefaultValue = true)]
public bool volPercentBool { get; set; }
[Parameter("Risk %", Group = "Money Management", DefaultValue = 2, MinValue = 1, Step = 1)]
public int volPercent { get; set; }
[Parameter("Volume Quantity", Group = "Money Management", DefaultValue = 2000, MinValue = 1000, Step = 1000)]
public int volQty { get; set; }
[Parameter("StopLoss Pips", Group = "Protection", DefaultValue = 30.0, Step = 1.0)]
public double StopLoss { get; set; }
[Parameter ("BreakEvenTrigger Pips", Group = "Protection", DefaultValue = 30, MinValue = 1)]
public double TriggerPips { get; set; }
[Parameter ("Locked in Profit", Group = "Protection", DefaultValue = 3.0, MinValue = 0.0)]
public double AddPips { get; set; }
[Parameter("Allowable Slippage", Group = "Filter", DefaultValue = 1.0, MinValue = 0.5, Step = 0.1)]
public double marketRangePips { get; set; }
[Parameter("Max Allowable Spread", Group = "Filter", DefaultValue = 2.0, MinValue = 0.1, MaxValue = 5.0)]
public double MaxSpread { get; set; }
[Parameter("Calculate OnBar ?", DefaultValue = true)]
public bool CalculateOnBar { get; set; }
#endregion
#region Indicator declarations
private int volume;
private IchimokuKinkoHyo Ichimoku;
private DirectionalMovementSystem dms;
private string Comment;
#endregion
#region Calculate Volume
private int CalculateVolume(double stopLossPips)
{
int result;
switch (volPercentBool)
{
case true:
double costPerPip = (double)((int)(Symbol.PipValue * 10000000)) / 100;
double posSizeForRisk = (Account.Balance * volPercent / 100) / (stopLossPips * costPerPip);
double posSizeToVol = (Math.Round(posSizeForRisk, 2) * 100000);
Print("costperppip = {0}, posSizeFoprRisk = {1}, posSizeLotsToVol = {2}", costPerPip, posSizeForRisk, posSizeToVol);
result = (int)Symbol.NormalizeVolumeInUnits(posSizeToVol, RoundingMode.ToNearest);
result = result > 150000 ? 150000 : result;
Print("{0}% of Account Balance used for Volume! Volume equals {1}", volPercent, result);
break;
default:
result = volQty;
Print("Volume Quantity Used! Volume equals {0}", result);
break;
}
return result;
}
#endregion
#region Standard event handlers
/// This is called when the robot first starts, it is only called once.
protected override void OnStart()
{
Ichimoku= Indicators.IchimokuKinkoHyo(9, 26, 52);
dms = Indicators.DirectionalMovementSystem (Period);
volume = CalculateVolume(StopLoss);
Comment = "Christopher G’Neil" ;
}
/// This event handler is called every tick or every time the price changes for the symbol.
protected override void OnTick()
{
if (CalculateOnBar)
{return;}
ManagePositions();
var positions = Positions.FindAll(InstanceName, SymbolName);
foreach (var position in positions)
if (position.Pips < TriggerPips)
{return; }
BreakEvenIfNeeded();
}
/// a special event handler that is called each time a new bar is drawn on chart.
/// if you want your robot to act only when the previous bar is closed, this standard handler is where you put your main trading code.
protected override void OnBar()
{
if (!CalculateOnBar)
{return;}
ManagePositions();
var positions = Positions.FindAll(InstanceName, SymbolName);
foreach (var position in positions)
if (position.Pips < TriggerPips)
{return; }
BreakEvenIfNeeded();
}
/// a handler that is called on stopping the cBot.
protected override void OnStop()
{
// unused
}
/// a special Robot class member that handles situations with errors.
protected override void OnError(Error error)
{Print("Error Code {0}", error.Code);}
#endregion
#region Position management
private void ManagePositions()
{
/// if there is no buy position open, open one and close any sell position that is open
if (!IsPositionOpenByType(TradeType.Buy))
{
if (Symbol.Spread < MaxSpread &&
Bars.ClosePrices.LastValue > Bars.OpenPrices.LastValue && Bars.ClosePrices.LastValue > Ichimoku.KijunSen.LastValue && dms.DIPlus.LastValue > dms.DIMinus.LastValue && dms.ADX.LastValue > Threshold)
{ OpenPosition(TradeType.Buy); }
if (Bars.ClosePrices.Last(2) > Bars.OpenPrices.Last(2) && Bars.ClosePrices.Last(1) > Bars.OpenPrices.Last(1))
{ ClosePosition(TradeType.Sell); }
}
/// if there is no sell position open, open one and close any buy position that is open
if (!IsPositionOpenByType(TradeType.Sell))
{
if (Symbol.Spread < MaxSpread &&
Bars.ClosePrices.LastValue < Bars.OpenPrices.LastValue && Bars.ClosePrices.LastValue < Ichimoku.KijunSen.LastValue && dms.DIPlus.LastValue < dms.DIMinus.LastValue && dms.ADX.LastValue > Threshold)
{ OpenPosition(TradeType.Sell); }
if (Bars.ClosePrices.Last(2) < Bars.OpenPrices.Last(2) && Bars.ClosePrices.Last(1) < Bars.OpenPrices.Last(1))
{ ClosePosition(TradeType.Buy); }
}
}
/// Call custom class method to move StopLoss to BreakEven
private void BreakEvenIfNeeded()
{
var positions = Positions.FindAll(InstanceName, SymbolName);
foreach (var position in positions)
{
var desiredNetProfitInDepositAsset = AddPips * Symbol.PipValue* position.VolumeInUnits;
var desiredGrossProfitInDepositAsset = desiredNetProfitInDepositAsset - position.Commissions *2 - position.Swap;
var quoteToDepositRate = Symbol.PipValue / Symbol.PipSize;
var priceDifference = desiredGrossProfitInDepositAsset / (position.VolumeInUnits * quoteToDepositRate);
var priceAdjustment = GetPriceAdjustmentByTradeType(position.TradeType, priceDifference);
var breakEvenLevel = position.EntryPrice + priceAdjustment;
var roundedBreakEvenLevel = RoundPrice(breakEvenLevel, position.TradeType);
ModifyPosition(position, roundedBreakEvenLevel, position.TakeProfit);
}
}
/// Call custom class method to send a market order || open a new position
private void OpenPosition(TradeType type)
{ExecuteMarketRangeOrder(type, this.Symbol.Name, volume, marketRangePips, Symbol.Bid, InstanceName, StopLoss, null, Comment);}
/// Standard event handler that triggers upon position closing.
private void ClosePosition(TradeType type)
{
var p = Positions.Find(InstanceName, SymbolName, type);
if (p != null)
{ClosePosition(p);}
}
/// Check for opened position
private bool IsPositionOpenByType(TradeType type)
{
var p = Positions.FindAll(InstanceName, SymbolName, type);
if (p.Count() >= 1)
{return true;}
return false;
}
private double RoundPrice(double price, TradeType tradeType)
{
var multiplier = Math.Pow(10, Symbol.Digits);
if (tradeType == TradeType.Buy)
return Math.Ceiling(price * multiplier) / multiplier;
return Math.Floor(price * multiplier) / multiplier;
}
private static double GetPriceAdjustmentByTradeType(TradeType tradeType, double priceDifference)
{
if (tradeType == TradeType.Buy)
return priceDifference;
return - priceDifference;
}
#endregion
}
}
hi thanks for the response. here i attached the Cbot.
@traderfxmaster007
PanagiotisCharalampous
19 Feb 2020, 08:20
Hi traderfxmaster007,
You are using InstanceName as a string but it is not. Use it as the example below
Positions.FindAll(InstanceName.ToString(), SymbolName)
Best Regards,
Panagiotis
@PanagiotisCharalampous
traderfxmaster007
19 Feb 2020, 09:50
Great, now it works the way I wanted. Thanks to you Sir Panos your the best.
@traderfxmaster007
PanagiotisCharalampous
18 Feb 2020, 10:43
Hi traderfxmaster007,
It is not clear what is the issue. Can you please post the complete cBot code so that we can reproduce the error messages you are receiving?
Best Regards,
Panagiotis
Join us on Telegram
@PanagiotisCharalampous