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

Help with coding, something really strange
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=5855
Page 1 of 1
Author:  LittleCaro [ Mon Jun 08, 2020 3:35 pm ]
Post subject:  Help with coding, something really strange

Hello guys,

I'm facing a really strange thing, that's several days i'm on it, and well ...

Here is the thing : i create something really basic with Steve's shell.

I want to set pending order + x pips to the high of the previous candle, the thing it :

It's working with High[0] and High[2], BUT it's not working with High[1] the one that i'm interested in.

The expert says error 130 invalid stops, and that's the same stops used with High[0] and High[2]


Maybe some of our geniuses can look at it.

Here is the code :

Code: Select all

 //Long 
   if (SendLong)
   {
       
     
      type=OP_BUYLIMIT;
      stype = " Buy Limit ";
      price = High[1] + (PipsAdjust/factor);
         
      if (!SendAlertNotTrade)
      {
         
         stop = CalculateStopLoss(OP_BUYLIMIT, price);
         
         
         take = CalculateTakeProfit(OP_BUYLIMIT, price);
         
         
         //Lot size calculated by risk
         if (!CloseEnough(RiskPercent, 0)) SendLots = CalculateLotSize(price, NormalizeDouble(stop + (HiddenPips / factor), Digits) );

         
         
      }//if (!SendAlertNotTrade)
      
      SendTrade = true;
And the code for calculating stoploss :

Code: Select all

double CalculateStopLoss(int type, double price)
{
   //Returns the stop loss for use in LookForTradingOpps and InsertMissingStopLoss
   double stop;

    
    
   RefreshRates();
   
   
   if (type == OP_BUYLIMIT)
   {
      if (!CloseEnough(StopLoss, 0) ) 
      {
         stop = price - (StopLoss / factor);
         HiddenStopLoss = stop;
      }//if (!CloseEnough(StopLoss, 0) ) 

      if (HiddenPips > 0 && stop > 0) stop = NormalizeDouble(stop - (HiddenPips / factor), Digits);
   }//if (type == OP_BUY)
Thanks guys !
Author:  LittleCaro [ Mon Jun 08, 2020 5:25 pm ]
Post subject:  Help with coding, something really strange

Hey guys,

When i was under my shower, i find partly a solution :

It's the setting : PostTradeAttemptWaitSecond, if i put it to zero, it works.

But now i have another problem, the limit order stays active for only 1 bar if he isn't triggerded, after he gets deleted.

You put a security to delete the pendings ? I looked through the code and find nothing ?
Author:  SteveHopwood [ Mon Jun 08, 2020 7:12 pm ]
Post subject:  Help with coding, something really strange

LittleCaro » Mon Jun 08, 2020 3:35 pm wrote:Hello guys,

I'm facing a really strange thing, that's several days i'm on it, and well ...

Here is the thing : i create something really basic with Steve's shell.

I want to set pending order + x pips to the high of the previous candle, the thing it :

It's working with High[0] and High[2], BUT it's not working with High[1] the one that i'm interested in.

The expert says error 130 invalid stops, and that's the same stops used with High[0] and High[2]


Maybe some of our geniuses can look at it.

Here is the code :

Code: Select all

 //Long 
   if (SendLong)
   {
       
     
      type=OP_BUYLIMIT;
      stype = " Buy Limit ";
      price = High[1] + (PipsAdjust/factor);
         
      if (!SendAlertNotTrade)
      {
         
         stop = CalculateStopLoss(OP_BUYLIMIT, price);
         
         
         take = CalculateTakeProfit(OP_BUYLIMIT, price);
         
         
         //Lot size calculated by risk
         if (!CloseEnough(RiskPercent, 0)) SendLots = CalculateLotSize(price, NormalizeDouble(stop + (HiddenPips / factor), Digits) );

         
         
      }//if (!SendAlertNotTrade)
      
      SendTrade = true;
And the code for calculating stoploss :

Thanks guys !
Good for you for getting your hands dirty by looking under the bonnet of CrapQl4. :clap: :clap: :clap: :clap: :clap: :clap: :clap: :clap: :clap:

Re this:
price = High[1] + (PipsAdjust/factor);

Supposing the market had fallen by the time your EA tries to execute this code. You could be trying to send a limit order at an impossible price because the market is already lower than your limit price. You need to check that 'price' is below the current market price so that a limit order is possible. From there, you either abort the trade or send a stop order.

Assuming you are happy to send a stop order, then the construct would be this:
type = BUYLIMIT;
price = High[1] + (PipsAdjust/factor);
if (price > Ask)//In English to follow
{
  • Abort the function if you will not allow stop orders instead of limit orders.
  • Adjust the type code etc if you are happy to accept stop orders.
}

Up an at 'em, Tiger. I hope this helps. Ask away if not.

:xm: :rocket:
Author:  SteveHopwood [ Mon Jun 08, 2020 7:22 pm ]
Post subject:  Help with coding, something really strange

LittleCaro » Mon Jun 08, 2020 5:25 pm wrote:Hey guys,

When i was under my shower, i find partly a solution :

It's the setting : PostTradeAttemptWaitSecond, if i put it to zero, it works.

But now i have another problem, the limit order stays active for only 1 bar if he isn't triggerded, after he gets deleted.

You put a security to delete the pendings ? I looked through the code and find nothing ?
Not that I can think of. Try my latest and see, then I will take a look if need be.

:xm: :rocket:
Author:  LittleCaro [ Tue Jun 09, 2020 7:42 am ]
Post subject:  Help with coding, something really strange

Hello Steve,

Thanks a lot for all your help.

I restarted from zero, with your latest shell code.

And all is working good.

I also realized that i made a mistake between stop and limit orders.

Sometimes i'm so silly :smile:


Thanks for all !
Author:  tzaro1 [ Wed Nov 03, 2021 2:04 pm ]
Post subject:  Help with coding, something really strange

LittleCaro » Tue Jun 09, 2020 5:12 pm wrote:Hello Steve,

Thanks a lot for all your help.

I restarted from zero, with your latest shell code.

And all is working good.

I also realized that i made a mistake between stop and limit orders.

Sometimes i'm so silly :smile:


Thanks for all !
Hi, glad you got it worked out. Regarding 1 bar expiry, the limit/ stop orders in Empty4 use an additional parameter variable of the type 'datetime'. It is the 2nd to last one and between magic number and color. Hope that helps.
All times are UTC Page 1 of 1