Stoploss bot need help whit the code
Stoploss bot need help whit the code
28 Apr 2023, 09:45
have done a stoploss bot whit some levels right now the levels are just for the test, when a trade is opend it puts the stop wher i want it, but then it dosent move the stop i need to restart the bot when a level i hit for it to move the stop any one have a sulotion for this ?
using System;
using System.Collections.Generic;
using cAlgo.API;
using cAlgo.API.Internals;
namespace cAlgo
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class SetStopLossBot : Robot
{
private Dictionary<long, int> stopLossLevel = new Dictionary<long, int>();
private Dictionary<long, int> highestStopLossLevel = new Dictionary<long, int>();
protected override void OnStart()
{
Positions.Closed += OnPositionClosed;
}
protected override void OnTick()
{
foreach (var position in Positions)
{
if (position.SymbolName != SymbolName)
continue;
double profitInPips = CalculateProfitInPips(position);
int currentLevel = CalculateStopLossLevel(profitInPips, highestStopLossLevel.ContainsKey(position.Id) ? highestStopLossLevel[position.Id] : 0);
if (stopLossLevel.ContainsKey(position.Id))
{
if (stopLossLevel[position.Id] < currentLevel)
{
stopLossLevel[position.Id] = currentLevel;
double slPrice = CalculateStopLossPrice(position, stopLossLevel[position.Id]);
if ((position.TradeType == TradeType.Buy && slPrice > position.StopLoss) || (position.TradeType == TradeType.Sell && slPrice < position.StopLoss))
{
if (position.TradeType == TradeType.Sell)
{
double sellStopLossPrice = CalculateStopLossPrice(position, stopLossLevel[position.Id]);
ModifyPosition(position, null, sellStopLossPrice);
}
else
{
ModifyPosition(position, slPrice, null);
}
}
}
}
else
{
stopLossLevel[position.Id] = -15;
double slPrice = CalculateStopLossPrice(position, stopLossLevel[position.Id]);
if (position.TradeType == TradeType.Sell)
{
double sellStopLossPrice = CalculateStopLossPrice(position, stopLossLevel[position.Id]);
ModifyPosition(position, null, sellStopLossPrice);
}
else
{
ModifyPosition(position, slPrice, null);
}
}
if (currentLevel > (highestStopLossLevel.ContainsKey(position.Id) ? highestStopLossLevel[position.Id] : 0))
highestStopLossLevel[position.Id] = currentLevel;
}
}
private double CalculateProfitInPips(Position position)
{
double priceDifference = position.TradeType == TradeType.Buy
? Symbol.Bid - position.EntryPrice
: position.EntryPrice - Symbol.Ask;
return priceDifference / Symbol.PipSize;
}
private int CalculateStopLossLevel(double profitInPips, int highestStopLossLevel)
{
int currentLevel = 0;
if (profitInPips >= 60)
return 59;
if (profitInPips >= 50)
return 45;
if (profitInPips >= 45)
return 40;
if (profitInPips >= 40)
return 35;
if (profitInPips >= 35)
return 30;
if (profitInPips >= 30)
return 25;
if (profitInPips >= 25)
return 20;
if (profitInPips >= 24)
return 15;
if (profitInPips >= 23)
return 13;
if (profitInPips >= 22)
return 12;
if (profitInPips >= 21)
return 11;
if (profitInPips >= 20)
return 10;
if (profitInPips >= 19)
return 4;
if (profitInPips >= 18)
return 3;
if (profitInPips >= 17)
return 2;
if (profitInPips >= 16)
return 1;
if (profitInPips >= 15)
return 0;
if (profitInPips >= 14)
return -1;
if (profitInPips >= 13)
return -2;
if (profitInPips >= 12)
return -3;
if (profitInPips >= 11)
return -4;
if (profitInPips >= 10)
return -5;
if (profitInPips >= 9)
return -6;
if (profitInPips >= 8)
return -7;
if (profitInPips >= 7)
return -8;
if (profitInPips >= 6)
return -9;
if (profitInPips >= 5)
return -10;
if (profitInPips >= 4)
return -11;
if (profitInPips >= 0.3)
return -12;
if (profitInPips >= 0.2)
return -13;
if (profitInPips >= 0.1)
return -14;
if (currentLevel > highestStopLossLevel)
return currentLevel;
else
return highestStopLossLevel;
}
private double CalculateStopLossPrice(Position position, int stopLossLevel)
{
if (stopLossLevel == 0)
return 0;
double stopLossPrice = position.EntryPrice;
if (position.TradeType == TradeType.Buy)
{
stopLossPrice += stopLossLevel * Symbol.PipSize;
}
else if (position.TradeType == TradeType.Sell)
{
stopLossPrice -= stopLossLevel * Symbol.PipSize;
}
return stopLossPrice;
}
private void OnPositionClosed(PositionClosedEventArgs args)
{
var position = args.Position;
if (position.SymbolName != SymbolName)
return;
if (stopLossLevel.ContainsKey(position.Id))
{
stopLossLevel.Remove(position.Id);
}
if (highestStopLossLevel.ContainsKey(position.Id))
{
highestStopLossLevel.Remove(position.Id);
}
}
}
}