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

Desky. TDesk's trading drone.
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=5545
Page 29 of 50
Author:  renexxxx [ Sun Apr 14, 2019 11:34 pm ]
Post subject:  Desky. TDesk's trading drone.

What I usually try to do is to calculate for each symbol a lotSizeMultiplier such that if that symbol moves its expected dailyATR pips (per day), its dollar profit is the same for each symbol.

So, we need to calculate for each symbol its dailyATR and a lotSizeMultiplier. (I do this with a struct for clarity, but you can do this you own way).

Let's say, you have an array of symbols. This array is populated with all the symbols that the EA is managing.

Code: Select all

struct SYMBOL {
   string name;
   double pipFactor;
   double dailyATR;
   double lotSizeMultiplier;
};

SYMBOL mySymbols[];
(During the initialization phase in your EA, you have set the name and the pipFactor for each symbol in that array).

In the OnTimer() event handler, you would put a call to updateSymbols(), with updateSymbols defined as:

Code: Select all

void updateSymbols (SYMBOL &symbols[]) {

   int    numATR = 0;
   double totalATRProfit = 0.0;

   for (int iSymbol=0; iSymbol < ArraySize(symbols); iSymbol++) {
   
      symbols[iSymbol].dailyATR = iATR( symbols[iSymbol].name, PERIOD_D1, atrPeriod, 1 );

      if ( symbols[iSymbol].dailyATR > 0.0 ) {                   // Only add to totalATRProfit if dailyATR > 0.0
         totalATRProfit += symbols[iSymbol].dailyATR * symbols[iSymbol].pipFactor * MarketInfo( symbols[iSymbol].name, MODE_TICKVALUE );
         numATR++;
      }
   }
   
   if ( numATR > 0 ) {
      double averageATR = totalATRProfit / numATR;
      for (int iSymbol = 0; iSymbol < ArraySize(symbols); iSymbol++) {
         if ( symbols[iSymbol].dailyATR > 0.0 ) 
            symbols[iSymbol].lotSizeMultiplier = averageATR / ( symbols[iSymbol].dailyATR * symbols[iSymbol].pipFactor * MarketInfo( symbols[iSymbol].name, MODE_TICKVALUE ) );
         else 
            symbols[iSymbol].lotSizeMultiplier = 1.0;
      }
   }
}
In this loop, we work out the dailyATR for each symbol in the symbols-array. If this value is non-zero (that should always be the case, unless we haven't loaded enough data), we try to find a lotSizeMultiplier such that the value: lotSizeMultiplier * dailyATR * pipFactor * MODE_TICKVALUE is the same for each symbol.

Once we have this lotSizeMultiplier for each symbol, a trade would typically be opened with:

Code: Select all

void openTrade( SYMBOL &symbol ) {

   double lotSize = LotSize * symbol.lotSizeMultiplier;
   
   if ( lotSize < MarketInfo( symbol.name, MODE_MINLOT ) ) {
      lotSize = MarketInfo( symbol.name, MODE_MINLOT );
   }
   else {
      lotSize = MathFloor( lotSize / MarketInfo(symbol.name, MODE_LOTSTEP) ) * MarketInfo(symbol.name, MODE_LOTSTEP);
   }
}
In this code, we use the lotSizeMultiplier to multiply the standard LotSize and there is some additional logic to ensure that the lotSize is not smaller than MODE_MINLOT and is a multiple of MODE_LOTSTEP.

Hope that this gives a general idea.
Author:  tomele [ Mon Apr 15, 2019 12:03 am ]
Post subject:  Desky. TDesk's trading drone.

Yet another example of René's brilliance.

I still learn from your code, my friend.

:clap: :clap: :clap: :clap: :clap: :clap: :clap:

Thomas
Author:  SteveHopwood [ Tue Apr 16, 2019 12:52 pm ]
Post subject:  Desky. TDesk's trading drone.

renexxxx » Sun Apr 14, 2019 11:34 pm wrote:What I usually try to do is ......................................

Hope that this gives a general idea.
Thanks Rene. That pointed me in the right direction. :clap: :clap: :clap: :clap: :clap:

Folks, the attached is intended only to test my code. We only need the lot size calculated at order send time, so I have hacked Rene's code. Run it once and then disable EA's. It will show each symbol and the result of the calculation in an Alert, so have a look and see if they look correct/ish.

The function is double GetDifferentialLotSize(string symbol) starting at line 6585 and the call is at line 6779, followed by the alert.

:xm: :rocket:
Author:  SteveHopwood [ Wed Apr 17, 2019 12:28 pm ]
Post subject:  Desky. TDesk's trading drone.

V 3j is in post 1. Rene has confirmed that my differential lot sizing code is accurate save one addition to cover an unlikely but possible, circumstance. Thanks again Rene. :clap: :clap: :clap: :clap: :clap: :clap: :clap: :clap:

There are a few lot sizing options now, so I have moved them to their own inputs section.

:xm: :rocket:
Author:  SteveHopwood [ Thu Apr 18, 2019 3:43 pm ]
Post subject:  Desky. TDesk's trading drone.

Some of you may notice that there is an update in post 1. You do not need it.

One of our dafter members loaded Desky onto several charts and then wondered why there were several trades all sent at the same price point. Firm grasp of reality demonstrated there. :arrrg: :arrrg: :arrrg: :arrrg: :arrrg:

This moron had a lucky escape because I ignored him and so did not spend several hours trying to find a bug that does not exist. You can perhaps imagine how I would have expressed a modicum of displeasure.

The update contains code sent by Thomas, to stop other certifiable nutters from making the same lunatic mistake.

:xm: :rocket:
Author:  SteveHopwood [ Wed Apr 24, 2019 1:52 pm ]
Post subject:  Desky. TDesk's trading drone.

V 3k is in post 1.

I have added a couple of inputs to the swap section:
  • BuyOnlyPairs: this is a comma separated list of pairs that you only want to trade long. Do not leave spaces and do not add a comma at the end. For example:
    • BuyOnlyPairs could hold GBPCHF, USDCHF
  • SellOnlyPairs: this is a comma separated list of pairs that you only want to trade short. Do not leave spaces and do not add a comma at the end. For example:
    • SellOnlyPairs could hold EURUSD,EURAUD
billv wrote about this in the PoS thread, so I decided to add it here and then copy it across. I shall add it to SCB as well.

:xm: :rocket:
Author:  SteveHopwood [ Wed Apr 24, 2019 3:44 pm ]
Post subject:  Desky. TDesk's trading drone.

kwchau has just written to say that the latest swap feature is not working for grid trading. That will be because I have missed a place where the checks are needed. I will sort that out easily enough.

Sing out if you find this feature not working elsewhere.

:xm: :rocket:
Author:  SteveHopwood [ Wed Apr 24, 2019 4:33 pm ]
Post subject:  Desky. TDesk's trading drone.

I have found the best place to add the swap checks. kwchau also wrote that the original checks were not working either - now I see why.

If you downloaded 3k before this bloop emerged and want to DIY then this is easy. Do a search for this function definition:
void CanWeSendTrades(string symbol, int pairIndex, int signal)

Insert this at the top of the function:

Code: Select all

   //Swap filter
   TradeDirectionBySwap(symbol);
   if (!BuySignal)
      if (!SellSignal)
         return;//Swap filter canceled the signal
  
I have not bothered with the CTS trading - I can always add it if there is a need later on.

:xm: :rocket:
Author:  SumTinWong [ Wed Apr 24, 2019 4:49 pm ]
Post subject:  Desky. TDesk's trading drone.

First Post here and I'm really trying to come accross as somewhat intelligent. Thank you Steve et al for this amazing repository of knowledge and very interesting tools.

I have been playing around with TDESK and Desky (currently version 3i)for several weeks now and noticed a problem with grid trading returning error 130: Invalid Stops. Naturally I assumed that I must have broken something in my settings. The lightbulb moment came when I realized this error only appeared where there were existing profitable trades on the specific pair.

I could not find any combination of settings to fix the situation but I did make this small adjustment to Desky which fixed the invalid stop error.

Steve, I hope the following meets your approval, I'd hate to be banned from here

Code: Select all

Line 5144 change
                  SendBuyGrid(symbol, OP_BUYSTOP, HighestBuyPrice, Lot, targetNoOfPendingTrades); 
To
                 SendBuyGrid(symbol, OP_BUYSTOP, MathMax(ask,HighestBuyPrice), Lot, targetNoOfPendingTrades);

Line 5148 change
                  SendBuyGrid(symbol, OP_BUYLIMIT, LowestBuyPrice, Lot, targetNoOfPendingTrades); 
To
                  SendBuyGrid(symbol, OP_BUYLIMIT, MathMin(ask,LowestBuyPrice), Lot, targetNoOfPendingTrades);

Line 5167 change
                  SendSellGrid(symbol, OP_SELLSTOP, LowestSellPrice, Lot, targetNoOfPendingTrades);   
To
                  SendSellGrid(symbol, OP_SELLSTOP, MathMin(ask,LowestSellPrice), Lot, targetNoOfPendingTrades);    

Line 5171 change
                  SendSellGrid(symbol, OP_SELLLIMIT, HighestSellPrice, Lot, targetNoOfPendingTrades);
To
                  SendSellGrid(symbol, OP_SELLLIMIT, MathMax(ask,HighestSellPrice), Lot, targetNoOfPendingTrades);
Author:  SteveHopwood [ Wed Apr 24, 2019 8:30 pm ]
Post subject:  Desky. TDesk's trading drone.

SumTinWong » Wed Apr 24, 2019 4:49 pm wrote:First Post here and I'm really trying to come accross as somewhat intelligent. Thank you Steve et al for this amazing repository of knowledge and very interesting tools.

I have been playing around with TDESK and Desky (currently version 3i)for several weeks now and noticed a problem with grid trading returning error 130: Invalid Stops. Naturally I assumed that I must have broken something in my settings. The lightbulb moment came when I realized this error only appeared where there were existing profitable trades on the specific pair.

I could not find any combination of settings to fix the situation but I did make this small adjustment to Desky which fixed the invalid stop error.

Steve, I hope the following meets your approval, I'd hate to be banned from here

Code: Select all

Line 5144 change
                  SendBuyGrid(symbol, OP_BUYSTOP, HighestBuyPrice, Lot, targetNoOfPendingTrades); 
To
                 SendBuyGrid(symbol, OP_BUYSTOP, MathMax(ask,HighestBuyPrice), Lot, targetNoOfPendingTrades);

Line 5148 change
                  SendBuyGrid(symbol, OP_BUYLIMIT, LowestBuyPrice, Lot, targetNoOfPendingTrades); 
To
                  SendBuyGrid(symbol, OP_BUYLIMIT, MathMin(ask,LowestBuyPrice), Lot, targetNoOfPendingTrades);

Line 5167 change
                  SendSellGrid(symbol, OP_SELLSTOP, LowestSellPrice, Lot, targetNoOfPendingTrades);   
To
                  SendSellGrid(symbol, OP_SELLSTOP, MathMin(ask,LowestSellPrice), Lot, targetNoOfPendingTrades);    

Line 5171 change
                  SendSellGrid(symbol, OP_SELLLIMIT, HighestSellPrice, Lot, targetNoOfPendingTrades);
To
                  SendSellGrid(symbol, OP_SELLLIMIT, MathMax(ask,HighestSellPrice), Lot, targetNoOfPendingTrades);
Thanks for a most helpful post. :clap: :clap: :clap: :clap: :clap:

I have updated Desky with this. Considering the changes today I have also updated the version number to 3l.

This is an update you need folks. Clearly if one member can encounter this problem then we all will at some point - even if only rarely.

I have replaced 'ask' with 'bid' in the sellstop/limit code. DIYers, here is how:

Do a search for:SendBuyGrid(symbol, OP_BUYSTOP, HighestBuyPrice, Lot, targetNoOfPendingTrades);
Replace it with:
SendBuyGrid(symbol, OP_BUYSTOP, MathMax(ask,HighestBuyPrice), Lot, targetNoOfPendingTrades);

Do a search for: SendBuyGrid(symbol, OP_BUYLIMIT, LowestBuyPrice, Lot, targetNoOfPendingTrades);
Replace it with:
SendBuyGrid(symbol, OP_BUYLIMIT, MathMin(ask,LowestBuyPrice), Lot, targetNoOfPendingTrades);

Do a search for: SendSellGrid(symbol, OP_SELLLIMIT, HighestSellPrice, Lot, targetNoOfPendingTrades);
Replace it with:
SendSellGrid(symbol, OP_SELLSTOP, MathMin(bid, LowestSellPrice), Lot, targetNoOfPendingTrades);

Do a search for:
SendSellGrid(symbol, OP_SELLLIMIT, HighestSellPrice, Lot, targetNoOfPendingTrades);
Replace it with:
SendSellGrid(symbol, OP_SELLLIMIT, MathMax(bid,HighestSellPrice), Lot, targetNoOfPendingTrades);

:xm: :rocket:
All times are UTC Page 29 of 50