How to Reverse aPosition
            
                 06 Sep 2018, 16:18
            
                    
hello
i want to edit this code , but it's not accepted by cAlgo , plz help
            if (position.GrossProfit > 0)
            {
                  ());ExecuteOrder(InitialVolume, GetRandomTradeType
            }
            else
            {
                ExecuteOrder((int)position.Volume * 2, position.TradeType);
            }
        }
the problem is when the code closes a position with no Gross profit , it creates a new postion with the same trade type , i .want it to create a new position but reverse the closed one .
like if a buy trade closed with losses , open a sell trade type , if that sell trade closes with loss , open a buy trade type ...etc
or
( how to use that code with no errors ( ReversePosition
sorry for my Bad english :)
Replies
                     useretinv
                     08 Sep 2018, 11:58
                                    
. Many thanks Mr.Panagiotis
.IT WORKS
PLZ help me to ADD server hours to Martingle sample code , Open and close cbot Automatically at specified hours
i tryied to put
( if (Server.Time.Hour >= 7 && Server.Time.Hour < 10
( as you advised in ( https://ctrader.com/forum/cbot-support/13565
but it's not working with Sample Martingle Code .plz help
//////////////////////////////////////////
Also i want to Add a simple logic to the code to prevent it opening new positions if the the first position hits the stop loss in the current bar .
for Example : if the code was working on 30min bar chart , and the first position hits a stop loss. don't open new position at the same bar (30min bar ) i want the cbot to wait
another 2 or 3 bars latter to open the new position with the Initial Volume .
/////////////////////////////////////
Also plz i want to add Maximum volume to prevent cbot to doubling position volume with no limit , just add parameter of maximum orders or maximum volume that cbot can use from Account to open position , after reaching the maximum orders , make cbot start over withthe Initial Volume .
ALL IN Martingle Sample code Plz
Many many Thanks in advance, Mr.Panagiotis
// -------------------------------------------------------------------------------------------------
//
//    This code is a cAlgo API sample.
//
//    This robot 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
//
//    All changes to this file will be lost on next application start.
//    If you are going to modify this file please make a copy using the "Duplicate" command.
//
//    The "Sample Martingale Robot" 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 robot 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.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class SampleMartingaleRobot : Robot
    {
        [Parameter("Initial Volume", DefaultValue = 10000, MinValue = 0)]
        public int InitialVolume { get; set; }
 
        [Parameter("Stop Loss", DefaultValue = 40)]
        public int StopLoss { get; set; }
 
        [Parameter("Take Profit", DefaultValue = 40)]
        public int TakeProfit { get; set; }
         
        private Random random = new Random();
 
        protected override void OnStart()
        {
            Positions.Closed += OnPositionsClosed;
 
            ExecuteOrder(InitialVolume, GetRandomTradeType());
        }
 
        private void ExecuteOrder(long volume, TradeType tradeType)
        {
            var result = ExecuteMarketOrder(tradeType, Symbol, volume, "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(InitialVolume, GetRandomTradeType());
            }
            else
            {
                ExecuteOrder((int)position.Volume * 2, position.TradeType);
            }
        }
 
        private TradeType GetRandomTradeType()
        {
            return random.Next(2) == 0 ? TradeType.Buy : TradeType.Sell;
        }
    }
}
@useretinv
                     PanagiotisCharalampous
                     10 Sep 2018, 10:06
                                    
Hi useretinv,
Unfortunately I cannot engage in custom development requests. If you want somebody to help you with developing your cBot, you can post a job or hire a professional consultant.
Best Regards,
Panagiotis
@PanagiotisCharalampous
                     useretinv
                     10 Sep 2018, 16:17
                                    
OK , just sample code to start and stop cbot within specified hours , plz
and maximum volume that cbot can use from account
thank you
@useretinv
                     tomopeov
                     23 May 2019, 06:35
                                    
RE:
Panagiotis Charalampous said:
Hi useretinv,
You can use the code below
if (position.GrossProfit > 0) { ExecuteOrder(InitialQuantity, GetRandomTradeType()); } else { if (position.TradeType == TradeType.Buy) ExecuteOrder(position.Quantity * 2, TradeType.Sell); else ExecuteOrder(position.Quantity * 2, TradeType.Buy); }Best Regards,
Panagiotis
Hi Panagiotis,
What if I want to place an order instead of executing a trade? For example if a buy trade is closed at a take profit target I want to place a sell trade order with the same entry price, trade size and take profit limit in pips as the closed trade and vice versa?
Thank you
@tomopeov
                     PanagiotisCharalampous
                     23 May 2019, 10:15
                                    
Hi tomopeov,
It would look something like this
        protected override void OnStart()
        {
            Positions.Closed += Positions_Closed;
        }
        private void Positions_Closed(PositionClosedEventArgs obj)
        {
            if (obj.Reason == PositionCloseReason.TakeProfit)
            {
                if (obj.Position.TradeType == TradeType.Buy)
                {
                   PlaceStopOrder(TradeType.Sell, Symbol, obj.Position.VolumeInUnits, obj.Position.EntryPrice);                 
                }
                else
                {
                    PlaceStopOrder(TradeType.Buy, Symbol, obj.Position.VolumeInUnits, obj.Position.EntryPrice);
                }
            }
        }
Best Regards,
Panagiotis
@PanagiotisCharalampous
                     tomopeov
                     05 Sep 2019, 17:53
                                    
RE:
Hi Panagiotis Charalampous,
Currently, I have this bot that can make a re-entry order when a trade is stopped out.
If I want it to make n+1 re-entry orders plus a reverse order when n trades are stopped out how do I do it?
For example, if I have 2 long 10k eurusd trades at 1.20 with stop loss 20 pips. When the price goes to 1.1980 both trades get stopped out and I want the bot to create 3 long 10k eurusd orders at 1.20 and 1 short 10k eurusd order at 1.20 as well with stop loss at 20 pips each. And vice versa
protected override void OnStart() { Positions.Closed += Positions_Closed; } private void Positions_Closed(PositionClosedEventArgs obj) { if (obj.Reason == PositionCloseReason.StopLoss) PlaceStopLimitOrder(obj.Position.TradeType, Symbol, obj.Position.VolumeInUnits, obj.Position.EntryPrice, stopLimitRangePips, "reEntry", StopLoss, TakeProfit); } } }Best Regards,
Tomo P
@tomopeov
                     PanagiotisCharalampous
                     09 Sep 2019, 09:50
                                    
Hi Tomo,
This needs some development and I cannot engage into custom development. If you need assistance in developing your cBot, I would suggest you contact a Consultant or post a Job.
Best Regards,
Panagiotis
@PanagiotisCharalampous

PanagiotisCharalampous
06 Sep 2018, 16:34
Hi useretinv,
You can use the code below
if (position.GrossProfit > 0) { ExecuteOrder(InitialQuantity, GetRandomTradeType()); } else { if (position.TradeType == TradeType.Buy) ExecuteOrder(position.Quantity * 2, TradeType.Sell); else ExecuteOrder(position.Quantity * 2, TradeType.Buy); }Best Regards,
Panagiotis
@PanagiotisCharalampous