stevehopwoodforex.com
https://www.stevehopwoodforex.com/phpBB3/
Print view

Desky. TDesk's trading drone.
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=5545
Page 38 of 50
Author:  SteveHopwood [ Thu Jan 02, 2020 11:39 am ]
Post subject:  Desky. TDesk's trading drone.

wallywonka » Thu Jan 02, 2020 11:05 am wrote:
SteveHopwood » Thu Jan 02, 2020 9:27 am wrote:
wallywonka » Wed Jan 01, 2020 11:07 pm wrote:Seems to be a bug in 4b where it won't open a sell trade if set to open trade immediately. If you set to pending it will place the pending sell okay.
Make sure your MaxTradesPerDay input is set to zero.

:xm: :rocket:
Steve, yeah it is on Zero but only opens Buys when set to send trades immediately...

I am just using pending now as a workaround, no idea why it's happening.
I don't see how this can be caused by the max daily trades thingy, but do me a favour please. Load the code into the editor and do a search for //Max trades during the day

Comment out the code block, recompile and see if the bot starts sending sells again.

:xm: :rocket:
Author:  SteveHopwood [ Thu Jan 02, 2020 1:16 pm ]
Post subject:  Desky. TDesk's trading drone.

V 4c is inpost 1, with the scaling back in code added. Details in the user guide.

DIYers, add the inputs. Search for: extern int NoOfLevels=4;

Add the scale back in inputs underneath:
extern bool UseScaleBackIn=false;
extern string ScaleBackInTradeComment="Scale in";

Go to: bool scaleOutStopLoss(int ticket)

Replace the entire function with this:

Code: Select all

bool scaleOutStopLoss(int ticket)
{

   /*
   Called from countOpenTrades()
   
   This function examines an open trade to see if the phased stop loss should be used.
   Returns 'true' if there is a part-closure, else false.
   
   This cannot be applied to hedged trades.
   */
   
   //Check the order is still open
   if (!betterOrderSelect(ticket, SELECT_BY_TICKET, MODE_TRADES))
      return(false);
      
   //Cannot work this function if there is no stop loss
   //if (closeEnough(OrderStopLoss(), 0) ) 
   //   return(false); 

   //Save stuff for easier typing
   int type = OrderType();
   string symbol = OrderSymbol();
   double price = OrderOpenPrice();
   double stop = OrderStopLoss();
   double take = OrderTakeProfit();
   if (HideStopLossAndTakeProfit)
      stop = calculateStopLoss(symbol, type, price);
      
   getBasics(symbol);//Always good to include this in a function

   //No further action needed if the trade is in profit
   if (type == OP_BUY)
      if (bid > price)
      {
         return(false);
      }//if (bid > price)
      
   if (type == OP_SELL)
      if (ask < price)
      {
         return(false);
      }//if (ask < price)
      
   
   //We have a trade and it is a loser. Calculate the distance
   //in between the order open price and the stop loss. The order open price
   //does not change following a partial closure. Not when tested on a GP demo, at least. Fuck
   //only knows what happens with the criminal element masquerading as 'brokers'.
   double slpips = MathAbs(price - stop);
   //Calculate the sl levels at which to partially close the trade.
   double slLevel = slpips / NoOfLevels;
   
   //A variable to tell the code that part-closure is needed.
   bool closeNeeded = false;
   //A variable to hold the calculated price at each level.
   double calculatedLevel = price;
   //A variable to hold the amount to part-close
   double lotsToClose = normalizeLots(symbol, Lot / NoOfLevels);
   //Alert(lotsToClose);
   double targetLots = 0;
   
   //Loop through the levels and set the closeNeeded bool if the market has moved to
   //the calculatedLevel price
   for (int cc = 1; cc <= NoOfLevels; cc++)
   {
            
      //Buy trade
      if (type == OP_BUY)
      {
         //The trade is losing by at least one level
         calculatedLevel-= slLevel;
         if (bid <= calculatedLevel)
         {
            //We need to know if there has already been a partial close at this stop level
            targetLots = Lot - (lotsToClose * cc);            
            if (OrderLots() > targetLots)
            {
               closeNeeded = true;
               break;
            }//if (OrderLots() > targetLots)
               
         }//if (bid <= calculatedLevel)
            
      }//if (type == OP_BUY)
      
      
      //Sell trade
      if (type == OP_SELL)
      {
         //The trade is losing by at least one level
         calculatedLevel+= slLevel;
         if (ask >= calculatedLevel)
         {
            //We need to know if there has already been a partial close at this stop level
            targetLots = Lot - (lotsToClose * cc);            
            if (OrderLots() > targetLots)
            {
               closeNeeded = true;
               break;
            }//if (OrderLots() > targetLots)
         }//if (ask <= calculatedLevel)
            
      }//if (type == OP_SELL)
      
      
      //Alert(calculatedLevel);
   }//for (int cc = 1; cc <= NoOfLevels; cc++)
   
   //Should part of the trade be closed:
   if (closeNeeded)
   {
      bool result = OrderClose(ticket, lotsToClose, OrderClosePrice(), 5, clrNONE);
      if (result)
      {
         //Replace the trade with a stop order if scaling back in
         if (UseScaleBackIn)
         {
            int tries = 0;//To exit an endless loop because something went wrong
            double newPrice = 0;

            //Buy stop
            if (type == OP_BUY)
            {
               //Calculate the price for the stop order
               newPrice = NormalizeDouble(calculatedLevel + slLevel, digits);
               
               //We must have the order sent
               result = false;
               while (!result)
               {
                  if (IsTradeContextBusy() )
                     Sleep(2000);

                  result = sendSingleTrade(symbol, OP_BUYSTOP, ScaleBackInTradeComment, lotsToClose, newPrice, stop, take);
                  
                  //Exit an endless loop if something went wrong
                  if (!result)
                  {
                     tries++;
                     if (tries >= 100)
                        break;
                     Sleep(1000);   
                  }//if (!result)
               }//while (!result)
            }//if (type == OP_BUY)
            
         
            //Sell stop
            if (type == OP_SELL)
            {
               //Calculate the price for the stop order
               newPrice = NormalizeDouble(calculatedLevel - slLevel, digits);

               //We must have the order sent
               result = false;
               while (!result)
               {
                  if (IsTradeContextBusy() )
                     Sleep(2000);

                  result = sendSingleTrade(symbol, OP_SELLSTOP, ScaleBackInTradeComment, lotsToClose, newPrice, stop, take);
                  //Exit an endless loop if something went wrong
                  if (!result)
                  {
                     tries++;
                     if (tries >= 100)
                        break;
                     Sleep(1000);   
                  }//if (!result)
               }//while (!result)
            }//if (type == OP_SELL)
            
         
         }//if (UseScaleBackIn)
         
         
         return(true);
      }//if (result)
   }//if (closeNeeded)
   

   //Got here, so no closure
   return(false);

}//End bool scaleOutStopLoss(int ticket)
The scale back in code is the same as the code I added earlier to SPB. There may be bugs.

:xm: :rocket:
Author:  SteveHopwood [ Fri Jan 03, 2020 1:41 pm ]
Post subject:  Desky. TDesk's trading drone.

4d is in post 1. The scale in/out code is working properly.

I explain how to DIY in this post: viewtopic.php?p=169327#p169327

:xm: :rocket:
Author:  Sojourner [ Fri Jan 03, 2020 7:27 pm ]
Post subject:  Desky. TDesk's trading drone.

Getting this when Trying to compile:
Author:  SteveHopwood [ Fri Jan 03, 2020 7:45 pm ]
Post subject:  Desky. TDesk's trading drone.

Sojourner » Fri Jan 03, 2020 7:27 pm wrote:Getting this when Trying to compile:
Get the correct TDesk stuff downloaded you idiot.

You are supposed to know what you are talking about when you post.

Or are you merely a dimwit in disguise? In need of demotion?
Author:  Sojourner [ Fri Jan 03, 2020 7:53 pm ]
Post subject:  Desky. TDesk's trading drone.

Sorry but I thought I did. I've been using Taipan's thing and got an email that said there was a new Desky so I downloaded it. Never had a problem compiling desky before.
Maybe I've got my posts mixed up.
Author:  Vokin [ Mon Jan 06, 2020 1:40 pm ]
Post subject:  Desky. TDesk's trading drone.

Sojourner » Fri Jan 03, 2020 8:53 pm wrote:Sorry but I thought I did. I've been using Taipan's thing and got an email that said there was a new Desky so I downloaded it. Never had a problem compiling desky before.
Maybe I've got my posts mixed up.
I had the same problem a long time ago, the mistake was always on my side. I compiled in the editor from the directory where the related files were missing. :youknow:
Author:  Vokin [ Fri Jan 10, 2020 2:05 pm ]
Post subject:  Desky. TDesk's trading drone.

Hi Steve,
maybe something wrong with basket - or my mistake. I'm set TP 75 for basket, but at screen is TP10 - so it also closed.
Can you help me?
Regards
V.
Author:  SteveHopwood [ Fri Jan 10, 2020 2:47 pm ]
Post subject:  Desky. TDesk's trading drone.

Vokin » Fri Jan 10, 2020 2:05 pm wrote:Hi Steve,
maybe something wrong with basket - or my mistake. I'm set TP 75 for basket, but at screen is TP10 - so it also closed.
Can you help me?
Regards
V.
Your mistake. It is Friday. Look at your BasketFridayCashTargets input.

:xm: :rocket:
Author:  Vokin [ Fri Jan 10, 2020 3:24 pm ]
Post subject:  Desky. TDesk's trading drone.

Understand - I thought the setting:
-- Trading done for the day inputs -- false, applies to the entire section.
Anyway - thanks for answer.
All times are UTC Page 38 of 50