Topics
Replies
esarku
06 Jul 2024, 12:42
RE: not reseting after profit
PanagiotisCharalampous said:
esarku said:
PanagiotisCharalampous said:
Hi there,
You are increasing the position size when a trade is successful and when the position closes. In position closing event, you should only reset the volume size and not increase it.
Best regards,
Panagiotis
Hello PanagiotisCharalampous,
thank you for the reply but when I reset the volume size, the martingale no longer works. can you please test code and see.
Thanks
Can you share the fixed code?
using System;
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 CandlePatternRobot : Robot
{
[Parameter("Initial Volume (units)", Group = "Volume", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
public double InitialVolumeInUnits { get; set; }
[Parameter("Martingale Factor", DefaultValue = 2)]
public double MartingaleFactor { get; set; }
private Position buyPosition;
private Position sellPosition;
private double currentVolume;
protected override void OnStart()
{
Positions.Opened += OnPositionOpened;
Positions.Closed += OnPositionClosed;
currentVolume = InitialVolumeInUnits;
}
protected override void OnBar()
{
// Get the close and open prices of the previous candle
double previousClose = Bars.ClosePrices.Last(1);
double previousOpen = Bars.OpenPrices.Last(1);
double volume = currentVolume;
// Check if previous close is greater than previous open
if (previousClose > previousOpen)
{
// Close sell position if there is a sell position
if (sellPosition != null)
{
ClosePosition(sellPosition);
}
// Place a buy trade if there's no buy position already
if (buyPosition == null)
{
TradeResult buyResult = ExecuteMarketOrder(TradeType.Buy, SymbolName, volume, "Buy Order", null, null, "yes");
if (!buyResult.IsSuccessful)
{
currentVolume *= MartingaleFactor; // Increase volume in case of loss
Print("Martingale applied: New Volume = " + currentVolume);
}
}
}
// Check if previous close is less than previous open
else if (previousClose < previousOpen)
{
// Close buy position if there is a buy position
if (buyPosition != null)
{
ClosePosition(buyPosition);
}
// Place a sell trade if there's no sell position already
if (sellPosition == null)
{
TradeResult sellResult = ExecuteMarketOrder(TradeType.Sell, SymbolName, volume, "Sell Order", null, null, "yes");
if (!sellResult.IsSuccessful)
{
currentVolume *= MartingaleFactor; // Increase volume in case of loss
Print("Martingale applied: New Volume = " + currentVolume);
}
}
}
else
{
// No action needed if previous close is equal to previous open
}
}
private void OnPositionOpened(PositionOpenedEventArgs args)
{
if (args.Position.TradeType == TradeType.Buy)
buyPosition = args.Position;
else if (args.Position.TradeType == TradeType.Sell)
sellPosition = args.Position;
}
private void OnPositionClosed(PositionClosedEventArgs args)
{
if (args.Position.TradeType == TradeType.Buy)
buyPosition = null;
else if (args.Position.TradeType == TradeType.Sell)
sellPosition = null;
// Reset the volume size
currentVolume = InitialVolumeInUnits;
}
}
}
@esarku
esarku
05 Jul 2024, 19:49
( Updated at: 06 Jul 2024, 05:42 )
RE: Martingale not reseting after profit
PanagiotisCharalampous said:
Hi there,
You are increasing the position size when a trade is successful and when the position closes. In position closing event, you should only reset the volume size and not increase it.
Best regards,
Panagiotis
Hello PanagiotisCharalampous,
thank you for the reply but when I reset the volume size, the martingale no longer works. can you please test code and see.
Thanks
@esarku
esarku
08 Jul 2024, 21:13
RE: not reseting after profit
PanagiotisCharalampous said:
I have tried to modify it but getiing the same result. any chance you could please add the correct line as a guide.
Thanks
@esarku