Check for free Margin then do Order ??

Created at 17 Dec 2020, 09:59
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
MA

Martin_I

Joined 14.09.2020

Check for free Margin then do Order ??
17 Dec 2020, 09:59


Hello,

i use a modified Version of NorsePiprunner,
How can the Programm check if i have enough Money for the Order?
And how does it know it is enough Margin for a BUY- or a SELL-Order left?

For Example:
If i have 1000 EUR and bought 20 Lots for 999 EUR, i could still sell 20 Lots.
How can the Program check that i cannot buy anymore but i still could sell 20 Lots?

Actual the Programm runs an Error on each tick if there are not enough Money (Margin) for a Trade, so the Server is a little busy!?!

 

 

 

 

 

        private int OrderSend(TradeType TrdTp, long iVol)
        {
            int orderStatus = 0;
            if (iVol > 0 && Account.FreeMargin > 0)
            {

                Print("OrderSend --- Before TradeResult ", TrdTp, " Margin: ", Account.FreeMargin);    


                TradeResult result = ExecuteMarketOrder(TrdTp, Symbol, iVol, Label, 0, 0, 0, "smart_grid");


                Print("OrderSend --- After TradeResult ", TrdTp, " Margin: ", Account.FreeMargin);
                Print("Account.FreeMargin", Account.FreeMargin, "Menge: ", iVol, " Ask Price: {0}", Symbol.Ask);
                Print(" V O L * A S K = ", iVol * Symbol.Ask, " Test2: ", Symbol.TickValue);

                if (result.IsSuccessful)
                {
                    Print(TrdTp, "Opened at: ", result.Position.EntryPrice);
                    orderStatus = 1;
                }
                else
                    Print(TrdTp, "Openning Error: ", result.Error);
            }
            else
                Print("Volume calculation error: Calculated Volume is: ", iVol, " FreeMargin= ", Account.FreeMargin);
            return orderStatus;
        }


@Martin_I
Replies

PanagiotisCharalampous
17 Dec 2020, 11:03

Hi Martin_I,

When you send an order and there is not enough margin available, the server will reject the orders with NOT_ENOUGH_MONEY message. To avoid this, you need to calculate the margin that will be required for your position (volume/leverage) and make sure that you have enough free margin, before executing the orders.

Best Regards,

Panagiotis 

Join us on Telegram 

 


@PanagiotisCharalampous

Martin_I
17 Dec 2020, 13:11 ( Updated at: 17 Dec 2020, 13:44 )

RE:

Hi,

and thank for your response.

I know the Error "NOT_ENOUGH_MONEY".
The Bot creates this Error on every Tick and the Broker called me, to stop the Bot.
My Problem is now, how to write the Programm, to avoid this Error --- ON THE SERVERSITE ---

I need to check the BUY-Margin / SELL-Margin before the Bots does 
                TradeResult result = ExecuteMarketOrder(TrdTp, Symbol, iVol, Label, 0, 0, 0, "smart_grid");
I would do this with an If-Loop ... but how ????

If Symbol.Margin.Buy ... Symbol.Margin ... ??? I run out of Ideas :(

My Bot is running 24/7 with expotentiell Lots. I wish a had so much Money that the Bot havent to stop ;)


@Martin_I

Martin_I
22 Dec 2020, 18:20

RE: RE:

Hello,

i have still the Problem, but i tried an other way.

Is it Possible to decide if its a BUY- or a SELL-ORDER.
When it is a NoMoneyError on the BUY site do this, when it is a NoMoney-Error on the Sell-Site do this?

I think there is an Error in there  ---  if (position.TradeType == TradeType.Buy)

 

This is my Error-Code


        protected override void OnError(Error error)
        {
                switch (error.Code)
                {
                        case ErrorCode.BadVolume: Print("Bad Volume");
                        break;
                        case ErrorCode.TechnicalError:Print("Technical Error");
                        break;
                        case ErrorCode.NoMoney:
                            if (position.TradeType == TradeType.Buy)
                            {
                                Kaufen = false;
                                Print("FEHLER - NO MONEY - KAUFEN = FALSE");
                            }
                            if (position.TradeType == TradeType.Sell)
                            {
                                Verkaufen = false;
                                Print("FEHLER - NO MONEY - VERKAUFEN = FALSE");
                            }
                        break;
                        case ErrorCode.Disconnected: Print("Disconnected");
                        break;
                        case ErrorCode.MarketClosed: Print("Market Closed");
                        break;
                }
        }


@Martin_I

PanagiotisCharalampous
23 Dec 2020, 08:11

Hi Martin_I,

Try this instead

        protected override void OnError(Error error)
        {
            
            switch (error.Code)
            {
                case ErrorCode.BadVolume:
                    Print("Bad Volume");
                    break;
                case ErrorCode.TechnicalError:
                    Print("Technical Error");
                    break;
                case ErrorCode.NoMoney:
                    if (error.TradeResult.Position.TradeType == TradeType.Buy)
                    {
                        Kaufen = false;
                        Print("FEHLER - NO MONEY - KAUFEN = FALSE");
                    }
                    if (error.TradeResult.Position.TradeType == TradeType.Sell)
                    {
                        Verkaufen = false;
                        Print("FEHLER - NO MONEY - VERKAUFEN = FALSE");
                    }
                    break;
                case ErrorCode.Disconnected:
                    Print("Disconnected");
                    break;
                case ErrorCode.MarketClosed:
                    Print("Market Closed");
                    break;
            }
        }


    }

Best Regards,

Panagiotis 

Join us on Telegram 


@PanagiotisCharalampous

Martin_I
23 Dec 2020, 10:07

RE:

Hi and thanks for your help.

Now i get this Error,  when i have not enough margin for buying:

23/12/2020 10:02:46.796 | Norse Piprunner, EURUSD, m1 | Crashed in OnError with NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.

 

Greetings

Martin


@Martin_I

... Deleted by UFO ...

PanagiotisCharalampous
23 Dec 2020, 11:31

Hi Martin_I,

Try checking if error.TradeResult is not null before you access it.

Best Regards,

Panagiotis 

Join us on Telegram 


@PanagiotisCharalampous