Method to find open positions
Method to find open positions
08 Oct 2018, 18:15
Dear Pangiotis!
I am trying to build a method to find open positions.
private void FindPositions()
{
longPositions = Positions.FindAll(instance, Symbol, TradeType.Buy);
shortPositions = Positions.FindAll(instance, Symbol, TradeType.Sell);
}
Obviously, it does'not work
it says "Explicit conversion exists. Are you missing a cast?"
Please, help
Regards,
Sasha
Replies
alexander.n.fedorov
09 Oct 2018, 10:20
Hi, Panagiotis!
I guess it is :
private positions[] longPositions, shortPositions;
"
Should there be a different type?
Regards,
Sasha
@alexander.n.fedorov
PanagiotisCharalampous
09 Oct 2018, 10:24
Hi Sasha,
That is correct. I do not get an error using that type. Can you share the full code so that I can reproduce?
Best Regards,
Panagiotis
@PanagiotisCharalampous
alexander.n.fedorov
09 Oct 2018, 10:29
Here it is:
"
using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.RussianStandardTime, AccessRights = AccessRights.FullAccess)]
public class FAN32 : Robot
{
[Parameter("Source")]
public DataSeries Source { get; set; }
[Parameter("Minimum Equity", DefaultValue = 1500, MinValue = 500, Step = 100)]
public double MinEquity { get; set; }
[Parameter("BB Periods", DefaultValue = 40)]
public int BbPeriods { get; set; }
[Parameter("BB Deviations", DefaultValue = 2.0)]
public double BbDeviations { get; set; }
[Parameter("Grid Stiep in Pips", DefaultValue = 20)]
public double GridStepInPips { get; set; }
[Parameter("Max Martingales", DefaultValue = 6)]
public int MaxMartingales { get; set; }
[Parameter("Close on Drawdonw %", DefaultValue = 35, Step = 1, MinValue = 5, MaxValue = 50)]
public double DrawDownClose { get; set; }
[Parameter("Close on Profit %", DefaultValue = 1, Step = 1, MaxValue = 50)]
public double ProfitClose { get; set; }
[Parameter("Close GrigStep %", DefaultValue = 50, Step = 5, MaxValue = 100)]
public double CloseGridStepPips { get; set; }
[Parameter("Stop Trading Hour", DefaultValue = 23, Step = 1)]
public int StopHour { get; set; }
[Parameter("Stop Trading minutes", DefaultValue = 45, Step = 1)]
public int StopMinutes { get; set; }
[Parameter("Open Trades Hour", DefaultValue = 2, Step = 1, MinValue = 0)]
public int OpenTradesHour { get; set; }
private const int maSlowPeriods = 200;
private MovingAverage maSlow;
private const double minVolume = 1000;
private const int exponent = 2;
private string instance;
private BollingerBands bb;
private double volume, profit, equity;
private double gridStep, orderVolume;
private positons[] longPositions, shortPositions;
protected override void OnStart()
{
instance = ToString() + ", " + Symbol.Code + ", " + TimeFrame + ", " + Account.BrokerName + ", " + Account.Number;
bb = Indicators.BollingerBands(Source, BbPeriods, BbDeviations, MovingAverageType.Simple);
gridStep = GridStepInPips * Symbol.PipSize;
maSlow = Indicators.MovingAverage(Source, maSlowPeriods, MovingAverageType.Simple);
}
protected override void OnBar()
{
if ((MarketSeries.OpenTime.Last(0).Hour >= StopHour && MarketSeries.OpenTime.Last(0).Minute >= StopMinutes) || (MarketSeries.OpenTime.Last(0).Hour <= OpenTradesHour))
{
return;
}
TradeManagement();
if (volume == 0)
{
return;
}
var longPositions = Positions.FindAll(instance, Symbol, TradeType.Buy);
var shortPositions = Positions.FindAll(instance, Symbol, TradeType.Sell);
var allPositions = Positions.FindAll(instance);
if (shortPositions.Count() < MaxMartingales && longPositions.Count() < MaxMartingales)
{
#region Open Positions
#region short entries
if (MarketSeries.Close.Last(2) > bb.Top.Last(2))
{
if (MarketSeries.Close.Last(1) < bb.Top.Last(1) && MarketSeries.Low.Last(1) > bb.Main.Last(1))
{
if ((shortPositions.Count() == 0) || (shortPositions.Count() > 0 && MarketSeries.Close.Last(1) > ShortPrice() + gridStep))
{
orderVolume = volume * Math.Pow(exponent, shortPositions.Count());
ExecuteMarketOrder(TradeType.Sell, Symbol, orderVolume, instance);
}
}
}
#endregion
#region long entries
if (MarketSeries.Close.Last(2) < bb.Bottom.Last(2))
{
if (MarketSeries.Close.Last(1) > bb.Bottom.Last(1) && MarketSeries.High.Last(1) < bb.Main.Last(1))
{
if (longPositions.Count() == 0)
{
orderVolume = volume * Math.Pow(exponent, longPositions.Count());
ExecuteMarketOrder(TradeType.Buy, Symbol, orderVolume, instance);
}
if (longPositions.Count() > 0 && MarketSeries.Close.Last(1) < LongPrice() - gridStep)
{
orderVolume = volume * Math.Pow(exponent, longPositions.Count());
ExecuteMarketOrder(TradeType.Buy, Symbol, orderVolume, instance);
}
}
}
#endregion
#endregion
#region close on opposite side of the river if all the positions are same Trade Type.
longPositions = Positions.FindAll(instance, Symbol, TradeType.Buy);
shortPositions = Positions.FindAll(instance, Symbol, TradeType.Sell);
allPositions = Positions.FindAll(instance);
#region close shorts
if (longPositions.Count() == 0 && shortPositions.Count() > 0)
{
if (MarketSeries.Low.Last(1) <= bb.Bottom.Last(1))
{
foreach (var position in shortPositions)
{
if (position.NetProfit > 0)
{
ClosePosition(position);
}
}
}
}
#endregion
#region close longs
if (longPositions.Count() > 0 && shortPositions.Count() == 0)
{
if (MarketSeries.High.Last(1) >= bb.Top.Last(1))
{
foreach (var position in longPositions)
{
if (position.NetProfit > 0)
{
ClosePosition(position);
}
}
}
}
#endregion
#endregion
#region close two positevely locked positions
if (longPositions.Count() == 1 && shortPositions.Count() == 1)
{
profit = 0;
foreach (var position in Positions.FindAll(instance))
{
profit += position.NetProfit;
}
if (profit > 0)
{
foreach (var position in Positions.FindAll(instance))
{
ClosePosition(position);
}
}
}
#endregion
#region close bunch of same positons in Profit
longPositions = Positions.FindAll(instance, Symbol, TradeType.Buy);
shortPositions = Positions.FindAll(instance, Symbol, TradeType.Sell);
#region close shorts
if (shortPositions.Count() > 1 && longPositions.Count() == 0)
{
profit = 0;
foreach (var position in shortPositions)
{
profit += position.NetProfit;
}
if (profit >= 0)
{
foreach (var position in shortPositions)
{
ClosePosition(position);
}
}
}
#endregion
#region close longs
if (longPositions.Count() > 1 && shortPositions.Count() == 0)
{
profit = 0;
foreach (var position in longPositions)
{
profit += position.NetProfit;
}
if (profit >= 0)
{
foreach (var position in longPositions)
{
ClosePosition(position);
}
}
}
#endregion
#endregion
#region close various positions on profit
longPositions = Positions.FindAll(instance, Symbol, TradeType.Buy);
shortPositions = Positions.FindAll(instance, Symbol, TradeType.Sell);
if (longPositions.Count() > 0 & shortPositions.Count() > 0)
{
profit = 0;
foreach (var position in Positions.FindAll(instance))
{
profit += position.NetProfit;
}
if (profit > 0)
{
foreach (var position in Positions.FindAll(instance))
{
ClosePosition(position);
}
}
}
#endregion
#region Close single position in pips profit
allPositions = Positions.FindAll(instance);
if (allPositions.Count() == 1)
{
var position = (Positions.First(x => x.Label == instance));
if (position.Pips > GridStepInPips * CloseGridStepPips * 0.01)
{
ClosePosition(position);
}
}
#endregion
}
//longPositions = Positions.FindAll(instance, Symbol, TradeType.Buy);
//shortPositions = Positions.FindAll(instance, Symbol, TradeType.Sell);
//if (longPositions.Count() >= MaxMartingales)
//{
// if (bb.Main.Last(1) > maSlow.Result.Last(1))
// {
// if (MarketSeries.Close.Last(2) < bb.Bottom.Last(1))
// {
// if (MarketSeries.Close.Last(1) > bb.Bottom.Last(1) && MarketSeries.High.Last(1) < bb.Main.Last(1))
// {
// orderVolume = volume * Math.Pow(exponent, longPositions.Count());
// ExecuteMarketOrder(TradeType.Buy, Symbol, orderVolume, instance);
// }
// }
// }
//}
//if (shortPositions.Count() >= MaxMartingales)
//{
// if (bb.Main.Last(1) < maSlow.Result.Last(1))
// {
// if (MarketSeries.Close.Last(2) > bb.Top.Last(2))
// {
// if (MarketSeries.Close.Last(1) < bb.Top.Last(1) && MarketSeries.High.Last(1) > bb.Main.Last(1))
// {
// orderVolume = volume * Math.Pow(exponent, longPositions.Count());
// ExecuteMarketOrder(TradeType.Sell, Symbol, orderVolume, instance);
// }
// }
// }
//}
}
#region tradeManagement
private void TradeManagement()
{
var totalPositions = Positions.FindAll(instance);
if (totalPositions.Count() == 0)
{
equity = Account.Equity;
var newVolume = Math.Floor(equity / MinEquity) * minVolume;
volume = (volume > newVolume ? volume : newVolume);
return;
}
if (totalPositions.Count() > 0)
{
profit = 0;
foreach (var position in Positions.FindAll(instance))
{
profit = profit + position.NetProfit;
}
if (profit > equity * ProfitClose / 100 || profit < -equity * DrawDownClose / 100)
{
foreach (var position in Positions.FindAll(instance))
{
ClosePosition(position);
}
Print("closed all, Profit = {0}", profit);
equity = Account.Equity;
}
}
}
#endregion
#region LongPrice, ShortPrice
private double LongPrice()
{
var longPositions = Positions.FindAll(instance, Symbol, TradeType.Buy);
return longPositions.OrderByDescending(x => x.EntryPrice).Last().EntryPrice;
}
private double ShortPrice()
{
var shortPositions = Positions.FindAll(instance, Symbol, TradeType.Sell);
return shortPositions.OrderByDescending(x => x.EntryPrice).First().EntryPrice;
}
#endregion
private void FindPositions()
{
longPositions = Positions.FindAll(instance, Symbol, TradeType.Buy);
shortPositions = Positions.FindAll(instance, Symbol, TradeType.Sell);
}
}
}
"
@alexander.n.fedorov
PanagiotisCharalampous
09 Oct 2018, 10:31
Check line 57. You wrote positons instead of Position.
@PanagiotisCharalampous
alexander.n.fedorov
09 Oct 2018, 10:54
It does work
I would like to discuss this one in details with you
I'll clean it up send you.
Regards
Sasha
@alexander.n.fedorov
PanagiotisCharalampous
09 Oct 2018, 09:50
Hi Sasha,
You did not share the declaration of longPositions and shortPositions. What is their type?
Best Regards,
Panagiotis
@PanagiotisCharalampous