Problem with the robot
Problem with the robot
31 Aug 2022, 14:31
There are two problems
1.
((((1. When moving 1 cuts moving 2 upwards, buy 2 pips higher than the moving cut and close the previous position.
2. When moving 1 cuts moving 2 down, settle 2 pips lower than the moving cut and close the previous position.)))))
These 2 pips are variable
2.((((The robot does not perform well when the fast disconnection occurs))))))
I am not a programmer, can anyone help me?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Collections;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo.Robots
{
[Robot(AccessRights = AccessRights.None)]
public class e : Robot
{
// start Strategy parameters
[Parameter("EMA1 - periods",DefaultValue=5, Group = "EMA")]
public int Periods1 { get; set; }
[Parameter("EMA2 - periods",DefaultValue=10, Group = "EMA")]
public int Periods2 { get; set; }
[Parameter("EMA - source", Group = "EMA")]
public DataSeries Source { get; set; }
//end Strategy parameters
// start Strategy parameters
[Parameter("Lots",DefaultValue=0.01, Group = "Strategy", MinValue = 0.01, MaxValue = 1)]
public double Lots { get; set; }
[Parameter("Takeprofit1", DefaultValue=10, Group = "Strategy")]
public double Takeprofit1 { get; set; }
[Parameter("Takeprofit2 (form EMA1)", DefaultValue=10, Group = "Strategy")]
public double Takeprofit2 { get; set; }
[Parameter("Takeprofit3 (form EMA2)", DefaultValue=10, Group = "Strategy")]
public double Takeprofit3 { get; set; }
[Parameter("Stoploss1", DefaultValue=0, Group = "Strategy")]
public double Stoploss1 { get; set; }
[Parameter("Stoploss2 (form EMA1)", DefaultValue=0, Group = "Strategy")]
public double Stoploss2 { get; set; }
[Parameter("Stoploss3 (form EMA2)", DefaultValue=0, Group = "Strategy")]
public double Stoploss3 { get; set; }
[Parameter("Candle Of Start Position", DefaultValue=0, Group = "Strategy", MinValue =0, MaxValue =1)]
public int CandleOfStartPosition { get; set; }
[Parameter("Close On Change Position", DefaultValue=true, Group = "Strategy")]
public bool COCP { get; set; }
//end Strategy parameters
//main properties
private ExponentialMovingAverage EMA1, EMA2;
private double OpenPositionPrice, OpenPositionEMA1, OpenPositionEMA2;
protected override void OnStart()
{
EMA1 = Indicators.ExponentialMovingAverage(Source,Periods1);
EMA2 = Indicators.ExponentialMovingAverage(Source,Periods2);
}
protected override void OnTick()
{
//buy
if(EMA1.Result.HasCrossedAbove(EMA2.Result,CandleOfStartPosition) && OpenPositionPrice != Bars.OpenPrices.Last(CandleOfStartPosition)){
OpenPositionEMA1 = EMA1.Result.Last(CandleOfStartPosition);
OpenPositionEMA2 = EMA2.Result.Last(CandleOfStartPosition);
Open(TradeType.Buy);
if(COCP){
Close(TradeType.Sell);
}
OpenPositionPrice = Bars.OpenPrices.Last(CandleOfStartPosition);
}
var BuyPosition = Positions.Find("EMA_bot", Symbol.Name, TradeType.Buy);
//buy takeprofit2
if(BuyPosition != null && Takeprofit2 != 0 && Bars.ClosePrices.Last(CandleOfStartPosition) > OpenPositionEMA1 + (Takeprofit2 * Symbol.PipSize)){
Print("close by takeprofit2");
Close(TradeType.Buy);
}
//buy takeprofit3
if(BuyPosition != null && Takeprofit3 != 0 && Bars.ClosePrices.Last(CandleOfStartPosition) > OpenPositionEMA2 + (Takeprofit3 * Symbol.PipSize)){
Print("close by takeprofit3");
Close(TradeType.Buy);
}
//buy stoploss2
if(BuyPosition != null && Stoploss2 != 0 && Bars.ClosePrices.Last(CandleOfStartPosition) < OpenPositionEMA1 - (Stoploss2 * Symbol.PipSize)){
Print("close by stoploss2");
Close(TradeType.Buy);
}
//buy stoploss2
if(BuyPosition != null && Stoploss3 != 0 && Bars.ClosePrices.Last(CandleOfStartPosition) < OpenPositionEMA1 - (Stoploss3 * Symbol.PipSize)){
Print("close by stoploss3");
Close(TradeType.Buy);
}
//sell
if(EMA1.Result.HasCrossedBelow(EMA2.Result,CandleOfStartPosition) && OpenPositionPrice != Bars.OpenPrices.Last(CandleOfStartPosition)){
OpenPositionEMA1 = EMA1.Result.Last(CandleOfStartPosition);
OpenPositionEMA2 = EMA2.Result.Last(CandleOfStartPosition);
Open(TradeType.Sell);
if(COCP){
Close(TradeType.Buy);
}
OpenPositionPrice = Bars.OpenPrices.Last(CandleOfStartPosition);
}
var SellPosition = Positions.Find("EMA_bot", Symbol.Name, TradeType.Sell);
//buy takeprofit2
if(SellPosition != null && Stoploss2 != 0 && Bars.ClosePrices.Last(CandleOfStartPosition) > OpenPositionEMA1 - (Stoploss2 * Symbol.PipSize)){
Print("close by takeprofit2");
Close(TradeType.Sell);
}
//buy takeprofit3
if(SellPosition != null && Stoploss3 != 0 && Bars.ClosePrices.Last(CandleOfStartPosition) > OpenPositionEMA2 - (Stoploss3 * Symbol.PipSize)){
Print("close by takeprofit3");
Close(TradeType.Sell);
}
//buy stoploss2
if(SellPosition != null && Stoploss2 != 0 && Bars.ClosePrices.Last(CandleOfStartPosition) < OpenPositionEMA1 + (Stoploss2 * Symbol.PipSize)){
Print("close by stoploss2");
Close(TradeType.Sell);
}
//buy stoploss2
if(SellPosition != null && Stoploss3 != 0 && Bars.ClosePrices.Last(CandleOfStartPosition) < OpenPositionEMA1 + (Stoploss3 * Symbol.PipSize)){
Print("close by stoploss3");
Close(TradeType.Sell);
}
}
private void Close(TradeType tradeType)
{
foreach (var position in Positions.FindAll("EMA_bot", Symbol.Name, tradeType)){
ClosePosition(position);
}
}
private void Open(TradeType tradeType)
{
var position = Positions.Find("EMA_bot", Symbol.Name, tradeType);
var volumeInUnits = Symbol.QuantityToVolumeInUnits(Lots);
if (position == null){
//set takeprofit1, stoploss1
ExecuteMarketOrder(tradeType, Symbol.Name, volumeInUnits, "EMA_bot", Stoploss1, Takeprofit1);
}
}
}
}