need help
need help
04 Jun 2018, 12:25
Hello good day
I would like to ask help i am learning to code.
I want to execute sell order when price close above upper band of Bollinger
and buy order when price close under lower band of bollinger
my problem is i cannot execute Close previous open position before oppening a new one.
// -------------------------------------------------------------------------------------------------
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 ambotlagi : Robot
{
[Parameter("Source")]
public DataSeries Source { get; set; }
[Parameter("Band Height (pips)", DefaultValue = 40.0, MinValue = 0)]
public double BandHeightPips { get; set; }
[Parameter("Stop Loss (pips)", DefaultValue = 20, MinValue = 1)]
public int StopLossInPips { get; set; }
[Parameter("Take Profit (pips)", DefaultValue = 40, MinValue = 1)]
public int TakeProfitInPips { get; set; }
[Parameter("Quantity (Lots)", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
public double Quantity { get; set; }
[Parameter("Bollinger Bands Deviations", DefaultValue = 2)]
public double Deviations { get; set; }
[Parameter("Bollinger Bands Periods", DefaultValue = 20)]
public int Periods { get; set; }
[Parameter("Bollinger Bands MA Type")]
public MovingAverageType MAType { get; set; }
[Parameter("Consolidation Periods", DefaultValue = 2)]
public int ConsolidationPeriods { get; set; }
BollingerBands bollingerBands;
string label = "Sample Breakout cBot";
int consolidation;
private Position longPosition;
protected override void OnStart()
{
bollingerBands = Indicators.BollingerBands(Source, Periods, Deviations, MAType);
}
protected override void OnBar()
{
var top = bollingerBands.Top.Last(1);
var bottom = bollingerBands.Bottom.Last(1);
if (top - bottom <= BandHeightPips * Symbol.PipSize)
{
consolidation = consolidation + 1;
}
else
{
consolidation = 0;
}
if (consolidation >= ConsolidationPeriods)
{
var volumeInUnits = Symbol.QuantityToVolume(Quantity);
if (Symbol.Ask > top)
{
CloseOpenPosition;
ExecuteMarketOrder(TradeType.Sell, Symbol, volumeInUnits, label, StopLossInPips, TakeProfitInPips);
consolidation = 0;
}
else if (Symbol.Bid < bottom)
{
CloseOpenPosition;
ExecuteMarketOrder(TradeType.Buy, Symbol, volumeInUnits, label, StopLossInPips, TakeProfitInPips);
consolidation = 0;
}
}
}
}
}
Replies
catecalvez
04 Jun 2018, 23:29
RE:
Panagiotis Charalampous said:
Hi catecalvez,
Thanks for posting in our forum. Why do you say you cannot do that?
Best Regards,
Panagiotis
thank you for the reply sir.
i also tried this script but still not working , will not trigger order.
if (Symbol.Ask > top)
{
ClosePosition(longPosition);
ExecuteMarketOrder(TradeType.Sell, Symbol, volumeInUnits, label, StopLossInPips, TakeProfitInPips);
consolidation = 0;
}
else if (Symbol.Bid < bottom)
{
ClosePosition(shortPosition);
ExecuteMarketOrder(TradeType.Buy, Symbol, volumeInUnits, label, StopLossInPips, TakeProfitInPips);
consolidation = 0;
}
}
}
}
}
@catecalvez
PanagiotisCharalampous
05 Jun 2018, 10:43
Hi catecalvez,
Can you post the full cBot code so that we can build it as well?
Best Regards,
Panagiotis
@PanagiotisCharalampous
catecalvez
05 Jun 2018, 19:15
RE:
Panagiotis Charalampous said:
Hi catecalvez,
Can you post the full cBot code so that we can build it as well?
Best Regards,
Panagiotis
THANK YOU SIR,
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 ambotnew : Robot
{
[Parameter("Source")]
public DataSeries Source { get; set; }
[Parameter("Band Height (pips)", DefaultValue = 40.0, MinValue = 0)]
public double BandHeightPips { get; set; }
[Parameter("Stop Loss (pips)", DefaultValue = 20, MinValue = 1)]
public int StopLossInPips { get; set; }
[Parameter("Take Profit (pips)", DefaultValue = 40, MinValue = 1)]
public int TakeProfitInPips { get; set; }
[Parameter("Quantity (Lots)", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
public double Quantity { get; set; }
[Parameter("Bollinger Bands Deviations", DefaultValue = 2)]
public double Deviations { get; set; }
[Parameter("Bollinger Bands Periods", DefaultValue = 20)]
public int Periods { get; set; }
[Parameter("Bollinger Bands MA Type")]
public MovingAverageType MAType { get; set; }
[Parameter("Consolidation Periods", DefaultValue = 2)]
public int ConsolidationPeriods { get; set; }
BollingerBands bollingerBands;
string label = "Sample Breakout cBot";
int consolidation;
protected override void OnStart()
{
bollingerBands = Indicators.BollingerBands(Source, Periods, Deviations, MAType);
}
protected override void OnBar()
{
var top = bollingerBands.Top.Last(1);
var bottom = bollingerBands.Bottom.Last(1);
if (top - bottom <= BandHeightPips * Symbol.PipSize)
{
consolidation = consolidation + 1;
}
else
{
consolidation = 0;
}
if (consolidation >= ConsolidationPeriods)
{
if (Symbol.Ask > top)
{
ClosePosition(longPosition);
ExecuteMarketOrder(TradeType.Sell, Symbol, volumeInUnits, label, StopLossInPips, TakeProfitInPips);
consolidation = 0;
}
else if (Symbol.Bid < bottom)
{
ClosePosition(shortPosition);
ExecuteMarketOrder(TradeType.Buy, Symbol, volumeInUnits, label, StopLossInPips, TakeProfitInPips);
consolidation = 0;
}
}
}
}
}
@catecalvez
PanagiotisCharalampous
06 Jun 2018, 09:53
Hi catecalvez,
We cannot build this cBot because longPosition, shortPosition and volumeInUnits are not defined anywhere. Did you write this code or are you trying to modify an existing cBot? Could you let us know what are the variables supposed to contain? Which positions are you trying to close?
Best Regards,
Panagiotis
@PanagiotisCharalampous
PanagiotisCharalampous
04 Jun 2018, 12:52
Hi catecalvez,
Thanks for posting in our forum. Why do you say you cannot do that?
Best Regards,
Panagiotis
@PanagiotisCharalampous