Ctrade Error

themasterx7
150
themasterx7  

hi 

I need your help i dont know what is wrong with this code it did nothing when i tested in the strategy tester here is the code 

if (signal =="sell" && PositionsTotal()<1)
   trade.Sell(0.10,NULL,Bid,(Bid+50*_Point),(Bid-150 * _Point),NULL);

if (signal =="buy" && PositionsTotal()<1)
   trade.Buy(0.10,NULL,Ask,(Ask-50*_Point),(Ask+150 * _Point),NULL);

------------------------------------------------------------------------------------------------------------------------------------------------

when i run this version its running just fine but i need to add stop lose i dont know how can i fix it please Help!!


if (signal =="sell" && PositionsTotal()<1)
  trade.Sell(0.10,NULL,Bid,0,(Bid-150 * _Point),NULL);

if (signal =="buy" && PositionsTotal()<1)
   trade.Buy(0.10,NULL,Ask,0,(Ask+150 * _Point),NULL);
The Fundamentals of Testing in MetaTrader 5
The Fundamentals of Testing in MetaTrader 5
  • www.mql5.com
The idea of ​​automated trading is appealing by the fact that the trading robot can work non-stop for 24 hours a day, seven days a week. The robot does not get tired, doubtful or scared, it's is totally free from any psychological problems. It is sufficient enough to clearly formalize the trading rules and implement them in the algorithms, and...
Vladimir Karputov
Moderator
224703
Vladimir Karputov  

1. Please use the button Code.

2. What is in your code 'Bid'  and 'Ask'?  How do you get these variables? How do you populate these variables?

themasterx7
150
themasterx7  

Vladimir Karputov:

1. Please use the button .

2. What is in your code 'Bid'  and 'Ask'?  How do you get these variables? How do you populate these variables?


this is my program this one is working but i need to do the other one the one with the stop lose

#include<Trade\Trade.mqh>
// Create a trade
CTrade trade;
      
void OnTick()
  {
// calculate the ask price
double Ask= NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);

// calculate the Bid price
double Bid= NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);

// get the acc Balance
double Balance=AccountInfoDouble(ACCOUNT_BALANCE);

// get the acc Equity
double Equity=AccountInfoDouble(ACCOUNT_EQUITY);





// create an array for the prices
MqlRates Priceinfo[];

// we fill the array with the price data
int PriceData =CopyRates(Symbol(),Period(),0,10,Priceinfo);

// create a string for the signal 
string signal="";

// create an array for several prices
double myPriceArraySMA[];
double myPriceArrayMacD[];

// define the properties of the moving average
int movingAverageDefinition =iMA (_Symbol,_Period,100,0,MODE_SMA,PRICE_CLOSE);

 // defined Ea one line, current candle, 3 candles , store result   
CopyBuffer(movingAverageDefinition,0,0,10,myPriceArraySMA);

// define the properties of the  MacD
int MacDDefinition= iMACD(_Symbol,_Period,12,26,9,PRICE_CLOSE);

// define MA1, one buffer, current candle, 3 candles, store result
CopyBuffer(MacDDefinition,0,0,10,myPriceArrayMacD);

// get the value of the current candle
double MacDValue=(myPriceArrayMacD[0]);


// if the price is above the SMA
if (Priceinfo[1].close > myPriceArraySMA[1])

// and was above the SMA before
if (Priceinfo[3].close > myPriceArraySMA[3])

if (MacDValue>0.000630)
{
signal="buy";
}   

// if the price is below the SMA
if (Priceinfo[1].close < myPriceArraySMA[1])

// and was above the SMA before
if (Priceinfo[3].close < myPriceArraySMA[3])


if (MacDValue<-0.000630)
{
signal="sell";
}

/*-------------------------------------------------------------------------------------------------------
//if no open positions SELL
if (PositionsTotal()==0 && OrdersTotal()==0)
{
trade.SellStop(0.10,Bid-10*_Point,_Symbol,Bid+50*_Point,Bid-150*_Point,ORDER_TIME_GTC,0,NULL);
}


//if no open positions BUY
if (PositionsTotal()==0 && OrdersTotal()==0)
{
trade.BuyStop(0.10,Ask-10*_Point,_Symbol,Ask+50*_Point,Ask-150*_Point,ORDER_TIME_GTC,0,NULL);
}
------------------------------------------------------------------------------------------------------------*/

//----------------------------------------------------------------------------------------------
//sell 10 microlot
if (signal =="sell" && PositionsTotal()<1)
trade.Sell(0.10,NULL,Bid,0,(Bid-150 * _Point),NULL);
//trade.SellLimit(0.10,NULL,_Symbol,50,150,ORDER_TIME_GTC,0,NULL);

// buy 10 microlot
if (signal =="buy" && PositionsTotal()<1)
trade.Buy(0.10,NULL,Ask,0,(Ask+150 * _Point),NULL);
//trade.BuyLimit(0.10,NULL,_Symbol,50,150,ORDER_TIME_GTC,0,NULL);
//-------------------------------------------------------------------------------------------------------
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
// chart output
Comment ("The Signl is now: ",signal);

  }
//+-----------------------------------------------------------------
Vladimir Karputov
Moderator
224703
Vladimir Karputov  
themasterx7 :


this is my program this one is working but i need to do the other one the one with the stop lose

The indicator handle needs to be received once in OnInit.

Correct your code. Until correct, I will not look further.

themasterx7
150
themasterx7  
Vladimir Karputov:

The indicator handle needs to be received once in OnInit.

Correct your code. Until correct, I will not look further.

Hi

Im new at this can you recommend a book or any learning material that help me on this issue?

Thank You 
themasterx7
150
themasterx7  

hello I need help guys the gentleman said that i had to correct my code so he can help me :) help me correct it this is what help means ??!! Thank You.

Vladimir Karputov
Moderator
224703
Vladimir Karputov  
themasterx7 :

hello I need help guys the gentleman said that i had to correct my code so he can help me :) help me correct it this is what help means ??!! Thank You.

Correct your code: now you are creating a new indicator handle FOR EVERY TICK - this is wrong, this is a mistake. An indicator handle must be created ONCE (the indicator handle is created in OnInit ()).

Seng Joo Thio
1792
Seng Joo Thio  
themasterx7:

hello I need help guys the gentleman said that i had to correct my code so he can help me :) help me correct it this is what help means ??!! Thank You.

He said "The indicator handle needs to be received once in OnInit.", to understand, you can refer to :

https://www.mql5.com/en/docs/indicators/ima and https://www.mql5.com/en/docs/indicators/imacd.

Documentation on MQL5: Technical Indicators / iMA
Documentation on MQL5: Technical Indicators / iMA
  • www.mql5.com
//|                                                     Demo_iMA.mq5 | //|                        Copyright 2011, MetaQuotes Software Corp. | //|                                              https://www.mql5.com | "The method of creation of the handle is set through the 'type' parameter (function type...
themasterx7
150
themasterx7  
Guye

Hi guys I cant grasp it Im sorry .... I know now what every fn means and the write place for the code but I need more books to fully get it so what books or learning materials do you recommend for me ? Thank You For Your Patience.  

One Last thing I learned this code from this youtuber I think he is bad do you recommend him for me ? https://www.youtube.com/channel/UCokIBdJXNOSOeYkKDvENWYA