0% found this document useful (0 votes)
26 views5 pages

Sierra

This document contains code for an expert advisor that sends trade order types (buy or sell) to an external API by making HTTP POST requests. It formats the trade data into a JSON string as the request body. The code checks for new trades, sends the request, and tracks the state of the request.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views5 pages

Sierra

This document contains code for an expert advisor that sends trade order types (buy or sell) to an external API by making HTTP POST requests. It formats the trade data into a JSON string as the request body. The code checks for new trades, sends the request, and tracks the state of the request.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include "sierrachart.

h"

SCDLLName("Requete5");
SCDateTime lasttime;
SCDateTime opentime;

SCString close_time(std::vector <s_ACSTrade> TradesList)


{
SCDateTime tradetime=TradesList[0].CloseDateTime;
int year, month, day, hour, minute, second, millisec;
tradetime.GetDateTimeYMDHMS_MS(year, month, day, hour, minute, second,
millisec);

// Formatage de la date et de l'heure


SCString dateAndTimeOpeningFormatted;
dateAndTimeOpeningFormatted.Format("%04d-%02d-%02dT%02d:%02d:%02d.
%03d+00:00", year, month, day, hour, minute, second, millisec);

return dateAndTimeOpeningFormatted;
}

SCSFExport scsf_SendOrderTypeToAPI(SCStudyInterfaceRef sc)


{
printf("aaa");
// Variable persistante pour suivre l'état de la requête
int& requestState = sc.GetPersistentInt(1);
// Nouvelle variable persistante pour suivre si l'ordre a été émis
int& orderSent = sc.GetPersistentInt(2);

if (sc.SetDefaults)
{
opentime=sc.CurrentSystemDateTime;
// Paramètres par défaut lors de la configuration de l'étude
sc.GraphName = "Send Order Type to API";
sc.StudyDescription = "Exemple d'envoi de type d'ordre à une API
distante";
sc.AutoLoop = 0; // Désactiver la boucle automatique car nous ne
traiterons les données qu'en cas d'événements spécifiques
sc.Input[0].Name = "Custom Name";
sc.Input[0].SetString("Your Name"); // Default value

sc.Input[1].Name = "password";
sc.Input[1].SetString("your password") ;

return;
}

std::vector <s_ACSTrade> TradesList;

s_ACSTrade TradeEntry;
int Size = sc.GetTradeListSize();

if(sc.GetTradeListEntry(Size-1, TradeEntry))
{
TradesList.push_back(TradeEntry);
}
SCDateTime tradetime =TradesList[0].OpenDateTime>TradesList[0].CloseDateTime
? TradesList[0].OpenDateTime: TradesList[0].CloseDateTime;

if( tradetime>lasttime && tradetime>opentime)


{
}
else
{
return;
}

// Vérifiez si l'ordre n'a pas encore été émis

// Exemple de détection du type d'ordre (Buy ou Sell)

if (true)
{
// Envoi du type d'ordre Buy à l'API distante
sc.AddMessageToLog("orderType: Buy", 0);

// Configuration des données à envoyer

SCString MessageOut;
const char* customName = sc.Input[0].GetString();
const char* customPqssword = sc.Input[1].GetString();
sc.AddMessageToLog(MessageOut, 0);
int ticketNumber = 225973981;
int identifier = 155411;
SCString orderType = "SELL";
SCString closurePosition = "Open";
int volume = TradesList[0].TradeQuantity;
SCString typeOfTransaction = "Buy";
int volume_remain =15845;

int year, month, day, hour, minute, second, millisec;


tradetime.GetDateTimeYMDHMS_MS(year, month, day, hour, minute,
second, millisec);

// Formatage de la date et de l'heure


SCString dateAndTimeOpeningFormatted;
dateAndTimeOpeningFormatted.Format("%04d-%02d-%02dT%02d:%02d:%02d.
%03d+00:00", year, month, day, hour, minute, second, millisec);

SCDateTimeMS dateAndTimeOpening = tradetime;

SCString dateAndTimeClosure = dateAndTimeOpeningFormatted;


int stopLoss = 15350;
int priceOpening = TradesList[0].MaxOpenQuantity;
double takeProfit = TradesList[0].MaximumOpenPositionProfit;
double SLOpen = TradesList[0].MaximumOpenPositionLoss;
double TPOpen = TradesList[0].MaximumOpenPositionProfit;

int balance = 0;
int point = 1;
int tick = 2;
int tradecount = 1;
// Créer une structure JSON
SCString postData;

postData.Format("{\"username\":\"%s\",\"password\":\"%s\",\"ticketNumber\":
%d,\"identifier\":%ld,\"orderType\":\"%s\",\"closurePosition\":\"%s\",\"volume\":
%ld,\"typeOfTransaction\":\"%s\",\"volume_remain\":
%ld,\"dateAndTimeOpening\":\"%s\",\"dateAndTimeClosure\":\"%s\",\"priceOpening\":
%ld,\"takeProfit\":%ld,\"stopLoss\":%ld,\"balance\":%ld, \"tick\":%ld, \"point\":
%ld, \"SLOpen\":%ld,\"tradecount\":%ld, \"TPOpen\":%ld}",
customName, customPqssword, ticketNumber, identifier,
orderType.GetChars(), closurePosition.GetChars(), volume,
typeOfTransaction.GetChars(), volume_remain,
dateAndTimeOpeningFormatted.GetChars(), dateAndTimeClosure.GetChars(),
priceOpening, takeProfit, stopLoss, balance, tick, point, SLOpen, TPOpen,
tradecount);
sc.AddMessageToLog(postData, 0);
n_ACSIL::s_HTTPHeader httpHeader;
httpHeader.Name = "Content-Type";
httpHeader.Value = "application/json";

// Faites une requête POST pour envoyer le type d'ordre à l'API


distante
if
(!sc.MakeHTTPPOSTRequest("https://apipython2.onrender.com/savetraderequest",
postData, &httpHeader, 1))
{
sc.AddMessageToLog("Erreur lors de la requête HTTP.", 1);
}
else
{
requestState = HTTP_REQUEST_MADE;
// Marquez l'ordre comme émis
lasttime=tradetime;
}

}
else if (true)
{
// Envoi du type d'ordre Sell à l'API distantesi
sc.AddMessageToLog("Order Type: Sell", 0);

// Configuration des données à envoyer

const char* customName = sc.Input[0].GetString();


// const char* customPqssword = sc.Input[1].GetString();
int ticketNumber = 225973981;
int identifier = 155411;
SCString orderType = TradesList[0].TradeType == 1? "BUY" : "SELL";
SCString closurePosition = "Open";
int volume = TradesList[0].TradeQuantity;
SCString typeOfTransaction = TradesList[0].TradeType == 1? "BUY" :
"SELL";
int volume_remain = 15485;

int year, month, day, hour, minute, second, millisec;


tradetime.GetDateTimeYMDHMS_MS(year, month, day, hour, minute,
second, millisec);
// Formatage de la date et de l'heure
SCString dateAndTimeOpeningFormatted;
dateAndTimeOpeningFormatted.Format("%04d-%02d-%02dT%02d:%02d:%02d.
%03d+00:00", year, month, day, hour, minute, second, millisec);

SCDateTimeMS dateAndTimeOpening = tradetime;

SCString dateAndTimeClosure = dateAndTimeOpeningFormatted;


int stopLoss = 15350;
int priceOpening = TradesList[0].EntryPrice;
double takeProfit =
TradesList[0].FlatToFlatMaximumOpenPositionProfit;
double SLOpen = TradesList[0].MaximumOpenPositionLoss;
double TPOpen = TradesList[0].MaximumOpenPositionProfit;

float balance = 0;
int point = 1;
int tick = 2;
int tradecount = 1;

// Créer une structure JSON


SCString postData;

postData.Format("{\"username\":\"%s\",\"password\":\"%s\",\"ticketNumber\":
%d,\"identifier\":%ld,\"orderType\":\"%s\",\"closurePosition\":\"%s\",\"volume\":
%ld,\"typeOfTransaction\":\"%s\",\"volume_remain\":
%ld,\"dateAndTimeOpening\":\"%s\",\"dateAndTimeClosure\":\"%s\",\"priceOpening\":
%ld,\"takeProfit\":%ld,\"stopLoss\":%ld,\"balance\":%ld, \"tick\":%ld, \"point\":
%ld, \"SLOpen\":%ld,\"tradecount\":%ld, \"TPOpen\":%ld}",
customName, customPqssword, ticketNumber, identifier,
orderType.GetChars(), closurePosition.GetChars(), volume,
typeOfTransaction.GetChars(), volume_remain,
dateAndTimeOpeningFormatted.GetChars(), dateAndTimeClosure.GetChars(),
priceOpening, takeProfit, stopLoss, balance, tick, point, SLOpen, TPOpen,
tradecount);
sc.AddMessageToLog(postData, 0);
n_ACSIL::s_HTTPHeader httpHeader;
httpHeader.Name = "Content-Type";
httpHeader.Value = "application/json";

// Faites une requête POST pour envoyer le type d'ordre à l'API


distante
if
(!sc.MakeHTTPPOSTRequest("https://apipython2.onrender.com/savetraderequest",
postData, &httpHeader, 1))
{
sc.AddMessageToLog("Erreur lors de la requête HTTP.", 1);
}
else
{
requestState = HTTP_REQUEST_MADE;
// Marquez l'ordre comme émis
lasttime=tradetime;
}

}
// Vérifiez si la requête est terminée et si la réponse est disponible
if (requestState == HTTP_REQUEST_MADE && sc.HTTPRequestID != 0)
{
requestState = HTTP_REQUEST_RECEIVED;
sc.AddMessageToLog(sc.HTTPResponse, 1);
}

You might also like