cbot martingale trade direction control
cbot martingale trade direction control
12 Mar 2019, 09:14
Hi all, can someone help me with the sample Martingale cbot . i need to change the trade direction to be non random. instead i need it to OPEN NEXT TRADE IN THE DIRECTION OF THE PREVIOUS TP or SL.
Thanks kindly.
// ------------------------------------------------------------------------------------------------- // // This code is a cAlgo API sample. // // This cBot is intended to be used as a sample and does not guarantee any particular outcome or // profit of any kind. Use it at your own risk // // The "Sample Martingale cBot" creates a random Sell or Buy order. If the Stop loss is hit, a new // order of the same type (Buy / Sell) is created with double the Initial Volume amount. The cBot will // continue to double the volume amount for all orders created until one of them hits the take Profit. // After a Take Profit is hit, a new random Buy or Sell order is created with the Initial Volume amount. // // ------------------------------------------------------------------------------------------------- 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 SampleMartingalecBot : Robot { [Parameter("Initial Quantity (Lots)", DefaultValue = 0.01, MinValue = 0.01, Step = 0.01)] public double InitialQuantity { get; set; } [Parameter("Stop Loss", DefaultValue = 3)] public int StopLoss { get; set; } [Parameter("Take Profit", DefaultValue = 3)] public int TakeProfit { get; set; } private Random random = new Random(); protected override void OnStart() { Positions.Closed += OnPositionsClosed; ExecuteOrder(InitialQuantity, GetRandomTradeType()); } private void ExecuteOrder(double quantity, TradeType tradeType) { var volumeInUnits = Symbol.QuantityToVolumeInUnits(quantity); var result = ExecuteMarketOrder(tradeType, Symbol, volumeInUnits, "Martingale", StopLoss, TakeProfit); if (result.Error == ErrorCode.NoMoney) Stop(); } private void OnPositionsClosed(PositionClosedEventArgs args) { Print("Closed"); var position = args.Position; if (position.Label != "Martingale" || position.SymbolCode != Symbol.Code) return; if (position.GrossProfit > 0) { ExecuteOrder(InitialQuantity, GetRandomTradeType()); } else { ExecuteOrder(position.Quantity * 2, position.TradeType); } } private TradeType GetRandomTradeType() { return random.Next(2) == 0 ? TradeType.Buy : TradeType.Sell; } } }
Replies
thamimsibi3
12 Mar 2019, 15:35
RE:
Panagiotis Charalampous said:
Hi thamimsibi3,
Thanks for posting in our forum. See below
// ------------------------------------------------------------------------------------------------- // // This code is a cAlgo API sample. // // This cBot is intended to be used as a sample and does not guarantee any particular outcome or // profit of any kind. Use it at your own risk // // The "Sample Martingale cBot" creates a random Sell or Buy order. If the Stop loss is hit, a new // order of the same type (Buy / Sell) is created with double the Initial Volume amount. The cBot will // continue to double the volume amount for all orders created until one of them hits the take Profit. // After a Take Profit is hit, a new random Buy or Sell order is created with the Initial Volume amount. // // ------------------------------------------------------------------------------------------------- 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 SampleMartingalecBot : Robot { [Parameter("Initial Quantity (Lots)", DefaultValue = 0.01, MinValue = 0.01, Step = 0.01)] public double InitialQuantity { get; set; } [Parameter("Stop Loss", DefaultValue = 3)] public int StopLoss { get; set; } [Parameter("Take Profit", DefaultValue = 3)] public int TakeProfit { get; set; } private Random random = new Random(); protected override void OnStart() { Positions.Closed += OnPositionsClosed; ExecuteOrder(InitialQuantity, GetRandomTradeType()); } private void ExecuteOrder(double quantity, TradeType tradeType) { var volumeInUnits = Symbol.QuantityToVolumeInUnits(quantity); var result = ExecuteMarketOrder(tradeType, Symbol, volumeInUnits, "Martingale", StopLoss, TakeProfit); if (result.Error == ErrorCode.NoMoney) Stop(); } private void OnPositionsClosed(PositionClosedEventArgs args) { Print("Closed"); var position = args.Position; if (position.Label != "Martingale" || position.SymbolCode != Symbol.Code) return; if (position.GrossProfit > 0) { ExecuteOrder(InitialQuantity, position.TradeType); } else { ExecuteOrder(position.Quantity * 2, position.TradeType); } } private TradeType GetRandomTradeType() { return random.Next(2) == 0 ? TradeType.Buy : TradeType.Sell; } } }Best Regards,
Panagiotis
Thank you Panagiotis i just tested it. i notice the difference it only opens sell trades all the time, which is not what im looking for. i need it to open the next trade on this condition ( if previous SL OR TP was upwards, then the next trade is upwards(BUY). If previous SL or TP closed downwards, the the next trade is downward(sell)
kind regards.
@thamimsibi3
... Deleted by UFO ...
PanagiotisCharalampous
12 Mar 2019, 15:47
Hi thamimsibi3,
Thanks for the clarification, that was not very clear.
See below the cBot based on your clarification
// ------------------------------------------------------------------------------------------------- // // This code is a cAlgo API sample. // // This cBot is intended to be used as a sample and does not guarantee any particular outcome or // profit of any kind. Use it at your own risk // // The "Sample Martingale cBot" creates a random Sell or Buy order. If the Stop loss is hit, a new // order of the same type (Buy / Sell) is created with double the Initial Volume amount. The cBot will // continue to double the volume amount for all orders created until one of them hits the take Profit. // After a Take Profit is hit, a new random Buy or Sell order is created with the Initial Volume amount. // // ------------------------------------------------------------------------------------------------- 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 SampleMartingalecBot : Robot { [Parameter("Initial Quantity (Lots)", DefaultValue = 0.01, MinValue = 0.01, Step = 0.01)] public double InitialQuantity { get; set; } [Parameter("Stop Loss", DefaultValue = 3)] public int StopLoss { get; set; } [Parameter("Take Profit", DefaultValue = 3)] public int TakeProfit { get; set; } private Random random = new Random(); protected override void OnStart() { Positions.Closed += OnPositionsClosed; ExecuteOrder(InitialQuantity, GetRandomTradeType()); } private void ExecuteOrder(double quantity, TradeType tradeType) { var volumeInUnits = Symbol.QuantityToVolumeInUnits(quantity); var result = ExecuteMarketOrder(tradeType, Symbol, volumeInUnits, "Martingale", StopLoss, TakeProfit); if (result.Error == ErrorCode.NoMoney) Stop(); } private void OnPositionsClosed(PositionClosedEventArgs args) { Print("Closed"); var position = args.Position; if (position.Label != "Martingale" || position.SymbolCode != Symbol.Code) return; if (position.GrossProfit > 0) { ExecuteOrder(InitialQuantity, position.TradeType); } else { if (position.TradeType == TradeType.Sell) ExecuteOrder(position.Quantity * 2, TradeType.Buy); else ExecuteOrder(position.Quantity * 2, TradeType.Sell); } } private TradeType GetRandomTradeType() { return random.Next(2) == 0 ? TradeType.Buy : TradeType.Sell; } } }
Best Regards,
Panagiotis
@PanagiotisCharalampous
thamimsibi3
12 Mar 2019, 17:23
RE:
Panagiotis Charalampous said:
Hi thamimsibi3,
Thanks for the clarification, that was not very clear.
See below the cBot based on your clarification
// ------------------------------------------------------------------------------------------------- // // This code is a cAlgo API sample. // // This cBot is intended to be used as a sample and does not guarantee any particular outcome or // profit of any kind. Use it at your own risk // // The "Sample Martingale cBot" creates a random Sell or Buy order. If the Stop loss is hit, a new // order of the same type (Buy / Sell) is created with double the Initial Volume amount. The cBot will // continue to double the volume amount for all orders created until one of them hits the take Profit. // After a Take Profit is hit, a new random Buy or Sell order is created with the Initial Volume amount. // // ------------------------------------------------------------------------------------------------- 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 SampleMartingalecBot : Robot { [Parameter("Initial Quantity (Lots)", DefaultValue = 0.01, MinValue = 0.01, Step = 0.01)] public double InitialQuantity { get; set; } [Parameter("Stop Loss", DefaultValue = 3)] public int StopLoss { get; set; } [Parameter("Take Profit", DefaultValue = 3)] public int TakeProfit { get; set; } private Random random = new Random(); protected override void OnStart() { Positions.Closed += OnPositionsClosed; ExecuteOrder(InitialQuantity, GetRandomTradeType()); } private void ExecuteOrder(double quantity, TradeType tradeType) { var volumeInUnits = Symbol.QuantityToVolumeInUnits(quantity); var result = ExecuteMarketOrder(tradeType, Symbol, volumeInUnits, "Martingale", StopLoss, TakeProfit); if (result.Error == ErrorCode.NoMoney) Stop(); } private void OnPositionsClosed(PositionClosedEventArgs args) { Print("Closed"); var position = args.Position; if (position.Label != "Martingale" || position.SymbolCode != Symbol.Code) return; if (position.GrossProfit > 0) { ExecuteOrder(InitialQuantity, position.TradeType); } else { if (position.TradeType == TradeType.Sell) ExecuteOrder(position.Quantity * 2, TradeType.Buy); else ExecuteOrder(position.Quantity * 2, TradeType.Sell); } } private TradeType GetRandomTradeType() { return random.Next(2) == 0 ? TradeType.Buy : TradeType.Sell; } } }Best Regards,
Panagiotis
Thank you very much it works. straight perfectly.
Thank You.
@thamimsibi3
thamimsibi3
25 Mar 2019, 15:18
RE: RE:
thamimsibi3 said:
Panagiotis Charalampous said:
Hi thamimsibi3,
Thanks for the clarification, that was not very clear.
See below the cBot based on your clarification
// ------------------------------------------------------------------------------------------------- // // This code is a cAlgo API sample. // // This cBot is intended to be used as a sample and does not guarantee any particular outcome or // profit of any kind. Use it at your own risk // // The "Sample Martingale cBot" creates a random Sell or Buy order. If the Stop loss is hit, a new // order of the same type (Buy / Sell) is created with double the Initial Volume amount. The cBot will // continue to double the volume amount for all orders created until one of them hits the take Profit. // After a Take Profit is hit, a new random Buy or Sell order is created with the Initial Volume amount. // // ------------------------------------------------------------------------------------------------- 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 SampleMartingalecBot : Robot { [Parameter("Initial Quantity (Lots)", DefaultValue = 0.01, MinValue = 0.01, Step = 0.01)] public double InitialQuantity { get; set; } [Parameter("Stop Loss", DefaultValue = 3)] public int StopLoss { get; set; } [Parameter("Take Profit", DefaultValue = 3)] public int TakeProfit { get; set; } private Random random = new Random(); protected override void OnStart() { Positions.Closed += OnPositionsClosed; ExecuteOrder(InitialQuantity, GetRandomTradeType()); } private void ExecuteOrder(double quantity, TradeType tradeType) { var volumeInUnits = Symbol.QuantityToVolumeInUnits(quantity); var result = ExecuteMarketOrder(tradeType, Symbol, volumeInUnits, "Martingale", StopLoss, TakeProfit); if (result.Error == ErrorCode.NoMoney) Stop(); } private void OnPositionsClosed(PositionClosedEventArgs args) { Print("Closed"); var position = args.Position; if (position.Label != "Martingale" || position.SymbolCode != Symbol.Code) return; if (position.GrossProfit > 0) { ExecuteOrder(InitialQuantity, position.TradeType); } else { if (position.TradeType == TradeType.Sell) ExecuteOrder(position.Quantity * 2, TradeType.Buy); else ExecuteOrder(position.Quantity * 2, TradeType.Sell); } } private TradeType GetRandomTradeType() { return random.Next(2) == 0 ? TradeType.Buy : TradeType.Sell; } } }Best Regards,
Panagiotis
Thank you very much it works. straight perfectly.
Thank You.
Hi Mr Charalampous. the cBot works very well. i did sme tests and I figured out i need one more tweek in the lot multiplication. I need to multiply the lot by 110%. which will give a result that has may numbers after the coma. then i need to round that result into 2 decimal places before sending the order to the broker otherwise it gets rejected.
here is the part im talking about...............
here i need the *2 to be *1.1
if previous lot was 0.12, then we multiply by 1.1 the answer will be 1.132... but i need to have 1.13 to send it as a lot size to the broker.
then i need to take only 2 numbers after the coma to make a valid Lot size
kind regards.
if (position.TradeType == TradeType.Sell) ExecuteOrder(position.Quantity * 2, TradeType.Buy); else ExecuteOrder(position.Quantity * 2, TradeType.Sell);
@thamimsibi3
thamimsibi3
25 Mar 2019, 15:28
Hi Mr Charalampous. the cBot works very well. i did sme tests and I figured out i need one more tweek in the lot multiplication. I need to multiply the lot by 110%. which will give a result that has may numbers after the coma. then i need to round that result into 2 decimal places before sending the order to the broker otherwise it gets rejected.
here is the part im talking about...............
here i need the *2 to be *1.1
if previous lot was 0.12, then we multiply by 1.1 the answer will be 1.132... but i need to have 1.13 to send it as a lot size to the broker.
then i need to take only 2 numbers after the coma to make a valid Lot size
kind regards.
if (position.GrossProfit > 0)
{
ExecuteOrder(InitialQuantity, position.TradeType);
}
else
{
if (position.TradeType == TradeType.Sell)
ExecuteOrder(position.Quantity * 2, TradeType.Buy);
else
ExecuteOrder(position.Quantity * 2, TradeType.Sell);
@thamimsibi3
PanagiotisCharalampous
26 Mar 2019, 11:14
Hi thamimsibi3,
Try NormalizeVolume function. It will return you a valid volume for trading.
Best Regards,
Panagiotis
@PanagiotisCharalampous
pedroesplago
20 Aug 2022, 16:04
RE: Martingale that always Buy/Sell
PanagiotisCharalampous said:
Hi thamimsibi3,
Try NormalizeVolume function. It will return you a valid volume for trading.
Best Regards,
Panagiotis
Hi Sir a pleasant day to you! would you mind if you can make a code that always buys only no matter what is the outcome
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 SampleMartingalecBot : Robot
{
[Parameter("Initial Quantity (Lots)", DefaultValue = 0.01, MinValue = 0.01, Step = 0.01)]
public double InitialQuantity { get; set; }
[Parameter("Stop Loss", DefaultValue = 3)]
public int StopLoss { get; set; }
[Parameter("Take Profit", DefaultValue = 3)]
public int TakeProfit { get; set; }
private Random random = new Random();
protected override void OnStart()
{
Positions.Closed += OnPositionsClosed;
ExecuteOrder(InitialQuantity, GetRandomTradeType());
}
private void ExecuteOrder(double quantity, TradeType tradeType)
{
var volumeInUnits = Symbol.QuantityToVolumeInUnits(quantity);
var result = ExecuteMarketOrder(tradeType, SymbolName, volumeInUnits, "Martingale", StopLoss, TakeProfit);
if (result.Error == ErrorCode.NoMoney)
Stop();
}
private void OnPositionsClosed(PositionClosedEventArgs args)
{
Print("Closed");
var position = args.Position;
if (position.Label != "Martingale" || position.SymbolName != Symbol.Name)
return;
if (position.GrossProfit > 0)
{
ExecuteOrder(InitialQuantity, position.TradeType);
}
else
{
if (position.TradeType == TradeType.Sell)
ExecuteOrder(position.Quantity * 2, TradeType.Buy);
else
ExecuteOrder(position.Quantity * 2, TradeType.Sell);
}
}
private TradeType GetRandomTradeType()
{
return random.Next(2) == 0 ? TradeType.Buy : TradeType.Sell;
}
}
}
@pedroesplago
pedroesplago
20 Aug 2022, 16:38
RE: Martingale that execute Buys only
PanagiotisCharalampous said:
Hi thamimsibi3,
Try NormalizeVolume function. It will return you a valid volume for trading.
Best Regards,
Panagiotis
Hi Sir a pleasant day to you! would you mind if you can make a code that execute from the start of the trade is buy on not random and what ever the outcomes tp/sl must execute buy only. Thank you so much!
@pedroesplago
pedroesplago
21 Aug 2022, 05:46
RE: Martingale that always Buy
CAN SOMEONE HELP EDIT THE CODE OF MARTINGALE INSTEAD OF RANDOM I WANT IT BUY ONLY CONTINUOSLY, PLEASEEEE
@pedroesplago
PanagiotisCharalampous
22 Aug 2022, 08:41
Dear pedroesplago,
ExecuteOrder(InitialQuantity, GetRandomTradeType());
to
ExecuteOrder(InitialQuantity, TradeType.Buy);
and
if (position.GrossProfit > 0)
{
ExecuteOrder(InitialQuantity, GetRandomTradeType());
}
else
{
ExecuteOrder(position.Quantity * 2, position.TradeType);
}
to
if (position.GrossProfit > 0)
{
ExecuteOrder(InitialQuantity, TradeType.Buy);
}
else
{
ExecuteOrder(position.Quantity * 2, TradeType.Buy);
}
Best Regards,
Panagiotis
Join us on Telegram and Facebook
@PanagiotisCharalampous
pedroesplago
22 Aug 2022, 16:01
RE:
PanagiotisCharalampous said:
Sir Thank you soo much this will help to analyzed which cbot is more profitable.
@pedroesplago
pedroesplago
22 Aug 2022, 16:09
RE:
Sir I just want also to share to you about your code from you previous conversation with someone else here in this forum. It works perfectly when you backtest it from Aug 2021 upto today for EUR/USD
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 SampleMartingalecBot : Robot
{
[Parameter("Initial Quantity (Lots)", DefaultValue = 0.01, MinValue = 0.01, Step = 0.01)]
public double InitialQuantity { get; set; }
[Parameter("Stop Loss", DefaultValue = 3)]
public int StopLoss { get; set; }
[Parameter("Take Profit", DefaultValue = 3)]
public int TakeProfit { get; set; }
private Random random = new Random();
protected override void OnStart()
{
Positions.Closed += OnPositionsClosed;
ExecuteOrder(InitialQuantity, TradeType.Buy);
}
private void ExecuteOrder(double quantity, TradeType tradeType)
{
var volumeInUnits = Symbol.QuantityToVolumeInUnits(quantity);
var result = ExecuteMarketOrder(tradeType, SymbolName, volumeInUnits, "Martingale", StopLoss, TakeProfit);
if (result.Error == ErrorCode.NoMoney)
Stop();
}
private void OnPositionsClosed(PositionClosedEventArgs args)
{
Print("Closed");
var position = args.Position;
if (position.Label != "Martingale" || position.SymbolName != Symbol.Name)
return;
if (position.GrossProfit > 0)
{
ExecuteOrder(InitialQuantity, TradeType.Buy);
}
else
{
ExecuteOrder(position.Quantity * 2, TradeType.Buy);
}
}
private TradeType GetRandomTradeType()
{
return random.Next(2) == 0 ? TradeType.Buy : TradeType.Sell;
}
}
}
@pedroesplago
PanagiotisCharalampous
12 Mar 2019, 14:45
Hi thamimsibi3,
Thanks for posting in our forum. See below
Best Regards,
Panagiotis
@PanagiotisCharalampous