Close all Positions on a target profit (Several symbols)
Created at 14 Feb 2023, 04:47
Close all Positions on a target profit (Several symbols)
14 Feb 2023, 04:47
Hi guys,
My code attached.
I want to incorporate a functions which closes all positions once my target profit/loss is reached and then rerun the bot instead of me having to click again.
Say the bots opens 10 positions, including several pairs, and it reaches my target of $1000. The bot closes all positions and the re runs again so it starts opening positions and suddenly it reaches my target of loss of $-500, it closes all positions and re runs again.
PLEASE
Thanks
using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
using System.Linq;
namespace cAlgo.Robots
{
[Robot(AccessRights = AccessRights.None)]
public class WORKSSSSSS : Robot
{
int upClose;
int insideBar;
int downClose;
//Parameters
[Parameter("ID", DefaultValue = "Instance", Group = "Indetificator")]
public string Instance { get; set; }
[Parameter(DefaultValue = 1000, Group = "Risk Management")]
public int Volume { get; set; }
[Parameter("Stop Loss", DefaultValue = 100, Group = "Risk Management")]
public int StopLoss { get; set; }
[Parameter("Take Profit", DefaultValue = 100, Group = "Risk Management")]
public int TakeProfit { get; set; }
[Parameter("From", DefaultValue = 0, Group = "Time Period")]
public int From { get; set; }
[Parameter("To", DefaultValue = 24, Group = "Time Period")]
public int To { get; set; }
[Parameter("Max Trades", DefaultValue = 1, Group = "Trades")]
public int MaxTrades { get; set; }
[Parameter("MA Period", DefaultValue = 20, Group = "Trades")]
public int MAperiod { get; set; }
[Parameter("Source", Group = "Data series")]
public DataSeries Source { get; set; }
public ExponentialMovingAverage EMA;
//MA
protected override void OnStart()
{
EMA = Indicators.ExponentialMovingAverage(Bars.ClosePrices, MAperiod);
}
//CODE
protected override void OnBar()
{
//Count trades
int CountOfBuyPositions = Positions.Count(x => x.TradeType == TradeType.Buy && x.SymbolName == SymbolName && x.Label == Instance);
int CountOfShortPositions = Positions.Count(x => x.TradeType == TradeType.Sell && x.SymbolName == SymbolName && x.Label == Instance);
int CountOfOpenTrades = CountOfBuyPositions + CountOfShortPositions;
//Inside Bar
if (Server.Time.Hour >= From && Server.Time.Hour < To)
{
if (Trade.IsExecuting)
{
return;
}
if (Bars.ClosePrices[Bars.ClosePrices.Count - 1] > Bars.ClosePrices[Bars.ClosePrices.Count - 2])
{
upClose = 1;
}
else
{
upClose = 0;
}
if ((Bars.HighPrices[Bars.HighPrices.Count - 2] < Bars.HighPrices[Bars.HighPrices.Count - 3]) && (Bars.LowPrices[Bars.LowPrices.Count - 2] > Bars.LowPrices[Bars.LowPrices.Count - 3]))
{
insideBar = 1;
}
else
{
insideBar = 0;
}
if (Bars.ClosePrices[Bars.ClosePrices.Count - 1] < Bars.ClosePrices[Bars.ClosePrices.Count - 2])
{
downClose = 1;
}
else
{
downClose = 0;
}
//Condition for buy/sell
if (CountOfOpenTrades<MaxTrades)
{
if (upClose == 1 && insideBar == 1 && EMA.Result.LastValue < Bars.ClosePrices.LastValue)
{
ExecuteMarketOrder(TradeType.Buy, SymbolName, Volume, Instance, StopLoss, TakeProfit);
}
if (downClose == 1 && insideBar == 1 && EMA.Result.LastValue > Bars.ClosePrices.LastValue)
{
ExecuteMarketOrder(TradeType.Sell, SymbolName, Volume, Instance, StopLoss, TakeProfit);
}
}
}
}
}
}
PanagiotisChar
14 Feb 2023, 11:01
Hi there,
Here is an example
You can adjust this based on your needs.
Aieden Technologies
Need help? Join us on Telegram
Need premium support? Trade with us
@PanagiotisChar