Why doens take Positions?
Why doens take Positions?
05 May 2018, 14:58
i have here this code, why it doesnt take positions?
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 MAandMA : Robot
{
[Parameter("Source1")]
public DataSeries Source1 { get; set; }
[Parameter("Source2")]
public DataSeries Source2 { get; set; }
[Parameter("MA1", DefaultValue = 3)]
public int Period { get; set; }
[Parameter("MA", DefaultValue = 10)]
public int Periods { get; set; }
[Parameter("Volume", DefaultValue = 1000)]
public int Volume { get; set; }
private MovingAverage firstMA1;
private MovingAverage doubleMA;
private const string label = "EMA";
private Position longPosition;
private Position shortPosition;
protected void Initialize()
{
doubleMA = Indicators.SimpleMovingAverage(Source2, Periods);
firstMA1 = Indicators.SimpleMovingAverage(Source1, Period);
}
protected override void OnStart()
{
// Put your initialization logic here
}
protected override void OnTick()
{
// Put your core logic here
}
protected override void OnBar()
{
var cBotPositions = Positions.FindAll(label);
longPosition = Positions.Find(label, Symbol, TradeType.Buy);
shortPosition = Positions.Find(label, Symbol, TradeType.Sell);
if (cBotPositions.Length >= 1)
return;
if (doubleMA.Result.Last(0) > firstMA1.Result.Last(0) && longPosition == null)
{
CP();
ExecuteMarketOrder(TradeType.Buy, Symbol, Volume);
}
if (doubleMA.Result.Last(0) < firstMA1.Result.Last(0) && shortPosition == null)
{
CP();
ExecuteMarketOrder(TradeType.Sell, Symbol, Volume);
}
}
private void CP()
{
foreach (var Position in Positions)
{
ClosePosition(Position, Volume);
}
}
protected override void OnStop()
{
// Put your deinitialization logic here
}
}
}
what is missing?
Thank you a lot.
carlosdrcunha
05 May 2018, 16:19
i need to wait for the next signal to open position and close position on the next inverse signal and open inverse position as well.
thank you a lot for your help.
@carlosdrcunha