[Parameter("Break-Even Trigger (pips)", DefaultValue = 10, MinValue = 1)]
public int BreakEvenPips { get; set; }
[Parameter("Break-Even Extra (pips)", DefaultValue = 2, MinValue = 1)]
public int BreakEvenExtraPips { get; set; }
#endregion
#region Indicator declarations
private SimpleMovingAverage sma2 { get; set; }
#endregion
#region cTrader events
/// <summary>
/// This is called when the robot first starts, it is only called once.
/// </summary>
protected override void OnStart()
{
// construct the indicators
sma2 = Indicators.SimpleMovingAverage(SourceSma2, PeriodsSma2);
}
/// <summary>
/// This method is called every time the price changes for the symbol
/// </summary>
protected override void OnTick()
{
if (CalculateOnBar)
{
return;
}
ManagePositions();
if (IncludeBreakEven)
{
BreakEvenAdjustment();
}
}
/// <summary>
/// This method is called at every candle (bar) close, when it has formed
/// </summary>
protected override void OnBar()
{
if (!CalculateOnBar)
{
return;
}
ManagePositions();
}
/// <summary>
/// This method is called when your robot stops, can be used to clean-up memory resources.
/// </summary>
protected override void OnStop()
{
// unused
}
#endregion
#region Position management
private void ManagePositions()
{
int index = Bars.ClosePrices.Count - 1;
if (sma2.Result[index - 2] > Bars.ClosePrices[index - 2] && sma2.Result[index - 1] < Bars.ClosePrices[index - 1])
{
// if there is no buy position open, open one and close any sell position that is open
if (!IsPositionOpenByType(TradeType.Buy))
{
OpenPosition(TradeType.Buy);
}
ClosePosition(TradeType.Sell);
}
// if a sell position is already open and signal is buy do nothing
if (sma2.Result[index - 2] < Bars.ClosePrices[index - 2] && sma2.Result[index - 1] > Bars.ClosePrices[index - 1])
{
// if there is no sell position open, open one and close any buy position that is open
if (!IsPositionOpenByType(TradeType.Sell))
{
OpenPosition(TradeType.Sell);
}
ClosePosition(TradeType.Buy);
}
}
/// <summary>
/// Opens a new long position
/// </summary>
/// <param name="type"></param>
private void OpenPosition(TradeType tradeType)
{
var position = Positions.Find(InstanceName, SymbolName, tradeType);
var volumeInUnits = Symbol.QuantityToVolumeInUnits(LotSize);
/// <summary>
/// Adjusts the break even plus (x) pips
/// </summary>
/// <param name="tradeType"></param>
private void BreakEvenAdjustment()
{
var allPositions = Positions.FindAll(InstanceName, SymbolName);
foreach (Position position in allPositions)
{
// if (position.StopLoss != null)
// return;
var entryPrice = position.EntryPrice;
var distance = position.TradeType == TradeType.Buy ? Symbol.Bid - entryPrice : entryPrice - Symbol.Ask;
// move stop loss to break even plus and additional (x) pips
if (distance >= BreakEvenPips * Symbol.PipSize)
{
if (position.TradeType == TradeType.Buy)
{
if (position.StopLoss <= position.EntryPrice + (Symbol.PipSize * BreakEvenExtraPips) || position.StopLoss == null)
{
ModifyPosition(position, position.EntryPrice + (Symbol.PipSize * BreakEvenExtraPips), position.TakeProfit);
Print("Stop Loss to Break Even set for BUY position {0}", position.Id);
}
}
else
{
if (position.StopLoss >= position.EntryPrice - (Symbol.PipSize * BreakEvenExtraPips) || position.StopLoss == null)
{
ModifyPosition(position, entryPrice - (Symbol.PipSize * BreakEvenExtraPips), position.TakeProfit);
Print("Stop Loss to Break Even set for SELL position {0}", position.Id);
}
}
}
}
}
Mcfray
28 Jan 2021, 22:20
RE: RE:
Hi everyone,
I am looking for something like that but that the entry is by crossing the price and sma, Could you help me please?
Question:
1. Add 5 pips above the entry, when price crossover sma.
2. I´m using Nas100 and cannot use less than 1.0 LotSize, the minimun is 0.10
3. Stop Loss 5 pips below the last bar low.
Thank you.
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 PriceSMA : Robot
{
#region User defined parameters
[Parameter("Instance Name", DefaultValue = "001")]
public string InstanceName { get; set; }
[Parameter("Lot Size", DefaultValue = 0.01)]
public double LotSize { get; set; }
[Parameter("Stop Loss (pips)", DefaultValue = 20, MinValue = 0)]
public int sl { get; set; }
[Parameter("Take Profit (pips)", DefaultValue = 50, MinValue = 0)]
public int tp { get; set; }
[Parameter("Source SMA #2")]
public DataSeries SourceSma2 { get; set; }
[Parameter("Period SMA #2", DefaultValue = 20, MinValue = 1, MaxValue = 100)]
public int PeriodsSma2 { get; set; }
[Parameter("Calculate OnBar", DefaultValue = false)]
public bool CalculateOnBar { get; set; }
[Parameter("Include Break-Even", DefaultValue = false)]
public bool IncludeBreakEven { get; set; }
[Parameter("Break-Even Trigger (pips)", DefaultValue = 10, MinValue = 1)]
public int BreakEvenPips { get; set; }
[Parameter("Break-Even Extra (pips)", DefaultValue = 2, MinValue = 1)]
public int BreakEvenExtraPips { get; set; }
#endregion
#region Indicator declarations
private SimpleMovingAverage sma2 { get; set; }
#endregion
#region cTrader events
/// <summary>
/// This is called when the robot first starts, it is only called once.
/// </summary>
protected override void OnStart()
{
// construct the indicators
sma2 = Indicators.SimpleMovingAverage(SourceSma2, PeriodsSma2);
}
/// <summary>
/// This method is called every time the price changes for the symbol
/// </summary>
protected override void OnTick()
{
if (CalculateOnBar)
{
return;
}
ManagePositions();
if (IncludeBreakEven)
{
BreakEvenAdjustment();
}
}
/// <summary>
/// This method is called at every candle (bar) close, when it has formed
/// </summary>
protected override void OnBar()
{
if (!CalculateOnBar)
{
return;
}
ManagePositions();
}
/// <summary>
/// This method is called when your robot stops, can be used to clean-up memory resources.
/// </summary>
protected override void OnStop()
{
// unused
}
#endregion
#region Position management
private void ManagePositions()
{
int index = Bars.ClosePrices.Count - 1;
if (sma2.Result[index - 2] > Bars.ClosePrices[index - 2] && sma2.Result[index - 1] < Bars.ClosePrices[index - 1])
{
// if there is no buy position open, open one and close any sell position that is open
if (!IsPositionOpenByType(TradeType.Buy))
{
OpenPosition(TradeType.Buy);
}
ClosePosition(TradeType.Sell);
}
// if a sell position is already open and signal is buy do nothing
if (sma2.Result[index - 2] < Bars.ClosePrices[index - 2] && sma2.Result[index - 1] > Bars.ClosePrices[index - 1])
{
// if there is no sell position open, open one and close any buy position that is open
if (!IsPositionOpenByType(TradeType.Sell))
{
OpenPosition(TradeType.Sell);
}
ClosePosition(TradeType.Buy);
}
}
/// <summary>
/// Opens a new long position
/// </summary>
/// <param name="type"></param>
private void OpenPosition(TradeType tradeType)
{
var position = Positions.Find(InstanceName, SymbolName, tradeType);
var volumeInUnits = Symbol.QuantityToVolumeInUnits(LotSize);
if (position == null)
ExecuteMarketOrder(tradeType, SymbolName, volumeInUnits, InstanceName, sl, tp);
}
/// <summary>
///
/// </summary>
/// <param name="type"></param>
private void ClosePosition(TradeType tradeType)
{
var p = Positions.Find(InstanceName, SymbolName, tradeType);
if (p != null)
{
ClosePosition(p);
}
}
#endregion
#region Position Information
/// <summary>
///
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
private bool IsPositionOpenByType(TradeType tradeType)
{
var p = Positions.FindAll(InstanceName, SymbolName, tradeType);
if (p.Count() >= 1)
{
return true;
}
return false;
}
#endregion
#region Break Even
/// <summary>
/// Adjusts the break even plus (x) pips
/// </summary>
/// <param name="tradeType"></param>
private void BreakEvenAdjustment()
{
var allPositions = Positions.FindAll(InstanceName, SymbolName);
foreach (Position position in allPositions)
{
// if (position.StopLoss != null)
// return;
var entryPrice = position.EntryPrice;
var distance = position.TradeType == TradeType.Buy ? Symbol.Bid - entryPrice : entryPrice - Symbol.Ask;
// move stop loss to break even plus and additional (x) pips
if (distance >= BreakEvenPips * Symbol.PipSize)
{
if (position.TradeType == TradeType.Buy)
{
if (position.StopLoss <= position.EntryPrice + (Symbol.PipSize * BreakEvenExtraPips) || position.StopLoss == null)
{
ModifyPosition(position, position.EntryPrice + (Symbol.PipSize * BreakEvenExtraPips), position.TakeProfit);
Print("Stop Loss to Break Even set for BUY position {0}", position.Id);
}
}
else
{
if (position.StopLoss >= position.EntryPrice - (Symbol.PipSize * BreakEvenExtraPips) || position.StopLoss == null)
{
ModifyPosition(position, entryPrice - (Symbol.PipSize * BreakEvenExtraPips), position.TakeProfit);
Print("Stop Loss to Break Even set for SELL position {0}", position.Id);
}
}
}
}
}
#endregion
}
}
@Mcfray